2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #if !defined(INCLUDED_GENERIC_REFERENCECOUNTED_H)
23 #define INCLUDED_GENERIC_REFERENCECOUNTED_H
26 /// \brief 'smart' pointers and references.
30 template<typename Type>
31 class IncRefDecRefCounter
34 void increment(Type& value)
38 void decrement(Type& value)
44 /// \brief A smart-pointer that uses a counter stored in the object pointed-to.
45 template<typename Type, typename Counter = IncRefDecRefCounter<Type> >
46 class SmartPointer : public Counter
51 SmartPointer(const SmartPointer& other)
52 : m_value(other.m_value)
54 Counter::increment(*m_value);
56 explicit SmartPointer(Type* value)
59 Counter::increment(*m_value);
63 Counter::decrement(*m_value);
65 SmartPointer& operator=(const SmartPointer& other)
67 SmartPointer temp(other);
71 SmartPointer& operator=(Type* value)
73 SmartPointer temp(value);
77 void swap(SmartPointer& other)
79 std::swap(m_value, other.m_value);
82 operator Type*() const
86 Type& operator*() const
90 Type* operator->() const
100 template<typename Type>
101 inline bool operator<(const SmartPointer<Type>& self, const SmartPointer<Type>& other)
103 return self.get() < other.get();
105 template<typename Type>
106 inline bool operator==(const SmartPointer<Type>& self, const SmartPointer<Type>& other)
108 return self.get() == other.get();
110 template<typename Type>
111 inline bool operator!=(const SmartPointer<Type>& self, const SmartPointer<Type>& other)
113 return !::operator==(self, other);
118 /// \brief Swaps the values of \p self and \p other.
119 /// Overloads std::swap().
120 template<typename Type>
121 inline void swap(SmartPointer<Type>& self, SmartPointer<Type>& other)
128 /// \brief A smart-reference that uses a counter stored in the object pointed-to.
129 template<typename Type, typename Counter = IncRefDecRefCounter<Type> >
130 class SmartReference : public Counter
135 SmartReference(const SmartReference& other)
136 : m_value(other.m_value)
138 Counter::increment(*m_value);
140 explicit SmartReference(Type& value)
143 Counter::increment(*m_value);
147 Counter::decrement(*m_value);
149 SmartReference& operator=(const SmartReference& other)
151 SmartReference temp(other);
155 SmartReference& operator=(Type& value)
157 SmartReference temp(value);
161 void swap(SmartReference& other)
163 std::swap(m_value, other.m_value);
166 operator Type&() const
174 Type* get_pointer() const
180 template<typename Type>
181 inline bool operator<(const SmartReference<Type>& self, const SmartReference<Type>& other)
183 return self.get() < other.get();
185 template<typename Type>
186 inline bool operator==(const SmartReference<Type>& self, const SmartReference<Type>& other)
188 return self.get() == other.get();
190 template<typename Type>
191 inline bool operator!=(const SmartReference<Type>& self, const SmartReference<Type>& other)
193 return !::operator==(self, other);
198 /// \brief Swaps the values of \p self and \p other.
199 /// Overloads std::swap().
200 template<typename Type>
201 inline void swap(SmartReference<Type>& self, SmartReference<Type>& other)