Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   Related Pages  

ref_ptr.h

00001 // -*- mode: C++ -*-
00002 /*
00003  * ref_ptr. A reference counted template class. Similar to the STL
00004  * auto_ptr, but generalised for reference counting.
00005  *
00006  * Copyright (C) 2002 Barnaby Gray <barnaby@beedesign.co.uk>
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * This library 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 GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00021  *
00022  */
00023 
00024 #ifndef REF_PTR_H
00025 #define REF_PTR_H
00026 
00027 namespace ICQ2000 {
00028 
00029   template <typename Object>
00030   class ref_ptr {
00031    protected:
00032     Object *m_instance;
00033     /* note: the actual reference count is controlled on the object -
00034        it turned out problematic having it stored in the ref_ptr,
00035        for reasons I won't go into here ;-) */
00036 
00037    public:
00038     ref_ptr()
00039       : m_instance(NULL)
00040     { }
00041 
00042     ref_ptr(const ref_ptr<Object>& that)
00043       : m_instance(that.m_instance)
00044     {
00045       if (m_instance != NULL)
00046         ++(m_instance->count);
00047     }
00048 
00049     ref_ptr(Object *o)
00050       : m_instance(o)
00051     {
00052       if (m_instance != NULL)
00053         ++(m_instance->count);
00054       /* zeroed inside Contact now */
00055     }
00056     
00057     ~ref_ptr()
00058     {
00059       if (m_instance != NULL && --(m_instance->count) == 0)
00060         delete m_instance;
00061     }
00062     
00063     Object* get()
00064     {
00065       return m_instance;
00066     }
00067     
00068     Object* operator->()
00069     {
00070       return m_instance;
00071     }
00072 
00073     const Object* operator->() const
00074     {
00075       return m_instance;
00076     }
00077 
00078     Object& operator*()
00079     {
00080       return *m_instance;
00081     }
00082 
00083     const Object& operator*() const
00084     {
00085       return *m_instance;
00086     }
00087 
00088     // for debugging
00089     unsigned int count()
00090     {
00091       return m_instance->count;
00092     }
00093       
00094     ref_ptr& operator=(const ref_ptr<Object>& that) {
00095       if (m_instance != NULL && --(m_instance->count) == 0) {
00096         delete m_instance;
00097       }
00098       m_instance = that.m_instance;
00099       if (m_instance != NULL)
00100         ++(m_instance->count);
00101       return *this;             
00102     }
00103 
00104   };
00105   
00106 }
00107 
00108 #endif

Generated on Sat Jul 20 16:59:08 2002 for libicq2000 by doxygen1.2.16