w_autodel.h

00001 /* -*- mode:C++; c-basic-offset:4 -*-
00002      Shore-MT -- Multi-threaded port of the SHORE storage manager
00003    
00004                        Copyright (c) 2007-2009
00005       Data Intensive Applications and Systems Labaratory (DIAS)
00006                Ecole Polytechnique Federale de Lausanne
00007    
00008                          All Rights Reserved.
00009    
00010    Permission to use, copy, modify and distribute this software and
00011    its documentation is hereby granted, provided that both the
00012    copyright notice and this permission notice appear in all copies of
00013    the software, derivative works or modified versions, and any
00014    portions thereof, and that both notices appear in supporting
00015    documentation.
00016    
00017    This code is distributed in the hope that it will be useful, but
00018    WITHOUT ANY WARRANTY; without even the implied warranty of
00019    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS
00020    DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER
00021    RESULTING FROM THE USE OF THIS SOFTWARE.
00022 */
00023 
00024 /*<std-header orig-src='shore' incl-file-exclusion='W_AUTODEL_H'>
00025 
00026  $Id: w_autodel.h,v 1.19 2010/05/26 01:20:23 nhall Exp $
00027 
00028 SHORE -- Scalable Heterogeneous Object REpository
00029 
00030 Copyright (c) 1994-99 Computer Sciences Department, University of
00031                       Wisconsin -- Madison
00032 All Rights Reserved.
00033 
00034 Permission to use, copy, modify and distribute this software and its
00035 documentation is hereby granted, provided that both the copyright
00036 notice and this permission notice appear in all copies of the
00037 software, derivative works or modified versions, and any portions
00038 thereof, and that both notices appear in supporting documentation.
00039 
00040 THE AUTHORS AND THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY
00041 OF WISCONSIN - MADISON ALLOW FREE USE OF THIS SOFTWARE IN ITS
00042 "AS IS" CONDITION, AND THEY DISCLAIM ANY LIABILITY OF ANY KIND
00043 FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
00044 
00045 This software was developed with support by the Advanced Research
00046 Project Agency, ARPA order number 018 (formerly 8230), monitored by
00047 the U.S. Army Research Laboratory under contract DAAB07-91-C-Q518.
00048 Further funding for this work was provided by DARPA through
00049 Rome Research Laboratory Contract No. F30602-97-2-0247.
00050 
00051 */
00052 
00053 #ifndef W_AUTODEL_H
00054 #define W_AUTODEL_H
00055 
00056 #include "w_defines.h"
00057 
00058 /*  -- do not edit anything above this line --   </std-header>*/
00059 
00060 /**\brief Delete object when this leaves scope (a la STL auto_ptr)
00061  *
00062  *  This class is used to ensure that a "new"ed object of type T 
00063  *  will be "delete"d when the scope is closed.
00064  *  During destruction, automatically call "delete" on the pointer
00065  *  supplied during construction.
00066  *
00067  *  eg. f()
00068  *  \code
00069  *    {
00070  *          int* p = new int;
00071  *        if (!p)  return OUTOFMEMORY;
00072  *        w_auto_delete_t<int> autodel(p);
00073  *
00074  *         ... do work ...
00075  *
00076  *        if (error)  {    // no need to call delete p
00077  *        return error;
00078  *        }
00079  *
00080  *        // no need to call delete p
00081  *        return OK;
00082  *    }
00083  *    \endcode
00084  *
00085  *  delete p will be called by the autodel object. Thus users do 
00086  *    not need to code 'delete p' explicitly, and can be assured
00087  *    that p will be deleted when the scope in which autodel 
00088  *  was constructed is closed.
00089  *
00090  *  This code predates the STL.
00091  *
00092  *********************************************************************/
00093 template <class T>
00094 class w_auto_delete_t {
00095 public:
00096     NORET            w_auto_delete_t()
00097     : obj(0)  {};
00098     NORET            w_auto_delete_t(T* t)
00099     : obj(t)  {};
00100     NORET            ~w_auto_delete_t()  {
00101     if (obj) delete obj;
00102     }
00103     w_auto_delete_t&        set(T* t)  {
00104     return obj = t, *this;
00105     }
00106     T* operator->() { return obj; }
00107     T &operator*() { return *obj; }
00108     operator T*() { return obj; }
00109     T const* operator->() const { return obj; }
00110     T const &operator*() const { return *obj; }
00111     operator T const*() const { return obj; }
00112 private:
00113     T*                obj;
00114 
00115     // disabled
00116     NORET            w_auto_delete_t(const w_auto_delete_t&) {};
00117     w_auto_delete_t&        operator=(const w_auto_delete_t &) {return *this;};
00118 };
00119 
00120 
00121 
00122 /**\brief Delete array object when this leaves scope.
00123  *  
00124  *
00125  *  Same as w_auto_delete_t, except that this class operates on
00126  *  arrays (i.e. the destructor calls delete[] instead of delete.)
00127  *
00128  *  eg. f()
00129  *    {
00130  *          int* p = new int[20];
00131  *        if (!p)  return OUTOFMEMORY;
00132  *        w_auto_delete_array_t<int> autodel(p);
00133  *
00134  *         ... do work ...
00135  *
00136  *        if (error)  {    // no need to call delete[] p
00137  *        return error;
00138  *        }
00139  *
00140  *        // no need to call delete[] p
00141  *        return OK;
00142  *    }
00143  *
00144  *    This code predates STL.
00145  *
00146  *********************************************************************/
00147 template <class T>
00148 class w_auto_delete_array_t {
00149 public:
00150     NORET            w_auto_delete_array_t()
00151     : obj(0)  {};
00152     NORET            w_auto_delete_array_t(T* t)
00153     : obj(t)  {};
00154     NORET            ~w_auto_delete_array_t()  {
00155     if (obj) delete [] obj;
00156     }
00157     w_auto_delete_array_t&    set(T* t)  {
00158     return obj = t, *this;
00159     }
00160     T* operator->() { return obj; }
00161     T &operator*() { return *obj; }
00162     operator T*() { return obj; }
00163     //    T &operator[](int idx) { return obj[idx]; }
00164 private:
00165     T*                obj;
00166 
00167     // disabled
00168     NORET            w_auto_delete_array_t(
00169     const w_auto_delete_array_t&)  {}
00170     w_auto_delete_array_t&    operator=(const w_auto_delete_array_t &) {return *this;};
00171 };
00172 
00173 /*<std-footer incl-file-exclusion='W_AUTODEL_H'>  -- do not edit anything below this line -- */
00174 
00175 #endif          /*</std-footer>*/

Generated on Thu Dec 9 08:42:27 2010 for Shore Storage Manager by  doxygen 1.4.7