… | |
… | |
333 | |
333 | |
334 | typedef tausworthe_random_generator rand_gen; |
334 | typedef tausworthe_random_generator rand_gen; |
335 | |
335 | |
336 | extern rand_gen rndm; |
336 | extern rand_gen rndm; |
337 | |
337 | |
|
|
338 | INTERFACE_CLASS (attachable) |
|
|
339 | struct 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 | |
|
|
350 | // to avoid branches with more advanced compilers |
|
|
351 | extern refcnt_base::refcnt_t refcnt_dummy; |
|
|
352 | |
338 | template<class T> |
353 | template<class T> |
339 | struct refptr |
354 | struct refptr |
340 | { |
355 | { |
|
|
356 | // p if not null |
|
|
357 | refcnt_base::refcnt_t *refcnt_ref () { return p ? &p->refcnt : &refcnt_dummy; } |
|
|
358 | |
|
|
359 | void refcnt_dec () |
|
|
360 | { |
|
|
361 | if (!is_constant (p)) |
|
|
362 | --*refcnt_ref (); |
|
|
363 | else if (p) |
|
|
364 | --p->refcnt; |
|
|
365 | } |
|
|
366 | |
|
|
367 | void refcnt_inc () |
|
|
368 | { |
|
|
369 | if (!is_constant (p)) |
|
|
370 | ++*refcnt_ref (); |
|
|
371 | else if (p) |
|
|
372 | ++p->refcnt; |
|
|
373 | } |
|
|
374 | |
341 | T *p; |
375 | T *p; |
342 | |
376 | |
343 | refptr () : p(0) { } |
377 | refptr () : p(0) { } |
344 | refptr (const refptr<T> &p) : p(p.p) { if (p) p->refcnt_inc (); } |
378 | refptr (const refptr<T> &p) : p(p.p) { refcnt_inc (); } |
345 | refptr (T *p) : p(p) { if (p) p->refcnt_inc (); } |
379 | refptr (T *p) : p(p) { refcnt_inc (); } |
346 | ~refptr () { if (p) p->refcnt_dec (); } |
380 | ~refptr () { refcnt_dec (); } |
347 | |
381 | |
348 | const refptr<T> &operator =(T *o) |
382 | const refptr<T> &operator =(T *o) |
349 | { |
383 | { |
|
|
384 | // if decrementing ever destroys we need to reverse the order here |
350 | if (p) p->refcnt_dec (); |
385 | refcnt_dec (); |
351 | p = o; |
386 | p = o; |
352 | if (p) p->refcnt_inc (); |
387 | refcnt_inc (); |
353 | |
|
|
354 | return *this; |
388 | return *this; |
355 | } |
389 | } |
356 | |
390 | |
357 | const refptr<T> &operator =(const refptr<T> o) |
391 | const refptr<T> &operator =(const refptr<T> &o) |
358 | { |
392 | { |
359 | *this = o.p; |
393 | *this = o.p; |
360 | return *this; |
394 | return *this; |
361 | } |
395 | } |
362 | |
396 | |
363 | T &operator * () const { return *p; } |
397 | T &operator * () const { return *p; } |
364 | T *operator ->() const { return p; } |
398 | T *operator ->() const { return p; } |
365 | |
399 | |
366 | operator T *() const { return p; } |
400 | operator T *() const { return p; } |
367 | }; |
401 | }; |
368 | |
402 | |
369 | typedef refptr<maptile> maptile_ptr; |
403 | typedef refptr<maptile> maptile_ptr; |
… | |
… | |
458 | return obj->*indexmember |
492 | return obj->*indexmember |
459 | ? this->begin () + obj->*indexmember - 1 |
493 | ? this->begin () + obj->*indexmember - 1 |
460 | : this->end (); |
494 | : this->end (); |
461 | } |
495 | } |
462 | |
496 | |
|
|
497 | void push_back (T *obj) |
|
|
498 | { |
|
|
499 | std::vector<T *, slice_allocator<T *> >::push_back (obj); |
|
|
500 | obj->*indexmember = this->size (); |
|
|
501 | } |
|
|
502 | |
463 | void insert (T *obj) |
503 | void insert (T *obj) |
464 | { |
504 | { |
465 | push_back (obj); |
505 | push_back (obj); |
466 | obj->*indexmember = this->size (); |
|
|
467 | } |
506 | } |
468 | |
507 | |
469 | void insert (T &obj) |
508 | void insert (T &obj) |
470 | { |
509 | { |
471 | insert (&obj); |
510 | insert (&obj); |
… | |
… | |
506 | // return current time as timestampe |
545 | // return current time as timestampe |
507 | tstamp now (); |
546 | tstamp now (); |
508 | |
547 | |
509 | int similar_direction (int a, int b); |
548 | int similar_direction (int a, int b); |
510 | |
549 | |
511 | // like printf, but returns a std::string |
550 | // like sprintf, but returns a "static" buffer |
512 | const std::string format (const char *format, ...); |
551 | const char *format (const char *format, ...); |
513 | |
552 | |
514 | #endif |
553 | #endif |
515 | |
554 | |