13#include <unordered_map>
53 typedef vector<shared_ptr<M>>& dataRef;
54 typedef std::unordered_map<const M*, set<int>>& lookupRef;
64 static int add(shared_ptr<M> obj) {
67 int idx =
static_cast<int>(data.size()) - 1;
69 if (lookup.count(obj.get())) {
70 lookup[obj.get()].insert(idx);
72 lookup[obj.get()] = {idx};
81 return static_cast<int>(
getData().size());
96 static void del(
int n) {
98 if (n >= 0 && n <
len(data)) {
100 if (!lookup.count(data[n].get())) {
102 "Lookup table does not contain reference to object.");
104 if (lookup[data[n].get()].
size() == 1) {
106 lookup.erase(data[n].get());
109 lookup[data[n].get()].erase(n);
114 "Attempt made to delete a non-existing object.");
121 static shared_ptr<M>&
at(
int n) {
123 if (n < 0 || n >=
len(data)) {
124 throw CanteraError(
"Cabinet::at",
"Index {} out of range.", n);
128 "Object with index {} has been deleted.", n);
137 static shared_ptr<T>
as(
int n) {
138 auto obj = std::dynamic_pointer_cast<T>(
at(n));
142 throw CanteraError(
"Cabinet::as",
"Item is not of the correct type.");
Template for classes to hold pointers to objects.
static lookupRef getLookup()
Static function that returns a pointer to the reverse lookup table of the singleton Cabinet<M> instan...
static Cabinet< M > * s_storage
Pointer to the single instance of this class.
static shared_ptr< M > & at(int n)
Return a shared pointer to object n.
static int reset()
Delete all objects and erase mapping.
std::unordered_map< const M *, set< int > > m_lookup
Reverse lookup table for the single instance of this class.
static void del(int n)
Delete the nth object.
static int add(shared_ptr< M > obj)
Add a new object.
static dataRef getData()
Static function that returns a pointer to the data member of the singleton Cabinet<M> instance.
static shared_ptr< T > as(int n)
Return object n, cast to the specified type.
static int size()
Return cabinet size.
vector< shared_ptr< M > > m_table
List to hold pointers to objects.
Base class for exceptions thrown by Cantera classes.
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...
U len(const T &container)
Get the size of a container, cast to a signed integer type.
Namespace for the Cantera kernel.
Various templated functions that carry out common vector and polynomial operations (see Templated Arr...