1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #ifndef INCLUDED_RTL_ALLOCATOR_HXX
20 #define INCLUDED_RTL_ALLOCATOR_HXX
22 #include "sal/config.h"
24 #include "sal/types.h"
25 #include "rtl/alloc.h"
30 //######################################################
31 // This is no general purpose STL allocator but one
32 // necessary to use STL for some implementation but
33 // avoid linking sal against the STLPort library!!!
34 // For more information on when and how to define a
35 // custom stl allocator have a look at Scott Meyers:
36 // "Effective STL", Nicolai M. Josuttis:
37 // "The C++ Standard Library - A Tutorial and Reference"
38 // and at http://www.josuttis.com/cppcode/allocator.html
48 typedef const T
* const_pointer
;
50 typedef const T
& const_reference
;
51 typedef ::std::size_t size_type
;
52 typedef ::std::ptrdiff_t difference_type
;
54 //-----------------------------------------
58 typedef Allocator
<U
> other
;
61 //-----------------------------------------
62 pointer
address (reference value
) const
67 //-----------------------------------------
68 const_pointer
address (const_reference value
) const
73 //-----------------------------------------
74 Allocator() SAL_THROW(())
77 //-----------------------------------------
79 Allocator (SAL_UNUSED_PARAMETER
const Allocator
<U
>&) SAL_THROW(())
82 //-----------------------------------------
83 Allocator(const Allocator
&) SAL_THROW(())
86 //-----------------------------------------
87 ~Allocator() SAL_THROW(())
90 //-----------------------------------------
91 size_type
max_size() const SAL_THROW(())
93 return size_type(-1)/sizeof(T
);
96 //-----------------------------------------
97 /* Normally the code for allocate should
98 throw a std::bad_alloc exception if the
99 requested memory could not be allocated:
100 (C++ standard 20.4.1.1):
102 pointer allocate (size_type n, const void* hint = 0)
104 pointer p = reinterpret_cast<pointer>(
105 rtl_allocateMemory(sal_uInt32(n * sizeof(T))));
108 throw ::std::bad_alloc();
113 but some compilers do not compile it if exceptions
114 are not enabled, e.g. GCC under Linux and it is
115 in general not desired to compile sal with exceptions
117 pointer
allocate (size_type n
, SAL_UNUSED_PARAMETER
const void* = 0)
119 return reinterpret_cast<pointer
>(
120 rtl_allocateMemory(sal_uInt32(n
* sizeof(T
))));
123 //-----------------------------------------
124 void deallocate (pointer p
, SAL_UNUSED_PARAMETER size_type
/* n */)
129 //-----------------------------------------
130 #if HAVE_CXX11_PERFECT_FORWARDING && !defined(_LIBCPP_VERSION)
131 template< typename
... Args
>
132 void construct (pointer p
, Args
&&... value
)
134 new ((void*)p
)T(std::forward
< Args
>(value
)...);
137 void construct (pointer p
, const T
& value
)
139 new ((void*)p
)T(value
);
143 //-----------------------------------------
144 void destroy (pointer p
)
147 (void)p
; //MSVC2005 annoyingly warns this is unused
151 //######################################################
152 // Custom STL allocators must be stateless (see
153 // references above) that's why the operators below
154 // return always true or false
156 template<class T
, class U
> inline bool operator ==(
157 SAL_UNUSED_PARAMETER
const Allocator
<T
>&,
158 SAL_UNUSED_PARAMETER
const Allocator
<U
>&) SAL_THROW(())
163 template<class T
, class U
>
164 inline bool operator!= (const Allocator
<T
>&, const Allocator
<U
>&) SAL_THROW(())
169 } /* namespace rtl */
173 #endif /* INCLUDED_RTL_ALLOCATOR_HXX */
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */