Cantera  3.2.0a4
Loading...
Searching...
No Matches
Cabinet< M > Class Template Reference

Template for classes to hold pointers to objects. More...

#include <Cabinet.h>

Detailed Description

template<class M>
class Cantera::Cabinet< M >

Template for classes to hold pointers to objects.

The Cabinet<M> class maintains a list of pointers to objects of class M (or of subclasses of M). These classes are used by the 'CLib' interface library functions that provide access to Cantera C++ objects from outside C++. To refer to an existing object, the library functions take an integer argument that specifies the location in the pointer list maintained by the appropriate Cabinet<M> instance. The pointer is retrieved from the list by the interface function, the desired method is invoked, and the result returned to the non-C++ calling procedure. By storing the pointers in a Cabinet, there is no need to encode them in a string or integer and pass them out to the non-C++ calling routine, as some other interfacing schemes do.

The Cabinet<M> class can be used to store pointers to arbitrary objects. In most cases, class M is a base class with virtual methods, and the base class versions of the methods throw CanteraError exceptions. The subclasses overload these methods to implement the desired functionality. Class Cabinet<M> stores only the base-class pointers, but since the methods are virtual, the method of the appropriate subclass will be invoked.

As the Cabinet<M> class uses smart pointers, it is set up to allow deleting objects in an inherently safe manner. Method 'del' does the following. If called with n >= 0, it dereferences the object. The original object is only destroyed if the reference is not shared by other objects. In this way, if it is deleted again inadvertently nothing happens, and if an attempt is made to reference the object by its index number, a standard exception is thrown.

The Cabinet<M> class is implemented as a singleton. The constructor is never explicitly called; instead, static function Cabinet<M>::Cabinet() is called to obtain a pointer to the instance. This function calls the constructor on the first call and stores the pointer to this instance. Subsequent calls simply return the already-created pointer.

Definition at line 50 of file Cabinet.h.

Public Types

typedef vector< shared_ptr< M > > & dataRef
 
typedef std::unordered_map< const M *, set< int > > & lookupRef
 

Public Member Functions

 Cabinet ()
 Constructor.
 

Static Public Member Functions

static int add (shared_ptr< M > obj)
 Add a new object.
 
static int size ()
 Return cabinet size.
 
static int reset ()
 Delete all objects and erase mapping.
 
static void del (int n)
 Delete the nth object.
 
static shared_ptr< M > & at (int n)
 Return a shared pointer to object n.
 
template<class T >
static shared_ptr< T > as (int n)
 Return object n, cast to the specified type.
 

Static Private Member Functions

static dataRef getData ()
 Static function that returns a pointer to the data member of the singleton Cabinet<M> instance.
 
static lookupRef getLookup ()
 Static function that returns a pointer to the reverse lookup table of the singleton Cabinet<M> instance.
 

Private Attributes

std::unordered_map< const M *, set< int > > m_lookup
 Reverse lookup table for the single instance of this class.
 
vector< shared_ptr< M > > m_table
 List to hold pointers to objects.
 

Static Private Attributes

static Cabinet< M > * s_storage
 Pointer to the single instance of this class.
 

Member Typedef Documentation

◆ dataRef

template<class M >
typedef vector<shared_ptr<M> >& dataRef

Definition at line 53 of file Cabinet.h.

◆ lookupRef

template<class M >
typedef std::unordered_map<const M*, set<int> >& lookupRef

Definition at line 54 of file Cabinet.h.

Constructor & Destructor Documentation

◆ Cabinet()

template<class M >
Cabinet ( )
inline

Constructor.

Definition at line 59 of file Cabinet.h.

Member Function Documentation

◆ add()

template<class M >
static int add ( shared_ptr< M >  obj)
inlinestatic

Add a new object.

The index of the object is returned.

Definition at line 64 of file Cabinet.h.

◆ size()

template<class M >
static int size ( )
inlinestatic

Return cabinet size.

Definition at line 80 of file Cabinet.h.

◆ reset()

template<class M >
static int reset ( )
inlinestatic

Delete all objects and erase mapping.

Definition at line 87 of file Cabinet.h.

◆ del()

template<class M >
static void del ( int  n)
inlinestatic

Delete the nth object.

Definition at line 96 of file Cabinet.h.

◆ at()

template<class M >
static shared_ptr< M > & at ( int  n)
inlinestatic

Return a shared pointer to object n.

Definition at line 121 of file Cabinet.h.

◆ as()

template<class M >
template<class T >
static shared_ptr< T > as ( int  n)
inlinestatic

Return object n, cast to the specified type.

Definition at line 137 of file Cabinet.h.

◆ getData()

template<class M >
static dataRef getData ( )
inlinestaticprivate

Static function that returns a pointer to the data member of the singleton Cabinet<M> instance.

All member functions should access the data through this function.

Definition at line 151 of file Cabinet.h.

◆ getLookup()

template<class M >
static lookupRef getLookup ( )
inlinestaticprivate

Static function that returns a pointer to the reverse lookup table of the singleton Cabinet<M> instance.

All member functions should access the lookup table through this function.

Definition at line 163 of file Cabinet.h.

Member Data Documentation

◆ s_storage

template<class M >
Cabinet<M>* s_storage
staticprivate

Pointer to the single instance of this class.

Definition at line 173 of file Cabinet.h.

◆ m_lookup

template<class M >
std::unordered_map<const M*, set<int> > m_lookup
private

Reverse lookup table for the single instance of this class.

Definition at line 178 of file Cabinet.h.

◆ m_table

template<class M >
vector<shared_ptr<M> > m_table
private

List to hold pointers to objects.

Definition at line 183 of file Cabinet.h.


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