#include <RefCountedObject.h>
Inheritance diagram for oasys::RefCountedObject:
This class maintains an integer count of the live references on it, and calls the virtual function no_more_refs() when the refcount goes to zero. The default implementation of no_more_refs() simply calls 'delete this'.
Also, this implementation declares add_ref and del_ref as const methods, and declares refcount_ to be mutable, the reason for which being that taking or removing a reference on an object doesn't actually modify the object itself.
Finally, the class also stores a Logger instance that's used for debug logging of add_ref/del_ref calls. Note that we use a contained logger (rather than inheritance) to avoid conflicts with descendent classes that may themselves inherit from Logger.
The RefCountedObject inherits from Formatter and defines a simple implementation of Format that just includes the pointer value, but other derived classes can (and should) override format to print something more useful.
Definition at line 49 of file RefCountedObject.h.
Public Member Functions | |
RefCountedObject (const char *logpath) | |
Constructor that takes the debug logging path to be used for add and delete reference logging. | |
virtual | ~RefCountedObject () |
Virtual destructor declaration. | |
void | add_ref (const char *what1, const char *what2="") const |
Bump up the reference count. | |
void | del_ref (const char *what1, const char *what2="") const |
Bump down the reference count. | |
virtual void | no_more_refs () const |
Hook called when the refcount goes to zero. | |
int | format (char *buf, size_t sz) const |
Virtual from Formatter. | |
u_int32_t | refcount () const |
Accessor for the refcount value. | |
Protected Attributes | |
atomic_t | refcount_ |
The reference count. | |
Logger | logger_ |
Logger object used for debug logging. |
oasys::RefCountedObject::RefCountedObject | ( | const char * | logpath | ) |
Constructor that takes the debug logging path to be used for add and delete reference logging.
Definition at line 23 of file RefCountedObject.cc.
oasys::RefCountedObject::~RefCountedObject | ( | ) | [virtual] |
void oasys::RefCountedObject::add_ref | ( | const char * | what1, | |
const char * | what2 = "" | |||
) | const |
Bump up the reference count.
what1 | debugging string identifying who is incrementing the refcount | |
what2 | optional additional debugging info |
Definition at line 36 of file RefCountedObject.cc.
References ASSERT, oasys::atomic_incr(), oasys::LOG_DEBUG, oasys::Logger::logf(), logger_, refcount_, and oasys::atomic_t::value.
void oasys::RefCountedObject::del_ref | ( | const char * | what1, | |
const char * | what2 = "" | |||
) | const |
Bump down the reference count.
what1 | debugging string identifying who is decrementing the refcount | |
what2 | optional additional debugging info |
Definition at line 49 of file RefCountedObject.cc.
References ASSERT, oasys::atomic_decr_test(), oasys::LOG_DEBUG, oasys::Logger::logf(), logger_, no_more_refs(), refcount_, and oasys::atomic_t::value.
void oasys::RefCountedObject::no_more_refs | ( | ) | const [virtual] |
Hook called when the refcount goes to zero.
Definition at line 71 of file RefCountedObject.cc.
References oasys::LOG_DEBUG, oasys::Logger::logf(), and logger_.
Referenced by del_ref().
int oasys::RefCountedObject::format | ( | char * | buf, | |
size_t | sz | |||
) | const [virtual] |
Virtual from Formatter.
Implements oasys::Formatter.
Reimplemented in dtn::Contact.
Definition at line 79 of file RefCountedObject.cc.
References snprintf().
u_int32_t oasys::RefCountedObject::refcount | ( | ) | const [inline] |
Accessor for the refcount value.
Definition at line 93 of file RefCountedObject.h.
References refcount_, and oasys::atomic_t::value.
atomic_t oasys::RefCountedObject::refcount_ [mutable, protected] |
The reference count.
Definition at line 97 of file RefCountedObject.h.
Referenced by add_ref(), del_ref(), and refcount().
Logger oasys::RefCountedObject::logger_ [protected] |
Logger object used for debug logging.
Definition at line 100 of file RefCountedObject.h.
Referenced by add_ref(), del_ref(), and no_more_refs().