Cbeam
Loading...
Searching...
No Matches
item_registry.hpp
Go to the documentation of this file.
1/*
2Copyright (c) 2025 acrion innovations GmbH
3Authors: Stefan Zipproth, s.zipproth@acrion.ch
4
5This file is part of Cbeam, see https://github.com/acrion/cbeam and https://cbeam.org
6
7Cbeam is offered under a commercial and under the AGPL license.
8For commercial licensing, contact us at https://acrion.ch/sales. For AGPL licensing, see below.
9
10AGPL licensing:
11
12Cbeam is free software: you can redistribute it and/or modify
13it under the terms of the GNU Affero General Public License as published by
14the Free Software Foundation, either version 3 of the License, or
15(at your option) any later version.
16
17Cbeam is distributed in the hope that it will be useful,
18but WITHOUT ANY WARRANTY; without even the implied warranty of
19MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20GNU Affero General Public License for more details.
21
22You should have received a copy of the GNU Affero General Public License
23along with Cbeam. If not, see <https://www.gnu.org/licenses/>.
24*/
25
26#pragma once
27
28#include <cbeam/error/overflow_error.hpp> // for cbeam::error:overflow_error
29#include <cbeam/error/runtime_error.hpp> // for cbeam::error:runtime_error
30
31#include <cstddef> // for std::size_t
32
33#include <mutex> // for std::mutex, std::lock_guard
34#include <set> // for std::allocator, std::set, std::operator!=, std::_Rb_tree_const_iterator
35#include <string> // for std::operator+, std::char_traits, std::to_string
36
38{
51 {
52 public:
64 explicit item_registry(std::size_t max_number_of_items = 0)
65 : _max_number_of_items(max_number_of_items)
66 {
67 for (std::size_t i = 0; i < _max_number_of_items; ++i)
68 {
69 _available_numbers.insert(i);
70 }
71 }
72
84 std::size_t register_item()
85 {
86 std::lock_guard<std::mutex> lock(_registry_mutex);
87
88 if (_max_number_of_items > 0)
89 {
90 if (!_available_numbers.empty())
91 {
92 auto it = _available_numbers.begin();
93 std::size_t item_number = *it;
94 _available_numbers.erase(it);
95 return item_number;
96 }
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.");
98 }
99 else
100 {
101 if (!_available_numbers.empty())
102 {
103 auto it = _available_numbers.begin();
104 std::size_t item_number = *it;
105 _available_numbers.erase(it);
106 return item_number;
107 }
108 if (_next_item_number == std::numeric_limits<decltype(_next_item_number)>::max())
109 {
110 throw cbeam::error::overflow_error("cbeam::lifecycle::item_registry::register_item: Maximum item count reached.");
111 }
112 return _next_item_number++;
113 }
114 }
115
125 void deregister_item(std::size_t item_number)
126 {
127 if (_max_number_of_items && item_number >= _max_number_of_items)
128 {
129 throw cbeam::error::runtime_error("cbeam::lifecycle::item_registry::deregister_item: Invalid item number " + std::to_string(item_number) + " for deregistration.");
130 }
131
132 std::lock_guard<std::mutex> lock(_registry_mutex);
133
134 if (_available_numbers.find(item_number) != _available_numbers.end())
135 {
136 throw cbeam::error::runtime_error("cbeam::lifecycle::item_registry::deregister_item: Item number " + std::to_string(item_number) + " already deregistered.");
137 }
138
139 _available_numbers.insert(item_number);
140 }
141
142 private:
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;
147 };
148}
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