RixStorage Class Reference

#include <RixInterfaces.h>

Inheritance diagram for RixStorage:
RixInterface

Public Member Functions

virtual void * Get (const char *key)=0
 Get the data associated with the given key, or NULL if there is none.
 
virtual void Set (const char *key, void *data, RixCleanupFunc cleanup=NULL)=0
 
virtual void Clear (const char *key)=0
 
virtual void Lock ()=0
 Lock this object. (Unnecessary unless it's used for global storage.)
 
virtual void Unlock ()=0
 Unlock this object.
 
- Public Member Functions inherited from RixInterface
virtual int GetVersion () const
 

Protected Member Functions

 RixStorage ()
 Constructor is for internal use only.
 
- Protected Member Functions inherited from RixInterface
 RixInterface (int version)
 Interfaces should not be constructed by users.
 
virtual ~RixInterface ()
 Interfaces should not be deleted by users.
 

Additional Inherited Members

- Protected Attributes inherited from RixInterface
int m_version
 Version number of this interface.
 

Detailed Description

RixStorage allows plugins to store and share arbitrary data by associating it with an arbitrary key. For example, a shader plugin might share per-thread data as follows:

RixStorage* storage = (RixStorage*)
rslContext->GetRixInterface(k_RixThreadData);
void* mydata = storage->Get("mydata");
if (mydata == NULL) {
mydata = MakeData();
storage->Set("mydata", mydata, CleanupData);
}

This code operates as follows:

  • Get the per-thread data storage interface from the RslContext.
  • Look up the key to see if the data already exists.
  • If not, allocate the data and save it in per-thread storage.
  • The specified cleanup function is called when the thread exits (typically at the end of a frame).

Per-thread storage is thread-safe, but all other storage must be locked, for example the per-grid storage below is locked:

RixStorage* storage = (RixStorage*) rslCtx->GetRixInterface(k_RixLocalData);
storage->Lock();
void* myglobal = storage->Get("myglobal");
if (myglobal == NULL) {
myglobal = MakeData();
storage->Set("myglobal", myglobal, CleanupData);
}
storage->Unlock();

Per-thread storage can also be used from the global RixContext, as in the example below:

RixContext *rixContext = RxGetRixContext();
RixStorage* storage = (RixStorage*)
rixContext->GetRixInterface(k_RixThreadData);
void* mydata = storage->Get("mydata");
if (mydata == NULL) {
mydata = MakeData();
storage->Set("mydata", mydata, CleanupData);
}

A cleanup function has a prototype like the following:

void myCleanup(RixContext* context, void* data);

The RixContext argument allows the cleanup function to obtain interfaces for reporting errors, etc. Note that a cleanup function for per-thread data can access the per-thread RixStorage interface (e.g. for maintaining memory usage statistics). But per-thread storage is not accessible in a cleanup function for per-frame or per-session data.

Member Function Documentation

virtual void RixStorage::Clear ( const char *  key)
pure virtual

Clear any data associated with the given key, calling its cleanup function (if any).

virtual void RixStorage::Set ( const char *  key,
void *  data,
RixCleanupFunc  cleanup = NULL 
)
pure virtual

Set the data associated with the given key, along with an optional cleanup function. Any previously associated data is discarded (calling its cleanup function, if any). The key is copied.


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