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 ){
37 void decrement( Type& value ){
42 /// \brief A smart-pointer that uses a counter stored in the object pointed-to.
43 template<typename Type, typename Counter = IncRefDecRefCounter<Type> >
44 class SmartPointer : public Counter
49 SmartPointer( const SmartPointer& other )
50 : m_value( other.m_value ){
51 Counter::increment( *m_value );
53 explicit SmartPointer( Type* value )
55 Counter::increment( *m_value );
58 Counter::decrement( *m_value );
60 SmartPointer& operator=( const SmartPointer& other ){
61 SmartPointer temp( other );
65 SmartPointer& operator=( Type* value ){
66 SmartPointer temp( value );
70 void swap( SmartPointer& other ){
71 std::swap( m_value, other.m_value );
74 operator Type*() const
78 Type& operator*() const {
81 Type* operator->() const {
89 template<typename Type>
90 inline bool operator<( const SmartPointer<Type>& self, const SmartPointer<Type>& other ){
91 return self.get() < other.get();
93 template<typename Type>
94 inline bool operator==( const SmartPointer<Type>& self, const SmartPointer<Type>& other ){
95 return self.get() == other.get();
97 template<typename Type>
98 inline bool operator!=( const SmartPointer<Type>& self, const SmartPointer<Type>& other ){
99 return !::operator==( self, other );
104 /// \brief Swaps the values of \p self and \p other.
105 /// Overloads std::swap().
106 template<typename Type>
107 inline void swap( SmartPointer<Type>& self, SmartPointer<Type>& other ){
113 /// \brief A smart-reference that uses a counter stored in the object pointed-to.
114 template<typename Type, typename Counter = IncRefDecRefCounter<Type> >
115 class SmartReference : public Counter
120 SmartReference( const SmartReference& other )
121 : m_value( other.m_value ){
122 Counter::increment( *m_value );
124 explicit SmartReference( Type& value )
126 Counter::increment( *m_value );
129 Counter::decrement( *m_value );
131 SmartReference& operator=( const SmartReference& other ){
132 SmartReference temp( other );
136 SmartReference& operator=( Type& value ){
137 SmartReference temp( value );
141 void swap( SmartReference& other ){
142 std::swap( m_value, other.m_value );
145 operator Type&() const
152 Type* get_pointer() const {
157 template<typename Type>
158 inline bool operator<( const SmartReference<Type>& self, const SmartReference<Type>& other ){
159 return self.get() < other.get();
161 template<typename Type>
162 inline bool operator==( const SmartReference<Type>& self, const SmartReference<Type>& other ){
163 return self.get() == other.get();
165 template<typename Type>
166 inline bool operator!=( const SmartReference<Type>& self, const SmartReference<Type>& other ){
167 return !::operator==( self, other );
172 /// \brief Swaps the values of \p self and \p other.
173 /// Overloads std::swap().
174 template<typename Type>
175 inline void swap( SmartReference<Type>& self, SmartReference<Type>& other ){