https://mooseframework.inl.gov
GeneralRegistry.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #pragma once
11 
12 #include <unordered_map>
13 #include <mutex>
14 #include <deque>
15 
16 #include "MooseError.h"
17 
18 template <class Key, class Item, class KeyHash = std::hash<Key>>
20 {
21 public:
22  GeneralRegistry(const std::string & name);
23 
27  std::size_t size() const;
28 
32  std::size_t id(const Key & key) const;
33 
37  bool keyExists(const Key & key) const;
41  bool idExists(const std::size_t id) const;
42 
46  const Item & item(const std::size_t id) const;
47 
48 protected:
52  const Item & itemNonLocking(const std::size_t id) const;
53 
63  template <typename CreateItem>
64  std::size_t registerItem(const Key & key, CreateItem & create_item);
65 
67  const std::string _name;
68 
70  std::unordered_map<Key, std::size_t, KeyHash> _key_to_id;
72  std::deque<Item> _id_to_item;
73 
76  mutable std::mutex _key_to_id_mutex;
79  mutable std::mutex _id_to_item_mutex;
80 };
81 
82 template <class Key, class Item, class KeyHash>
84 {
85 }
86 
87 template <class Key, class Item, class KeyHash>
88 std::size_t
90 {
91  std::lock_guard<std::mutex> lock(_id_to_item_mutex);
92  return _id_to_item.size();
93 }
94 
95 template <class Key, class Item, class KeyHash>
96 std::size_t
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 }
105 
106 template <class Key, class Item, class KeyHash>
107 bool
109 {
110  std::lock_guard<std::mutex> lock(_key_to_id_mutex);
111  return _key_to_id.count(key);
112 }
113 
114 template <class Key, class Item, class KeyHash>
115 bool
117 {
118  std::lock_guard<std::mutex> lock(_id_to_item_mutex);
119  return id < _id_to_item.size();
120 }
121 
122 template <class Key, class Item, class KeyHash>
123 const Item &
124 GeneralRegistry<Key, Item, KeyHash>::item(const std::size_t id) const
125 {
126  std::lock_guard<std::mutex> lock(_id_to_item_mutex);
127  return itemNonLocking(id);
128 }
129 
130 template <class Key, class Item, class KeyHash>
131 const Item &
133 {
134  if (id >= _id_to_item.size())
135  mooseError(_name, ": ID '", id, "' is not registered");
136  return _id_to_item[id];
137 }
138 
139 template <class Key, class Item, class KeyHash>
140 template <typename CreateItem>
141 std::size_t
142 GeneralRegistry<Key, Item, KeyHash>::registerItem(const Key & key, CreateItem & create_item)
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::string name(const ElemQuality q)
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
const Item & itemNonLocking(const std::size_t id) const
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.
bool idExists(const std::size_t id) const
std::size_t id(const Key &key) const
const Item & item(const std::size_t id) const
std::deque< Item > _id_to_item
Vector of IDs to Items.
std::size_t size() const
std::size_t registerItem(const Key &key, CreateItem &create_item)
Registers an item with key key if said key does not exist.
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...
bool keyExists(const Key &key) const
GeneralRegistry(const std::string &name)