OGRE  1.8
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-2013 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 
160  {
161  // call placement new
162  new(static_cast<void*>(p)) T();
163  }
164 
165  void construct(pointer p, const T& val)
166  {
167  // call placement new
168  new(static_cast<void*>(p)) T(val);
169  }
170 
171  void destroy(pointer p)
172  {
173  // do we have to protect against non-classes here?
174  // some articles suggest yes, some no
175  p->~T();
176  }
177 
178  };
179 
182  template<typename T, typename T2, typename P>
183  inline bool operator==(STLAllocator<T,P> const&,
184  STLAllocator<T2,P> const&)
185  {
186  // same alloc policy (P), memory can be freed
187  return true;
188  }
189 
192  template<typename T, typename P, typename OtherAllocator>
193  inline bool operator==(STLAllocator<T,P> const&,
194  OtherAllocator const&)
195  {
196  return false;
197  }
200  template<typename T, typename T2, typename P>
201  inline bool operator!=(STLAllocator<T,P> const&,
202  STLAllocator<T2,P> const&)
203  {
204  // same alloc policy (P), memory can be freed
205  return false;
206  }
207 
210  template<typename T, typename P, typename OtherAllocator>
211  inline bool operator!=(STLAllocator<T,P> const&,
212  OtherAllocator const&)
213  {
214  return true;
215  }
216 
217 
221 }// namespace Ogre
222 
223 #include "OgreHeaderSuffix.h"
224 #endif // _MemorySTLAllocator_H__
225 
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++)