ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
AllocatorAware.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------- File AllocatorAware.h --------------------------*/
3/*--------------------------------------------------------------------------*/
39/*--------------------------------------------------------------------------*/
40/*----------------------------- DEFINITIONS --------------------------------*/
41/*--------------------------------------------------------------------------*/
42
43#ifndef __ADS_AllocatorAware
44 #define __ADS_AllocatorAware
45/* self-identification: #endif constant */
46
47/*--------------------------------------------------------------------------*/
48/*------------------------------ INCLUDES ----------------------------------*/
49/*--------------------------------------------------------------------------*/
50
51#include "ADS/Contract.h"
52
53#include <memory>
54#include <type_traits>
55#include <utility>
56#include <vector>
57
58/*--------------------------------------------------------------------------*/
59/*--------------------------- NAMESPACE ------------------------------------*/
60/*--------------------------------------------------------------------------*/
61
62namespace ADS
63{
64
65namespace detail
66{
67
69template< class A , class T >
70using RebindAlloc =
71 typename std::allocator_traits< A >::template rebind_alloc< T >;
72
74template< class A , class T >
75using AllocVector = std::vector< T , RebindAlloc< A , T > >;
76
79template< class A >
80inline constexpr bool isStdAllocator = false;
81
83template< class T >
84inline constexpr bool isStdAllocator< std::allocator< T > > = true;
85
89template< class A >
90inline constexpr bool alwaysEqualAlloc =
91 std::allocator_traits< A >::is_always_equal::value;
92
95template< class V >
96[[nodiscard]] V emptyLike( const V & v )
97{
98 if constexpr( alwaysEqualAlloc< typename V::allocator_type > &&
99 std::is_default_constructible_v< typename V::allocator_type > )
100 return( V() );
101 else
102 return( V( v.get_allocator() ) );
103 }
104
108template< class V >
109[[nodiscard]] V emptyCopy( const V & v )
110{
111 using Al = typename V::allocator_type;
112 if constexpr( alwaysEqualAlloc< Al > &&
113 std::is_default_constructible_v< Al > )
114 return( V() );
115 else
116 return(
117 V( std::allocator_traits< Al >::select_on_container_copy_construction(
118 v.get_allocator() ) ) );
119 }
120
122template< class A >
123inline constexpr bool copyPropagates =
124 std::allocator_traits< A >::propagate_on_container_copy_assignment::value;
125
127template< class A >
128inline constexpr bool movePropagates =
129 std::allocator_traits< A >::propagate_on_container_move_assignment::value;
130
132template< class A >
133inline constexpr bool swapPropagates =
134 std::allocator_traits< A >::propagate_on_container_swap::value;
135
137template< class A >
138[[nodiscard]] A copyAlloc( const A & a )
139{
140 using Traits = std::allocator_traits< A >;
141 return( Traits::select_on_container_copy_construction( a ) );
142 }
143
146template< class A >
147[[nodiscard]] bool stealAlloc( const A & a , const A & b ) noexcept
148{
149 if constexpr( movePropagates< A > || alwaysEqualAlloc< A > )
150 return( true );
151 else
152 return( a == b );
153 }
154
156template< class A >
157void copyAssignAlloc( A & a , const A & b ) noexcept
158{
159 if constexpr( copyPropagates< A > )
160 a = b;
161 }
162
164template< class A >
165void moveAssignAlloc( A & a , A & b ) noexcept
166{
167 if constexpr( movePropagates< A > )
168 a = std::move( b );
169 }
170
175template< class A >
176inline constexpr bool swapNoexcept =
177 swapPropagates< A > || alwaysEqualAlloc< A >;
178
181template< class A >
182void swapAlloc( A & a , A & b ) noexcept( swapNoexcept< A > )
183{
184 if constexpr( swapPropagates< A > ) {
185 using std::swap;
186 swap( a , b );
187 }
188 else if constexpr( ! alwaysEqualAlloc< A > )
189 ADS_PRE( a == b );
190 }
191
195template< class A , bool = std::is_empty_v< A > && ! std::is_final_v< A > >
196class AllocHolder
197{
198 public:
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 ); }
207
208 private:
209 A f_a;
210 };
211
213template< class A >
214class AllocHolder< A , true > : private A
215{
216 public:
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 ); }
225 };
226
231template< class A , class T >
232class AllocArray : private AllocHolder< RebindAlloc< A , T > >
233{
234 using Al = RebindAlloc< A ,
235 T >;
236 using Traits = std::allocator_traits< Al >;
237 using Holder = AllocHolder< Al >;
238
239 public:
241 AllocArray( void ) = default;
242
244 AllocArray( const A & a , std::size_t n ) : Holder( Al( a ) ) , f_n( n ) {
245 f_p = Traits::allocate( this->alloc() , n );
246 std::size_t i = 0;
247 try {
248 for( ; i < n ; ++i )
249 ::new( static_cast< void * >( f_p + i ) ) T();
250 }
251 catch( ... ) {
252 std::destroy_n( f_p , i );
253 Traits::deallocate( this->alloc() , f_p , n );
254 throw;
255 }
256 }
257
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 ) ) { }
263
266 AllocArray & operator=( AllocArray && o ) noexcept {
267 if( this != &o ) {
268 std::destroy_at( this );
269 std::construct_at( this , std::move( o ) );
270 }
271 return( *this );
272 }
273
275 ~AllocArray() { release(); }
276
278 [[nodiscard]] T & operator[]( std::size_t i ) const noexcept {
279 return( f_p[ i ] );
280 }
281
283 [[nodiscard]] T * get( void ) const noexcept { return( f_p ); }
284
285 private:
287 void release( void ) noexcept {
288 if( f_p ) {
289 std::destroy_n( f_p , f_n );
290 Traits::deallocate( this->alloc() , f_p , f_n );
291 f_p = nullptr;
292 }
293 }
294
295 T * f_p = nullptr;
296 std::size_t f_n = 0;
297 };
298
302template< class A >
303void swapCheck( [[maybe_unused]] const A & a ,
304 [[maybe_unused]] const A & b ) noexcept( swapNoexcept< A > )
305{
306 if constexpr( ! swapPropagates< A > && ! alwaysEqualAlloc< A > )
307 ADS_PRE( a == b );
308 }
309
312template< class C >
313void replaceBy( C & c , C && t ) noexcept
314{
315 std::destroy_at( &c );
316 std::construct_at( &c , std::move( t ) );
317 }
318
323template< class C >
324C & copyAssign( C & c , const C & o )
325{
326 using A = typename C::allocator_type;
327 if( &c != &o ) {
328 C t( o , copyPropagates< A > ? o.get_allocator() : c.get_allocator() );
329 replaceBy( c , std::move( t ) );
330 }
331 return( c );
332 }
333
338template< class C >
339C & moveAssign( C & c , C && o )
340{
341 if( &c != &o ) {
342 if( stealAlloc( c.get_allocator() , o.get_allocator() ) )
343 replaceBy( c , std::move( o ) );
344 else {
345 C t( std::move( o ) , c.get_allocator() );
346 replaceBy( c , std::move( t ) );
347 }
348 }
349 return( c );
350 }
351
352} // end( namespace detail )
353
354/*--------------------------------------------------------------------------*/
355
356} // end( namespace ADS )
357
358/*--------------------------------------------------------------------------*/
359
360#endif /* AllocatorAware.h included */
361
362/*--------------------------------------------------------------------------*/
363/*----------------------- End File AllocatorAware.h ------------------------*/
364/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
Definition ADT.h:96