1 #ifndef ZSERIO_UNIQUE_PTR_H_INC
2 #define ZSERIO_UNIQUE_PTR_H_INC
19 template <
class ALLOC_T>
20 struct UniquePtrDeleter :
public AllocatorHolder<ALLOC_T>
23 using T =
typename allocator_type::value_type;
27 ~UniquePtrDeleter() =
default;
29 UniquePtrDeleter(UniquePtrDeleter&& other) =
default;
30 UniquePtrDeleter&
operator=(UniquePtrDeleter&& other) =
default;
39 UniquePtrDeleter(
const UniquePtrDeleter& other) =
delete;
40 UniquePtrDeleter&
operator=(
const UniquePtrDeleter& other) =
delete;
48 template <
typename ALLOC_U = ALLOC_T>
50 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>
104 using AllocTraits = std::allocator_traits<Allocator>;
106 Allocator typedAllocator = allocator;
107 typename AllocTraits::pointer ptr = AllocTraits::allocate(typedAllocator, 1);
110 AllocTraits::construct(typedAllocator, std::addressof(*ptr), std::forward<Args>(args)...);
115 AllocTraits::deallocate(typedAllocator, ptr, 1);
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
std::unique_ptr< T, detail::UniquePtrDeleter< ALLOC > > unique_ptr