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;
49 UniquePtrDeleter(ALLOC_T())
55 UniquePtrDeleter(
const ALLOC_T& allocator) :
59 void operator()(T* ptr)
62 using AllocTraits = std::allocator_traits<allocator_type>;
63 AllocTraits::destroy(alloc, std::addressof(*ptr));
64 AllocTraits::deallocate(alloc, ptr, 1);
75 template <
typename T,
typename ALLOC = std::allocator<T>>
76 using UniquePtr = std::unique_ptr<T, detail::UniquePtrDeleter<ALLOC>>;
87 template <
typename T,
typename ALLOC,
class... Args>
91 using AllocTraits = std::allocator_traits<Allocator>;
93 Allocator typedAllocator = allocator;
94 typename AllocTraits::pointer ptr = AllocTraits::allocate(typedAllocator, 1);
97 AllocTraits::construct(typedAllocator, std::addressof(*ptr), std::forward<Args>(args)...);
102 AllocTraits::deallocate(typedAllocator, ptr, 1);
allocator_type get_allocator() const
AllocatorHolder & operator=(const AllocatorHolder &other)=default
std::unique_ptr< T, detail::UniquePtrDeleter< ALLOC > > UniquePtr
typename std::allocator_traits< ALLOC >::template rebind_alloc< T > RebindAlloc
UniquePtr< T, RebindAlloc< ALLOC, T > > allocate_unique(const ALLOC &allocator, Args &&... args)