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  

comstl_task_allocator.h

Go to the documentation of this file.
00001 /* 
00002  * File:        comstl_task_allocator.h
00003  *
00004  * Purpose:     task_allocator class.
00005  *
00006  * Created:     19th January 2002
00007  * Updated:     11th September 2004
00008  *
00009  * Home:        http://stlsoft.org/
00010  *
00011  * Copyright (c) 2002-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 COMSTL_INCL_H_COMSTL_TASK_ALLOCATOR
00046 #define COMSTL_INCL_H_COMSTL_TASK_ALLOCATOR
00047 
00048 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00049 # define COMSTL_VER_H_COMSTL_TASK_ALLOCATOR_MAJOR       2
00050 # define COMSTL_VER_H_COMSTL_TASK_ALLOCATOR_MINOR       0
00051 # define COMSTL_VER_H_COMSTL_TASK_ALLOCATOR_REVISION    1
00052 # define COMSTL_VER_H_COMSTL_TASK_ALLOCATOR_EDIT        50
00053 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00054 
00055 /* 
00056  * Includes
00057  */
00058 
00059 #ifndef COMSTL_INCL_H_COMSTL
00060 # include "comstl.h"                    // Include the COMSTL root header
00061 #endif /* !COMSTL_INCL_H_COMSTL */
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 
00066 /* 
00067  * Namespace
00068  *
00069  * The COMSTL components are contained within the comstl namespace. This is
00070  * actually an alias for stlsoft::comstl_project,
00071  *
00072  * The definition matrix is as follows:
00073  *
00074  * _STLSOFT_NO_NAMESPACE    _COMSTL_NO_NAMESPACE    comstl definition
00075  * ---------------------    --------------------    -----------------
00076  *  not defined              not defined             = stlsoft::comstl_project
00077  *  not defined              defined                 not defined
00078  *  defined                  not defined             comstl
00079  *  defined                  defined                 not defined
00080  *
00081  */
00082 
00083 #ifndef _COMSTL_NO_NAMESPACE
00084 # if defined(_STLSOFT_NO_NAMESPACE) || \
00085      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00086 /* There is no stlsoft namespace, so must define ::comstl */
00087 namespace comstl
00088 {
00089 # else
00090 /* Define stlsoft::comstl_project */
00091 
00092 namespace stlsoft
00093 {
00094 
00095 namespace comstl_project
00096 {
00097 
00098 # endif /* _STLSOFT_NO_NAMESPACE */
00099 #endif /* !_COMSTL_NO_NAMESPACE */
00100 
00101 /* 
00102 
00105 
00109 
00114 
00115 /* 
00116  * Classes
00117  */
00118 
00119 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00120 
00121 template <ss_typename_param_k T>
00122 class task_allocator;
00123 
00124 // Specialisation for void
00125 STLSOFT_TEMPLATE_SPECIALISATION
00126 class task_allocator<void>
00127 {
00128 public:
00129     typedef void                            value_type;
00130     typedef task_allocator<void>            class_type;
00131     typedef void                            *pointer;
00132     typedef void const                      *const_pointer;
00133     typedef ptrdiff_t                       difference_type;
00134     typedef cs_size_t                       size_type;
00135 
00136 #ifdef STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT
00137 
00138     template <ss_typename_param_k U>
00139     struct rebind
00140     {
00141         typedef task_allocator<U>           other;
00142     };
00143 #endif /* STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT */
00144 };
00145 
00146 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00147 
00151 template <ss_typename_param_k T>
00152 class task_allocator
00153 {
00154 public:
00156     typedef T                               value_type;
00158     typedef task_allocator<value_type>      class_type;
00160     typedef value_type                      *pointer;
00162     typedef value_type const                *const_pointer;
00164     typedef value_type                      &reference;
00166     typedef value_type const                &const_reference;
00168     typedef ptrdiff_t                       difference_type;
00170     typedef cs_size_t                       size_type;
00172 #ifdef __STLSOFT_CF_ALLOCATOR_TYPED_DEALLOCATE_POINTER
00173     typedef pointer                             deallocate_pointer;
00174 #else
00175     typedef void                                *deallocate_pointer;
00176 #endif /* __STLSOFT_COMPILER_IS_MSVC && _MSC_VER == 1200 */
00177 
00178 #ifdef STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT
00179 
00180     template <ss_typename_param_k U>
00181     struct rebind
00182     {
00183         typedef task_allocator<U>           other;
00184     };
00185 #endif /* STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT */
00186 
00187 // Construction
00188 public:
00190     task_allocator() comstl_throw_0()
00191     {}
00193 #ifndef __STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
00194     task_allocator(const task_allocator &/* rhs */) comstl_throw_0()
00195     {}
00196 #else
00197     template <ss_typename_param_k U>
00198     task_allocator(const task_allocator<U> &/* rhs */) comstl_throw_0()
00199     {}
00200 #endif /* !__STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT */
00201 
00202     ~task_allocator() comstl_throw_0()
00203     {}
00204 
00205 // Attributes
00206 public:
00208     size_type max_size() const comstl_throw_0()
00209     {
00210         return static_cast<size_type>(-1) / sizeof(value_type);
00211     }
00212 
00213 // Conversion
00214 public:
00218     pointer address(reference x) const comstl_throw_0()
00219     {
00220         return &x;
00221     }
00225     const_pointer address(const_reference x) const comstl_throw_0()
00226     {
00227         return &x;
00228     }
00229 
00230 // Allocation
00231 public:
00237 #ifdef __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS
00238     static
00239 #endif /* __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS */
00240     pointer allocate(size_type n, task_allocator<void>::const_pointer pv = NULL)
00241     {
00242         STLSOFT_SUPPRESS_UNUSED(pv);
00243 
00244         task_allocator<void>::pointer   p   =   static_cast<task_allocator<void>::pointer>(::CoTaskMemAlloc(n * sizeof(value_type)));
00245 
00246 #ifdef __STLSOFT_CF_THROW_BAD_ALLOC
00247         if(p == NULL)
00248         {
00249             throw stlsoft_ns_qual_std(bad_alloc)();
00250         }
00251 #endif /* __STLSOFT_CF_THROW_BAD_ALLOC */
00252 
00253         return static_cast<pointer>(p);
00254     }
00255 
00256 #ifdef __STLSOFT_CF_ALLOCATOR_CHARALLOC_METHOD
00257 
00258     char *_Charalloc(size_type n)
00259     {
00260         return reinterpret_cast<char*>(allocate(n, NULL));
00261     }
00262 #endif /* __STLSOFT_CF_ALLOCATOR_CHARALLOC_METHOD */
00263 
00268 #ifdef __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS
00269     static
00270 #endif /* __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS */
00271     void deallocate(pointer p, size_type n)
00272     {
00273         STLSOFT_SUPPRESS_UNUSED(n);
00274 
00275         ::CoTaskMemFree(static_cast<HGLOBAL>(p));
00276     }
00277 
00281 #ifdef __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS
00282     static
00283 #endif /* __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS */
00284     void deallocate(pointer p)
00285     {
00286         ::CoTaskMemFree(static_cast<HGLOBAL>(p));
00287     }
00288 
00289 // Operations
00290 public:
00295     void construct(pointer p, value_type const &x)
00296     {
00297         new(p) value_type(x);
00298     }
00299 
00303     void construct(pointer p)
00304     {
00305         new(p) value_type();
00306     }
00307 
00311     void destroy(pointer p) comstl_throw_0()
00312     {
00313         comstl_destroy_instance(T, value_type, p);
00314     }
00315 
00316 // Not to be implemented
00317 private:
00318     class_type const &operator =(class_type const &rhs);
00319 };
00320 
00321 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00322 
00323 template <ss_typename_param_k T>
00324 inline cs_bool_t operator ==(const task_allocator<T> &/* lhs */, const task_allocator<T> &/* rhs */)
00325 {
00326     return cs_true_v;
00327 }
00328 
00329 template <ss_typename_param_k T>
00330 inline cs_bool_t operator !=(const task_allocator<T> &/* lhs */, const task_allocator<T> &/* rhs */)
00331 {
00332     return cs_false_v;
00333 }
00334 
00335 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00336 
00338 // Unit-testing
00339 
00340 #ifdef STLSOFT_UNITTEST
00341 
00342 namespace unittest
00343 {
00344     ss_bool_t test_comstl_task_allocator(unittest_reporter *r)
00345     {
00346         using stlsoft::unittest::unittest_initialiser;
00347 
00348         ss_bool_t               bSuccess    =   true;
00349 
00350         unittest_initialiser    init(r, "COMSTL", "task_allocator", __FILE__);
00351 
00352         typedef task_allocator<int>  int_allocator_t;
00353 
00354         int_allocator_t ator1;
00355 
00356         int     *pi1    =   ator1.allocate(100);
00357 
00358         if(NULL != pi1)
00359         {
00360             ator1.construct(pi1, 1968);
00361 
00362             if(1968 != *pi1)
00363             {
00364                 r->report("new_allocator failed ", __LINE__);
00365                 bSuccess = false;
00366             }
00367         }
00368 
00369         ator1.deallocate(pi1);
00370 
00371         return bSuccess;
00372     }
00373 
00374     unittest_registrar    unittest_comstl_task_allocator(test_comstl_task_allocator);
00375 
00376 } // namespace unittest
00377 
00378 #endif /* STLSOFT_UNITTEST */
00379 
00380 /* 
00381 
00383 
00384 /* 
00385 
00386 #ifndef _COMSTL_NO_NAMESPACE
00387 # if defined(_STLSOFT_NO_NAMESPACE) || \
00388      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00389 } // namespace comstl
00390 # else
00391 } // namespace comstl_project
00392 } // namespace stlsoft
00393 # endif /* _STLSOFT_NO_NAMESPACE */
00394 #endif /* !_COMSTL_NO_NAMESPACE */
00395 
00396 /* 
00397 
00398 #endif /* !COMSTL_INCL_H_COMSTL_TASK_ALLOCATOR */
00399 
00400 /* 

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