reflection

This commit is contained in:
2025-12-15 21:24:55 -05:00
parent fac5494bee
commit a46749a7a6
2 changed files with 76 additions and 9 deletions

View File

@@ -0,0 +1,32 @@
//
// 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

View File

@@ -8,14 +8,49 @@
namespace fgl::size
{
std::string toHuman( std::size_t size )
{
constexpr std::size_t mod { 1024 };
if ( size < mod ) return std::format( "{}B", size );
if ( size < mod * mod ) return std::format( "{}KiB", static_cast< int >( size / mod ) );
if ( size < mod * mod * mod ) return std::format( "{}MiB", static_cast< int >( size / ( mod * mod ) ) );
if ( size < mod * mod * mod * mod ) return std::format( "{}GiB", static_cast< int >( size / ( mod * mod * mod ) ) );
if ( size < mod * mod * mod * mod )
return std::format( "{}GiB", static_cast< int >( size / ( mod * mod * mod ) ) );
return std::format( "{}TiB", static_cast< int >( size / ( mod * mod * mod * mod ) ) );
}
namespace literals
{
constexpr unsigned long long int operator""_B( const unsigned long long int size )
{
return size;
}
constexpr unsigned long long int operator""_KiB( const unsigned long long int size )
{
return size * 1024;
}
constexpr unsigned long long int operator""_MiB( const unsigned long long int size )
{
return size * 1024_KiB;
}
constexpr unsigned long long int operator""_GiB( const unsigned long long int size )
{
return size * 1024_MiB;
}
inline std::string toString( const unsigned long long int size )
{
if ( size < 1024_B ) return std::to_string( size ) + " B";
if ( size < 1024_KiB ) return std::to_string( size / 1024 ) + " KiB";
if ( size < 1024_MiB ) return std::to_string( size / 1024_KiB ) + " MiB";
if ( size < 1024_GiB ) return std::to_string( size / 1024_MiB ) + " GiB";
return std::to_string( size / 1024_GiB ) + " TiB";
}
} // namespace literals
} // namespace fgl::size