STLSoft - ... Robust, Lightweight, Cross-platform, Template Software ... ATLSTL - Template Software for the Active Template Library COMSTL - The Standard Template Library meets the Component Object Model .netSTL - Standard Template Library meets the Microsoft.NET Common Language Runtime InetSTL - The Standard Template Library meets WinInet MFCSTL - Template Software for the Microsoft Foundation Classes UNIXSTL - Template Software for the UNIX Operating System WinSTL - where the Standard Template Library meets the Win32 API

Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

stlsoft_null_allocator.h

Go to the documentation of this file.
00001 /* 
00002  * File:        stlsoft_null_allocator.h (originally MRAtors.h; ::SynesisCrt)
00003  *
00004  * Purpose:     stlsoft_null_allocator class - doesn't allocate.
00005  *
00006  * Created:     2nd January 2001
00007  * Updated:     11th September 2004
00008  *
00009  * Home:        http://stlsoft.org/
00010  *
00011  * Copyright (c) 2001-2004, 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 
00044 
00045 #ifndef STLSOFT_INCL_H_STLSOFT_NULL_ALLOCATOR
00046 #define STLSOFT_INCL_H_STLSOFT_NULL_ALLOCATOR
00047 
00048 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00049 # define STLSOFT_VER_H_STLSOFT_NULL_ALLOCATOR_MAJOR       2
00050 # define STLSOFT_VER_H_STLSOFT_NULL_ALLOCATOR_MINOR       0
00051 # define STLSOFT_VER_H_STLSOFT_NULL_ALLOCATOR_REVISION    1
00052 # define STLSOFT_VER_H_STLSOFT_NULL_ALLOCATOR_EDIT        47
00053 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00054 
00055 /* 
00056  * Includes
00057  */
00058 
00059 #ifndef STLSOFT_INCL_H_STLSOFT
00060 # include "stlsoft.h"                   // Include the STLSoft root header
00061 #endif /* !STLSOFT_INCL_H_STLSOFT */
00062 #ifndef STLSOFT_INCL_H_STLSOFT_ALLOCATOR_BASE
00063 # include "stlsoft_allocator_base.h"    // Include the STLSoft root header
00064 #endif /* !STLSOFT_INCL_H_STLSOFT_ALLOCATOR_BASE */
00065 #ifndef STLSOFT_INCL_H_STLSOFT_SAP_CAST
00066 # include "stlsoft_sap_cast.h"
00067 #endif /* !STLSOFT_INCL_H_STLSOFT_SAP_CAST */
00068 
00069 /* 
00070  * Namespace
00071  */
00072 
00073 #ifndef _STLSOFT_NO_NAMESPACE
00074 namespace stlsoft
00075 {
00076 #endif /* _STLSOFT_NO_NAMESPACE */
00077 
00078 /* 
00079 
00082 
00086 
00091 
00092 /* 
00093  * Classes
00094  */
00095 
00096 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00097 
00098 template <ss_typename_param_k T>
00099 class null_allocator;
00100 
00101 // Specialisation for void
00102 STLSOFT_TEMPLATE_SPECIALISATION
00103 class null_allocator<void>
00104 {
00105 public:
00106     typedef void                                value_type;
00107     typedef null_allocator<void>                class_type;
00108     typedef void                                *pointer;
00109     typedef void const                          *const_pointer;
00110     typedef ptrdiff_t                           difference_type;
00111     typedef ss_size_t                           size_type;
00112 
00113 #ifdef STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT
00114 
00115     template <ss_typename_param_k U>
00116     struct rebind
00117     {
00118         typedef null_allocator<U>             other;
00119     };
00120 #endif /* STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT */
00121 };
00122 
00123 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00124 
00128 template <ss_typename_param_k T>
00129 class null_allocator
00130 {
00131 public:
00133     typedef T                                   value_type;
00135     typedef null_allocator<value_type>          class_type;
00137     typedef value_type                          *pointer;
00139     typedef value_type const                    *const_pointer;
00141     typedef value_type                          &reference;
00143     typedef value_type const                    &const_reference;
00145     typedef ptrdiff_t                           difference_type;
00147     typedef ss_size_t                           size_type;
00149 #ifdef __STLSOFT_CF_ALLOCATOR_TYPED_DEALLOCATE_POINTER
00150     typedef pointer                             deallocate_pointer;
00151 #else
00152     typedef void                                *deallocate_pointer;
00153 #endif /* __STLSOFT_COMPILER_IS_MSVC && _MSC_VER == 1200 */
00154 
00155 #ifdef STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT
00156 
00157     template <ss_typename_param_k U>
00158     struct rebind
00159     {
00160         typedef null_allocator<U>             other;
00161     };
00162 #endif /* STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT */
00163 
00164 // Construction
00165 public:
00167     null_allocator() stlsoft_throw_0()
00168     {}
00170 #ifndef __STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
00171     null_allocator(const null_allocator &/* rhs */) stlsoft_throw_0()
00172     {}
00173 #else
00174     template <ss_typename_param_k U>
00175     null_allocator(const null_allocator<U> &/* rhs */) stlsoft_throw_0()
00176     {}
00177 #endif /* !__STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT */
00178 
00179     ~null_allocator() stlsoft_throw_0()
00180     {}
00181 
00182 // Attributes
00183 public:
00185     size_type max_size() const stlsoft_throw_0()
00186     {
00187         return static_cast<size_type>(-1) / sizeof(value_type);
00188     }
00189 
00190 // Conversion
00191 public:
00195     pointer address(reference x) const stlsoft_throw_0()
00196     {
00197         return &x;
00198     }
00202     const_pointer address(const_reference x) const stlsoft_throw_0()
00203     {
00204         return &x;
00205     }
00206 
00207 // Allocation
00208 public:
00214 #ifdef __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS
00215     static
00216 #endif /* __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS */
00217     pointer allocate(size_type n, null_allocator<void>::const_pointer pv = NULL)
00218     {
00219         STLSOFT_SUPPRESS_UNUSED(n);
00220         STLSOFT_SUPPRESS_UNUSED(pv);
00221 
00222         null_allocator<void>::pointer p   =   NULL;
00223 
00224 #ifdef __STLSOFT_CF_THROW_BAD_ALLOC
00225         if(p == NULL)
00226         {
00227             throw stlsoft_ns_qual_std(bad_alloc)();
00228         }
00229 #endif /* __STLSOFT_CF_THROW_BAD_ALLOC */
00230 
00231         return static_cast<pointer>(p);
00232     }
00233 
00234 #ifdef __STLSOFT_CF_ALLOCATOR_CHARALLOC_METHOD
00235 
00236     char *_Charalloc(size_type n)
00237     {
00238         return sap_cast<char*>(allocate(n, NULL));
00239     }
00240 #endif /* __STLSOFT_CF_ALLOCATOR_CHARALLOC_METHOD */
00241 
00246 #ifdef __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS
00247     static
00248 #endif /* __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS */
00249     void deallocate(pointer p, size_type n)
00250     {
00251         STLSOFT_SUPPRESS_UNUSED(p);
00252         STLSOFT_SUPPRESS_UNUSED(n);
00253     }
00254 
00258     void deallocate(pointer p)
00259     {
00260         STLSOFT_SUPPRESS_UNUSED(p);
00261     }
00262 
00263 // Operations
00264 public:
00269     void construct(pointer p, value_type const &x)
00270     {
00271         stlsoft_assert(p != NULL);
00272 
00273         new(p) value_type(x);
00274     }
00275 
00279     void construct(pointer p)
00280     {
00281         stlsoft_assert(p != NULL);
00282 
00283         new(p) value_type();
00284     }
00285 
00289     void destroy(pointer p) stlsoft_throw_0()
00290     {
00291         stlsoft_assert(p != NULL);
00292 
00293         stlsoft_destroy_instance(T, value_type, p);
00294     }
00295 
00296 // Not to be implemented
00297 private:
00298     class_type const &operator =(class_type const &rhs);
00299 };
00300 
00301 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00302 
00303 template <ss_typename_param_k T>
00304 inline ss_bool_t operator ==(const null_allocator<T> &/* lhs */, const null_allocator<T> &/* rhs */)
00305 {
00306     return ss_true_v;
00307 }
00308 
00309 template <ss_typename_param_k T>
00310 inline ss_bool_t operator !=(const null_allocator<T> &/* lhs */, const null_allocator<T> &/* rhs */)
00311 {
00312     return ss_false_v;
00313 }
00314 
00315 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00316 
00318 // Unit-testing
00319 
00320 #ifdef STLSOFT_UNITTEST
00321 
00322 namespace unittest
00323 {
00324     ss_bool_t test_stlsoft_null_allocator(unittest_reporter *r)
00325     {
00326         ss_bool_t               bSuccess    =   true;
00327 
00328         unittest_initialiser    init(r, "STLSoft", "null_allocator", __FILE__);
00329 
00330         typedef null_allocator<int>  int_allocator_t;
00331 
00332         int             i;
00333         int_allocator_t ator1;
00334 
00335         ator1.construct(&i, 1968);
00336 
00337         if(1968 != i)
00338         {
00339             r->report("failed to construct ", __LINE__);
00340             bSuccess = false;
00341         }
00342 
00343 #ifdef __STLSOFT_CF_THROW_BAD_ALLOC
00344         try
00345         {
00346             ator1.allocate(1);
00347 
00348             r->report("null_allocator provided a non-NULL allocation ", __LINE__);
00349             bSuccess = false;
00350         }
00351         catch(std::bad_alloc &)
00352         {
00353         }
00354 #else /* ? __STLSOFT_CF_THROW_BAD_ALLOC */
00355         if(ator1.allocate(1) != NULL)
00356         {
00357             r->report("null_allocator provided a non-NULL allocation ", __LINE__);
00358             bSuccess = false;
00359         }
00360 #endif /* __STLSOFT_CF_THROW_BAD_ALLOC */
00361 
00362         return bSuccess;
00363     }
00364 
00365     unittest_registrar    unittest_stlsoft_null_allocator(test_stlsoft_null_allocator);
00366 
00367 } // namespace unittest
00368 
00369 #endif /* STLSOFT_UNITTEST */
00370 
00371 /* 
00372 
00374 
00375 /* 
00376 
00377 #ifndef _STLSOFT_NO_NAMESPACE
00378 } // namespace stlsoft
00379 #endif /* _STLSOFT_NO_NAMESPACE */
00380 
00381 /* 
00382 
00383 #endif /* !STLSOFT_INCL_H_STLSOFT_NULL_ALLOCATOR */
00384 
00385 /* 

STLSoft Libraries documentation © Synesis Software Pty Ltd, 2001-2004