Files
libFGL/include/fgl/reflection/enumFromString.hpp
2025-12-15 21:24:55 -05:00

32 lines
717 B
C++

//
// Created by kj16609 on 11/24/25.
//
#pragma once
namespace fgl
{
template < typename E >
consteval E enumFromString( std::string_view s )
{
constexpr std::meta::info info { ^^E };
static_assert( std::meta::is_enum_type( info ), "E must be an enum" );
for ( const auto member : std::meta::enumerators_of( info ) )
{
const auto name { std::meta::identifier_of( member ) };
if ( name == s ) return std::meta::extract< E >( member );
}
throw std::meta::exception( "Unable to map string to enum", info );
}
enum TestEnum
{
MY_VAR = 1
};
static_assert(
enumFromString< TestEnum >( "MY_VAR" ) == TestEnum::MY_VAR, "Enum from string did not get expected output" );
} // namespace fgl