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