65 : _max_number_of_items(max_number_of_items)
67 for (std::size_t i = 0; i < _max_number_of_items; ++i)
69 _available_numbers.insert(i);
86 std::lock_guard<std::mutex> lock(_registry_mutex);
88 if (_max_number_of_items > 0)
90 if (!_available_numbers.empty())
92 auto it = _available_numbers.begin();
93 std::size_t item_number = *it;
94 _available_numbers.erase(it);
97 throw cbeam::error::overflow_error(
"cbeam::lifecycle::item_registry::register_item: No more item slots available. increase the maximum number of items to register more.");
101 if (!_available_numbers.empty())
103 auto it = _available_numbers.begin();
104 std::size_t item_number = *it;
105 _available_numbers.erase(it);
108 if (_next_item_number == std::numeric_limits<
decltype(_next_item_number)>::max())
112 return _next_item_number++;
127 if (_max_number_of_items && item_number >= _max_number_of_items)
129 throw cbeam::error::runtime_error(
"cbeam::lifecycle::item_registry::deregister_item: Invalid item number " + std::to_string(item_number) +
" for deregistration.");
132 std::lock_guard<std::mutex> lock(_registry_mutex);
134 if (_available_numbers.find(item_number) != _available_numbers.end())
136 throw cbeam::error::runtime_error(
"cbeam::lifecycle::item_registry::deregister_item: Item number " + std::to_string(item_number) +
" already deregistered.");
139 _available_numbers.insert(item_number);
143 const std::size_t _max_number_of_items;
144 std::size_t _next_item_number{0};
145 std::set<std::size_t> _available_numbers;
146 std::mutex _registry_mutex;
A Cbeam-specific logic error that also behaves like std::overflow_error.
Definition overflow_error.hpp:46
A Cbeam-specific runtime error that also acts like std::runtime_error.
Definition runtime_error.hpp:46
std::size_t register_item()
Registers an item and returns its unique identifier.
Definition item_registry.hpp:84
void deregister_item(std::size_t item_number)
Deregisters an item given its unique identifier.
Definition item_registry.hpp:125
item_registry(std::size_t max_number_of_items=0)
Constructor for item_registry.
Definition item_registry.hpp:64
Manages the lifecycle of singletons, item registries, and scoped variables. This namespace introduces...
Definition item_registry.hpp:38