CoCoALib-0.9905 date: 23 May 2007


SmartPtrIRC.H

Go to the documentation of this file.
00001 #ifndef CoCoA_SmartPtrIRC_H
00002 #define CoCoA_SmartPtrIRC_H
00003 
00004 //   Copyright (c)  2006  John Abbott
00005 
00006 //   This file is part of the source of CoCoALib, the CoCoA Library.
00007 
00008 //   CoCoALib is free software; you can redistribute it and/or modify
00009 //   it under the terms of the GNU General Public License (version 2)
00010 //   as published by the Free Software Foundation.  A copy of the full
00011 //   licence may be found in the file COPYING in this directory.
00012 
00013 //   CoCoALib is distributed in the hope that it will be useful,
00014 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 //   GNU General Public License for more details.
00017 
00018 //   You should have received a copy of the GNU General Public License
00019 //   along with CoCoA; if not, write to the Free Software
00020 //   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 
00022 
00023 #include <cstdlib>
00024 //using std::size_t;
00025 
00026 namespace CoCoA
00027 {
00028 
00029   /*-----------------------------------------------------------------*/
00030   /** \include SmartPtrIRC.txt  */
00031   /*-----------------------------------------------------------------*/
00032 
00033   // This template class assumes that T derives from the class IntrusiveReferenceCount (below).
00034   template <typename T> class SmartPtrIRC
00035   {
00036   public:
00037     explicit SmartPtrIRC(T* RawPtr): myRawPtrValue(RawPtr) { myRawPtrValue->myRefCountInc(); }
00038 //    template<typename T1> explicit SmartPtrIRC(T1* RawPtr); //???
00039     SmartPtrIRC(const SmartPtrIRC<T>& ToBeCopied): myRawPtrValue(ToBeCopied.myRawPtrValue) { myRawPtrValue->myRefCountInc(); }
00040     ~SmartPtrIRC() { myRawPtrValue->myRefCountDec(); }
00041   private: // disable assignment
00042     SmartPtrIRC<T>& operator=(const SmartPtrIRC<T>& rhs); // NEVER DEFINED -- assignment disabled
00043   public:
00044     T* myRawPtr() const { return myRawPtrValue; }    ///< to avoid too many friend stmts
00045     T* operator->() const { return myRawPtrValue; }  ///< allow const member fns to be called
00046 
00047   private: // Data members
00048     T* const myRawPtrValue;
00049   };
00050 
00051   // For  ==  and  !=  both args must be pointers to exactly the same type.
00052   template <typename T>
00053   inline bool operator==(const SmartPtrIRC<T>& ptr1, const SmartPtrIRC<T>& ptr2)
00054   {
00055     return ptr1.myRawPtr() == ptr2.myRawPtr();
00056   }
00057 
00058   template <typename T>
00059   inline bool operator!=(const SmartPtrIRC<T>& ptr1, const SmartPtrIRC<T>& ptr2)
00060   {
00061     return !(ptr1 == ptr2);
00062   }
00063 
00064   // This class is intended to be used solely as a (protected) base class.
00065   class IntrusiveReferenceCount
00066   {
00067     // Note: this class has no public members.
00068   protected:
00069     IntrusiveReferenceCount();          ///< ctor just sets ref count to 0
00070     virtual ~IntrusiveReferenceCount(); ///< dtor MUST BE VIRTUAL (see doc)
00071     void myRefCountInc() const;         ///< increments ref count
00072     void myRefCountDec() const;         ///< decrements ref count, and self destructs if count becomes 0
00073     void myRefCountZero() const;        ///< sets myRefCount to 0
00074   private: // data member
00075     mutable std::size_t myRefCount;     ///< intrusive reference count value
00076   };
00077 
00078   inline IntrusiveReferenceCount::IntrusiveReferenceCount():
00079       myRefCount(0)
00080   {}
00081 
00082   inline IntrusiveReferenceCount::~IntrusiveReferenceCount()
00083   {}
00084 
00085 
00086   inline void IntrusiveReferenceCount::myRefCountInc() const
00087   {
00088     ++myRefCount;
00089   }
00090 
00091   inline void IntrusiveReferenceCount::myRefCountDec() const
00092   {
00093     --myRefCount;
00094     if (myRefCount == 0) delete this;
00095   }
00096 
00097   inline void IntrusiveReferenceCount::myRefCountZero() const
00098   {
00099     myRefCount = 0;
00100   }
00101 
00102 
00103 } // end of namespace CoCoA
00104 
00105 
00106 // RCS header/log in the next few lines
00107 // $Header: /Volumes/Home/cocoa/cvs-repository/CoCoALib-0.99/include/CoCoA/SmartPtrIRC.H,v 1.1.1.1 2007/03/09 15:16:11 abbott Exp $
00108 // $Log: SmartPtrIRC.H,v $
00109 // Revision 1.1.1.1  2007/03/09 15:16:11  abbott
00110 // Imported files
00111 //
00112 // Revision 1.7  2006/12/06 17:25:43  cocoa
00113 // -- removed #include "config.H"
00114 //
00115 // Revision 1.6  2006/11/20 15:55:03  cocoa
00116 // ring is now a class again.  Improved definitions of operator-> in derived classes.
00117 //
00118 // Revision 1.5  2006/11/11 14:58:28  cocoa
00119 // Documented SmartPtrIRC and IntrusiveReferenceCount, especially explaining
00120 // about the virtual destructor.
00121 //
00122 // Revision 1.4  2006/11/03 16:16:13  cocoa
00123 // changed data member from protected to private
00124 //
00125 // Revision 1.3  2006/11/02 13:25:44  cocoa
00126 // Simplification of header files: the OpenMath classes have been renamed.
00127 // Many minor consequential changes.
00128 //
00129 // Revision 1.2  2006/10/06 14:04:15  cocoa
00130 // Corrected position of #ifndef in header files.
00131 // Separated CoCoA_ASSERT into assert.H from config.H;
00132 // many minor consequential changes (have to #include assert.H).
00133 // A little tidying of #include directives (esp. in Max's code).
00134 //
00135 // Revision 1.1.1.1  2006/05/30 11:39:37  cocoa
00136 // Imported files
00137 //
00138 // Revision 1.3  2006/04/28 16:33:51  cocoa
00139 // Used SmartPtrIRC for PPOrderings.
00140 //
00141 // Revision 1.2  2006/01/20 17:44:20  cocoa
00142 // Friday afternoon check-in: you have been warned!
00143 // Fixed ref counting bug in SmartPtrIRC.
00144 // Added sum/product functions in tmp.H.
00145 // Minor tidying here and there.
00146 //
00147 // Revision 1.1  2006/01/17 10:23:08  cocoa
00148 // Updated DivMask; many consequential changes.
00149 // A few other minor fixes.
00150 //
00151 
00152 #endif

Generated on Wed May 23 13:43:36 2007 for CoCoALib by  doxygen 1.4.6