1 #ifndef ZSERIO_UNIQUE_PTR_H_INC 2 #define ZSERIO_UNIQUE_PTR_H_INC 18 template <
class ALLOC_T>
19 struct UniquePtrDeleter :
public AllocatorHolder<ALLOC_T>
22 using T =
typename allocator_type::value_type;
26 ~UniquePtrDeleter() =
default;
28 UniquePtrDeleter(UniquePtrDeleter&& other) =
default;
29 UniquePtrDeleter&
operator=(UniquePtrDeleter&& other) =
default;
38 UniquePtrDeleter(
const UniquePtrDeleter& other) =
delete;
39 UniquePtrDeleter&
operator=(
const UniquePtrDeleter& other) =
delete;
47 template <
typename ALLOC_U = ALLOC_T>
49 UniquePtrDeleter(ALLOC_U())
56 template <
typename ALLOC_U = ALLOC_T>
57 UniquePtrDeleter(
const ALLOC_U& allocator) :
60 static_assert(std::is_same<allocator_type, RebindAlloc<ALLOC_U, T>>::value,
61 "UniquePtrDeleter requires same allocator in constructor!");
67 template <
typename ALLOC_U>
68 UniquePtrDeleter(
const UniquePtrDeleter<ALLOC_U>& deleter) :
72 void operator()(T* ptr)
75 using AllocTraits = std::allocator_traits<allocator_type>;
76 AllocTraits::destroy(alloc, std::addressof(*ptr));
77 AllocTraits::deallocate(alloc, ptr, 1);
88 template <
typename T,
typename ALLOC = std::allocator<T>>
89 using unique_ptr = std::unique_ptr<T, detail::UniquePtrDeleter<ALLOC>>;
100 template <
typename T,
typename ALLOC,
class ...Args>
105 using AllocTraits = std::allocator_traits<Allocator>;
107 Allocator typedAllocator = allocator;
108 typename AllocTraits::pointer ptr = AllocTraits::allocate(typedAllocator, 1);
111 AllocTraits::construct(typedAllocator, std::addressof(*ptr), std::forward<Args>(args)...);
116 AllocTraits::deallocate(typedAllocator, ptr, 1);
123 #endif // ZSERIO_UNIQUE_PTR_H_INC
std::unique_ptr< T, detail::UniquePtrDeleter< ALLOC >> unique_ptr
AllocatorHolder & operator=(const AllocatorHolder &other)=default
allocator_type get_allocator() const
zserio::unique_ptr< T, RebindAlloc< ALLOC, T > > allocate_unique(const ALLOC &allocator, Args &&...args)
typename std::allocator_traits< ALLOC >::template rebind_alloc< T > RebindAlloc