]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/generic/referencecounted.h
Merge branch 'NateEag-master-patch-12920' into 'master'
[xonotic/netradiant.git] / libs / generic / referencecounted.h
1 /*
2    Copyright (C) 2001-2006, William Joseph.
3    All Rights Reserved.
4
5    This file is part of GtkRadiant.
6
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.
11
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.
16
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
20  */
21
22 #if !defined( INCLUDED_GENERIC_REFERENCECOUNTED_H )
23 #define INCLUDED_GENERIC_REFERENCECOUNTED_H
24
25 /// \file
26 /// \brief 'smart' pointers and references.
27
28 #include <algorithm>
29
30 template<typename Type>
31 class IncRefDecRefCounter
32 {
33 public:
34 void increment( Type& value ){
35         value.IncRef();
36 }
37 void decrement( Type& value ){
38         value.DecRef();
39 }
40 };
41
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
45 {
46 Type* m_value;
47 public:
48
49 SmartPointer( const SmartPointer& other )
50         : m_value( other.m_value ){
51         Counter::increment( *m_value );
52 }
53 explicit SmartPointer( Type* value )
54         : m_value( value ){
55         Counter::increment( *m_value );
56 }
57 ~SmartPointer(){
58         Counter::decrement( *m_value );
59 }
60 SmartPointer& operator=( const SmartPointer& other ){
61         SmartPointer temp( other );
62         temp.swap( *this );
63         return *this;
64 }
65 SmartPointer& operator=( Type* value ){
66         SmartPointer temp( value );
67         temp.swap( *this );
68         return *this;
69 }
70 void swap( SmartPointer& other ){
71         std::swap( m_value, other.m_value );
72 }
73
74 operator Type*() const
75 {
76         return m_value;
77 }
78 Type& operator*() const {
79         return *m_value;
80 }
81 Type* operator->() const {
82         return m_value;
83 }
84 Type* get() const {
85         return m_value;
86 }
87 };
88
89 template<typename Type>
90 inline bool operator<( const SmartPointer<Type>& self, const SmartPointer<Type>& other ){
91         return self.get() < other.get();
92 }
93 template<typename Type>
94 inline bool operator==( const SmartPointer<Type>& self, const SmartPointer<Type>& other ){
95         return self.get() == other.get();
96 }
97 template<typename Type>
98 inline bool operator!=( const SmartPointer<Type>& self, const SmartPointer<Type>& other ){
99         return !::operator==( self, other );
100 }
101
102 namespace std
103 {
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 ){
108         self.swap( other );
109 }
110 }
111
112
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
116 {
117 Type* m_value;
118 public:
119
120 SmartReference( const SmartReference& other )
121         : m_value( other.m_value ){
122         Counter::increment( *m_value );
123 }
124 explicit SmartReference( Type& value )
125         : m_value( &value ){
126         Counter::increment( *m_value );
127 }
128 ~SmartReference(){
129         Counter::decrement( *m_value );
130 }
131 SmartReference& operator=( const SmartReference& other ){
132         SmartReference temp( other );
133         temp.swap( *this );
134         return *this;
135 }
136 SmartReference& operator=( Type& value ){
137         SmartReference temp( value );
138         temp.swap( *this );
139         return *this;
140 }
141 void swap( SmartReference& other ){
142         std::swap( m_value, other.m_value );
143 }
144
145 operator Type&() const
146 {
147         return *m_value;
148 }
149 Type& get() const {
150         return *m_value;
151 }
152 Type* get_pointer() const {
153         return m_value;
154 }
155 };
156
157 template<typename Type>
158 inline bool operator<( const SmartReference<Type>& self, const SmartReference<Type>& other ){
159         return self.get() < other.get();
160 }
161 template<typename Type>
162 inline bool operator==( const SmartReference<Type>& self, const SmartReference<Type>& other ){
163         return self.get() == other.get();
164 }
165 template<typename Type>
166 inline bool operator!=( const SmartReference<Type>& self, const SmartReference<Type>& other ){
167         return !::operator==( self, other );
168 }
169
170 namespace std
171 {
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 ){
176         self.swap( other );
177 }
178 }
179
180 #endif