API Guides > ConceptRT 3.x
SharedResource< T > Class Template Reference

SharedResource is a pattern that allocates only one instance of a class, with reference counting. SharedResource instance is automatically allocated/freed when needed. More...

Detailed Description

template<class T>
class ConceptRT::SharedResource< T >

SharedResource is a pattern that allocates only one instance of a class, with reference counting. SharedResource instance is automatically allocated/freed when needed.

Example :

class Example: public SharedResource<Example>
{
public:
private: // Mandatory
Example() { } // Mandatory
friend class SharedResource<Example> // Mandatory
};
Warning
Constructor must be declared in private and the parent class must be friend.
+ Inheritance diagram for SharedResource< T >:

Static Public Member Functions

static T * AcquirePointer ()
 Allocates the resource if needed, and increments the reference count by 1. More...
 
static T & Acquire ()
 Allocates the resource if needed, and increments the reference count by 1. More...
 
static T * GetResourcePointer ()
 Get the pointer on the currently allocated resource. More...
 
static T & GetResource ()
 Get the currently allocated resource. More...
 
static void Release ()
 Releases the shared ressource. More...
 

Methods Documentation

T & Acquire ( )
static

Allocates the resource if needed, and increments the reference count by 1.

Returns
The resource.
Warning
Non-Deterministic: May create the ressource if not yet created. Determinism when using ConceptRT library can be ensure by using Context Time Critical mechanisms.
RT Exceptions:

RT_ERROR_NEW_INSTANCE_NOT_ABLE_TO_CREATE

RT_ERROR_SHARED_RESSOURCE_REFERENCE_COUNT_MISSMATCH

T * AcquirePointer ( )
static

Allocates the resource if needed, and increments the reference count by 1.

Returns
The pointer on resource.
Warning
Non-Deterministic: May create the ressource if not yet created. Determinism when using ConceptRT library can be ensure by using Context Time Critical mechanisms.
RT Exceptions:

RT_ERROR_NEW_INSTANCE_NOT_ABLE_TO_CREATE

RT_ERROR_SHARED_RESSOURCE_REFERENCE_COUNT_MISSMATCH

T & GetResource ( )
static

Get the currently allocated resource.

Returns
The resource.
Warning
Call order dependence : Should be called only after the SharedResource is Acquired. Don't forget to release the ressource after usage.
RT Exceptions:
RT_ERROR_SHARED_RESSOURCE_REFERENCE_COUNT_MISSMATCH
T * GetResourcePointer ( )
static

Get the pointer on the currently allocated resource.

Returns
The pointer on resource.
Warning
Call order dependence : Should be called only after the SharedResource is Acquired. Don't forget to release the ressource after usage.
RT Exceptions:
RT_ERROR_SHARED_RESSOURCE_REFERENCE_COUNT_MISSMATCH
void Release ( )
static

Releases the shared ressource.

Resource is freed when reference counting reaches 0.

Warning
Non-Deterministic: May delete the ressource if there is no references anymore on it. Determinism when using ConceptRT library can be ensure by using Context Time Critical mechanisms.
RT Exceptions:

RT_ERROR_SHARED_RESSOURCE_REFERENCE_COUNT_MISSMATCH

RT_ERROR_INTERNAL_DELETE_A_NON_NULL_OBJECT