ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
enumrecords.inl
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header file is part of module \alib_enums of the \aliblong.
4///
5/// \emoji :copyright: 2013-2024 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8#ifndef HPP_ALIB_ENUMS_DETAIL_ENUMRECORDS
9#define HPP_ALIB_ENUMS_DETAIL_ENUMRECORDS 1
10#pragma once
11#if !defined(HPP_ALIB_ENUMS_RECORDS)
12# error "ALib sources with ending '.inl' must not be included from outside."
13#endif
15
16namespace alib { namespace enums {
17
18/// Details of namespace #alib::enums.
19namespace detail {
20
21// #################################################################################################
22// Detail functions
23// #################################################################################################
24//==================================================================================================
25/// Stores the \p{record} for enum \p{element} of the given enum type \p{rtti}.
26/// If a value already exists, it is ignored. This allows having multiple records, while only
27/// the first is found.
28/// @param rtti The enumeration type.
29/// @param integral The integral value of the enum element.
30/// @param record The record to associate.
31//==================================================================================================
33void setEnumRecord( const std::type_info& rtti, integer integral, const void* record );
34
35//==================================================================================================
36/// Retrieves the enum record stored for the enum element with integral value \p{integral} of
37/// enum type \p{rtti}.
38/// @param rtti The enumeration type.
39/// @param integral The integral value of the enum element.
40/// @return A pointer to the record, \c nullptr if not found.
41//==================================================================================================
43const void* getEnumRecord( const std::type_info& rtti, integer integral );
44
45
46//==================================================================================================
47/// This is the internal singleton that provides a link to the first
48/// \ref alib_enums_records "ALib Enum Record".
49/// The class inherit's \alib{singletons;Singleton} and has a private default constructor, what
50/// makes this type a \ref alib_singleton_strict "strict singleton".
51///
52/// Because enum record types are ensured to be trivially destructible types, no destructor is given.
53///
54/// @tparam TEnum The enumeration type associated with enum records.
55//==================================================================================================
56template<typename TEnum>
57struct EnumRecordHook : public Singleton<EnumRecordHook<TEnum>>
58{
59 #if !DOXYGEN
61 #endif
62
63 /// The enum's underlying integer type.
64 using TIntegral= typename std::underlying_type<TEnum>::type;
65
66 /// The enum's associated record type.
68
69
70 /// A node of the forward list that contains the custom record data
71 struct Node
72 {
73 /// The enum element's underlying integral value.
75
76 /// The data record.
78
79 /// Pointer to the next node.
81
82 /// Constructs this instance taking variadic template arguments to construct the contained
83 /// enum record of custom type.
84 ///
85 /// @tparam TArgs Types of the variadic arguments \p{args}.
86 /// @param element The enum element
87 /// @param args Variadic arguments forwarded to constructor of field #record.
88 template <typename... TArgs>
89 Node( TEnum element, TArgs&&... args) noexcept
90 : integral( UnderlyingIntegral( element) )
91 , record ( std::forward<TArgs>(args)... )
92 {}
93
94 /// Default constructor.
95 Node() noexcept =default;
96
97 };
98
99 /// The hook to the first record defined.
101
102 /// Helper methods that returns the address of field
103 /// \alib{enums::detail;EnumRecordHook::Node::next} the last element contained in the
104 /// list. If no elements have been initialized, yet, the address of field #first is
105 /// returned.
106 /// @return A pointer to the pointer of the last element or this hook.
108 {
109 auto** last= &first;
110 while( (*last) != nullptr )
111 last= &(*last)->next;
112 return last;
113 }
114
115 private:
116 /// Private default constructor, what makes this type a
117 /// \ref alib_singleton_strict "strict singleton".
118 ///
119 /// \note As enum record types are trivially destructible types, no destructor is given.
121 : first( nullptr )
122 {}
123
124};
125
126}}} // namespace [alib::enums::detail]
127
128
129#endif // HPP_ALIB_ENUMS_DETAIL_ENUMRECORDS
130
#define ALIB_API
Definition alib.hpp:639
ALIB_API void setEnumRecord(const std::type_info &rtti, integer integral, const void *record)
Definition records.cpp:50
ALIB_API const void * getEnumRecord(const std::type_info &rtti, integer integral)
Definition records.cpp:59
constexpr std::underlying_type< TEnum >::type UnderlyingIntegral(TEnum element) noexcept
Definition alib.cpp:69
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:273
A node of the forward list that contains the custom record data.
Node * next
Pointer to the next node.
TIntegral integral
The enum element's underlying integral value.
Node(TEnum element, TArgs &&... args) noexcept
Node() noexcept=default
Default constructor.
typename std::underlying_type< TEnum >::type TIntegral
The enum's underlying integer type.
Node * first
The hook to the first record defined.
typename T_EnumRecords< TEnum >::Type TRecord
The enum's associated record type.