43#ifndef __ADS_AllocatorAware
44 #define __ADS_AllocatorAware
69template<
class A ,
class T >
71 typename std::allocator_traits< A >::template rebind_alloc< T >;
74template<
class A ,
class T >
75using AllocVector = std::vector< T , RebindAlloc< A , T > >;
80inline constexpr bool isStdAllocator =
false;
84inline constexpr bool isStdAllocator< std::allocator< T > > =
true;
90inline constexpr bool alwaysEqualAlloc =
91 std::allocator_traits< A >::is_always_equal::value;
96[[nodiscard]] V emptyLike(
const V & v )
98 if constexpr( alwaysEqualAlloc< typename V::allocator_type > &&
99 std::is_default_constructible_v< typename V::allocator_type > )
102 return( V( v.get_allocator() ) );
109[[nodiscard]] V emptyCopy(
const V & v )
111 using Al =
typename V::allocator_type;
112 if constexpr( alwaysEqualAlloc< Al > &&
113 std::is_default_constructible_v< Al > )
117 V( std::allocator_traits< Al >::select_on_container_copy_construction(
118 v.get_allocator() ) ) );
123inline constexpr bool copyPropagates =
124 std::allocator_traits< A >::propagate_on_container_copy_assignment::value;
128inline constexpr bool movePropagates =
129 std::allocator_traits< A >::propagate_on_container_move_assignment::value;
133inline constexpr bool swapPropagates =
134 std::allocator_traits< A >::propagate_on_container_swap::value;
138[[nodiscard]] A copyAlloc(
const A & a )
140 using Traits = std::allocator_traits< A >;
141 return( Traits::select_on_container_copy_construction( a ) );
147[[nodiscard]]
bool stealAlloc(
const A & a ,
const A & b )
noexcept
149 if constexpr( movePropagates< A > || alwaysEqualAlloc< A > )
157void copyAssignAlloc( A & a ,
const A & b )
noexcept
159 if constexpr( copyPropagates< A > )
165void moveAssignAlloc( A & a , A & b )
noexcept
167 if constexpr( movePropagates< A > )
176inline constexpr bool swapNoexcept =
177 swapPropagates< A > || alwaysEqualAlloc< A >;
182void swapAlloc( A & a , A & b )
noexcept( swapNoexcept< A > )
184 if constexpr( swapPropagates< A > ) {
188 else if constexpr( ! alwaysEqualAlloc< A > )
195template<
class A ,
bool = std::is_empty_v< A > && ! std::is_final_v< A > >
200 AllocHolder(
void ) =
default;
202 explicit AllocHolder(
const A & a ) noexcept : f_a( a ) { }
204 [[nodiscard]] A & alloc(
void )
noexcept {
return( f_a ); }
206 [[nodiscard]]
const A & alloc(
void )
const noexcept {
return( f_a ); }
214class AllocHolder< A , true > :
private A
218 AllocHolder(
void ) =
default;
220 explicit AllocHolder(
const A & a ) noexcept : A( a ) { }
222 [[nodiscard]] A & alloc(
void )
noexcept {
return( *
this ); }
224 [[nodiscard]]
const A & alloc(
void )
const noexcept {
return( *
this ); }
231template<
class A ,
class T >
232class AllocArray :
private AllocHolder< RebindAlloc< A , T > >
234 using Al = RebindAlloc< A ,
236 using Traits = std::allocator_traits< Al >;
237 using Holder = AllocHolder< Al >;
241 AllocArray(
void ) =
default;
244 AllocArray(
const A & a , std::size_t n ) : Holder( Al( a ) ) , f_n( n ) {
245 f_p = Traits::allocate( this->alloc() , n );
249 ::new(
static_cast< void *
>( f_p + i ) ) T();
252 std::destroy_n( f_p , i );
253 Traits::deallocate( this->alloc() , f_p , n );
259 AllocArray( AllocArray && o ) noexcept
260 : Holder( std::move( o.alloc() ) ) ,
261 f_p( std::exchange( o.f_p ,
nullptr ) ) ,
262 f_n( std::exchange( o.f_n , 0 ) ) { }
266 AllocArray & operator=( AllocArray && o )
noexcept {
268 std::destroy_at(
this );
269 std::construct_at(
this , std::move( o ) );
275 ~AllocArray() { release(); }
278 [[nodiscard]] T & operator[]( std::size_t i )
const noexcept {
283 [[nodiscard]] T * get(
void )
const noexcept {
return( f_p ); }
287 void release(
void )
noexcept {
289 std::destroy_n( f_p , f_n );
290 Traits::deallocate( this->alloc() , f_p , f_n );
303void swapCheck( [[maybe_unused]]
const A & a ,
304 [[maybe_unused]]
const A & b )
noexcept( swapNoexcept< A > )
306 if constexpr( ! swapPropagates< A > && ! alwaysEqualAlloc< A > )
313void replaceBy( C & c , C && t )
noexcept
315 std::destroy_at( &c );
316 std::construct_at( &c , std::move( t ) );
324C & copyAssign( C & c ,
const C & o )
326 using A =
typename C::allocator_type;
328 C t( o , copyPropagates< A > ? o.get_allocator() : c.get_allocator() );
329 replaceBy( c , std::move( t ) );
339C & moveAssign( C & c , C && o )
342 if( stealAlloc( c.get_allocator() , o.get_allocator() ) )
343 replaceBy( c , std::move( o ) );
345 C t( std::move( o ) , c.get_allocator() );
346 replaceBy( c , std::move( t ) );
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
#define ADS_PRE(cond)
[check]
Definition Contract.h:121