MyGUI  3.2.1
MyGUI_CustomAllocator.h
Go to the documentation of this file.
1 /*
2  * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3  * Distributed under the MIT License
4  * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5  */
6 
7 #ifndef __MYGUI_CUSTOM_ALLOCATOR_H__
8 #define __MYGUI_CUSTOM_ALLOCATOR_H__
9 
10 #include <memory>
11 #include <limits>
12 
13 // for Ogre version
14 #include <OgrePrerequisites.h>
15 
16 #if OGRE_VERSION < MYGUI_DEFINE_VERSION(1, 6, 0)
17 #include <OgreMemoryManager.h>
18 #include <OgreNoMemoryMacros.h>
19 #endif
20 
21 namespace MyGUI
22 {
23 
24  template<typename T>
25  class Allocator
26  {
27  public:
28  // typedefs
29  typedef T value_type;
30  typedef value_type* pointer;
31  typedef const value_type* const_pointer;
33  typedef const value_type& const_reference;
34  typedef std::size_t size_type;
35  typedef std::ptrdiff_t difference_type;
36 
37  public:
38  // convert an allocator<T> to allocator<U>
39  template<typename U>
40  struct rebind
41  {
43  };
44 
45  public:
46  inline explicit Allocator() { }
47  inline ~Allocator() { }
48  template<typename U>
49  inline explicit Allocator(Allocator<U> const&) { }
50 
51  // address
53  {
54  return &r;
55  }
57  {
58  return &r;
59  }
60 
61  // memory allocation
62  inline pointer allocate(size_type cnt, typename std::allocator<void>::const_pointer = 0)
63  {
64  return reinterpret_cast<pointer>(::operator new (cnt * sizeof (T)));
65  }
66  inline void deallocate(pointer p, size_type)
67  {
68  ::operator delete (p);
69  }
70 
71  // size
72  inline size_type max_size() const
73  {
74  return (std::numeric_limits<size_type>::max)() / sizeof(T);
75  }
76 
77  // construction/destruction
78  inline void construct(pointer p, const T& t)
79  {
80  new (p) T(t);
81  }
82  inline void destroy(pointer p)
83  {
84  p->~T();
85  }
86 
87  inline bool operator==(Allocator const&)
88  {
89  return true;
90  }
91  inline bool operator!=(Allocator const& a)
92  {
93  return !operator==(a);
94  }
95  };
96 
97 } // namespace MyGUI
98 
99 #if OGRE_VERSION < MYGUI_DEFINE_VERSION(1, 6, 0)
100 #include <OgreMemoryMacros.h>
101 #endif
102 
103 #endif // __MYGUI_CUSTOM_ALLOCATOR_H__