ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/util.h
(Generate patch)

Comparing deliantra/server/include/util.h (file contents):
Revision 1.53 by root, Fri Jul 13 15:54:40 2007 UTC vs.
Revision 1.54 by root, Mon Aug 6 10:54:12 2007 UTC

333 333
334typedef tausworthe_random_generator rand_gen; 334typedef tausworthe_random_generator rand_gen;
335 335
336extern rand_gen rndm; 336extern rand_gen rndm;
337 337
338INTERFACE_CLASS (attachable)
339struct refcnt_base
340{
341 typedef int refcnt_t;
342 mutable refcnt_t ACC (RW, refcnt);
343
344 MTH void refcnt_inc () const { ++refcnt; }
345 MTH void refcnt_dec () const { --refcnt; }
346
347 refcnt_base () : refcnt (0) { }
348};
349
350extern refcnt_base::refcnt_t refcnt_dummy;
351
338template<class T> 352template<class T>
339struct refptr 353struct refptr
340{ 354{
355 // p if not null
356 refcnt_base::refcnt_t *refcnt_ref () { return p ? &p->refcnt : &refcnt_dummy; }
357
358 void refcnt_dec ()
359 {
360 if (!is_constant (p))
361 --*refcnt_ref ();
362 else if (p)
363 --p->refcnt;
364 }
365
366 void refcnt_inc ()
367 {
368 if (!is_constant (p))
369 ++*refcnt_ref ();
370 else if (p)
371 ++p->refcnt;
372 }
373
341 T *p; 374 T *p;
342 375
343 refptr () : p(0) { } 376 refptr () : p(0) { }
344 refptr (const refptr<T> &p) : p(p.p) { if (p) p->refcnt_inc (); } 377 refptr (const refptr<T> &p) : p(p.p) { refcnt_inc (); }
345 refptr (T *p) : p(p) { if (p) p->refcnt_inc (); } 378 refptr (T *p) : p(p) { refcnt_inc (); }
346 ~refptr () { if (p) p->refcnt_dec (); } 379 ~refptr () { refcnt_dec (); }
347 380
348 const refptr<T> &operator =(T *o) 381 const refptr<T> &operator =(T *o)
349 { 382 {
383 // if decrementing ever destroys we need to reverse the order here
350 if (p) p->refcnt_dec (); 384 refcnt_dec ();
351 p = o; 385 p = o;
352 if (p) p->refcnt_inc (); 386 refcnt_inc ();
353
354 return *this; 387 return *this;
355 } 388 }
356 389
357 const refptr<T> &operator =(const refptr<T> o) 390 const refptr<T> &operator =(const refptr<T> &o)
358 { 391 {
359 *this = o.p; 392 *this = o.p;
360 return *this; 393 return *this;
361 } 394 }
362 395
363 T &operator * () const { return *p; } 396 T &operator * () const { return *p; }
364 T *operator ->() const { return p; } 397 T *operator ->() const { return p; }
365 398
366 operator T *() const { return p; } 399 operator T *() const { return p; }
367}; 400};
368 401
369typedef refptr<maptile> maptile_ptr; 402typedef refptr<maptile> maptile_ptr;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines