ALib C++ Library
Library Version: 2510 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
enums_serialization.inl
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of the module \alib_resources of the \aliblong.
4///
5/// \emoji :copyright: 2013-2025 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8#if !DOXYGEN
10
11ALIB_EXPORT namespace alib::strings {
12
13template<typename TEnum, typename TChar, typename TAllocator>
14requires ( enumrecords::IsSerializable<TEnum> && !enumops::IsBitwise<TEnum> )
16{
17 void operator()( TAString<TChar,TAllocator>& target, TEnum element )
18 {
19 ALIB_ASSERT_ERROR( EnumRecords<TEnum>().begin() != EnumRecords<TEnum>().end(), "ENUMS",
20 "No Enum Records for type <{}> found.", &typeid(TEnum) )
21
22// CHANGE 1 (compared to original implementation, without module Resources in the ALib Build)
23 target << ResourcedType<TEnum>::TypeNamePrefix();
24 auto* record= enumrecords::TryRecord( element );
25 if( record != nullptr )
26 target << record->EnumElementName;
27 else
28 target << UnderlyingIntegral( element );
29// CHANGE 1 (compared to original implementation, without module Resources in the ALib Build)
30 target << ResourcedType<TEnum>::TypeNamePostfix();
31 }
32};
33
34
35template<typename TBitwiseEnum, typename TChar, typename TAllocator>
36requires ( alib::enumrecords::IsSerializable<TBitwiseEnum>
37 && alib::enumops::IsBitwise <TBitwiseEnum> )
39
40 void operator()( TAString<TChar,TAllocator>& target, TBitwiseEnum elements )
41 {
43 "ENUMS", "No Enum Records for type <{}> found.", &typeid(TBitwiseEnum) )
44
45// CHANGE 1 (compared to original implementation, without module Resources in the ALib Build)
46 target << ResourcedType<TBitwiseEnum>::TypeNamePrefix();
47
48 // check what has been covered and omit double entries
49 TBitwiseEnum covered= TBitwiseEnum(0);
50
51 // loop over entry 2 to end, check bit
52 integer len= target.Length();
53
54 for( auto recordIt= EnumRecords<TBitwiseEnum>().begin() ;
55 recordIt != EnumRecords<TBitwiseEnum>().end() ; ++recordIt )
56 {
57 // no bits are set and this entry does not contain bits, then stop here
58 if( recordIt.Integral() == 0 )
59 {
60 if( elements == TBitwiseEnum(0) )
61 {
62 target << recordIt->EnumElementName;
63// CHANGE 2 (compared to original implementation, without module Resources in the ALib Build)
64 target << ResourcedType<TBitwiseEnum>::TypeNamePostfix();
65 return;
66 }
67 }
68 else if( HasBits( elements, recordIt.Enum() )
69 && !HasBits( covered , recordIt.Enum() ) )
70 {
71 covered|= recordIt.Enum();
72 target << recordIt->EnumElementName << ',';
73 }
74 }
75 len= target.Length() - len;
76
77 // remove the last comma
78 if( len != 0 )
79 target.DeleteEnd( 1 );
80
81 ALIB_ASSERT_ERROR( covered == elements, "ENUMS",
82 "Not all bits have been covered while writing bitset '{}' of enumeration type <{}>. "
83 "Remaining bits are '{}'.", NString(NString128(NBin( elements ))),
84 &typeid(TBitwiseEnum), NString(NString128(NBin( covered & elements ))) )
85
86// CHANGE 3 (compared to original implementation, without module Resources in the ALib Build)
87 target << ResourcedType<TBitwiseEnum>::TypeNamePostfix();
88 }
89};
90
91} // namespace [alib::strings]
92#include "ALib.Lang.CIMethods.H"
93
94#endif // !DOXYGEN
95
96
97
#define ALIB_EXPORT
Definition alib.inl:488
#define ALIB_ASSERT_ERROR(cond, domain,...)
Definition alib.inl:1049
constexpr bool HasBits(TEnum element, TEnum selection) noexcept
Definition bitwise.inl:336
std::underlying_type_t< TEnum > constexpr UnderlyingIntegral(TEnum element) noexcept
const RecordsTraits< TEnum >::Type * TryRecord(TEnum element)
Definition records.inl:218
NLocalString< 128 > NString128
Type alias name for TLocalString<nchar,128>.
lang::integer integer
Type alias in namespace alib.
Definition integers.inl:149
strings::TString< nchar > NString
Type alias in namespace alib.
Definition string.inl:2390
enumrecords::EnumRecords< TEnum > EnumRecords
Type alias in namespace alib.
Definition records.inl:519
strings::TBin< nchar > NBin
Type alias in namespace alib.
Definition format.inl:575