This is used to get RAII semantics when not provided inherently. More...
#include <managedptr.h>
Public Types | |
typedef Handle::TargetPtrType | TargetPtrType |
Used to set the return type of pointer like returns (could be a smartpointer) | |
typedef Handle::TargetType | TargetType |
Used to set the value and reference type for returns. | |
Public Member Functions | |
ManagedPtr (Handle StatefulHandle=Handle()) | |
Constructs the target after passing it the desired state information. More... | |
ManagedPtr (ManagedPtr &Other) | |
Creates the Target from an already constructed one but invalidates Other ManagedPtr. More... | |
~ManagedPtr () | |
Destroy the target and invalidate it. | |
TargetPtrType | Get () |
Get the raw pointer to the managed object. More... | |
TargetPtrType | get () |
Get the raw pointer to the managed object. More... | |
operator Boole () | |
Returns true if this pointer points to something. More... | |
TargetType & | operator* () |
Dereference operator. More... | |
TargetPtrType | operator-> () |
The Structure dereference operator. More... | |
ManagedPtr & | operator= (ManagedPtr &Other) |
Assigns the Target but invalidates Other ManagedPtr. More... | |
Boole | operator== (ManagedPtr &Other) |
Compares the underlying pointer of this and another ManagedPtr, (This had better return false) More... | |
This is used to get RAII semantics when not provided inherently.
Some APIs have functions that return raw pointers and expect the call to clean up the mess. Even worse sometimes doing this involves more than This is exactly the size of the handle class used. It could be as small as raw pointer if constructed carefully.
To use this a 'Handle' must be created first. A good handle is trivial copyable, assignable and implements a few functions. This is similar to the Pimpl idiom in that the details of the implemention could be abstracted into this 'Handle'. The 'Handle' will encapsulate/have the knowledge of how to correctly create and destroy the pointer while the ManagedPtr provides The pointer-like semantics and esures it is destructed exactly once. This is expected to have members:
The semantics of this class also presume that copying and assigning a Handle is safe. To do that uses these functions as well. It is intended that pointer copies be shallow copies.
Definition at line 115 of file managedptr.h.
|
inline |
Constructs the target after passing it the desired state information.
StatefulHandle | A way to pass parameters into the constructors of the underlying class being targeted. |
The Handle class can be implemented any way that is appropriate for the underlying data. So it is not limited to the members defined here. This constructor will call the copy copystructor of the handle before Calling Construct() on it.
Definition at line 153 of file managedptr.h.
|
inline |
Creates the Target from an already constructed one but invalidates Other ManagedPtr.
This transfer ownership (who will deconstruct the handle) from the Other ManagedPtr to this one. This Does not call Construct().
Other | The ManagedPtr to copy then invalidate. |
Definition at line 163 of file managedptr.h.
|
inline |
Get the raw pointer to the managed object.
Definition at line 206 of file managedptr.h.
|
inline |
Get the raw pointer to the managed object.
Definition at line 211 of file managedptr.h.
|
inline |
Returns true if this pointer points to something.
Definition at line 216 of file managedptr.h.
|
inline |
Dereference operator.
If | the pointerNothing This member function does not throw exceptions unless the underlying handle does. |
Definition at line 183 of file managedptr.h.
|
inline |
The Structure dereference operator.
Definition at line 194 of file managedptr.h.
|
inline |
Assigns the Target but invalidates Other ManagedPtr.
This transfer ownership (who will deconstruct the handle) from the Other ManagedPtr to this one. This Does not call Construct().
Other | The ManagedPtr to copy then invalidate. |
Definition at line 174 of file managedptr.h.
|
inline |
Compares the underlying pointer of this and another ManagedPtr, (This had better return false)
Other | The right hand portion of the comparison |
Definition at line 222 of file managedptr.h.