Open Inventor Release 2024.2.1
 
Loading...
Searching...
No Matches
SoRef< T > Class Template Reference

Smart pointer for any class inheriting SoRefCounter. More...

#include <Inventor/misc/SoRef.h>

+ Inheritance diagram for SoRef< T >:

Public Member Functions

 SoRef ()
 Default constructor.
 
 SoRef (const SoRef< T > &that)
 Copy constructor.
 
 SoRef (T *pIn)
 Construct from C++ pointer.
 
template<typename Y >
 SoRef (const SoRef< Y > &that)
 Construct from another SoRef.
 
T * releaseNoDelete ()
 Release reference and return the object stored.
 
SoRef< T > & operator= (const SoRef< T > &that)
 Assign another SoRef.
 
template<typename Y >
SoRef< T > & operator= (const SoRef< Y > &that)
 Assign to another SoRef if the types are assignable (from derived to base classes).
 
SoRef< T > & operator= (T *ptr)
 Assign ordinary C pointer.
 
T & operator* () const
 Dereference pointer.
 
T * operator-> () const
 Cast to C pointer.
 
T * ptr () const
 Cast to C pointer.
 
- Public Member Functions inherited from SoAutoRef
 SoAutoRef ()
 Default constructor.
 
 SoAutoRef (const SoAutoRef &that)
 Copy constructor.
 
 SoAutoRef (SoRefCounter *pIn)
 Construct from C++ pointer.
 
 ~SoAutoRef ()
 Destructor.
 
SoAutoRefoperator= (const SoAutoRef &that)
 Assign another SoAutoRef.
 
SoAutoRefoperator= (SoRefCounter *ptr)
 Assign ordinary C pointer.
 
bool operator== (const SoAutoRef &other) const
 
bool operator!= (const SoAutoRef &other) const
 
bool operator< (const SoAutoRef &other) const
 
bool operator<= (const SoAutoRef &other) const
 
bool operator> (const SoAutoRef &other) const
 
bool operator>= (const SoAutoRef &other) const
 
bool operator== (const SoRefCounter *ptr) const
 
bool operator!= (const SoRefCounter *ptr) const
 
bool operator< (const SoRefCounter *ptr) const
 
bool operator<= (const SoRefCounter *ptr) const
 
bool operator> (const SoRefCounter *ptr) const
 
bool operator>= (const SoRefCounter *ptr) const
 
 operator bool () const
 Conversion to boolean.
 
bool operator! () const
 NOT operator.
 
SoRefCounteroperator* () const
 Dereference pointer.
 
SoRefCounteroperator-> () const
 Cast to C pointer.
 
SoRefCounterptr () const
 Cast to C pointer.
 

Detailed Description

template<typename T>
class SoRef< T >

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
// Use the buffer object
. . .
cpuObj->unmap();
// Buffer object will be automatically cleaned up
}
@ READ_ONLY
The target buffer initially has the same contents as the source buffer.
<a href="IconLegend.html"><img src="extTGS.gif" alt="VSG extension" border="0"></a> CPU buffer objec...
Smart pointer for any class inheriting SoRefCounter.
Definition SoRef.h:90

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 );
Cone shape node.
Definition SoCone.h:143
T * ptr() const
Cast to C pointer.
Definition SoRef.h:167

SEE ALSO

SoRefCounter

Definition at line 89 of file SoRef.h.

Constructor & Destructor Documentation

◆ SoRef() [1/4]

template<typename T >
SoRef< T >::SoRef ( )
inline

Default constructor.

Definition at line 93 of file SoRef.h.

◆ SoRef() [2/4]

template<typename T >
SoRef< T >::SoRef ( const SoRef< T > &  that)
inline

Copy constructor.

Definition at line 97 of file SoRef.h.

◆ SoRef() [3/4]

template<typename T >
SoRef< T >::SoRef ( T *  pIn)
inline

Construct from C++ pointer.

Definition at line 103 of file SoRef.h.

◆ SoRef() [4/4]

template<typename T >
template<typename Y >
SoRef< T >::SoRef ( const SoRef< Y > &  that)
inline

Construct from another SoRef.

Definition at line 110 of file SoRef.h.

Member Function Documentation

◆ operator*()

template<typename T >
T & SoRef< T >::operator* ( ) const
inline

Dereference pointer.

Definition at line 159 of file SoRef.h.

◆ operator->()

template<typename T >
T * SoRef< T >::operator-> ( ) const
inline

Cast to C pointer.

Definition at line 163 of file SoRef.h.

◆ operator=() [1/3]

template<typename T >
SoRef< T > & SoRef< T >::operator= ( const SoRef< T > &  that)
inline

Assign another SoRef.

Definition at line 131 of file SoRef.h.

◆ operator=() [2/3]

template<typename T >
template<typename Y >
SoRef< T > & SoRef< T >::operator= ( const SoRef< Y > &  that)
inline

Assign to another SoRef if the types are assignable (from derived to base classes).

Definition at line 139 of file SoRef.h.

◆ operator=() [3/3]

template<typename T >
SoRef< T > & SoRef< T >::operator= ( T *  ptr)
inline

Assign ordinary C pointer.

Definition at line 146 of file SoRef.h.

◆ ptr()

template<typename T >
T * SoRef< T >::ptr ( ) const
inline

Cast to C pointer.

Definition at line 167 of file SoRef.h.

◆ releaseNoDelete()

template<typename T >
T * SoRef< T >::releaseNoDelete ( )
inline

Release reference and return the object stored.

Definition at line 118 of file SoRef.h.


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