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