Smart pointer for any class inheriting SoRefCounter. More...
#include <Inventor/misc/SoRef.h>
Public Member Functions | |
SoRef () | |
SoRef (const SoRef< T > &that) | |
SoRef (T *pIn) | |
template<typename Y > | |
SoRef (const SoRef< Y > &that) | |
virtual | ~SoRef () |
T * | releaseNoDelete () |
SoRef< T > & | operator= (const SoRef< T > &that) |
template<typename Y > | |
SoRef< T > & | operator= (const SoRef< Y > &that) |
SoRef< T > & | operator= (T *ptr) |
T & | operator* () const |
T * | operator-> () const |
T * | ptr () const |
Deprecated | |
| |
SoDEPRECATED | operator T * () const |
Smart pointer for any class inheriting SoRefCounter.
This class implements a smart pointer for any reference counted class (class that inherits SoRefCounter). This includes nodes, engines, paths, buffer objects and others. We strongly recommend using SoRef to manage the life span of these objects (instead of the old style ref() and unref() methods).
The reference count of the managed object is automatically incremented when the SoRef is created and then decremented when the SoRef is destroyed, for example by going out of scope, or when 'null' is assigned to the SoRef object. If this decrement changes the managed object's reference count to zero, the managed object is destroyed.
For example SoRef allows a reference counted object to be safely cleaned up when it goes out of scope in a local block, as shown in the example below.
{ // Create temporary buffer object SoRef<SoCpuBufferObject> cpuObj = new SoCpuBufferObject; cpuObj->map(SoBufferObject::READ_ONLY); // Use the buffer object . . . cpuObj->unmap(); // Buffer object will be automatically cleaned up }
You can also explicitly cleanup a reference counted object like this:
SoRef<SoPath> tempPath = ... // Create temporary object action->apply( tempPath ); tempPath = nullptr; // Cleanup temporary object
Generally, in a function, you should not use SoRef on a reference counted object that will be returned by the function. In this case the calling code would receive a pointer to an invalid object after the SoRef goes out of scope and unref's the object.
Note that implicit casting to a pointer is not supported. So when a method expects a pointer, for example addChild(), you must use the explicit ptr() method. For example:
SoRef<SoCone> cone = new SoCone(); // Use root->addChild( cone.ptr() ); // NOT root->addChild( cone );
Construct from another SoRef.
SoDEPRECATED SoRef< T >::operator T * | ( | ) | const [inline] |
Cast to C pointer.
T& SoRef< T >::operator* | ( | ) | const [inline] |
Dereference pointer.
Reimplemented from SoAutoRef.
T* SoRef< T >::operator-> | ( | ) | const [inline] |
Cast to C pointer.
Reimplemented from SoAutoRef.
Assign ordinary C pointer.
T* SoRef< T >::ptr | ( | ) | const [inline] |
Cast to C pointer.
Reimplemented from SoAutoRef.
T* SoRef< T >::releaseNoDelete | ( | ) | [inline] |
Release reference and return the object stored.