MyGUI 3.2.0

- Main Page
- Related Pages
- Namespaces
- Data Structures
- Files
- Examples

- File List
- Globals

MyGUI_Allocator.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_ALLOCATOR_H__
23 #define __MYGUI_ALLOCATOR_H__
24
25 #include <memory>
26 #include <limits>
27
28 namespace MyGUI
29 {
30
31 template<typename T>
32 class Allocator
33 {
34 public:
35 // typedefs
36 typedef T value_type;
37 typedef value_type* pointer;
38 typedef const value_type* const_pointer;
39 typedef value_type& reference;
40 typedef const value_type& const_reference;
41 typedef std::size_t size_type;
42 typedef std::ptrdiff_t difference_type;
43
44 public:
45 // convert an allocator<T> to allocator<U>
46 template<typename U>
47 struct rebind
48 {
49 typedef Allocator<U> other;
50 };
51
52 public:
53 inline explicit Allocator() { }
54 inline ~Allocator() { }
55 template<typename U>
56 inline explicit Allocator(Allocator<U> const&) { }
57
58 // address
59 inline pointer address(reference r)
60 {
61 return &r;
62 }
63 inline const_pointer address(const_reference r)
64 {
65 return &r;
66 }
67
68 // memory allocation
69 inline pointer allocate(size_type cnt, typename std::allocator<void>::const_pointer = 0)
70 {
71 return reinterpret_cast<pointer>(::operator new (cnt * sizeof (T)));
72 }
73 inline void deallocate(pointer p, size_type)
74 {
75 ::operator delete (p);
76 }
77
78 // size
79 inline size_type max_size() const
80 {
81 return (std::numeric_limits<size_type>::max)() / sizeof(T);
82 }
83
84 // construction/destruction
85 inline void construct(pointer p, const T& t)
86 {
87 new (p) T(t);
88 }
89 inline void destroy(pointer p)
90 {
91 p->~T();
92 }
93
94 inline bool operator==(Allocator const&)
95 {
96 return true;
97 }
98 inline bool operator!=(Allocator const& a)
99 {
100 return !operator==(a);
101 }
102 };
103
104 } // namespace MyGUI
105
106 #endif // __MYGUI_ALLOCATOR_H__
MyGUI::Allocator::Allocator
Allocator()
Definition: MyGUI_Allocator.h:53
MyGUI::Allocator::operator==
bool operator==(Allocator const &)
Definition: MyGUI_Allocator.h:94
MyGUI::Allocator::const_reference
const value_type & const_reference
Definition: MyGUI_Allocator.h:40
MyGUI::Allocator::difference_type
std::ptrdiff_t difference_type
Definition: MyGUI_Allocator.h:42
MyGUI::Allocator::operator!=
bool operator!=(Allocator const &a)
Definition: MyGUI_Allocator.h:98
MyGUI::Allocator
Definition: MyGUI_Allocator.h:32
MyGUI::Allocator::rebind::other
Allocator< U > other
Definition: MyGUI_Allocator.h:49
MyGUI::Allocator::Allocator
Allocator(Allocator< U > const &)
Definition: MyGUI_Allocator.h:56
MyGUI::Allocator::max_size
size_type max_size() const
Definition: MyGUI_Allocator.h:79
MyGUI::Allocator::value_type
T value_type
Definition: MyGUI_Allocator.h:36
MyGUI::Allocator::deallocate
void deallocate(pointer p, size_type)
Definition: MyGUI_Allocator.h:73
MyGUI::Allocator::address
const_pointer address(const_reference r)
Definition: MyGUI_Allocator.h:63
MyGUI::Allocator::const_pointer
const value_type * const_pointer
Definition: MyGUI_Allocator.h:38
MyGUI::Allocator::rebind
Definition: MyGUI_Allocator.h:47
MyGUI::Allocator::destroy
void destroy(pointer p)
Definition: MyGUI_Allocator.h:89
MyGUI::Allocator::reference
value_type & reference
Definition: MyGUI_Allocator.h:39
MyGUI::Allocator::size_type
std::size_t size_type
Definition: MyGUI_Allocator.h:41
MyGUI::Allocator::address
pointer address(reference r)
Definition: MyGUI_Allocator.h:59
MyGUI::Allocator::pointer
value_type * pointer
Definition: MyGUI_Allocator.h:37
MyGUI::Allocator::allocate
pointer allocate(size_type cnt, typename std::allocator< void >::const_pointer=0)
Definition: MyGUI_Allocator.h:69
MyGUI::Allocator::~Allocator
~Allocator()
Definition: MyGUI_Allocator.h:54
MyGUI::Allocator::construct
void construct(pointer p, const T &t)
Definition: MyGUI_Allocator.h:85