UDK 3.2.7 C/C++ API Reference
rtl/allocator.hxx
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /*************************************************************************
00003  *
00004  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
00005  *
00006  * Copyright 2000, 2010 Oracle and/or its affiliates.
00007  *
00008  * OpenOffice.org - a multi-platform office productivity suite
00009  *
00010  * This file is part of OpenOffice.org.
00011  *
00012  * OpenOffice.org is free software: you can redistribute it and/or modify
00013  * it under the terms of the GNU Lesser General Public License version 3
00014  * only, as published by the Free Software Foundation.
00015  *
00016  * OpenOffice.org is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU Lesser General Public License version 3 for more details
00020  * (a copy is included in the LICENSE file that accompanied this code).
00021  *
00022  * You should have received a copy of the GNU Lesser General Public License
00023  * version 3 along with OpenOffice.org.  If not, see
00024  * <http://www.openoffice.org/license.html>
00025  * for a copy of the LGPLv3 License.
00026  *
00027  ************************************************************************/
00028 #if !defined INCLUDED_RTL_ALLOCATOR_HXX
00029 #define INCLUDED_RTL_ALLOCATOR_HXX
00030 
00031 #if ! defined(_SAL_TYPES_H_)
00032 #include "sal/types.h"
00033 #endif
00034 #if ! defined(_RTL_ALLOC_H_)
00035 #include "rtl/alloc.h"
00036 #endif
00037 
00038 #include <cstddef>
00039 
00041 
00042 //######################################################
00043 // This is no general purpose STL allocator but one
00044 // necessary to use STL for some implementation but
00045 // avoid linking sal against the STLPort library!!!
00046 // For more information on when and how to define a
00047 // custom stl allocator have a look at Scott Meyers:
00048 // "Effective STL", Nicolai M. Josuttis:
00049 // "The C++ Standard Library - A Tutorial and Reference"
00050 // and at http://www.josuttis.com/cppcode/allocator.html
00051 
00052 namespace rtl {
00053 
00054 template<class T>
00055 class Allocator
00056 {
00057 public:
00058     typedef T value_type;
00059     typedef T* pointer;
00060     typedef const T* const_pointer;
00061     typedef T& reference;
00062     typedef const T& const_reference;
00063     typedef ::std::size_t size_type;
00064     typedef ::std::ptrdiff_t difference_type;
00065 
00066     //-----------------------------------------
00067     template<class U>
00068     struct rebind
00069     {
00070         typedef Allocator<U> other;
00071     };
00072 
00073     //-----------------------------------------
00074     pointer address (reference value) const
00075     {
00076         return &value;
00077     }
00078 
00079     //-----------------------------------------
00080     const_pointer address (const_reference value) const
00081     {
00082         return &value;
00083     }
00084 
00085     //-----------------------------------------
00086     Allocator() SAL_THROW(())
00087     {}
00088 
00089     //-----------------------------------------
00090     template<class U>
00091     Allocator (SAL_UNUSED_PARAMETER const Allocator<U>&) SAL_THROW(())
00092     {}
00093 
00094     //-----------------------------------------
00095     Allocator(const Allocator&) SAL_THROW(())
00096     {}
00097 
00098     //-----------------------------------------
00099     ~Allocator() SAL_THROW(())
00100     {}
00101 
00102     //-----------------------------------------
00103     size_type max_size() const SAL_THROW(())
00104     {
00105         return size_type(-1)/sizeof(T);
00106     }
00107 
00108     //-----------------------------------------
00109     /* Normally the code for allocate should
00110        throw a std::bad_alloc exception if the
00111        requested memory could not be allocated:
00112        (C++ standard 20.4.1.1):
00113 
00114        pointer allocate (size_type n, const void* hint = 0)
00115        {
00116          pointer p = reinterpret_cast<pointer>(
00117              rtl_allocateMemory(sal_uInt32(n * sizeof(T))));
00118 
00119          if (NULL == p)
00120              throw ::std::bad_alloc();
00121 
00122          return p;
00123        }
00124 
00125        but some compilers do not compile it if exceptions
00126        are not enabled, e.g. GCC under Linux and it is
00127        in general not desired to compile sal with exceptions
00128        enabled. */
00129     pointer allocate (size_type n, SAL_UNUSED_PARAMETER const void* = 0)
00130     {
00131         return reinterpret_cast<pointer>(
00132             rtl_allocateMemory(sal_uInt32(n * sizeof(T))));
00133     }
00134 
00135     //-----------------------------------------
00136     void deallocate (pointer p, SAL_UNUSED_PARAMETER size_type /* n */)
00137     {
00138         rtl_freeMemory(p);
00139     }
00140 
00141     //-----------------------------------------
00142     void construct (pointer p, const T& value)
00143     {
00144         new ((void*)p)T(value);
00145     }
00146 
00147     //-----------------------------------------
00148     void destroy (pointer p)
00149     {
00150         p->~T();
00151         (void)p; //MSVC2005 annoyingly warns this is unused
00152     }
00153 };
00154 
00155 //######################################################
00156 // Custom STL allocators must be stateless (see
00157 // references above) that's why the operators below
00158 // return always true or false
00159 
00160 template<class T, class U> inline bool operator ==(
00161     SAL_UNUSED_PARAMETER const Allocator<T>&,
00162     SAL_UNUSED_PARAMETER const Allocator<U>&) SAL_THROW(())
00163 {
00164     return true;
00165 }
00166 
00167 template<class T, class U>
00168 inline bool operator!= (const Allocator<T>&, const Allocator<U>&) SAL_THROW(())
00169 {
00170     return false;
00171 }
00172 
00173 } /* namespace rtl */
00174 
00179 namespace _STL
00180 {
00181     template<class T, class U>
00182     inline ::rtl::Allocator<U> & __stl_alloc_rebind (::rtl::Allocator<T> & a, U const *)
00183     {
00184         return (::rtl::Allocator<U>&)(a);
00185     }
00186 }
00187 
00189 
00190 #endif /* INCLUDED_RTL_ALLOCATOR_HXX */
00191 
00192 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines