Update ooo320-m1
[ooovba.git] / sal / inc / rtl / allocator.hxx
blob6ef40cac37c79dc6838bed37ca18adc81d39e1a7
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: allocator.hxx,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #if !defined INCLUDED_RTL_ALLOCATOR_HXX
31 #define INCLUDED_RTL_ALLOCATOR_HXX
33 #if ! defined(_SAL_TYPES_H_)
34 #include "sal/types.h"
35 #endif
36 #if ! defined(_RTL_ALLOC_H_)
37 #include "rtl/alloc.h"
38 #endif
40 #include <cstddef>
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
52 namespace rtl {
54 /** @internal */
55 template<class T>
56 class Allocator
58 public:
59 typedef T value_type;
60 typedef T* pointer;
61 typedef const T* const_pointer;
62 typedef T& reference;
63 typedef const T& const_reference;
64 typedef ::std::size_t size_type;
65 typedef ::std::ptrdiff_t difference_type;
67 //-----------------------------------------
68 template<class U>
69 struct rebind
71 typedef Allocator<U> other;
74 //-----------------------------------------
75 pointer address (reference value) const
77 return &value;
80 //-----------------------------------------
81 const_pointer address (const_reference value) const
83 return &value;
86 //-----------------------------------------
87 Allocator() SAL_THROW(())
90 //-----------------------------------------
91 template<class U>
92 Allocator (const Allocator<U>&) SAL_THROW(())
95 //-----------------------------------------
96 Allocator(const Allocator&) SAL_THROW(())
99 //-----------------------------------------
100 ~Allocator() SAL_THROW(())
103 //-----------------------------------------
104 size_type max_size() const SAL_THROW(())
106 return size_type(-1)/sizeof(T);
109 //-----------------------------------------
110 /* Normally the code for allocate should
111 throw a std::bad_alloc exception if the
112 requested memory could not be allocated:
113 (C++ standard 20.4.1.1):
115 pointer allocate (size_type n, const void* hint = 0)
117 pointer p = reinterpret_cast<pointer>(
118 rtl_allocateMemory(sal_uInt32(n * sizeof(T))));
120 if (NULL == p)
121 throw ::std::bad_alloc();
123 return p;
126 but some compilers do not compile it if exceptions
127 are not enabled, e.g. GCC under Linux and it is
128 in general not desired to compile sal with exceptions
129 enabled. */
130 pointer allocate (size_type n, const void* hint = 0)
132 hint = hint; /* avoid warnings */
133 return reinterpret_cast<pointer>(
134 rtl_allocateMemory(sal_uInt32(n * sizeof(T))));
137 //-----------------------------------------
138 void deallocate (pointer p, size_type /* n */)
140 rtl_freeMemory(p);
143 //-----------------------------------------
144 void construct (pointer p, const T& value)
146 new ((void*)p)T(value);
149 //-----------------------------------------
150 void destroy (pointer p)
152 p->~T();
156 //######################################################
157 // Custom STL allocators must be stateless (see
158 // references above) that's why the operators below
159 // return always true or false
161 /** @internal */
162 template<class T, class U>
163 inline bool operator== (const Allocator<T>&, const Allocator<U>&) SAL_THROW(())
165 return true;
168 /** @internal */
169 template<class T, class U>
170 inline bool operator!= (const Allocator<T>&, const Allocator<U>&) SAL_THROW(())
172 return false;
175 } /* namespace rtl */
177 //######################################################
178 /** REQUIRED BY STLPort (see stlport '_alloc.h'):
179 Hack for compilers that do not support member
180 template classes (e.g. MSVC 6)
182 @internal
184 namespace _STL
186 /** @internal */
187 template<class T, class U>
188 inline ::rtl::Allocator<U> & __stl_alloc_rebind (::rtl::Allocator<T> & a, U const *)
190 return (::rtl::Allocator<U>&)(a);
194 #endif /* INCLUDED_RTL_ALLOCATOR_HXX */