MyGUI  3.2.0
MyGUI_CustomAllocator.h
Go to the documentation of this file.
1 
6 /*
7  This file is part of MyGUI.
8 
9  MyGUI is free software: you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  MyGUI is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
21 */
22 #ifndef __MYGUI_CUSTOM_ALLOCATOR_H__
23 #define __MYGUI_CUSTOM_ALLOCATOR_H__
24 
25 #include <memory>
26 #include <limits>
27 
28 // for Ogre version
29 #include <OgrePrerequisites.h>
30 
31 #if OGRE_VERSION < MYGUI_DEFINE_VERSION(1, 6, 0)
32 #include <OgreMemoryManager.h>
33 #include <OgreNoMemoryMacros.h>
34 #endif
35 
36 namespace MyGUI
37 {
38 
39  template<typename T>
40  class Allocator
41  {
42  public:
43  // typedefs
44  typedef T value_type;
45  typedef value_type* pointer;
46  typedef const value_type* const_pointer;
48  typedef const value_type& const_reference;
49  typedef std::size_t size_type;
50  typedef std::ptrdiff_t difference_type;
51 
52  public:
53  // convert an allocator<T> to allocator<U>
54  template<typename U>
55  struct rebind
56  {
58  };
59 
60  public:
61  inline explicit Allocator() { }
62  inline ~Allocator() { }
63  template<typename U>
64  inline explicit Allocator(Allocator<U> const&) { }
65 
66  // address
68  {
69  return &r;
70  }
72  {
73  return &r;
74  }
75 
76  // memory allocation
77  inline pointer allocate(size_type cnt, typename std::allocator<void>::const_pointer = 0)
78  {
79  return reinterpret_cast<pointer>(::operator new (cnt * sizeof (T)));
80  }
81  inline void deallocate(pointer p, size_type)
82  {
83  ::operator delete (p);
84  }
85 
86  // size
87  inline size_type max_size() const
88  {
89  return (std::numeric_limits<size_type>::max)() / sizeof(T);
90  }
91 
92  // construction/destruction
93  inline void construct(pointer p, const T& t)
94  {
95  new (p) T(t);
96  }
97  inline void destroy(pointer p)
98  {
99  p->~T();
100  }
101 
102  inline bool operator==(Allocator const&)
103  {
104  return true;
105  }
106  inline bool operator!=(Allocator const& a)
107  {
108  return !operator==(a);
109  }
110  };
111 
112 } // namespace MyGUI
113 
114 #if OGRE_VERSION < MYGUI_DEFINE_VERSION(1, 6, 0)
115 #include <OgreMemoryMacros.h>
116 #endif
117 
118 #endif // __MYGUI_CUSTOM_ALLOCATOR_H__