referencecounted.h

Go to the documentation of this file.
00001 /*
00002 Copyright (C) 2001-2006, William Joseph.
00003 All Rights Reserved.
00004 
00005 This file is part of GtkRadiant.
00006 
00007 GtkRadiant is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 GtkRadiant is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with GtkRadiant; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 */
00021 
00022 #if !defined(INCLUDED_GENERIC_REFERENCECOUNTED_H)
00023 #define INCLUDED_GENERIC_REFERENCECOUNTED_H
00024 
00027 
00028 #include <algorithm>
00029 
00030 template<typename Type>
00031 class IncRefDecRefCounter {
00032 public:
00033     void increment(Type& value) {
00034         value.IncRef();
00035     }
00036     void decrement(Type& value) {
00037         value.DecRef();
00038     }
00039 };
00040 
00042 template < typename Type, typename Counter = IncRefDecRefCounter<Type> >
00043 class SmartPointer : public Counter {
00044     Type* m_value;
00045 public:
00046 
00047     SmartPointer(const SmartPointer& other)
00048             : m_value(other.m_value) {
00049         Counter::increment(*m_value);
00050     }
00051     explicit SmartPointer(Type* value)
00052             : m_value(value) {
00053         Counter::increment(*m_value);
00054     }
00055     ~SmartPointer() {
00056         Counter::decrement(*m_value);
00057     }
00058     SmartPointer& operator=(const SmartPointer& other) {
00059         SmartPointer temp(other);
00060         temp.swap(*this);
00061         return *this;
00062     }
00063     SmartPointer& operator=(Type* value) {
00064         SmartPointer temp(value);
00065         temp.swap(*this);
00066         return *this;
00067     }
00068     void swap(SmartPointer& other) {
00069         std::swap(m_value, other.m_value);
00070     }
00071 
00072     operator Type*() const {
00073         return m_value;
00074     }
00075     Type& operator*() const {
00076         return *m_value;
00077     }
00078     Type* operator->() const {
00079         return m_value;
00080     }
00081     Type* get() const {
00082         return m_value;
00083     }
00084 };
00085 
00086 template<typename Type>
00087 inline bool operator<(const SmartPointer<Type>& self, const SmartPointer<Type>& other) {
00088     return self.get() < other.get();
00089 }
00090 template<typename Type>
00091 inline bool operator==(const SmartPointer<Type>& self, const SmartPointer<Type>& other) {
00092     return self.get() == other.get();
00093 }
00094 template<typename Type>
00095 inline bool operator!=(const SmartPointer<Type>& self, const SmartPointer<Type>& other) {
00096     return !::operator==(self, other);
00097 }
00098 
00099 namespace std {
00102 template<typename Type>
00103 inline void swap(SmartPointer<Type>& self, SmartPointer<Type>& other) {
00104     self.swap(other);
00105 }
00106 }
00107 
00108 
00110 template < typename Type, typename Counter = IncRefDecRefCounter<Type> >
00111 class SmartReference : public Counter {
00112     Type* m_value;
00113 public:
00114 
00115     SmartReference(const SmartReference& other)
00116             : m_value(other.m_value) {
00117         Counter::increment(*m_value);
00118     }
00119     explicit SmartReference(Type& value)
00120             : m_value(&value) {
00121         Counter::increment(*m_value);
00122     }
00123     ~SmartReference() {
00124         Counter::decrement(*m_value);
00125     }
00126     SmartReference& operator=(const SmartReference& other) {
00127         SmartReference temp(other);
00128         temp.swap(*this);
00129         return *this;
00130     }
00131     SmartReference& operator=(Type& value) {
00132         SmartReference temp(value);
00133         temp.swap(*this);
00134         return *this;
00135     }
00136     void swap(SmartReference& other) {
00137         std::swap(m_value, other.m_value);
00138     }
00139 
00140     operator Type&() const {
00141         return *m_value;
00142     }
00143     Type& get() const {
00144         return *m_value;
00145     }
00146     Type* get_pointer() const {
00147         return m_value;
00148     }
00149 };
00150 
00151 template<typename Type>
00152 inline bool operator<(const SmartReference<Type>& self, const SmartReference<Type>& other) {
00153     return self.get() < other.get();
00154 }
00155 template<typename Type>
00156 inline bool operator==(const SmartReference<Type>& self, const SmartReference<Type>& other) {
00157     return self.get() == other.get();
00158 }
00159 template<typename Type>
00160 inline bool operator!=(const SmartReference<Type>& self, const SmartReference<Type>& other) {
00161     return !::operator==(self, other);
00162 }
00163 
00164 namespace std {
00167 template<typename Type>
00168 inline void swap(SmartReference<Type>& self, SmartReference<Type>& other) {
00169     self.swap(other);
00170 }
00171 }
00172 
00173 #endif

Generated by  doxygen 1.6.2