0023569: Adding NCollection_StdAllocator
[occt.git] / src / NCollection / NCollection_StdAllocator.hxx
CommitLineData
ed9161a4
RL
1// Author: Roman Lygin, 2012.
2// This file is put into Public Domain and thus can freely be used for any purpose.
3// The author disclaims any rights and liabilities.
4
5#ifndef _NCollection_StdAllocator_HeaderFile
6#define _NCollection_StdAllocator_HeaderFile
7
8#include <NCollection_BaseAllocator.hxx>
9
10#if _MSC_VER
11 //Workaround for false "unreferenced parameter" warning in destroy().
12 #pragma warning (push)
13 #pragma warning (disable: 4100)
14#endif
15
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
21 cases.
22
23 The underlying NCollection_BaseAllocator instance can be received using the Allocator()
24 method.
25
26 Example of use:
27 \code
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);
33 \endcode
34*/
35template<typename T>
36class NCollection_StdAllocator {
37public:
38 typedef T value_type;
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;
47 };
48
49 //! Constructor.
50 /*! Creates an object using default Open CASCADE allocation mechanism, i.e. which uses
51 Standard::Allocate() and Standard::Free() underneath.
52 */
53 NCollection_StdAllocator() throw()
54 { myAlloc = NCollection_BaseAllocator::CommonBaseAllocator(); }
55
56 //! Constructor.
57 /*! Saves \a theAlloc as an underlying allocator instance.*/
58 NCollection_StdAllocator( const Handle(NCollection_BaseAllocator)& theAlloc) throw()
59 { myAlloc = theAlloc; }
60
61 //! Constructor.
62 /*! Copies Allocator() from \a X.*/
63 NCollection_StdAllocator( const NCollection_StdAllocator& X) throw() { myAlloc = X.myAlloc; }
64
65 //! Destructor.
66 /*! Empty implementation.*/
67 ~NCollection_StdAllocator() throw() {}
68
69 //! Constructor.
70 /*! Copies Allocator() from \a Y.*/
71 template<typename U> NCollection_StdAllocator( const NCollection_StdAllocator<U>& Y) throw()
72 { myAlloc = Y.Allocator(); }
73
74 //! Returns an object address.
75 /*! Returns &x.*/
76 pointer address( reference x ) const { return &x; }
77
78 //! Returns an object address.
79 /*! Returns &x.*/
80 const_pointer address( const_reference x ) const { return &x; }
81
82 //! Allocates memory for \a n objects.
83 /*! Uses underlying allocator to allocate memory.*/
84 pointer allocate( size_type n, const void* /*hint*/ = 0 )
85 { return pointer( myAlloc->Allocate( n * sizeof( value_type ))); }
86
87 //! Frees previously allocated memory.
88 /*! Uses underlying allocator to deallocate memory.*/
89 void deallocate( pointer p, size_type ) { myAlloc->Free( p ); }
90
91 //! Returns the largest value for which method allocate might succeed.
92 size_type max_size() const throw()
93 {
94 size_type aMax = static_cast<size_type>( -1 ) / sizeof( value_type );
95 return aMax;
96 }
97
98 //! Constructs an object.
99 /*! Uses placement new operator and copy constructor to construct an object.*/
100 void construct( pointer p, const_reference val )
101 { new( static_cast<void*>( p )) value_type( val ); }
102
103 //! Destroys the object.
104 /*! Uses object destructor.*/
105 void destroy( pointer p ) { p->~value_type(); }
106
107 //! Returns an underlying NCollection_BaseAllocator instance.
108 /*! Returns an object specified in the constructor.*/
109 const Handle(NCollection_BaseAllocator)& Allocator() const { return myAlloc; }
110
111protected:
112 Handle(NCollection_BaseAllocator) myAlloc;
113};
114
115#if _MSC_VER
116 #pragma warning (pop)
117#endif
118
119
120//! Implements specialization NCollection_StdAllocator<void>.
121/*! Specialization is of low value and should normally be avoided in favor of a typed specialization.
122
123 Example of use:
124 \code
125 Handle(NCollection_IncAllocator) anIncAlloc = new NCollection_IncAllocator();
126 NCollection_StdAllocator<void> aVAlloc (anIncAlloc);
127 std::vector<double, NCollection_StdAllocator<double> > aV3 (aVAlloc);
128 aV3.push_back (10.);
129 \endcode
130*/
131template<>
132class NCollection_StdAllocator<void> {
133public:
134 typedef void* pointer;
135 typedef const void* const_pointer;
136 typedef void value_type;
137 template<typename U> struct rebind {
138 typedef NCollection_StdAllocator<U> other;
139 };
140
141 //! Constructor.
142 /*! Creates an object using default Open CASCADE allocation mechanism, i.e. which uses
143 Standard::Allocate() and Standard::Free() underneath.
144 */
145 NCollection_StdAllocator() throw()
146 { myAlloc = NCollection_BaseAllocator::CommonBaseAllocator(); }
147
148 //! Constructor.
149 /*! Saves \a theAlloc as an underlying allocator instance.*/
150 NCollection_StdAllocator( const Handle(NCollection_BaseAllocator)& theAlloc) throw()
151 { myAlloc = theAlloc; }
152
153 //! Constructor.
154 /*! Copies Allocator() from \a X.*/
155 NCollection_StdAllocator( const NCollection_StdAllocator& X) throw() { myAlloc = X.myAlloc; }
156
157 //! Returns an underlying NCollection_BaseAllocator instance.
158 /*! Returns an object specified in the constructor.*/
159 const Handle(NCollection_BaseAllocator)& Allocator() const { return myAlloc; }
160
161protected:
162 Handle(NCollection_BaseAllocator) myAlloc;
163};
164
165template<typename T, typename U>
166inline bool operator==( const NCollection_StdAllocator<T>& X, const NCollection_StdAllocator<U>& Y)
167{ return !!(X.Allocator() == Y.Allocator()); }
168
169template<typename T, typename U>
170inline bool operator!=( const NCollection_StdAllocator<T>& X, const NCollection_StdAllocator<U>& Y)
171{ return !(X == Y); }
172
173
174#endif