ALib C++ Library
Library Version: 2402 R1
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
11#if !defined(HPP_ALIB_ENUMS_RECORDS)
12# error "ALib sources with ending '.inl' must not be included from outside."
13#endif
14
15#if !defined(HPP_ALIB_LANG_INTEGERS)
16# include "alib/lang/integers.hpp"
17#endif
18
19
20namespace alib { namespace enums {
21
22/**
23 * Details of namespace #alib::enums.
24 */
25namespace detail {
26
27// #################################################################################################
28// Detail functions
29// #################################################################################################
30/** ************************************************************************************************
31 * Stores the \p{record} for enum \p{element} of the given enum type \p{rtti}.
32 * If a value already exists, it is ignored. This allows to have multiple records, while only
33 * the first is found.
34 * @param rtti The enumeration type.
35 * @param integral The integral value of the enum element.
36 * @param record The record to associate.
37 */
39void setEnumRecord( const std::type_info& rtti, integer integral, const void* record );
40
41/** ************************************************************************************************
42 * Retrieves the enum record stored for the enum element with integral value \p{integral} of
43 * enum type \p{rtti}.
44 * @param rtti The enumeration type.
45 * @param integral The integral value of the enum element.
46 * @return A pointer to the record, \c nullptr if not found.
47 **************************************************************************************************/
49const void* getEnumRecord( const std::type_info& rtti, integer integral );
50
51
52/** ************************************************************************************************
53 * This is the internal singleton that provides a link to the first
54 * \ref alib_enums_records "ALib Enum Record".
55 * The class inherit's \alib{singletons;Singleton} and has a private default constructor, what
56 * makes this type a \ref alib_singleton_strict "strict singleton".
57 *
58 * Because enum record types are ensured to be trivially destructible types, no destructor is given.
59 *
60 * @tparam TEnum The enumeration type associated with enum records.
61 **************************************************************************************************/
62template<typename TEnum>
63struct EnumRecordHook : public Singleton<EnumRecordHook<TEnum>>
64{
65 #if !defined(ALIB_DOX)
67 #endif
68
69 /** The enum's underlying integer type. */
70 using TIntegral= typename std::underlying_type<TEnum>::type;
71
72 /** The enum's associated record type. */
74
75
76 /** A node of the forward list that contains the custom record data */
77 struct Node
78 {
79 /** The enum element's underlying integral value. */
81
82 /** The data record. */
84
85 /** Pointer to the next node. */
87
88 /**
89 * Constructs this instance taking variadic template arguments to construct the contained
90 * enum record of custom type.
91 *
92 * @tparam TArgs Types of the variadic arguments \p{args}.
93 * @param element The enum element
94 * @param args Variadic arguments forwarded to constructor of field #record.
95 */
96 template <typename... TArgs>
97 Node( TEnum element, TArgs&&... args) noexcept
98 : integral( UnderlyingIntegral( element) )
99 , record ( std::forward<TArgs>(args)... )
100 {}
101
102 /** Default constructor. */
103 Node() noexcept =default;
104
105 };
106
107 /** The hook to the first record defined. */
109
110 /**
111 * Helper methods that returns the address of field
112 * \alib{enums::detail;EnumRecordHook::Node::next} the last element contained in the
113 * list. If no elements have been initialized, yet, the address of field #first is
114 * returned.
115 * @return A pointer to the pointer of the last element or this hook.
116 */
118 {
119 auto** last= &first;
120 while( (*last) != nullptr )
121 last= &(*last)->next;
122 return last;
123 }
124
125 private:
126 /**
127 * Private default constructor, what makes this type a
128 * \ref alib_singleton_strict "strict singleton".
129 *
130 * \note As enum record types are trivially destructible types, no destructor is given.
131 */
133 : first( nullptr )
134 {}
135
136};
137
138}}} // namespace [alib::enums::detail]
139
140
141#endif // HPP_ALIB_ENUMS_DETAIL_ENUMRECORDS
#define ALIB_API
Definition alib.hpp:538
ALIB_API void setEnumRecord(const std::type_info &rtti, integer integral, const void *record)
Definition records.cpp:51
ALIB_API const void * getEnumRecord(const std::type_info &rtti, integer integral)
Definition records.cpp:60
constexpr std::underlying_type< TEnum >::type UnderlyingIntegral(TEnum element) noexcept(true)
Definition alib.cpp:57
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:286
Node(TEnum element, TArgs &&... args) noexcept
typename std::underlying_type< TEnum >::type TIntegral
typename T_EnumRecords< TEnum >::Type TRecord