luna-sysmgr-common
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sptr< T > Class Template Reference

#include <sptr.h>

Public Member Functions

 sptr (T *ptr)
 
 sptr ()
 
 sptr (const sptr< T > &s)
 
 ~sptr ()
 
void operator= (const sptr< T > &s)
 
void operator= (T *ptr)
 
T * operator-> () const
 
T * get () const
 
bool operator== (const sptr< T > &p) const
 
bool operator!= (const sptr< T > &p) const
 
bool operator== (T *p) const
 
bool operator!= (T *p) const
 

Detailed Description

template<class T>
class sptr< T >

Smart pointer object

Todo:
Limit this class to only being used on derivatives of RefCounted.

Smart pointers are a way of having a pointer that automatically tracks use and cleans up the object it points to when nobody is using it. It's a convenience class so pointer users don't have to worry about cleaning up objects they're pointing to. If everything that uses a pointer uses this class, memory should stay much cleaner over long running times by preventing memory leaks from code not explicitly cleaning up the memory it uses.

This class is basically a drop-in replacement for a regular pointer which can be used for pointing to any class that derives from RefCounted. It handles the details of tracking the reference count automatically. By using this smart pointer object, you should never have to worry about calling RefCounted::ref(), RefCounted::deref(), or cleaning up memory.

Constructor & Destructor Documentation

template<class T>
sptr< T >::sptr ( T *  ptr)
inline

Initializes a smart pointer to a reference counted object

Since this is a new pointer to the object, its reference count needs to track that there's a new pointer to it. By doing that here, we insulate the user of this smart pointer from having to worry about that.

Parameters
ptrPointer to object to make a smart pointer to
template<class T>
sptr< T >::sptr ( )
inline

Initialize a null smart pointer

There may be times where a function requires a smart pointer but you want to indicate a null. This constructor allows that without trying to call RefCounted::ref() on a null pointer and crashing the program.

template<class T>
sptr< T >::sptr ( const sptr< T > &  s)
inline

Copy another smart pointer

This copy constructor allows you to copy another smart pointer into this one. Since it is a new pointer to the same object, we need to indicate that to the RefCounted object so it can track when to clean up. So we increment the reference count here so if the "copied from" smart pointer is deleted, it's still tracking our new pointer.

Parameters
sSmart pointer to copy
template<class T>
sptr< T >::~sptr ( )
inline

Clean up smart pointer

We're now getting rid of a smart pointer, which means we need to let our target object know that. Tell it by calling RefCounted::deref() on it. Once we no longer have any pointers to the object, it may just clean itself up, but that's up to the target object to decide.

Member Function Documentation

template<class T>
T* sptr< T >::get ( ) const
inline

Gets a pointer to the target object for this smart pointer

Gets the "regular pointer" version of this smart pointer - essentially a regular pointer straight to the target object without using any reference counting functionality.

Returns
Pointer to object being pointed to
template<class T>
bool sptr< T >::operator!= ( const sptr< T > &  p) const
inline

Smart pointer inequality operator

Checks to see if this smart pointer does not point to the same class instance as another smart pointer.

Parameters
pSmart pointer to check for inequality
template<class T>
bool sptr< T >::operator!= ( T *  p) const
inline

Regular pointer inequality operator

Checks to see if this smart pointer does not point to the same class instance as a regular pointer.

Parameters
pPointer to check for inequality
template<class T>
T* sptr< T >::operator-> ( ) const
inline

Dereference operator

This operator allows code to access the object being pointed to by a smart pointer using exactly the same dereferencing syntax of a regular pointer, once again making smart pointers largely a drop-in replacement for regular pointers with minimal code changes.

Returns
Pointer to object being pointed to
template<class T>
void sptr< T >::operator= ( const sptr< T > &  s)
inline

Smart pointer copy operator

This operator is called when you set a smart pointer = another smart pointer. But it's not quite that simple. Since we're copying a pointer to another pointer, we're having a new pointer point to the same object, which means we need to tell that object about it so it can track it for cleanup. Without this, it could potentially delete itself from memory if the "copied from" pointer is cleaned up, leaving the new copy with an invalid pointer.

Parameters
sSmart pointer to copy
template<class T>
void sptr< T >::operator= ( T *  ptr)
inline

Pointer copy operator

This operator is similar to the smart pointer copy operator, but has one interesting difference. This is an overloaded version that is triggered when copying from a regular pointer instead of another smart pointer. This allows for compatibility with non-smart pointer code and largely allows smart pointers to be used as a drop-in replacement for regular pointers.

Parameters
ptrPointer to object to point to
template<class T>
bool sptr< T >::operator== ( const sptr< T > &  p) const
inline

Smart pointer equality operator

Checks to see if this smart pointer points to the same class instance as another smart pointer.

Parameters
pSmart pointer to check for equality
template<class T>
bool sptr< T >::operator== ( T *  p) const
inline

Regular pointer equality operator

Checks to see if this smart pointer points to the same class instance as a regular pointer.

Parameters
pPointer to check for equality

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