1 // Author: Roman Lygin, 2012.
2 // This file is in the Public Domain and thus can freely be used for any purpose.
3 // The author disclaims any rights and liabilities.
5 #ifndef _NCollection_StdAllocator_HeaderFile
6 #define _NCollection_StdAllocator_HeaderFile
8 #include <NCollection_BaseAllocator.hxx>
11 //Workaround for false "unreferenced parameter" warning in destroy().
12 #pragma warning (push)
13 #pragma warning (disable: 4100)
16 //! Implements allocator requirements as defined in ISO C++ Standard 2003, section 20.1.5.
17 /*! The allocator uses instance of the NCollection_BaseAllocator (sub)class for memory
18 allocation/deallocation. The allocator can be used with standard
19 containers (std::vector, std::map, etc) to take advantage of NCollection_IncAllocator
20 which implements memory region concept, and hence to increase performance in specific
23 The underlying NCollection_BaseAllocator instance can be received using the Allocator()
28 Handle(NCollection_IncAllocator) anIncAlloc = new NCollection_IncAllocator();
29 NCollection_StdAllocator<TopoDS_Shape> aSAlloc (anIncAlloc);
30 std::list<TopoDS_Shape, NCollection_StdAllocator<TopoDS_Shape> > aL (aSAlloc);
31 TopoDS_Solid aSolid = BRepPrimAPI_MakeBox (10., 20., 30.);
32 aL.push_back (aSolid);
36 class NCollection_StdAllocator {
39 typedef value_type* pointer;
40 typedef const value_type* const_pointer;
41 typedef value_type& reference;
42 typedef const value_type& const_reference;
43 typedef size_t size_type;
44 typedef ptrdiff_t difference_type;
45 template<typename U> struct rebind {
46 typedef NCollection_StdAllocator<U> other;
50 /*! Creates an object using default Open CASCADE allocation mechanism, i.e. which uses
51 Standard::Allocate() and Standard::Free() underneath.
53 NCollection_StdAllocator() throw()
54 { myAlloc = NCollection_BaseAllocator::CommonBaseAllocator(); }
57 /*! Saves \a theAlloc as an underlying allocator instance.*/
58 NCollection_StdAllocator( const Handle(NCollection_BaseAllocator)& theAlloc) throw()
59 { myAlloc = theAlloc; }
62 /*! Copies Allocator() from \a Y.*/
63 template<typename U> NCollection_StdAllocator( const NCollection_StdAllocator<U>& Y) throw()
64 { myAlloc = Y.Allocator(); }
66 //! Assignment operator
67 template<typename U> NCollection_StdAllocator& operator= (const NCollection_StdAllocator<U>& Y) throw()
68 { myAlloc = Y.Allocator(); return *this; }
70 //! Returns an object address.
72 pointer address( reference x ) const { return &x; }
74 //! Returns an object address.
76 const_pointer address( const_reference x ) const { return &x; }
78 //! Allocates memory for \a n objects.
79 /*! Uses underlying allocator to allocate memory.*/
80 pointer allocate( size_type n, const void* /*hint*/ = 0 )
81 { return pointer( myAlloc->Allocate( n * sizeof( value_type ))); }
83 //! Frees previously allocated memory.
84 /*! Uses underlying allocator to deallocate memory.*/
85 void deallocate( pointer p, size_type ) { myAlloc->Free( p ); }
87 //! Returns the largest value for which method allocate might succeed.
88 size_type max_size() const throw()
90 size_type aMax = static_cast<size_type>( -1 ) / sizeof( value_type );
94 //! Constructs an object.
95 /*! Uses placement new operator and copy constructor to construct an object.*/
96 void construct( pointer p, const_reference val )
97 { new( static_cast<void*>( p )) value_type( val ); }
99 //! Destroys the object.
100 /*! Uses object destructor.*/
101 void destroy( pointer p ) { p->~value_type(); }
103 //! Returns an underlying NCollection_BaseAllocator instance.
104 /*! Returns an object specified in the constructor.*/
105 const Handle(NCollection_BaseAllocator)& Allocator() const { return myAlloc; }
108 Handle(NCollection_BaseAllocator) myAlloc;
112 #pragma warning (pop)
116 //! Implements specialization NCollection_StdAllocator<void>.
117 /*! Specialization is of low value and should normally be avoided in favor of a typed specialization.
121 Handle(NCollection_IncAllocator) anIncAlloc = new NCollection_IncAllocator();
122 NCollection_StdAllocator<void> aVAlloc (anIncAlloc);
123 std::vector<double, NCollection_StdAllocator<double> > aV3 (aVAlloc);
128 class NCollection_StdAllocator<void> {
130 typedef void* pointer;
131 typedef const void* const_pointer;
132 typedef void value_type;
133 template<typename U> struct rebind {
134 typedef NCollection_StdAllocator<U> other;
138 /*! Creates an object using default Open CASCADE allocation mechanism, i.e. which uses
139 Standard::Allocate() and Standard::Free() underneath.
141 NCollection_StdAllocator() throw()
142 { myAlloc = NCollection_BaseAllocator::CommonBaseAllocator(); }
145 /*! Saves \a theAlloc as an underlying allocator instance.*/
146 NCollection_StdAllocator( const Handle(NCollection_BaseAllocator)& theAlloc) throw()
147 { myAlloc = theAlloc; }
150 /*! Copies Allocator() from \a X.*/
151 NCollection_StdAllocator( const NCollection_StdAllocator& X) throw() { myAlloc = X.myAlloc; }
153 //! Returns an underlying NCollection_BaseAllocator instance.
154 /*! Returns an object specified in the constructor.*/
155 const Handle(NCollection_BaseAllocator)& Allocator() const { return myAlloc; }
157 //! Assignment operator
158 NCollection_StdAllocator& operator=(const NCollection_StdAllocator& X) throw()
165 Handle(NCollection_BaseAllocator) myAlloc;
168 template<typename T, typename U>
169 inline bool operator==( const NCollection_StdAllocator<T>& X, const NCollection_StdAllocator<U>& Y)
170 { return !!(X.Allocator() == Y.Allocator()); }
172 template<typename T, typename U>
173 inline bool operator!=( const NCollection_StdAllocator<T>& X, const NCollection_StdAllocator<U>& Y)
174 { return !(X == Y); }