C++ (Qt)enum { param_Amount = 0, param_Velocity = 1, param_.., ...}; std::vector<QString> paramNames = { "Amount", "Velocity", ...};
C++ (Qt)QString Param2Name( TParam param ) { static QHash<TParam, QString> names; if (!names.size()) { names[param_Amount] = "Amount"; names[param_Velocity] = "Velocity"; ... } return names.value(param);}
C++ (Qt)class ItemQuality{ Q_GADGET Q_ENUMS(ItemQualityEnum) public: enum ItemQualityEnum { LowQuality = 1, Normal, HighQuality, Magic, Set, Rare, Unique, Crafted, Honorific };}; const char *quality = metaEnumFromName<Enums::ItemQuality>("ItemQualityEnum").valueToKey(item->quality);
C++ (Qt)const char *quality = metaEnumFromName<Enums::ItemQuality>("ItemQualityEnum").valueToKey(item->quality);
#include <iostream>enum Param { First, Second, Third};template <Param> const char* ParamName = "Not associated!";template <> const char* ParamName<First> = "First parameter name";template <> const char* ParamName<Second> = "Second parameter name";// Для третьего параметра "забыли" специализацию, поэтому будет "Not associated!".int main(){ std::cout << ParamName<First> << std::endl; std::cout << ParamName<Second> << std::endl; std::cout << ParamName<Third> << std::endl; // Not associated! return 0;}
template <Param> const char* ParamName = "Not associated!";заменить наtemplate <Param> const char* ParamName = [](){ static_assert(0, "Not associated!"); return nullptr; }();
C++ (Qt)template < typename _Type, int >struct EnumName { static const char * name () { return "<Unknown>"; } }; #define ENUM_NAME( Enum, param_value, param_name ) \ template <> struct EnumName< Enum, param_value > { static const char * name () { return param_name; } }; \ template < typename _Enum, int _value >struct EnumNameHelper{ static const char * name ( int value ) { if ( _value == value ) return EnumName< _Enum, _value >::name(); else if ( _value > value ) return EnumNameHelper< _Enum, _value - 1 >::name( value ); else return "<Incorrect>"; }}; template < typename _Enum >struct EnumNameHelper< _Enum, -1 >{ static const char * name ( int ) { return "<Corrupted>"; }}; template < typename _Enum >const char * enumValueName ( _Enum value ){ return EnumNameHelper< _Enum, param_Count >::name( value );}
C++ (Qt)enum MyEnum{ param_Amount, param_Velocity, param_Color, param_Coordinate, param_Count}; // или так писатьtemplate <> struct EnumName< MyEnum, param_Amount > { static const char * name () { return "Amount"; } };// или такENUM_NAME( MyEnum, param_Velocity, "Velocity" )ENUM_NAME( MyEnum, param_Color, "Color" )ENUM_NAME( MyEnum, param_Coordinate, "Coordinate" ) #include <iostream> int main ( int narg, char ** vargs ){ // В runtime тоже работает for ( size_t i = 0; i < param_Count; ++i ) ::std::cout << enumValueName< MyEnum >( i ) << ::std::endl; return 0;}