MyGUI  3.2.0
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;
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  {
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
60  {
61  return &r;
62  }
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__