OgreMemorySTLAllocator.h
Go to the documentation of this file.
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4(Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2013 Torus Knot Software Ltd
8
9Permission is hereby granted, free of charge, to any person obtaining a copy
10of this software and associated documentation files (the "Software"), to deal
11in the Software without restriction, including without limitation the rights
12to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13copies of the Software, and to permit persons to whom the Software is
14furnished to do so, subject to the following conditions:
15
16The above copyright notice and this permission notice shall be included in
17all copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25THE SOFTWARE.
26-----------------------------------------------------------------------------
27*/
28
29#ifndef _MemorySTLAllocator_H__
30#define _MemorySTLAllocator_H__
31
32#include "OgrePrerequisites.h"
33#include "OgreHeaderPrefix.h"
34
35namespace Ogre
36{
37
38
62 // Base STL allocator class.
63 template<typename T>
65 { // base class for generic allocators
66 typedef T value_type;
67 };
68
69 // Base STL allocator class. (const T version).
70 template<typename T>
72 { // base class for generic allocators for const T
73 typedef T value_type;
74 };
75
76 template
77 <
78 typename T,
79 typename AllocPolicy
80 >
82 {
83 public :
86 typedef typename Base::value_type value_type;
88 typedef const value_type* const_pointer;
91 typedef std::size_t size_type;
92 typedef std::ptrdiff_t difference_type;
93
94
96 template<typename U>
97 struct rebind
98 {
100 };
101
103 inline explicit STLAllocator()
104 { }
105
108 { }
109
111 inline STLAllocator( STLAllocator const& )
112 { }
113
115 template <typename U>
117 { }
118
120 template <typename U, typename P>
122 { }
123
126 typename std::allocator<void>::const_pointer ptr = 0 )
127 {
128 (void)ptr;
129 // convert request to bytes
130 register size_type sz = count*sizeof( T );
131 pointer p = static_cast<pointer>(AllocPolicy::allocateBytes(sz));
132 return p;
133 }
134
136 inline void deallocate( pointer ptr, size_type )
137 {
138 // convert request to bytes, but we can't use this?
139 // register size_type sz = count*sizeof( T );
140 AllocPolicy::deallocateBytes(ptr);
141 }
142
144 {
145 return &x;
146 }
147
149 {
150 return &x;
151 }
152
154 {
155 // maximum size this can handle, delegate
156 return AllocPolicy::getMaxAllocationSize();
157 }
158
159#if __cplusplus < 201103L
161 {
162 // call placement new
163 new(static_cast<void*>(p)) T();
164 }
165#endif
166
167 void construct(pointer p, const T& val)
168 {
169 // call placement new
170 new(static_cast<void*>(p)) T(val);
171 }
172
174 {
175 // do we have to protect against non-classes here?
176 // some articles suggest yes, some no
177 p->~T();
178 }
179 };
180
183 template<typename T, typename T2, typename P>
184 inline bool operator==(STLAllocator<T,P> const&,
185 STLAllocator<T2,P> const&)
186 {
187 // same alloc policy (P), memory can be freed
188 return true;
189 }
190
193 template<typename T, typename P, typename OtherAllocator>
194 inline bool operator==(STLAllocator<T,P> const&,
195 OtherAllocator const&)
196 {
197 return false;
198 }
201 template<typename T, typename T2, typename P>
202 inline bool operator!=(STLAllocator<T,P> const&,
203 STLAllocator<T2,P> const&)
204 {
205 // same alloc policy (P), memory can be freed
206 return false;
207 }
208
211 template<typename T, typename P, typename OtherAllocator>
212 inline bool operator!=(STLAllocator<T,P> const&,
213 OtherAllocator const&)
214 {
215 return true;
216 }
217
218
222}// namespace Ogre
223
224
225#include "OgreHeaderSuffix.h"
226
227#endif // _MemorySTLAllocator_H__
228
pointer allocate(size_type count, typename std::allocator< void >::const_pointer ptr=0)
memory allocation (elements, used by STL)
const value_type * const_pointer
STLAllocatorBase< T > Base
define our types, as per ISO C++
STLAllocator(STLAllocator< U, AllocPolicy > const &)
cast
pointer address(reference x) const
const value_type & const_reference
const_pointer address(const_reference x) const
STLAllocator(STLAllocator< U, P > const &)
cast
void construct(pointer p, const T &val)
Base::value_type value_type
STLAllocator(STLAllocator const &)
copy ctor - done component wise
void deallocate(pointer ptr, size_type)
memory deallocation (elements, used by STL)
Reference-counted shared pointer, used for objects where implicit destruction is required.
bool operator!=(STLAllocator< T, P > const &, STLAllocator< T2, P > const &)
determine equality, can memory from another allocator be released by this allocator,...
bool operator==(STLAllocator< T, P > const &, STLAllocator< T2, P > const &)
determine equality, can memory from another allocator be released by this allocator,...
Wrapper class for operating as an STL container allocator.
the standard rebind mechanism
STLAllocator< U, AllocPolicy > other

Copyright © 2012 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.