Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

MCRefPtr.h

00001 #if !defined(__MCREFPTR_H_)
00002 #define __MCREFPTR_H_
00003 
00004 #ident "@(#)$Id: MCRefPtr.h,v 1.6 2004/12/02 07:05:20 mike Exp $"
00005 
00006 /*
00007  * Copyright Miba Consulting Ltd. (c) 2000-2003
00008  * 
00009  * This library is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 2.1 of the License, or (at your option) any later version.
00013  * 
00014  * This library is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00017  * Lesser General Public License for more details.
00018  * 
00019  * You should have received a copy of the GNU Lesser General Public
00020  * License along with this library; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00022  *
00023  */
00024 
00025 #include "mcllib/MCRefCountAccessor.h"
00026 
00027 namespace mcllib
00028 {
00050     template<typename T,
00051         MCThreadModel::EModel MODEL = MCThreadModel::THRMODELMULTI>
00052     class MCRefPtr : public MCRefCountAccessorTmpl<MODEL>
00053     {
00054     public:
00060         MCRefPtr(T* pHdl = 0)
00061         {
00062             attach(pHdl);
00063         }
00069         ~MCRefPtr()
00070         {
00071             detach();
00072         }
00079         MCRefPtr(const MCRefPtr& r)
00080         {
00081             attach(r.m_pHdl);
00082         }
00092         MCRefPtr& operator=(const MCRefPtr& r)
00093         {
00094             // Don't do anything if assigning to self
00095             if (&r == this)
00096                 return(*this);
00097             // Don't do anything if the pointers are the same
00098             if (r.m_pHdl == m_pHdl)
00099                 return(*this);
00100             detach();
00101             attach(r);
00102             return(*this);
00103         }
00107         inline operator MCRefPtr<const T>&() const
00108         {
00109             return(*(reinterpret_cast<MCRefPtr<const T> *>(this)));
00110         }
00115         inline operator T*() const
00116         {
00117             return(m_pHdl);
00118         }
00123         inline T* operator->() const
00124         {
00125             return(m_pHdl);
00126         }
00135         inline bool operator<(const MCRefPtr<T>& t) const
00136         {
00137             return(m_pHdl < t.m_pHdl);
00138         }
00139 
00140     protected:
00141         void attach(T* pHdl);
00142         void detach();
00144         T* m_pHdl;
00145     };
00146 
00147     template<typename T, MCThreadModel::EModel MODEL>
00148     inline void MCRefPtr<T, MODEL>::attach(T* pHdl)
00149     {
00150         if (pHdl) {
00151             incCnt(pHdl);
00152         }
00153         m_pHdl = pHdl;
00154     }
00155 
00156     template<typename T, MCThreadModel::EModel MODEL>
00157     inline void MCRefPtr<T, MODEL>::detach()
00158     {
00159         if (m_pHdl && (decCnt(m_pHdl) == 0)) {
00160             delete m_pHdl;
00161         }
00162     }
00163 
00172     template<typename T,
00173         MCThreadModel::EModel MODEL = MCThreadModel::THRMODELMULTI>
00174     class MCRefPtrAttacher : public MCRefPtr<T, MODEL>
00175     {
00176     public:
00177         MCRefPtrAttacher(T* pHdl)
00178         {
00179             attach(pHdl);
00180             // Avoid detach
00181             m_pHdl = 0;
00182         }
00183     };
00184 
00190     template<typename T,
00191         MCThreadModel::EModel MODEL = MCThreadModel::THRMODELMULTI>
00192     class MCRefPtrDetacher : public MCRefPtr<T, MODEL>
00193     {
00194     public:
00195         MCRefPtrDetacher(T* pHdl)
00196         {
00197             // Avoid attach
00198             m_pHdl = pHdl;
00199         }
00200     };
00201 
00202 };
00203 
00204 #endif

Generated on Wed Jan 12 19:05:48 2005 for MCLLIB by  doxygen 1.3.9.1