My Project
UDK 3.2.7 C/C++ API Reference
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
allocator.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * Copyright 2000, 2010 Oracle and/or its affiliates.
7  *
8  * OpenOffice.org - a multi-platform office productivity suite
9  *
10  * This file is part of OpenOffice.org.
11  *
12  * OpenOffice.org is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License version 3
14  * only, as published by the Free Software Foundation.
15  *
16  * OpenOffice.org is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License version 3 for more details
20  * (a copy is included in the LICENSE file that accompanied this code).
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * version 3 along with OpenOffice.org. If not, see
24  * <http://www.openoffice.org/license.html>
25  * for a copy of the LGPLv3 License.
26  *
27  ************************************************************************/
28 #if !defined INCLUDED_RTL_ALLOCATOR_HXX
29 #define INCLUDED_RTL_ALLOCATOR_HXX
30 
31 #if ! defined(_SAL_TYPES_H_)
32 #include "sal/types.h"
33 #endif
34 #if ! defined(_RTL_ALLOC_H_)
35 #include "rtl/alloc.h"
36 #endif
37 
38 #include <cstddef>
39 
41 
42 //######################################################
43 // This is no general purpose STL allocator but one
44 // necessary to use STL for some implementation but
45 // avoid linking sal against the STLPort library!!!
46 // For more information on when and how to define a
47 // custom stl allocator have a look at Scott Meyers:
48 // "Effective STL", Nicolai M. Josuttis:
49 // "The C++ Standard Library - A Tutorial and Reference"
50 // and at http://www.josuttis.com/cppcode/allocator.html
51 
52 namespace rtl {
53 
54 template<class T>
55 class Allocator
56 {
57 public:
58  typedef T value_type;
59  typedef T* pointer;
60  typedef const T* const_pointer;
61  typedef T& reference;
62  typedef const T& const_reference;
63  typedef ::std::size_t size_type;
64  typedef ::std::ptrdiff_t difference_type;
65 
66  //-----------------------------------------
67  template<class U>
68  struct rebind
69  {
70  typedef Allocator<U> other;
71  };
72 
73  //-----------------------------------------
74  pointer address (reference value) const
75  {
76  return &value;
77  }
78 
79  //-----------------------------------------
80  const_pointer address (const_reference value) const
81  {
82  return &value;
83  }
84 
85  //-----------------------------------------
86  Allocator() SAL_THROW(())
87  {}
88 
89  //-----------------------------------------
90  template<class U>
91  Allocator (SAL_UNUSED_PARAMETER const Allocator<U>&) SAL_THROW(())
92  {}
93 
94  //-----------------------------------------
95  Allocator(const Allocator&) SAL_THROW(())
96  {}
97 
98  //-----------------------------------------
99  ~Allocator() SAL_THROW(())
100  {}
101 
102  //-----------------------------------------
103  size_type max_size() const SAL_THROW(())
104  {
105  return size_type(-1)/sizeof(T);
106  }
107 
108  //-----------------------------------------
109  /* Normally the code for allocate should
110  throw a std::bad_alloc exception if the
111  requested memory could not be allocated:
112  (C++ standard 20.4.1.1):
113 
114  pointer allocate (size_type n, const void* hint = 0)
115  {
116  pointer p = reinterpret_cast<pointer>(
117  rtl_allocateMemory(sal_uInt32(n * sizeof(T))));
118 
119  if (NULL == p)
120  throw ::std::bad_alloc();
121 
122  return p;
123  }
124 
125  but some compilers do not compile it if exceptions
126  are not enabled, e.g. GCC under Linux and it is
127  in general not desired to compile sal with exceptions
128  enabled. */
129  pointer allocate (size_type n, SAL_UNUSED_PARAMETER const void* = 0)
130  {
131  return reinterpret_cast<pointer>(
132  rtl_allocateMemory(sal_uInt32(n * sizeof(T))));
133  }
134 
135  //-----------------------------------------
136  void deallocate (pointer p, SAL_UNUSED_PARAMETER size_type /* n */)
137  {
138  rtl_freeMemory(p);
139  }
140 
141  //-----------------------------------------
142 #if defined HAVE_CXX11_PERFECT_FORWARDING
143  template< typename... Args >
144  void construct (pointer p, Args &&... value)
145  {
146  new ((void*)p)T(std::forward< Args >(value)...);
147  }
148 #else
149  void construct (pointer p, const T& value)
150  {
151  new ((void*)p)T(value);
152  }
153 #endif
154 
155  //-----------------------------------------
156  void destroy (pointer p)
157  {
158  p->~T();
159  (void)p; //MSVC2005 annoyingly warns this is unused
160  }
161 };
162 
163 //######################################################
164 // Custom STL allocators must be stateless (see
165 // references above) that's why the operators below
166 // return always true or false
167 
168 template<class T, class U> inline bool operator ==(
169  SAL_UNUSED_PARAMETER const Allocator<T>&,
170  SAL_UNUSED_PARAMETER const Allocator<U>&) SAL_THROW(())
171 {
172  return true;
173 }
174 
175 template<class T, class U>
176 inline bool operator!= (const Allocator<T>&, const Allocator<U>&) SAL_THROW(())
177 {
178  return false;
179 }
180 
181 } /* namespace rtl */
182 
187 namespace _STL
188 {
189  template<class T, class U>
190  inline ::rtl::Allocator<U> & __stl_alloc_rebind (::rtl::Allocator<T> & a, U const *)
191  {
192  return (::rtl::Allocator<U>&)(a);
193  }
194 }
195 
197 
198 #endif /* INCLUDED_RTL_ALLOCATOR_HXX */
199 
200 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */