https://mooseframework.inl.gov
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
GeneralRegistry< Key, Item, KeyHash > Class Template Reference

#include <GeneralRegistry.h>

Public Member Functions

 GeneralRegistry (const std::string &name)
 
std::size_t size () const
 
std::size_t id (const Key &key) const
 
bool keyExists (const Key &key) const
 
bool idExists (const std::size_t id) const
 
const Item & item (const std::size_t id) const
 

Protected Member Functions

const Item & itemNonLocking (const std::size_t id) const
 
template<typename CreateItem >
std::size_t registerItem (const Key &key, CreateItem &create_item)
 
Registers an item with key key if said key does not exist. More...
 

Protected Attributes

const std::string _name
 The name of this registry; used in error handling. More...
 
std::unordered_map< Key, std::size_t, KeyHash > _key_to_id
 Map of keys to IDs. More...
 
std::deque< Item > _id_to_item
 Vector of IDs to Items. More...
 
std::mutex _key_to_id_mutex
 Mutex for locking access to _key_to_id NOTE: These can be changed to shared_mutexes once we get C++17. More...
 
std::mutex _id_to_item_mutex
 Mutex for locking access to _id_to_item NOTE: These can be changed to shared_mutexes once we get C++17. More...
 

Detailed Description

template<class Key, class Item, class KeyHash = std::hash<Key>>
class GeneralRegistry< Key, Item, KeyHash >

Definition at line 19 of file GeneralRegistry.h.

Constructor & Destructor Documentation

◆ GeneralRegistry()

template<class Key , class Item , class KeyHash >
GeneralRegistry< Key, Item, KeyHash >::GeneralRegistry ( const std::string &  name)

Definition at line 83 of file GeneralRegistry.h.

83  : _name(name)
84 {
85 }
const std::string _name
The name of this registry; used in error handling.

Member Function Documentation

◆ id()

template<class Key, class Item , class KeyHash >
std::size_t GeneralRegistry< Key, Item, KeyHash >::id ( const Key &  key) const
Returns
The ID assocated with the key key

Definition at line 97 of file GeneralRegistry.h.

Referenced by dataLoad(), and SolutionInvalidity::syncIteration().

98 {
99  std::lock_guard<std::mutex> lock(_key_to_id_mutex);
100  const auto it = _key_to_id.find(key);
101  if (it == _key_to_id.end())
102  mooseError(_name, ": Key '", key, "' is not registered");
103  return it->second;
104 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
std::mutex _key_to_id_mutex
Mutex for locking access to _key_to_id NOTE: These can be changed to shared_mutexes once we get C++17...
const std::string _name
The name of this registry; used in error handling.
std::unordered_map< Key, std::size_t, KeyHash > _key_to_id
Map of keys to IDs.

◆ idExists()

template<class Key , class Item , class KeyHash >
bool GeneralRegistry< Key, Item, KeyHash >::idExists ( const std::size_t  id) const
Returns
Whether or not the id id is registered

Definition at line 116 of file GeneralRegistry.h.

117 {
118  std::lock_guard<std::mutex> lock(_id_to_item_mutex);
119  return id < _id_to_item.size();
120 }
std::deque< Item > _id_to_item
Vector of IDs to Items.
std::mutex _id_to_item_mutex
Mutex for locking access to _id_to_item NOTE: These can be changed to shared_mutexes once we get C++1...

◆ item()

template<class Key , class Item , class KeyHash >
const Item & GeneralRegistry< Key, Item, KeyHash >::item ( const std::size_t  id) const
Returns
The item associated with the key key (thread safe)

Definition at line 124 of file GeneralRegistry.h.

Referenced by dataStore(), SolutionInvalidity::printDebug(), SolutionInvalidity::summaryTable(), SolutionInvalidity::syncIteration(), and SolutionInvalidity::transientTable().

125 {
126  std::lock_guard<std::mutex> lock(_id_to_item_mutex);
127  return itemNonLocking(id);
128 }
const Item & itemNonLocking(const std::size_t id) const
std::mutex _id_to_item_mutex
Mutex for locking access to _id_to_item NOTE: These can be changed to shared_mutexes once we get C++1...

◆ itemNonLocking()

template<class Key , class Item , class KeyHash >
const Item & GeneralRegistry< Key, Item, KeyHash >::itemNonLocking ( const std::size_t  id) const
protected
Returns
The item associated with the key key (not thread safe)

Definition at line 132 of file GeneralRegistry.h.

133 {
134  if (id >= _id_to_item.size())
135  mooseError(_name, ": ID '", id, "' is not registered");
136  return _id_to_item[id];
137 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
const std::string _name
The name of this registry; used in error handling.
std::size_t id(const Key &key) const
std::deque< Item > _id_to_item
Vector of IDs to Items.

◆ keyExists()

template<class Key, class Item , class KeyHash >
bool GeneralRegistry< Key, Item, KeyHash >::keyExists ( const Key &  key) const
Returns
Whether or not the key key is registered

Definition at line 108 of file GeneralRegistry.h.

Referenced by dataLoad(), and SolutionInvalidity::syncIteration().

109 {
110  std::lock_guard<std::mutex> lock(_key_to_id_mutex);
111  return _key_to_id.count(key);
112 }
std::mutex _key_to_id_mutex
Mutex for locking access to _key_to_id NOTE: These can be changed to shared_mutexes once we get C++17...
std::unordered_map< Key, std::size_t, KeyHash > _key_to_id
Map of keys to IDs.

◆ registerItem()

template<class Key, class Item , class KeyHash >
template<typename CreateItem >
std::size_t GeneralRegistry< Key, Item, KeyHash >::registerItem ( const Key &  key,
CreateItem &  create_item 
)
protected


Registers an item with key key if said key does not exist.

Parameters
keyThe key
create_itemLambda called to create an item if the key does not exist. Takes a single argument std::size_t which is the

new ID and should return an Item

Returns
The ID of the item

Definition at line 142 of file GeneralRegistry.h.

143 {
144  std::lock_guard<std::mutex> lock_key(_key_to_id_mutex);
145 
146  // Is it already registered?
147  const auto it = _key_to_id.find(key);
148  if (it != _key_to_id.end())
149  return it->second;
150 
151  // It's not registered
152  std::lock_guard<std::mutex> lock_id(_id_to_item_mutex);
153  const auto id = _id_to_item.size();
154  _key_to_id.emplace(key, id);
155  _id_to_item.emplace_back(std::move(create_item(id)));
156  return id;
157 }
std::mutex _key_to_id_mutex
Mutex for locking access to _key_to_id NOTE: These can be changed to shared_mutexes once we get C++17...
std::unordered_map< Key, std::size_t, KeyHash > _key_to_id
Map of keys to IDs.
std::size_t id(const Key &key) const
std::deque< Item > _id_to_item
Vector of IDs to Items.
std::mutex _id_to_item_mutex
Mutex for locking access to _id_to_item NOTE: These can be changed to shared_mutexes once we get C++1...

◆ size()

template<class Key , class Item , class KeyHash >
std::size_t GeneralRegistry< Key, Item, KeyHash >::size ( ) const
Returns
The number of registered items

Definition at line 89 of file GeneralRegistry.h.

90 {
91  std::lock_guard<std::mutex> lock(_id_to_item_mutex);
92  return _id_to_item.size();
93 }
std::deque< Item > _id_to_item
Vector of IDs to Items.
std::mutex _id_to_item_mutex
Mutex for locking access to _id_to_item NOTE: These can be changed to shared_mutexes once we get C++1...

Member Data Documentation

◆ _id_to_item

template<class Key, class Item, class KeyHash = std::hash<Key>>
std::deque<Item> GeneralRegistry< Key, Item, KeyHash >::_id_to_item
protected

Vector of IDs to Items.

Definition at line 72 of file GeneralRegistry.h.

Referenced by dataStore().

◆ _id_to_item_mutex

template<class Key, class Item, class KeyHash = std::hash<Key>>
std::mutex GeneralRegistry< Key, Item, KeyHash >::_id_to_item_mutex
mutableprotected

Mutex for locking access to _id_to_item NOTE: These can be changed to shared_mutexes once we get C++17.

Definition at line 79 of file GeneralRegistry.h.

◆ _key_to_id

template<class Key, class Item, class KeyHash = std::hash<Key>>
std::unordered_map<Key, std::size_t, KeyHash> GeneralRegistry< Key, Item, KeyHash >::_key_to_id
protected

Map of keys to IDs.

Definition at line 70 of file GeneralRegistry.h.

◆ _key_to_id_mutex

template<class Key, class Item, class KeyHash = std::hash<Key>>
std::mutex GeneralRegistry< Key, Item, KeyHash >::_key_to_id_mutex
mutableprotected

Mutex for locking access to _key_to_id NOTE: These can be changed to shared_mutexes once we get C++17.

Definition at line 76 of file GeneralRegistry.h.

◆ _name

template<class Key, class Item, class KeyHash = std::hash<Key>>
const std::string GeneralRegistry< Key, Item, KeyHash >::_name
protected

The name of this registry; used in error handling.

Definition at line 67 of file GeneralRegistry.h.


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