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