00001 /* ///////////////////////////////////////////////////////////////////////// 00002 * File: stlsoft/memory/null_allocator.hpp 00003 * 00004 * Purpose: null_allocator class - doesn't allocate. 00005 * 00006 * Created: 2nd January 2001 00007 * Updated: 10th August 2009 00008 * 00009 * Home: http://stlsoft.org/ 00010 * 00011 * Copyright (c) 2001-2009, Matthew Wilson and Synesis Software 00012 * All rights reserved. 00013 * 00014 * Redistribution and use in source and binary forms, with or without 00015 * modification, are permitted provided that the following conditions are met: 00016 * 00017 * - Redistributions of source code must retain the above copyright notice, this 00018 * list of conditions and the following disclaimer. 00019 * - Redistributions in binary form must reproduce the above copyright notice, 00020 * this list of conditions and the following disclaimer in the documentation 00021 * and/or other materials provided with the distribution. 00022 * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of 00023 * any contributors may be used to endorse or promote products derived from 00024 * this software without specific prior written permission. 00025 * 00026 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00027 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00028 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00029 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00030 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00031 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00032 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00033 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00034 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00035 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00036 * POSSIBILITY OF SUCH DAMAGE. 00037 * 00038 * ////////////////////////////////////////////////////////////////////// */ 00039 00040 00047 #ifndef STLSOFT_INCL_STLSOFT_MEMORY_HPP_NULL_ALLOCATOR 00048 #define STLSOFT_INCL_STLSOFT_MEMORY_HPP_NULL_ALLOCATOR 00049 00050 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION 00051 # define STLSOFT_VER_STLSOFT_MEMORY_HPP_NULL_ALLOCATOR_MAJOR 4 00052 # define STLSOFT_VER_STLSOFT_MEMORY_HPP_NULL_ALLOCATOR_MINOR 0 00053 # define STLSOFT_VER_STLSOFT_MEMORY_HPP_NULL_ALLOCATOR_REVISION 3 00054 # define STLSOFT_VER_STLSOFT_MEMORY_HPP_NULL_ALLOCATOR_EDIT 79 00055 #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */ 00056 00057 /* ///////////////////////////////////////////////////////////////////////// 00058 * Compatibility 00059 */ 00060 00061 /* 00062 [DocumentationStatus:Ready] 00063 */ 00064 00065 /* ///////////////////////////////////////////////////////////////////////// 00066 * Includes 00067 */ 00068 00069 #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT 00070 # include <stlsoft/stlsoft.h> 00071 #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */ 00072 #ifndef STLSOFT_INCL_STLSOFT_MEMORY_HPP_ALLOCATOR_BASE 00073 # include <stlsoft/memory/allocator_base.hpp> 00074 #endif /* !STLSOFT_INCL_STLSOFT_MEMORY_HPP_ALLOCATOR_BASE */ 00075 00076 #ifdef STLSOFT_CF_EXCEPTION_SUPPORT 00077 # include <new> 00078 #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */ 00079 00080 /* ///////////////////////////////////////////////////////////////////////// 00081 * Namespace 00082 */ 00083 00084 #ifndef _STLSOFT_NO_NAMESPACE 00085 namespace stlsoft 00086 { 00087 #endif /* _STLSOFT_NO_NAMESPACE */ 00088 00089 /* ///////////////////////////////////////////////////////////////////////// 00090 * Classes 00091 */ 00092 00105 template <ss_typename_param_k T> 00106 class null_allocator 00107 : public allocator_base<T, null_allocator<T> > 00108 { 00109 private: 00110 typedef allocator_base<T, null_allocator<T> > parent_class_type; 00111 public: 00113 typedef null_allocator<T> class_type; 00115 typedef ss_typename_type_k parent_class_type::value_type value_type; 00117 typedef ss_typename_type_k parent_class_type::pointer pointer; 00119 typedef ss_typename_type_k parent_class_type::const_pointer const_pointer; 00121 typedef ss_typename_type_k parent_class_type::reference reference; 00123 typedef ss_typename_type_k parent_class_type::const_reference const_reference; 00125 typedef ss_typename_type_k parent_class_type::difference_type difference_type; 00127 typedef ss_typename_type_k parent_class_type::size_type size_type; 00128 00129 public: 00130 #ifdef STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT 00132 template <ss_typename_param_k U> 00133 struct rebind 00134 { 00135 typedef null_allocator<U> other; 00136 }; 00137 #endif /* STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT */ 00138 00141 public: 00143 null_allocator() stlsoft_throw_0() 00144 {} 00146 #ifdef STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT 00147 template <ss_typename_param_k U> 00148 null_allocator(null_allocator<U> const&) 00149 {} 00150 #else /* ? STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT */ 00151 null_allocator(class_type const&) 00152 {} 00153 #endif /* STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT */ 00155 00156 private: 00157 friend class allocator_base<T, null_allocator<T> >; 00158 00159 void *do_allocate(size_type n, void const* hint) 00160 { 00161 STLSOFT_SUPPRESS_UNUSED(n); 00162 STLSOFT_SUPPRESS_UNUSED(hint); 00163 00164 #ifdef STLSOFT_CF_EXCEPTION_SUPPORT 00165 STLSOFT_THROW_X(stlsoft_ns_qual_std(bad_alloc)()); 00166 #else /* ? STLSOFT_CF_EXCEPTION_SUPPORT */ 00167 // TODO: change this to 00168 00169 STLSOFT_MESSAGE_ASSERT("may not be called when exception support is not enabled", 0); 00170 #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */ 00171 00172 return NULL; 00173 } 00174 void do_deallocate(void *pv, size_type n) 00175 { 00176 STLSOFT_SUPPRESS_UNUSED(n); 00177 STLSOFT_SUPPRESS_UNUSED(pv); 00178 } 00179 void do_deallocate(void *pv) 00180 { 00181 STLSOFT_SUPPRESS_UNUSED(pv); 00182 } 00183 }; 00184 00185 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION 00186 00187 // Specialisation for void 00188 STLSOFT_TEMPLATE_SPECIALISATION 00189 class null_allocator<void> 00190 { 00191 public: 00192 typedef void value_type; 00193 typedef null_allocator<void> class_type; 00194 typedef void* pointer; 00195 typedef void const* const_pointer; 00196 typedef ss_ptrdiff_t difference_type; 00197 typedef ss_size_t size_type; 00198 00199 #ifdef STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT 00201 template <ss_typename_param_k U> 00202 struct rebind 00203 { 00204 typedef null_allocator<U> other; 00205 }; 00206 #endif /* STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT */ 00207 }; 00208 00209 #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */ 00210 00211 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION 00212 00213 template <ss_typename_param_k T> 00214 inline ss_bool_t operator ==(const null_allocator<T> &/* lhs */, const null_allocator<T> &/* rhs */) 00215 { 00216 return ss_true_v; 00217 } 00218 00219 template <ss_typename_param_k T> 00220 inline ss_bool_t operator !=(const null_allocator<T> &/* lhs */, const null_allocator<T> &/* rhs */) 00221 { 00222 return ss_false_v; 00223 } 00224 00225 #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */ 00226 00228 // Unit-testing 00229 00230 #ifdef STLSOFT_UNITTEST 00231 # include "./unittest/null_allocator_unittest_.h" 00232 #endif /* STLSOFT_UNITTEST */ 00233 00234 /* ////////////////////////////////////////////////////////////////////// */ 00235 00236 #ifndef _STLSOFT_NO_NAMESPACE 00237 } // namespace stlsoft 00238 #endif /* _STLSOFT_NO_NAMESPACE */ 00239 00240 /* ////////////////////////////////////////////////////////////////////// */ 00241 00242 #endif /* !STLSOFT_INCL_STLSOFT_MEMORY_HPP_NULL_ALLOCATOR */ 00243 00244 /* ///////////////////////////// end of file //////////////////////////// */