|
UDK 3.2.7 C/C++ API Reference
|
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 00002 /* 00003 * This file is part of the LibreOffice project. 00004 * 00005 * This Source Code Form is subject to the terms of the Mozilla Public 00006 * License, v. 2.0. If a copy of the MPL was not distributed with this 00007 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 00008 * 00009 * This file incorporates work covered by the following license notice: 00010 * 00011 * Licensed to the Apache Software Foundation (ASF) under one or more 00012 * contributor license agreements. See the NOTICE file distributed 00013 * with this work for additional information regarding copyright 00014 * ownership. The ASF licenses this file to you under the Apache 00015 * License, Version 2.0 (the "License"); you may not use this file 00016 * except in compliance with the License. You may obtain a copy of 00017 * the License at http://www.apache.org/licenses/LICENSE-2.0 . 00018 */ 00019 #ifndef INCLUDED_RTL_ALLOCATOR_HXX 00020 #define INCLUDED_RTL_ALLOCATOR_HXX 00021 00022 #include "sal/types.h" 00023 #include "rtl/alloc.h" 00024 #include <cstddef> 00025 00027 00028 //###################################################### 00029 // This is no general purpose STL allocator but one 00030 // necessary to use STL for some implementation but 00031 // avoid linking sal against the STLPort library!!! 00032 // For more information on when and how to define a 00033 // custom stl allocator have a look at Scott Meyers: 00034 // "Effective STL", Nicolai M. Josuttis: 00035 // "The C++ Standard Library - A Tutorial and Reference" 00036 // and at http://www.josuttis.com/cppcode/allocator.html 00037 00038 namespace rtl { 00039 00040 template<class T> 00041 class Allocator 00042 { 00043 public: 00044 typedef T value_type; 00045 typedef T* pointer; 00046 typedef const T* const_pointer; 00047 typedef T& reference; 00048 typedef const T& const_reference; 00049 typedef ::std::size_t size_type; 00050 typedef ::std::ptrdiff_t difference_type; 00051 00052 //----------------------------------------- 00053 template<class U> 00054 struct rebind 00055 { 00056 typedef Allocator<U> other; 00057 }; 00058 00059 //----------------------------------------- 00060 pointer address (reference value) const 00061 { 00062 return &value; 00063 } 00064 00065 //----------------------------------------- 00066 const_pointer address (const_reference value) const 00067 { 00068 return &value; 00069 } 00070 00071 //----------------------------------------- 00072 Allocator() SAL_THROW(()) 00073 {} 00074 00075 //----------------------------------------- 00076 template<class U> 00077 Allocator (SAL_UNUSED_PARAMETER const Allocator<U>&) SAL_THROW(()) 00078 {} 00079 00080 //----------------------------------------- 00081 Allocator(const Allocator&) SAL_THROW(()) 00082 {} 00083 00084 //----------------------------------------- 00085 ~Allocator() SAL_THROW(()) 00086 {} 00087 00088 //----------------------------------------- 00089 size_type max_size() const SAL_THROW(()) 00090 { 00091 return size_type(-1)/sizeof(T); 00092 } 00093 00094 //----------------------------------------- 00095 /* Normally the code for allocate should 00096 throw a std::bad_alloc exception if the 00097 requested memory could not be allocated: 00098 (C++ standard 20.4.1.1): 00099 00100 pointer allocate (size_type n, const void* hint = 0) 00101 { 00102 pointer p = reinterpret_cast<pointer>( 00103 rtl_allocateMemory(sal_uInt32(n * sizeof(T)))); 00104 00105 if (NULL == p) 00106 throw ::std::bad_alloc(); 00107 00108 return p; 00109 } 00110 00111 but some compilers do not compile it if exceptions 00112 are not enabled, e.g. GCC under Linux and it is 00113 in general not desired to compile sal with exceptions 00114 enabled. */ 00115 pointer allocate (size_type n, SAL_UNUSED_PARAMETER const void* = 0) 00116 { 00117 return reinterpret_cast<pointer>( 00118 rtl_allocateMemory(sal_uInt32(n * sizeof(T)))); 00119 } 00120 00121 //----------------------------------------- 00122 void deallocate (pointer p, SAL_UNUSED_PARAMETER size_type /* n */) 00123 { 00124 rtl_freeMemory(p); 00125 } 00126 00127 //----------------------------------------- 00128 #if defined HAVE_CXX11_PERFECT_FORWARDING 00129 template< typename... Args > 00130 void construct (pointer p, Args &&... value) 00131 { 00132 new ((void*)p)T(std::forward< Args >(value)...); 00133 } 00134 #else 00135 void construct (pointer p, const T& value) 00136 { 00137 new ((void*)p)T(value); 00138 } 00139 #endif 00140 00141 //----------------------------------------- 00142 void destroy (pointer p) 00143 { 00144 p->~T(); 00145 (void)p; //MSVC2005 annoyingly warns this is unused 00146 } 00147 }; 00148 00149 //###################################################### 00150 // Custom STL allocators must be stateless (see 00151 // references above) that's why the operators below 00152 // return always true or false 00153 00154 template<class T, class U> inline bool operator ==( 00155 SAL_UNUSED_PARAMETER const Allocator<T>&, 00156 SAL_UNUSED_PARAMETER const Allocator<U>&) SAL_THROW(()) 00157 { 00158 return true; 00159 } 00160 00161 template<class T, class U> 00162 inline bool operator!= (const Allocator<T>&, const Allocator<U>&) SAL_THROW(()) 00163 { 00164 return false; 00165 } 00166 00167 } /* namespace rtl */ 00168 00173 namespace _STL 00174 { 00175 template<class T, class U> 00176 inline ::rtl::Allocator<U> & __stl_alloc_rebind (::rtl::Allocator<T> & a, U const *) 00177 { 00178 return (::rtl::Allocator<U>&)(a); 00179 } 00180 } 00181 00183 00184 #endif /* INCLUDED_RTL_ALLOCATOR_HXX */ 00185 00186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
1.7.3