SoRef< T > Class Template Reference
[General]

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

#include <Inventor/misc/SoRef.h>

Inheritance diagram for SoRef< T >:
SoAutoRef

List of all members.

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

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
      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 );

SEE ALSO

SoRefCounter


Constructor & Destructor Documentation

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

Default constructor.

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

Copy constructor.

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

Construct from C++ pointer.

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

Construct from another SoRef.

template<typename T>
virtual SoRef< T >::~SoRef (  )  [inline, virtual]

Destructor.


Member Function Documentation

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

Dereference pointer.

Reimplemented from SoAutoRef.

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

Cast to C pointer.

Reimplemented from SoAutoRef.

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

Assign ordinary C pointer.

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).

Reimplemented from SoAutoRef.

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

Assign another SoRef.

Reimplemented from SoAutoRef.

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

Cast to C pointer.

Reimplemented from SoAutoRef.

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

Release reference and return the object stored.


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

Open Inventor Toolkit reference manual, generated on 15 Mar 2023
Copyright © Thermo Fisher Scientific All rights reserved.
http://www.openinventor.com/