https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
18template <class Key, class Item, class KeyHash = std::hash<Key>>
20{
21public:
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
48protected:
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
82template <class Key, class Item, class KeyHash>
83GeneralRegistry<Key, Item, KeyHash>::GeneralRegistry(const std::string & name) : _name(name)
84{
85}
86
87template <class Key, class Item, class KeyHash>
88std::size_t
90{
91 std::lock_guard<std::mutex> lock(_id_to_item_mutex);
92 return _id_to_item.size();
93}
94
95template <class Key, class Item, class KeyHash>
96std::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
106template <class Key, class Item, class KeyHash>
107bool
109{
110 std::lock_guard<std::mutex> lock(_key_to_id_mutex);
111 return _key_to_id.count(key);
112}
113
114template <class Key, class Item, class KeyHash>
115bool
117{
118 std::lock_guard<std::mutex> lock(_id_to_item_mutex);
119 return id < _id_to_item.size();
120}
121
122template <class Key, class Item, class KeyHash>
123const Item &
125{
126 std::lock_guard<std::mutex> lock(_id_to_item_mutex);
127 return itemNonLocking(id);
128}
129
130template <class Key, class Item, class KeyHash>
131const 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
139template <class Key, class Item, class KeyHash>
140template <typename CreateItem>
141std::size_t
142GeneralRegistry<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}
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
std::deque< Item > _id_to_item
Vector of IDs to Items.
std::unordered_map< Key, std::size_t, KeyHash > _key_to_id
Map of keys to IDs.
const Item & itemNonLocking(const std::size_t id) const
std::size_t registerItem(const Key &key, CreateItem &create_item)
Registers an item with key key if said key does not exist.
bool keyExists(const Key &key) const
const std::string _name
The name of this registry; used in error handling.
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...
std::size_t size() const
std::size_t id(const Key &key) const
GeneralRegistry(const std::string &name)
const Item & item(const std::size_t id) const
bool idExists(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...