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_new_allocator.h

Go to the documentation of this file.
00001 /* 
00002  * File:        stlsoft_new_allocator.h (originally MRAtors.h; ::SynesisCrt)
00003  *
00004  * Purpose:     stlsoft_new_allocator class - use new & delete operators.
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_NEW_ALLOCATOR
00046 #define STLSOFT_INCL_H_STLSOFT_NEW_ALLOCATOR
00047 
00048 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00049 # define STLSOFT_VER_H_STLSOFT_NEW_ALLOCATOR_MAJOR       2
00050 # define STLSOFT_VER_H_STLSOFT_NEW_ALLOCATOR_MINOR       0
00051 # define STLSOFT_VER_H_STLSOFT_NEW_ALLOCATOR_REVISION    1
00052 # define STLSOFT_VER_H_STLSOFT_NEW_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 new_allocator;
00100 
00101 // Specialisation for void
00102 STLSOFT_TEMPLATE_SPECIALISATION
00103 class new_allocator<void>
00104 {
00105 public:
00106     typedef void                                value_type;
00107     typedef new_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 new_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 new_allocator
00130 {
00131 public:
00133     typedef T                                   value_type;
00135     typedef new_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 new_allocator<U>                other;
00161     };
00162 #endif /* STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT */
00163 
00164 // Construction
00165 public:
00167     new_allocator() stlsoft_throw_0()
00168     {}
00170 #ifndef __STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
00171     new_allocator(const new_allocator &/* rhs */) stlsoft_throw_0()
00172     {}
00173 #else
00174     template <ss_typename_param_k U>
00175     new_allocator(const new_allocator<U> &/* rhs */) stlsoft_throw_0()
00176     {}
00177 #endif /* !__STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT */
00178 
00179     ~new_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, new_allocator<void>::const_pointer pv = NULL)
00218     {
00219         STLSOFT_SUPPRESS_UNUSED(pv);
00220 
00221         new_allocator<void>::pointer p   =   ::operator new (n * sizeof(value_type));
00222 
00223 #ifdef __STLSOFT_CF_THROW_BAD_ALLOC
00224         if(p == NULL)
00225         {
00226             throw stlsoft_ns_qual_std(bad_alloc)();
00227         }
00228 #endif /* __STLSOFT_CF_THROW_BAD_ALLOC */
00229 
00230         return static_cast<pointer>(p);
00231     }
00232 
00233 #ifdef __STLSOFT_CF_ALLOCATOR_CHARALLOC_METHOD
00234 
00235     char *_Charalloc(size_type n)
00236     {
00237         return sap_cast<char*>(allocate(n, NULL));
00238     }
00239 #endif /* __STLSOFT_CF_ALLOCATOR_CHARALLOC_METHOD */
00240 
00245 #ifdef __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS
00246     static
00247 #endif /* __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS */
00248     void deallocate(deallocate_pointer p, size_type n)
00249     {
00250         STLSOFT_SUPPRESS_UNUSED(n);
00251 
00252         ::operator delete (p);
00253     }
00254 
00258     void deallocate(deallocate_pointer p)
00259     {
00260         ::operator delete (p);
00261     }
00262 
00263 // Operations
00264 public:
00269     void construct(pointer p, value_type const &x)
00270     {
00271         stlsoft_assert(NULL != p);
00272 
00273         new(p) value_type(x);
00274     }
00275 
00279     void construct(pointer p)
00280     {
00281         stlsoft_assert(NULL != p);
00282 
00283         new(p) value_type();
00284     }
00285 
00289     void destroy(pointer p) stlsoft_throw_0()
00290     {
00291         stlsoft_assert(NULL != p);
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 new_allocator<T> &/* lhs */, const new_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 new_allocator<T> &/* lhs */, const new_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_new_allocator(unittest_reporter *r)
00325     {
00326         ss_bool_t               bSuccess    =   true;
00327 
00328         unittest_initialiser    init(r, "STLSoft", "new_allocator", __FILE__);
00329 
00330         typedef new_allocator<int>  int_allocator_t;
00331 
00332         int_allocator_t ator1;
00333 
00334         int     *pi1    =   ator1.allocate(100);
00335 
00336         if(NULL != pi1)
00337         {
00338             ator1.construct(pi1, 1968);
00339 
00340             if(1968 != *pi1)
00341             {
00342                 r->report("construct() failed ", __LINE__);
00343                 bSuccess = false;
00344             }
00345         }
00346 
00347         ator1.deallocate(pi1);
00348 
00349         return bSuccess;
00350     }
00351 
00352     unittest_registrar    unittest_stlsoft_new_allocator(test_stlsoft_new_allocator);
00353 
00354 } // namespace unittest
00355 
00356 #endif /* STLSOFT_UNITTEST */
00357 
00358 /* 
00359 
00361 
00362 /* 
00363 
00364 #ifndef _STLSOFT_NO_NAMESPACE
00365 } // namespace stlsoft
00366 #endif /* _STLSOFT_NO_NAMESPACE */
00367 
00368 /* 
00369 
00370 #endif /* !STLSOFT_INCL_H_STLSOFT_NEW_ALLOCATOR */
00371 
00372 /* 

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