| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#if !defined( INCLUDED_GENERIC_REFERENCECOUNTED_H ) |
| 23 |
#define INCLUDED_GENERIC_REFERENCECOUNTED_H |
| 24 |
|
| 25 |
|
| 26 |
|
| 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 |
|
| 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 |
|
| 105 |
|
| 106 |
template<typename Type> |
| 107 |
inline void swap( SmartPointer<Type>& self, SmartPointer<Type>& other ){ |
| 108 |
self.swap( other ); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 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 |
|
| 173 |
|
| 174 |
template<typename Type> |
| 175 |
inline void swap( SmartReference<Type>& self, SmartReference<Type>& other ){ |
| 176 |
self.swap( other ); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
#endif |