OGRE  1.9
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
OgreMemorySTLAllocator.h
Go to the documentation of this file.
1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4 (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org/
6 
7 Copyright (c) 2000-2014 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 
29 #ifndef _MemorySTLAllocator_H__
30 #define _MemorySTLAllocator_H__
31 
32 #include "OgrePrerequisites.h"
33 #include "OgreHeaderPrefix.h"
34 
35 namespace 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>
71  struct STLAllocatorBase<const 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  >
81  class STLAllocator : public STLAllocatorBase<T>
82  {
83  public :
86  typedef typename Base::value_type value_type;
87  typedef value_type* pointer;
88  typedef const value_type* const_pointer;
90  typedef const value_type& const_reference;
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 
107  virtual ~STLAllocator()
108  { }
109 
111  inline STLAllocator( STLAllocator const& )
112  { }
113 
115  template <typename U>
117  { }
118 
120  template <typename U, typename P>
122  { }
123 
125  inline pointer allocate( size_type count,
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 
153  size_type max_size() const throw()
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 
173  void destroy(pointer p)
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 
STLAllocator< U, AllocPolicy > other
the standard rebind mechanism
pointer allocate(size_type count, typename std::allocator< void >::const_pointer ptr=0)
memory allocation (elements, used by STL)
void deallocate(pointer ptr, size_type)
memory deallocation (elements, used by STL)
const_pointer address(const_reference x) const
std::ptrdiff_t difference_type
pointer address(reference x) const
const value_type * const_pointer
STLAllocator(STLAllocator const &)
copy ctor - done component wise
void construct(pointer p, const T &val)
Wrapper class for operating as an STL container allocator.
STLAllocator(STLAllocator< U, P > const &)
cast
Base::value_type value_type
STLAllocator(STLAllocator< U, AllocPolicy > const &)
cast
STLAllocatorBase< T > Base
define our types, as per ISO C++
const value_type & const_reference
size_type max_size() const
bool operator==(STLAllocator< T, P > const &, STLAllocator< T2, P > const &)
determine equality, can memory from another allocator be released by this allocator, (ISO C++)
bool operator!=(STLAllocator< T, P > const &, STLAllocator< T2, P > const &)
determine equality, can memory from another allocator be released by this allocator, (ISO C++)