00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00024 #ifndef SHREFCOUNTIMPL_HPP
00025 #define SHREFCOUNTIMPL_HPP
00026
00027 #include "ShRefCount.hpp"
00028
00029 namespace SH {
00030
00031 template<typename T>
00032 inline
00033 ShPointer<T>::ShPointer()
00034 : m_object(0)
00035 {
00036 #ifdef SH_REFCOUNT_DEBUGGING
00037 SH_RCDEBUG_BLUE;
00038 std::cerr << "[cons] " << std::flush << std::setw(10) << "0" << " (default)" << std::endl;
00039 SH_RCDEBUG_NORMAL;
00040 #endif
00041 }
00042
00043 template<typename T>
00044 inline
00045 ShPointer<T>::ShPointer(T* object)
00046 : m_object(object)
00047 {
00048 #ifdef SH_REFCOUNT_DEBUGGING
00049 SH_RCDEBUG_BLUE;
00050 std::cerr << "[cons] " << std::flush << std::setw(10) << object << " <" << (object ? typeid(*(object)).name() : "n/a") << ">" << std::endl;
00051 SH_RCDEBUG_NORMAL;
00052 #endif
00053 if (m_object) m_object->acquireRef();
00054 }
00055
00056 template<typename T>
00057 inline
00058 ShPointer<T>::ShPointer(const ShPointer<T>& other)
00059 : m_object(other.m_object)
00060 {
00061 #ifdef SH_REFCOUNT_DEBUGGING
00062 SH_DEBUG_ASSERT((unsigned long)m_object < 0xb0000000L);
00063 SH_RCDEBUG_BLUE;
00064 std::cerr << "[copy] " << std::flush << std::setw(10) << other.m_object << " <" << (other.m_object ? typeid(*(other.m_object)).name() : "n/a") << ">" << std::endl;
00065 SH_RCDEBUG_NORMAL;
00066 #endif
00067
00068
00069 if (m_object) m_object->acquireRef();
00070 }
00071
00072 template<typename T>
00073 template<typename S>
00074 inline
00075 ShPointer<T>::ShPointer(const ShPointer<S>& other)
00076 : m_object(other.object())
00077 {
00078 #ifdef SH_REFCOUNT_DEBUGGING
00079 SH_RCDEBUG_BLUE;
00080 std::cerr << "[cpct] " << std::flush << std::setw(10) << other.object()
00081 << " <" << (other.object() ? typeid(*(other.object())).name() : "n/a") << ">"
00082 << " -> " << typeid(T).name()
00083 << std::endl;
00084 SH_RCDEBUG_NORMAL;
00085 #endif
00086
00087 if (m_object) m_object->acquireRef();
00088 }
00089
00090 template<typename T>
00091 inline
00092 void ShPointer<T>::releaseRef()
00093 {
00094 if (m_object && m_object->releaseRef() == 0) {
00095 #ifdef SH_REFCOUNT_DEBUGGING
00096 SH_RCDEBUG_RED;
00097 std::cerr << "[dstr] " << std::flush << std::setw(10) << m_object << " <" << typeid(*m_object).name() << ">" << std::endl;
00098 SH_RCDEBUG_NORMAL;
00099 #endif
00100 delete m_object;
00101 #ifdef SH_REFCOUNT_DEBUGGING
00102 m_object = 0;
00103 #endif
00104 }
00105 }
00106
00107 template<typename T>
00108 inline
00109 ShPointer<T>::~ShPointer()
00110 {
00111 #ifdef SH_REFCOUNT_DEBUGGING
00112 SH_RCDEBUG_BLUE;
00113 std::cerr << "[dest] " << std::flush << std::setw(10) << m_object << " <" << (m_object ? typeid(*(m_object)).name() : "n/a") << ">" << std::endl;
00114 SH_RCDEBUG_NORMAL;
00115 #endif
00116
00117 releaseRef();
00118 }
00119
00120 template<typename T>
00121 inline
00122 void ShPointer<T>::swap(ShPointer<T>& other)
00123 {
00124 #ifdef SH_REFCOUNT_DEBUGGING
00125 SH_RCDEBUG_BLUE;
00126 std::cerr << "[swap] " << std::flush << std::setw(10) << other.m_object << " <" << (other.m_object ? typeid(*(other.m_object)).name() : "n/a") << ">" << " (was " << m_object << " <" << (m_object ? typeid(*(m_object)).name() : "n/a") << ">" << ")" << std::endl;
00127 SH_RCDEBUG_NORMAL;
00128 #endif
00129 T* t = m_object;
00130 m_object = other.m_object;
00131 other.m_object = t;
00132 }
00133
00134 template<typename T>
00135 inline
00136 ShPointer<T>& ShPointer<T>::operator=(T* object)
00137 {
00138 #ifdef SH_REFCOUNT_DEBUGGING
00139 SH_RCDEBUG_BLUE;
00140 std::cerr << "[asn*] " << std::flush << std::setw(10) << object << " <" << (object ? typeid(*(object)).name() : "n/a") << ">" << " (was " << m_object << " <" << (m_object ? typeid(*(m_object)).name() : "n/a") << ">" << ")" << std::endl;
00141 SH_RCDEBUG_NORMAL;
00142 #endif
00143
00144 ShPointer<T> t(object);
00145 t.swap(*this);
00146
00147 return *this;
00148 }
00149
00150 template<typename T>
00151 inline
00152 ShPointer<T>& ShPointer<T>::operator=(const ShPointer<T>& other)
00153 {
00154 #ifdef SH_REFCOUNT_DEBUGGING
00155 SH_RCDEBUG_BLUE;
00156 std::cerr << "[assn] " << std::flush << std::setw(10) << other.m_object << " <" << (other.m_object ? typeid(*(other.m_object)).name() : "n/a") << ">" << " (was " << m_object << " <" << (m_object ? typeid(*(m_object)).name() : "n/a") << ">" << ")" << std::endl;
00157 SH_RCDEBUG_NORMAL;
00158 #endif
00159
00160 ShPointer<T> t(other);
00161 t.swap(*this);
00162
00163 return *this;
00164 }
00165
00166 template<typename T>
00167 template<typename S>
00168 inline
00169 ShPointer<T>& ShPointer<T>::operator=(const ShPointer<S>& other)
00170 {
00171 #ifdef SH_REFCOUNT_DEBUGGING
00172 SH_RCDEBUG_BLUE;
00173 std::cerr << "[assn] " << std::flush << std::setw(10) << other.object() << " <" << (other.object() ? typeid(*(other.object())).name() : "n/a") << ">" << " (was " << m_object << " <" << (m_object ? typeid(*(m_object)).name() : "n/a") << ">" << ")" << std::endl;
00174 SH_RCDEBUG_NORMAL;
00175 #endif
00176
00177 ShPointer<T> t(other);
00178 t.swap(*this);
00179
00180 return *this;
00181 }
00182
00183 template<typename T>
00184 inline
00185 bool ShPointer<T>::operator==(const ShPointer<T>& other) const
00186 {
00187 return m_object == other.m_object;
00188 }
00189
00190 template<typename T>
00191 inline
00192 bool ShPointer<T>::operator!=(const ShPointer<T>& other) const
00193 {
00194 return m_object != other.m_object;
00195 }
00196
00197 template<typename T>
00198 inline
00199 bool ShPointer<T>::operator<(const ShPointer<T>& other) const
00200 {
00201 return m_object < other.m_object;
00202 }
00203
00204 template<typename T>
00205 inline
00206 T& ShPointer<T>::operator*() const
00207 {
00208 return *m_object;
00209 }
00210
00211 template<typename T>
00212 inline
00213 T* ShPointer<T>::operator->() const
00214 {
00215 return m_object;
00216 }
00217
00218 template<typename T>
00219 inline
00220 ShPointer<T>::operator bool() const
00221 {
00222 return m_object != 0;
00223 }
00224
00225 template<typename T>
00226 inline
00227 int ShPointer<T>::refCount() const
00228 {
00229 if (!m_object)
00230 return 0;
00231 else
00232 return m_object->refCount();
00233 }
00234
00235 template<typename T>
00236 inline
00237 T* ShPointer<T>::object() const
00238 {
00239 return m_object;
00240 }
00241
00242
00243 template<typename T, typename S>
00244 ShPointer<T> shref_static_cast(const ShPointer<S>& other)
00245 {
00246 #ifdef SH_REFCOUNT_DEBUGGING
00247 SH_RCDEBUG_BLUE;
00248 std::cerr << "[csts] " << std::flush << std::setw(10) << other.object() << " <" << (other.object() ? typeid(*(other.object())).name() : "n/a") << ">"
00249 << " -> " << typeid(T).name() << std::endl;
00250 SH_RCDEBUG_NORMAL;
00251 #endif
00252
00253 return ShPointer<T>(static_cast<T*>(other.object()));
00254 }
00255
00256 template<typename T, typename S>
00257 ShPointer<T> shref_dynamic_cast(const ShPointer<S>& other)
00258 {
00259 #ifdef SH_REFCOUNT_DEBUGGING
00260 SH_RCDEBUG_BLUE;
00261 std::cerr << "[cstd] " << std::flush << std::setw(10) << other.object() << " <" << (other.object() ? typeid(*(other.object())).name() : "n/a") << ">"
00262 << " -> " << typeid(T).name() << std::endl;
00263 SH_RCDEBUG_NORMAL;
00264 #endif
00265 return ShPointer<T>(dynamic_cast<T*>(other.object()));
00266 }
00267
00268 template<typename T, typename S>
00269 ShPointer<T> shref_const_cast(const ShPointer<S>& other)
00270 {
00271 #ifdef SH_REFCOUNT_DEBUGGING
00272 SH_RCDEBUG_BLUE;
00273 std::cerr << "[cstc] " << std::flush << std::setw(10) << other.object() << " <" << (other.object() ? typeid(*(other.object())).name() : "n/a") << ">"
00274 << " -> " << typeid(T).name() << std::endl;
00275 SH_RCDEBUG_NORMAL;
00276 #endif
00277 return ShPointer<T>(const_cast<T*>(other.object()));
00278 }
00279
00280 }
00281
00282 #endif