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

Go to the documentation of this file.
00001 /* 
00002  * File:        stlsoft_auto_destructor.h (originally MLAutDtr.h, ::SynesisStd)
00003  *
00004  * Purpose:     Contains the auto_destructor and auto_array_destructor template
00005  *              classes.
00006  *
00007  * Created:     19th January 2002
00008  * Updated:     11th September 2004
00009  *
00010  * Home:        http://stlsoft.org/
00011  *
00012  * Copyright (c) 2002-2004, Matthew Wilson and Synesis Software
00013  * All rights reserved.
00014  *
00015  * Redistribution and use in source and binary forms, with or without
00016  * modification, are permitted provided that the following conditions are met:
00017  *
00018  * - Redistributions of source code must retain the above copyright notice, this
00019  *   list of conditions and the following disclaimer.
00020  * - Redistributions in binary form must reproduce the above copyright notice,
00021  *   this list of conditions and the following disclaimer in the documentation
00022  *   and/or other materials provided with the distribution.
00023  * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
00024  *   any contributors may be used to endorse or promote products derived from
00025  *   this software without specific prior written permission.
00026  *
00027  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00028  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00029  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00030  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00031  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00032  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00033  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00034  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00035  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00036  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00037  * POSSIBILITY OF SUCH DAMAGE.
00038  *
00039  * 
00040 
00041 
00045 
00046 #ifndef STLSOFT_INCL_H_STLSOFT_AUTO_DESTRUCTOR
00047 #define STLSOFT_INCL_H_STLSOFT_AUTO_DESTRUCTOR
00048 
00049 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00050 # define STLSOFT_VER_H_STLSOFT_AUTO_DESTRUCTOR_MAJOR      2
00051 # define STLSOFT_VER_H_STLSOFT_AUTO_DESTRUCTOR_MINOR      0
00052 # define STLSOFT_VER_H_STLSOFT_AUTO_DESTRUCTOR_REVISION   1
00053 # define STLSOFT_VER_H_STLSOFT_AUTO_DESTRUCTOR_EDIT       43
00054 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00055 
00056 /* 
00057  * Includes
00058  */
00059 
00060 #ifndef STLSOFT_INCL_H_STLSOFT
00061 # include "stlsoft.h"   // Include the STLSoft root header
00062 #endif /* !STLSOFT_INCL_H_STLSOFT */
00063 
00064 /* 
00065  * Namespace
00066  */
00067 
00068 #ifndef _STLSOFT_NO_NAMESPACE
00069 namespace stlsoft
00070 {
00071 #endif /* _STLSOFT_NO_NAMESPACE */
00072 
00073 /* 
00074  * Forward declarations
00075  */
00076 
00077 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00078 
00079 template <ss_typename_param_k T>
00080 class auto_destructor;
00081 
00082 template <ss_typename_param_k T>
00083 class return_value_destructor;
00084 
00085 template <ss_typename_param_k T>
00086 class auto_array_destructor;
00087 
00088 template <ss_typename_param_k T>
00089 class return_value_array_destructor;
00090 
00091 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00092 
00093 /* 
00094  * Classes
00095  */
00096 
00097 // class auto_destructor
00102 template <ss_typename_param_k T>
00103 class auto_destructor
00104 {
00105 public:
00107     typedef T                                               value_type;
00109     typedef auto_destructor<T>                              class_type;
00111     typedef return_value_destructor<T>                      return_value_type;
00113     typedef stlsoft_define_move_rhs_type(return_value_type) return_value_rhs_type;
00114 
00115 // Construction
00116 public:
00118     ss_explicit_k auto_destructor(value_type *t)
00119         : m_value(t)
00120     {}
00122     auto_destructor(return_value_rhs_type rhs) // Note: not explicit
00123         : m_value(move_lhs_from_rhs(rhs).detach())
00124     {}
00126     ~auto_destructor() stlsoft_throw_0()
00127     {
00128         delete m_value;
00129     }
00130 
00131 // Operations
00132 public:
00136     value_type *detach()
00137     {
00138         value_type  *t = m_value;
00139 
00140         m_value = 0;
00141 
00142         return t;
00143     }
00144 
00145 // Conversion
00146 public:
00148     operator value_type *()
00149     {
00150         return m_value;
00151     }
00153     operator const value_type *() const
00154     {
00155         return m_value;
00156     }
00157 
00158 // Members
00159 private:
00160     value_type  *m_value;
00161 
00162 // Not to be implemented
00163 private:
00164     auto_destructor(class_type const &rhs);
00165     auto_destructor const &operator =(class_type const &rhs);
00166 };
00167 
00168 // class auto_array_destructor
00173 template <ss_typename_param_k T>
00174 class auto_array_destructor
00175 {
00176 public:
00178     typedef T                                               value_type;
00180     typedef auto_array_destructor<T>                        class_type;
00182     typedef return_value_array_destructor<T>                return_value_type;
00184     typedef stlsoft_define_move_rhs_type(return_value_type) return_value_rhs_type;
00185 
00186 // Construction
00187 public:
00189     ss_explicit_k auto_array_destructor(value_type t[])
00190         : m_value(t)
00191     {}
00193     auto_array_destructor(return_value_rhs_type rhs) // Note: not explicit
00194         : m_value(move_lhs_from_rhs(rhs).detach())
00195     {}
00197     ~auto_array_destructor() stlsoft_throw_0()
00198     {
00199         delete [] m_value;
00200     }
00201 
00202 // Operations
00203 public:
00207     value_type *detach()
00208     {
00209         value_type  *t = m_value;
00210 
00211         m_value = 0;
00212 
00213         return t;
00214     }
00215 
00216 // Conversion
00217 public:
00219     operator value_type *()
00220     {
00221         return m_value;
00222     }
00224     operator const value_type *() const
00225     {
00226         return m_value;
00227     }
00228 
00229 // Members
00230 private:
00231     value_type  *m_value;
00232 
00233 // Not to be implemented
00234 private:
00235     auto_array_destructor(class_type const &rhs);
00236     auto_array_destructor const &operator =(class_type const &rhs);
00237 };
00238 
00239 
00240 // class return_value_destructor
00245 template <ss_typename_param_k T>
00246 class return_value_destructor
00247 {
00248 public:
00250     typedef T                                           value_type;
00252     typedef return_value_destructor<T>                  class_type;
00254     typedef auto_destructor<T>                          auto_type;
00256     typedef stlsoft_define_move_rhs_type(class_type)    rhs_type;
00257 
00258 // Construction
00259 public:
00260 #ifdef _STLSOFT_RETURN_VALUE_DESTRUCTOR_ENABLE_DIRECT_CTOR
00261 
00262     ss_explicit_k return_value_destructor(value_type *pt)
00263         : m_value(pt)
00264     {}
00265 #endif /* _STLSOFT_RETURN_VALUE_DESTRUCTOR_ENABLE_DIRECT_CTOR */
00266 
00267     return_value_destructor(auto_type &rhs) // Note: not explicit
00268         : m_value(rhs.detach())
00269     {}
00271     return_value_destructor(rhs_type rhs) // Note: Move-constructor
00272         : m_value(move_lhs_from_rhs(rhs).detach())
00273     {}
00275     ~return_value_destructor() stlsoft_throw_0()
00276     {
00277         // This fires a report if value is non-zero, which indicates that
00278         // the return value had not been used. This is arguably ok since this
00279         // only ever happens in debug builds (what would be the point in
00280         // including in a release build?), so its violation of the rules on
00281         // no-throw destructors can be forgiven.
00282 
00283 #ifndef _STLSOFT_RETURN_VALUE_DESTRUCTOR_DISABLE_UNUSED_ASSERT
00284 #if defined(__STLSOFT_COMPILER_IS_WATCOM)
00285         stlsoft_assert(m_value == 0);
00286 #else
00287         stlsoft_message_assert("This return value was not used", m_value == 0);
00288 #endif /* __STLSOFT_COMPILER_IS_WATCOM */
00289 #endif /* !_STLSOFT_RETURN_VALUE_DESTRUCTOR_DISABLE_UNUSED_ASSERT */
00290 
00291         delete m_value;
00292     }
00293 
00294 // Operations
00295 private:
00296     friend class auto_destructor<T>;
00297 
00301     value_type *detach()
00302     {
00303         value_type  *t = m_value;
00304 
00305         m_value = 0;
00306 
00307         return t;
00308     }
00309 
00310 // Members
00311 private:
00312     value_type  *m_value;
00313 
00314 // Not to be implemented
00315 private:
00316     return_value_destructor const &operator = (class_type const &rhs);
00317 };
00318 
00319 // class return_value_array_destructor
00324 template <ss_typename_param_k T>
00325 class return_value_array_destructor
00326 {
00327 public:
00329     typedef T                                           value_type;
00331     typedef return_value_array_destructor<T>            class_type;
00333     typedef auto_array_destructor<T>                    auto_type;
00335     typedef stlsoft_define_move_rhs_type(class_type)    rhs_type;
00336 
00337 // Construction
00338 public:
00339 #ifdef _STLSOFT_RETURN_VALUE_DESTRUCTOR_ENABLE_DIRECT_CTOR
00340 
00341     ss_explicit_k return_value_array_destructor(value_type t[])
00342         : m_value(pt)
00343     {}
00344 #endif /* _STLSOFT_RETURN_VALUE_DESTRUCTOR_ENABLE_DIRECT_CTOR */
00345 
00346     return_value_array_destructor(auto_type &rhs) // Note: not explicit
00347         : m_value(rhs.detach())
00348     {}
00350     return_value_array_destructor(rhs_type rhs) // Note: Move-constructor
00351         : m_value(move_lhs_from_rhs(rhs).detach())
00352     {}
00354     ~return_value_array_destructor() stlsoft_throw_0()
00355     {
00356         // This fires a report if value is non-zero, which indicates that
00357         // the return value had not been used. This is arguably ok since this
00358         // only ever happens in debug builds (what would be the point in
00359         // including in a release build?), so its violation of the rules on
00360         // no-throw destructors can be forgiven.
00361 
00362 #ifndef _STLSOFT_RETURN_VALUE_DESTRUCTOR_DISABLE_UNUSED_ASSERT
00363 #if defined(__STLSOFT_COMPILER_IS_WATCOM)
00364         stlsoft_assert(m_value == 0);
00365 #else
00366         stlsoft_message_assert("This return value was not used", m_value == 0);
00367 #endif /* __STLSOFT_COMPILER_IS_WATCOM */
00368 #endif /* !_STLSOFT_RETURN_VALUE_DESTRUCTOR_DISABLE_UNUSED_ASSERT */
00369 
00370         delete [] m_value;
00371     }
00372 
00373 // Operations
00374 private:
00375     friend class auto_array_destructor<T>;
00376 
00380     value_type *detach()
00381     {
00382         value_type  *t = m_value;
00383 
00384         m_value = 0;
00385 
00386         return t;
00387     }
00388 
00389 // Members
00390 private:
00391     value_type  *m_value;
00392 
00393 // Not to be implemented
00394 private:
00395     return_value_array_destructor const &operator = (class_type const &rhs);
00396 };
00397 
00398 
00399 /* 
00400 
00401 #ifndef _STLSOFT_NO_NAMESPACE
00402 } // namespace stlsoft
00403 #endif /* _STLSOFT_NO_NAMESPACE */
00404 
00405 /* 
00406 
00407 #endif /* !STLSOFT_INCL_H_STLSOFT_AUTO_DESTRUCTOR */
00408 
00409 /* 

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