Updated Changelog
[centerim/davrieb.git] / libicq2000 / libicq2000 / ref_ptr.h
blobc3d99a32bd58a0e059315edb8048443221c9348b
1 // -*- mode: C++ -*-
2 /*
3 * ref_ptr. A reference counted template class. Similar to the STL
4 * auto_ptr, but generalised for reference counting.
6 * Copyright (C) 2002 Barnaby Gray <barnaby@beedesign.co.uk>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #ifndef REF_PTR_H
25 #define REF_PTR_H
27 namespace ICQ2000 {
29 template <typename Object>
30 class ref_ptr {
31 protected:
32 Object *m_instance;
33 /* note: the actual reference count is controlled on the object -
34 it turned out problematic having it stored in the ref_ptr,
35 for reasons I won't go into here ;-) */
37 public:
38 ref_ptr()
39 : m_instance(NULL)
40 { }
42 ref_ptr(const ref_ptr<Object>& that)
43 : m_instance(that.m_instance)
45 if (m_instance != NULL)
46 ++(m_instance->count);
49 ref_ptr(Object *o)
50 : m_instance(o)
52 if (m_instance != NULL)
53 ++(m_instance->count);
54 /* zeroed inside Contact now */
57 ~ref_ptr()
59 if (m_instance != NULL && --(m_instance->count) == 0)
60 delete m_instance;
63 Object* get()
65 return m_instance;
68 const Object* get() const
70 return m_instance;
73 Object* operator->()
75 return m_instance;
78 const Object* operator->() const
80 return m_instance;
83 Object& operator*()
85 return *m_instance;
88 const Object& operator*() const
90 return *m_instance;
93 // for debugging
94 unsigned int count() const
96 return m_instance->count;
99 ref_ptr& operator=(const ref_ptr<Object>& that) {
100 if (m_instance != NULL && --(m_instance->count) == 0) {
101 delete m_instance;
103 m_instance = that.m_instance;
104 if (m_instance != NULL)
105 ++(m_instance->count);
106 return *this;
113 #endif