Radium Engine  1.5.20
Loading...
Searching...
No Matches
VariableSetEnumManagement.hpp
1#pragma once
2
3#include <Core/Containers/VariableSet.hpp>
4#include <Core/Utils/EnumConverter.hpp>
5
6#include <string>
7
9namespace Ra {
10namespace Core {
11namespace VariableSetEnumManagement {
12
19template <typename EnumBaseType>
20void addEnumConverter( VariableSet& vs,
21 const std::string& name,
22 std::shared_ptr<Core::Utils::EnumConverter<EnumBaseType>> converter );
23
30template <typename EnumBaseType>
31Core::Utils::optional<std::shared_ptr<Core::Utils::EnumConverter<EnumBaseType>>>
32getEnumConverter( const VariableSet& vs, const std::string& name );
33
41template <typename Enum, typename std::enable_if<std::is_enum<Enum> {}, bool>::type = true>
42std::string getEnumString( const VariableSet& vs, const std::string& name, Enum value );
43
50template <typename EnumBaseType>
52getEnumString( const VariableSet& vs,
53 const std::string& name,
54 EnumBaseType value,
55 typename std::enable_if<!std::is_enum<EnumBaseType> {}, bool>::type = true );
56
63void RA_CORE_API setEnumVariable( VariableSet& vs,
64 const std::string& name,
65 const std::string& value );
66
67void RA_CORE_API setEnumVariable( VariableSet& vs, const std::string& name, const char* value );
68
69template <typename T>
70void setEnumVariable( VariableSet& vs, const std::string& name, T value ) {
71 auto v = static_cast<typename std::underlying_type<T>::type>( value );
72 vs.setVariable( name, v );
73}
74
75template <typename T>
76auto getEnumVariable( const VariableSet& vs, const std::string& name ) -> const T& {
77 static_assert( std::is_enum<T>::value );
78 return reinterpret_cast<const T&>(
79 vs.getVariable<typename std::underlying_type<T>::type>( name ) );
80}
81
82template <typename T>
83auto getEnumVariable( VariableSet& vs, const std::string& name ) -> T& {
84 return const_cast<T&>( getEnumVariable<T>( const_cast<const VariableSet&>( vs ), name ) );
85}
86
87template <typename EnumBaseType>
88void addEnumConverter( VariableSet& vs,
89 const std::string& name,
90 std::shared_ptr<Core::Utils::EnumConverter<EnumBaseType>> converter ) {
91
92 // typed converter
93 auto converterHandle = vs.setVariable( name, converter );
94
95 // string string converter/setter for setEnumVariable
96 std::function<void( Core::VariableSet&, const std::string&, const std::string& )>
97 convertingFunction = [converter_ = converterHandle.first]( Core::VariableSet& vs_,
98 const std::string& name_,
99 const std::string& value_ ) {
100 vs_.setVariable( name_, converter_->second->getEnumerator( value_ ) );
101 };
102 vs.setVariable( name, convertingFunction );
103}
104
105template <typename EnumBaseType>
106Core::Utils::optional<std::shared_ptr<Core::Utils::EnumConverter<EnumBaseType>>>
107getEnumConverter( const VariableSet& vs, const std::string& name ) {
108 auto storedConverter =
110 if ( storedConverter ) { return ( *storedConverter )->second; }
111 return {};
112}
113
114template <typename EnumBaseType>
115std::string getEnumString( const VariableSet& vs,
116 const std::string& name,
117 EnumBaseType value,
118 typename std::enable_if<!std::is_enum<EnumBaseType> {}, bool>::type ) {
119 auto storedConverter =
121 if ( storedConverter ) { return ( *storedConverter )->second->getEnumerator( value ); }
122 LOG( Ra::Core::Utils::logWARNING ) << name + " is not a registered Enum with underlying type " +
123 Ra::Core::Utils::demangleType<EnumBaseType>() + ".";
124 return "";
125}
126
127template <typename Enum, typename std::enable_if<std::is_enum<Enum> {}, bool>::type>
128std::string getEnumString( const VariableSet& vs, const std::string& name, Enum value ) {
129 using EnumBaseType = typename std::underlying_type_t<Enum>;
130 return getEnumString( vs, name, EnumBaseType( value ) );
131}
132
133} // namespace VariableSetEnumManagement
134} // namespace Core
135} // namespace Ra
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:3