Skip to content
Snippets Groups Projects
Commit 9e9f3f75 authored by Martin Nolte's avatar Martin Nolte
Browse files

implement a "PolyAllocator" wrapping an STL allocator (see FS#766)

Warning: I don't consider this code to be conforming to the standard.
         Its purpose is purely for discussion.

[[Imported from SVN: r6230]]
parent 12fff77d
No related branches found
No related tags found
No related merge requests found
......@@ -26,6 +26,45 @@ namespace Dune
}
};
// STLPolyAllocator
// ----------------
/** This PolyAllocator tries to use an STL allocator to create polymorphic
* objects (i.e., where the type on create differs from the type on destroy).
*
* Note: It is totally unclear whether this works or is in any way conforming
* to the standard. See FS#766 for details.
*/
template< class A = std::allocator< void > >
struct STLPolyAllocator
{
explicit STLPolyAllocator ( const A &a = A() )
: allocator_( a )
{}
template< class T >
T *create ( const T &value )
{
typename A::template rebind< T >::other allocator( allocator_ );
T *p = allocator.allocate( 1 );
allocator.construct( p, value );
return p;
}
template< class T >
void destroy ( T *p )
{
typename A::template rebind< T >::other allocator( allocator_ );
allocator.destroy( p );
allocator.deallocate( p, 1 );
}
private:
typename A::template rebind< void * >::other allocator_;
};
}
#endif // #ifndef DUNE_POLYALLOCATOR_HH
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment