MyGUI: MyGUI_CustomAllocator.h Source File

MyGUI 3.2.2

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;
32 typedef value_type& reference;
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 {
42 typedef Allocator<U> other;
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
52 inline pointer address(reference r)
53 {
54 return &r;
55 }
56 inline const_pointer address(const_reference r)
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_
MyGUI::Allocator::const_reference
const typedef value_type & const_reference
Definition: MyGUI_Allocator.h:25
MyGUI::Allocator::max_size
size_type max_size() const
Definition: MyGUI_CustomAllocator.h:72
MyGUI::Allocator::operator==
bool operator==(Allocator const &)
Definition: MyGUI_CustomAllocator.h:87
MyGUI::Allocator::difference_type
std::ptrdiff_t difference_type
Definition: MyGUI_CustomAllocator.h:35
MyGUI::Allocator::Allocator
Allocator(Allocator< U > const &)
Definition: MyGUI_CustomAllocator.h:49
MyGUI::Allocator::operator!=
bool operator!=(Allocator const &a)
Definition: MyGUI_CustomAllocator.h:91
MyGUI::Allocator::address
const_pointer address(const_reference r)
Definition: MyGUI_CustomAllocator.h:56
MyGUI::Allocator::rebind
Definition: MyGUI_Allocator.h:32
MyGUI::Allocator::const_pointer
const typedef value_type * const_pointer
Definition: MyGUI_Allocator.h:23
MyGUI::Allocator::value_type
T value_type
Definition: MyGUI_CustomAllocator.h:29
MyGUI::Allocator::address
pointer address(reference r)
Definition: MyGUI_CustomAllocator.h:52
MyGUI::Allocator::reference
value_type & reference
Definition: MyGUI_CustomAllocator.h:32
MyGUI::Allocator::deallocate
void deallocate(pointer p, size_type)
Definition: MyGUI_CustomAllocator.h:66
MyGUI::Allocator::destroy
void destroy(pointer p)
Definition: MyGUI_CustomAllocator.h:82
MyGUI::Allocator::pointer
value_type * pointer
Definition: MyGUI_CustomAllocator.h:30
MyGUI::Allocator::allocate
pointer allocate(size_type cnt, typename std::allocator< void >::const_pointer=0)
Definition: MyGUI_CustomAllocator.h:62
MyGUI::Allocator::size_type
std::size_t size_type
Definition: MyGUI_CustomAllocator.h:34
MyGUI::Allocator
Definition: MyGUI_Allocator.h:17
MyGUI::Allocator::construct
void construct(pointer p, const T &t)
Definition: MyGUI_CustomAllocator.h:78
MyGUI::Allocator::~Allocator
~Allocator()
Definition: MyGUI_CustomAllocator.h:47
MyGUI
Definition: MyGUI_ActionController.h:14
MyGUI::Allocator::rebind::other
Allocator< U > other
Definition: MyGUI_CustomAllocator.h:42
MyGUI::Allocator::Allocator
Allocator()
Definition: MyGUI_CustomAllocator.h:46