MIT License
Usagi Ito usagi@WonderRabbitProject.net
#pragma once | |
#include <string> | |
#include <stdexcept> | |
#include <boost/preprocessor.hpp> | |
// WRP_MAKE_ENUM の内部で switch-case の case を生成するための補助マクロ | |
#define WRP_MAKE_ENUM_TO_STRING_ELEMENT( r, data, elem ) \ | |
case data :: elem : return BOOST_PP_STRINGIZE( elem ); \ | |
// enum class _symbol_name : _base_type { enumerators }; と to_string( const _symbol_name ) を生成するためのマクロ | |
#define WRP_MAKE_ENUM( _symbol_name, _base_type, enumerators ) \ | |
enum class _symbol_name \ | |
: _base_type \ | |
{ BOOST_PP_SEQ_ENUM( enumerators ) \ | |
}; \ | |
\ | |
template < typename T = void > \ | |
inline auto to_string( const _symbol_name value ) \ | |
-> std::string \ | |
{ \ | |
switch ( value ) \ | |
{ \ | |
BOOST_PP_SEQ_FOR_EACH( \ | |
WRP_MAKE_ENUM_TO_STRING_ELEMENT, \ | |
_symbol_name, \ | |
enumerators \ | |
) \ | |
default: throw std::runtime_error( "undefined enum member" );\ | |
} \ | |
} \ |
ppp | |
qqq | |
rrr |
#include <iostream> | |
#include <cstdint> | |
#include "enum.hxx" | |
namespace abc | |
{ | |
WRP_MAKE_ENUM | |
( def | |
, std::uint8_t | |
, ( ppp ) | |
( qqq ) | |
( rrr ) | |
) | |
} | |
auto main() | |
-> int | |
{ | |
std::cout | |
<< to_string( abc::def::ppp ) << "\n" | |
<< to_string( abc::def::qqq ) << "\n" | |
<< to_string( abc::def::rrr ) << "\n" | |
; | |
} |
MIT License
Usagi Ito usagi@WonderRabbitProject.net