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