luna-sysmgr-common
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RefCounted Class Reference

#include <sptr.h>

Inheritance diagram for RefCounted:
[legend]

Public Member Functions

 RefCounted ()
 
virtual ~RefCounted ()
 
void ref ()
 
void deref ()
 

Detailed Description

Base class for reference counted objects

This class keeps a "reference count" of how many other objects currently reference this object so it is not deleted from memory while other objects are still using it. By deriving from this class, subclasses will automatically clean themselves up from memory once they are no longer being used.

Every time a new pointer is created that points to this object, the code managing the pointer should call RefCounted::ref() to increment the reference count, indicating that there's something new referencing this instance. When that pointer is no longer going to be used, the code managing the pointer should call RefCounted::deref() to decrement the reference count, indicating that there is one less pointer to this object floating around in memory. When this object detects that the reference count reaches zero (meaning that there is nothing currently using the object), RefCounted::deref() automatically cleans it up from memory.

This class is not intended to be instantiated on its own, though there is nothing preventing that. It is designed as a utility base class for other classes to derive from. With this class around, all you have to do to have your object automatically clean up its own memory is to derive from this class. The functionality in RefCounted takes care of all of the details for you.

Constructor & Destructor Documentation

RefCounted::RefCounted ( )
inline

Initializes the reference count to zero

By setting the initial reference count to zero, it indicates that this object is not currently being used by any other classes or pieces of code. Shortly after being constructed, in most cases the code constructing the subclass should call RefCounted::ref() to indicate that it is, in fact, being used.

virtual RefCounted::~RefCounted ( )
inlinevirtual

Cleans up a reference counted object

Since the reference counter is automatically cleaned up at destruction, this serves as a placeholder to ensure that the destructor is declared as virtual. This makes it so that when a RefCounted pointer is deleted, the destructor of the derived class is called instead of just the destructor of RefCounted.

Member Function Documentation

void RefCounted::deref ( )
inline

Decrements the reference count and cleans up the object if there are no more references

Calling of this method denotes that a previously-used pointer to this object will no longer be used. This method allows for automatic cleanup of a derived class when it is no longer used.

void RefCounted::ref ( )
inline

Increments the reference count

Calling of this method denotes that there is a new pointer to this object somewhere in memory. Every time a new pointer is created to a class derived from RefCounted, RefCounted::ref() should be called on that object so it can track how many open pointers there are for automatic memory cleanup.


The documentation for this class was generated from the following file: