sandbox
Loading...
Searching...
No Matches
type_id.hpp
1// SPDX-License-Identifier: MIT
2#ifndef LIBSBX_UTILITY_TYPE_ID_HPP_
3#define LIBSBX_UTILITY_TYPE_ID_HPP_
4
5#include <cstdint>
6#include <type_traits>
7
8namespace sbx::utility {
9
10namespace detail {
11
12template<typename Scope>
13struct id_generator final {
14 [[nodiscard]] static auto next() noexcept -> std::uint32_t {
15 static auto id = std::uint32_t{};
16 return id++;
17 }
18}; // struct id_generator
19
21
22} // namespace detail
23
30template<typename Scope, typename Type>
32
33 using type = Type;
34 using scope = Scope;
35
41 [[nodiscard]] static auto value() noexcept -> std::uint32_t {
42 static const auto value = detail::id_generator<Scope>::next();
43
44 return value;
45 }
46
47 [[nodiscard]] constexpr operator std::uint32_t() const noexcept {
48 return value();
49 }
50
51}; // struct type_id
52
58template<typename Type>
59using type_id = scoped_type_id<detail::default_type_id_scope, Type>;
60
61} // namespace sbx::utility
62
63#endif // LIBSBX_UTILITY_TYPE_ID_HPP_
Definition: type_id.hpp:13
A scoped type ID generator. Allows for generating unique IDs for types within a specific scope.
Definition: type_id.hpp:31
static auto value() noexcept -> std::uint32_t
Generates a unique ID for the type.
Definition: type_id.hpp:41