stellarlib 0.1.0
Loading...
Searching...
No Matches
utility.hpp
1/* clang-format off */
2
3/*
4 stellarlib
5 Copyright (C) 2025-2026 Domán Zana
6
7 This software is provided 'as-is', without any express or implied
8 warranty. In no event will the authors be held liable for any damages
9 arising from the use of this software.
10
11 Permission is granted to anyone to use this software for any purpose,
12 including commercial applications, and to alter it and redistribute it
13 freely, subject to the following restrictions:
14
15 1. The origin of this software must not be misrepresented; you must not
16 claim that you wrote the original software. If you use this software
17 in a product, an acknowledgment in the product documentation would be
18 appreciated but is not required.
19 2. Altered source versions must be plainly marked as such, and must not be
20 misrepresented as being the original software.
21 3. This notice may not be removed or altered from any source distribution.
22*/
23
24#ifndef STELLARLIB_EXT_UTILITY_HPP
25#define STELLARLIB_EXT_UTILITY_HPP
26
27#include <atomic>
28#include <cstddef>
29#include <limits>
30
31/**
32 * @brief Standard library extensions
33 */
34namespace stellarlib::ext
35{
36/**
37 * @brief Thread-safe sequential ID generator scoped by type
38 * @tparam Scope Distinct type used to separate ID sequences
39 * @tparam SizeType Integral type of the ID
40 * @return Next ID in the sequence for the given scope
41 */
42template <typename Scope, typename SizeType = std::size_t>
43[[nodiscard]]
44constexpr auto sequential_id() noexcept
45{
46 static std::atomic<SizeType> id{std::numeric_limits<SizeType>::max()};
47 return ++id;
48}
49
50/**
51 * @brief Generates a unique ID per type within a scope
52 * @tparam Scope Distinct type used to separate ID spaces
53 * @tparam T Type to retrieve an ID for
54 * @tparam SizeType Integral type of the ID
55 * @return Unique ID corresponding to type T within the scope
56 */
57template <typename Scope, typename T, typename SizeType = std::size_t>
58[[nodiscard]]
59constexpr auto scoped_typeid() noexcept
60{
61 static const auto id{sequential_id<Scope, SizeType>()};
62 return id;
63}
64}
65
66#endif
Standard library extensions.
Definition bit.hpp:33
constexpr auto sequential_id() noexcept
Thread-safe sequential ID generator scoped by type.
Definition utility.hpp:44
constexpr auto scoped_typeid() noexcept
Generates a unique ID per type within a scope.
Definition utility.hpp:59