12 #define KK_PTR_OUT(x) cout<<x;
46 virtual operator bool()
const=0;
47 virtual long int GID()
const=0;
72 inline sptr(T*d_=0) : data(d_), count(0), gid(-1)
79 KK_PTR_OUT(
"built sptr for address "<<data<<
", gid "<<gid<<
", count "<<(count ? (*count) : -1)<<endl);
83 inline sptr(
const sptr<T> &p ) : data(p.data), count(p.count), gid(p.gid)
86 KK_PTR_OUT(
"built sptr for address "<<data<<
", gid "<<gid<<
", count "<<(count ? (*count) : -1)<<endl);
93 KK_PTR_OUT(
"del sptr for address "<<data<<
", gid "<<gid<<
", count "<<(count ? (*count) : -1)<<endl);
97 else if ( (*count)<0 )
104 if ( *
this==p )
return *
this;
115 KK_PTR_OUT(
"opr= sptr for address "<<data<<
", gid "<<gid<<
", count "<<(count ? (*count) : -1)<<endl);
125 KK::Assert<sptr_Err> ( (data==p.data && gid==p.gid) || (data!=p.data && gid!=p.gid),
126 "ERROR: sptr_Err: sptr::== called but data and global id do not match!" );
128 KK_PTR_OUT(
"this : "<<data<<
" "<<gid<<
" ; p : "<<p.data<<
" "<<p.gid<<endl);
129 return (data==p.data && gid==p.gid);
137 {
return !(*
this==p); }
142 KK::Assert<sptr_Err> ( data,
"ERROR: sptr_Err: sptr-> called for null pointer!" );
149 KK::Assert<sptr_Err> ( data,
"ERROR: sptr_Err: sptr-> called for null pointer!" );
156 KK::Assert<sptr_Err> ( data,
"ERROR: sptr_Err: *sptr called for null pointer!" );
163 KK::Assert<sptr_Err> ( data,
"ERROR: sptr_Err: *sptr called for null pointer!" );
168 inline const T*
raw()
const
172 inline operator bool()
const
174 KK::Assert<sptr_Err>( (data==0&&gid==-1)||(data!=0 && gid>-1) );
175 return (data!=0 && gid>-1);
179 inline long int GID()
const {
return gid; }
182 inline int nRefs()
const {
return count ? *count : 0; }
187 sptr( T* rp,
int * start_count,
long int gid_) : data(rp), count(start_count), gid(gid_)
189 KK::Assert(start_count>0,
"ERROR : protected sptr constructor : invalid initial count");
198 KK::Assert<sptr_Err>( data,
"ERROR: sptr_Err: sptr::increment called with no data!" );
206 KK_PTR_OUT(
"count is at address "<<count<<endl);
207 KK_PTR_OUT(
"incrementing "<<data<<
", gid "<<gid<<
", count "<<(count ? (*count) : -1)<<endl);
216 KK::Assert<sptr_Err>( data,
"ERROR: sptr_Err: sptr::decrement called with no data!" );
218 KK::Assert<sptr_Err>( count && (*count)>0,
219 "ERROR: sptr_Err: sptr::decrement called with invalid reference count!" );
220 KK_PTR_OUT(
"decrementing "<<data<<
", gid "<<gid<<
", count "<<(count ? (*count) : -1)<<endl);
227 KK::Assert<sptr_Err>( (*count)==0,
"ERROR: sptr_Err: sptr::destroy called with nonzero count!" );
242 template<
typename TO,
typename FROM>
246 TO * to =
static_cast<TO*
>((FROM*)p.
raw());
249 return sptr<TO>(to, p.count, p.gid);
254 template<
typename TO,
typename FROM>
261 template<
typename TO,
typename FROM>
265 TO * to =
dynamic_cast<TO*
>((FROM*)p.
raw());
268 return sptr<TO>(to, p.count, p.gid);
274 template<
typename TO>
276 sptr<TO> vptr_cast( vptr &p )
278 sptr<TO> *sp =
dynamic_cast< sptr<TO>*
>(&p);