|
luna-sysmgr-common
|
#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 |
Smart pointer object
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.
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.
| ptr | Pointer to object to make a smart pointer to |
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.
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.
| s | Smart pointer to copy |
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.
|
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.
Smart pointer inequality operator
Checks to see if this smart pointer does not point to the same class instance as another smart pointer.
| p | Smart pointer to check for inequality |
|
inline |
Regular pointer inequality operator
Checks to see if this smart pointer does not point to the same class instance as a regular pointer.
| p | Pointer to check for inequality |
|
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.
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.
| s | Smart pointer to copy |
|
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.
| ptr | Pointer to object to point to |
Smart pointer equality operator
Checks to see if this smart pointer points to the same class instance as another smart pointer.
| p | Smart pointer to check for equality |
|
inline |
Regular pointer equality operator
Checks to see if this smart pointer points to the same class instance as a regular pointer.
| p | Pointer to check for equality |