Un exemple est souvent préférable à une longue explication.
Vous pouvez comstackr et exécuter cet extrait sur Coliru .
( Un autre exemple est également disponible)
#include
#define
aussi peu que possible. enum
manuel -> mappage de ssortingng
. enum
partir d’un nombre différent de zéro enum
négative enum
fragmentées class enum
support class enum
(C ++ 11) class enum :
support class enum :
ayant tout
autorisé (C ++ 11) std::map
n’est pas une bonne idée …) constexpr
(C ++ 11, relaxé en C ++ 14) noexcept
(C ++ 11) Une idée possible pourrait être d’utiliser les fonctionnalités du compilateur C ++ pour générer du code C ++ au moment de la compilation en utilisant des astuces de méta-programmation basées sur variadic template class
et des fonctions constexpr
…
Ceci est similaire à Yuri Finkelstein; mais ne nécessite pas de boost. J’utilise une carte afin que vous puissiez atsortingbuer n’importe quelle valeur aux énumérations, à n’importe quel ordre.
Déclaration de la classe enum comme:
DECLARE_ENUM_WITH_TYPE(TestEnumClass, int32_t, ZERO = 0x00, TWO = 0x02, ONE = 0x01, THREE = 0x03, FOUR);
Le code suivant créera automatiquement la classe enum et la surcharge:
Aucun boost requirejs, toutes les fonctions requirejses sont fournies.
Code:
#include #include #include
Exemple:
DECLARE_ENUM_WITH_TYPE(TestEnumClass, int32_t, ZERO = 0x00, TWO = 0x02, ONE = 0x01, THREE = 0x03, FOUR); int main(void) { TestEnumClass first, second; first = TestEnumClass::FOUR; second = TestEnumClass::TWO; std::cout << first << "(" << static_cast(first) << ")" << std::endl; // FOUR(4) std::string strOne; strOne = ~first; std::cout << strOne << std::endl; // FOUR std::string strTwo; strTwo = ("Enum-" + second) + (TestEnumClass::THREE + "-test"); std::cout << strTwo << std::endl; // Enum-TWOTHREE-test std::string strThree("TestEnumClass: "); strThree += second; std::cout << strThree << std::endl; // TestEnumClass: TWO std::cout << "Enum count=" << *first << std::endl; }
Vous pouvez exécuter le code ici
(L’approche de la bibliothèque better_enums )
Il existe un moyen d’énumérer les chaînes dans C ++ en cours qui ressemble à ceci:
ENUM(Channel, char, Red = 1, Green, Blue) // "Same as": // enum class Channel : char { Red = 1, Green, Blue };
Usage:
Channel c = Channel::_from_ssortingng("Green"); // Channel::Green (2) c._to_ssortingng(); // ssortingng "Green" for (Channel c : Channel::_values()) std::cout << c << std::endl; // And so on...
Toutes les opérations peuvent être faites constexpr
. Vous pouvez également implémenter la proposition de reflection C ++ 17 mentionnée dans la réponse de @ecatmur.
#
) est le seul moyen de convertir un jeton en chaîne dans le C ++ actuel. constexpr
. Il peut également être fait pour fonctionner avec C ++ 98 + __VA_ARGS__
. C'est définitivement le C ++ moderne. La définition de la macro est un peu compliquée, alors je réponds de plusieurs manières.
Il est facile d’étendre cette réponse aux caractéristiques de la bibliothèque - rien d’important n’est laissé de côté. Il est cependant assez fastidieux et pose des problèmes de portabilité du compilateur.
Disclaimer : Je suis l'auteur de l'article de CodeProject et de la bibliothèque.
Vous pouvez essayer le code dans cette réponse , la bibliothèque et l'implémentation de N4428 en ligne en direct dans Wandbox. La documentation de la bibliothèque contient également un aperçu de la manière de l'utiliser comme N4428 , ce qui explique la partie énumérée de cette proposition.
Le code ci-dessous implémente les conversions entre les énumérations et les chaînes. Cependant, il peut être étendu pour faire d'autres choses, comme l'itération. Cette réponse enveloppe un enum dans une struct
. Vous pouvez également générer une struct
traits à côté d'un enum.
La stratégie consiste à générer quelque chose comme ceci:
struct Channel { enum _enum : char { __VA_ARGS__ }; constexpr static const Channel _values[] = { __VA_ARGS__ }; constexpr static const char * const _names[] = { #__VA_ARGS__ }; static const char* _to_ssortingng(Channel v) { /* easy */ } constexpr static Channel _from_ssortingng(const char *s) { /* easy */ } };
Les problèmes sont les suivants:
{Red = 1, Green, Blue}
comme initialiseur pour le tableau de valeurs. Ce n'est pas valide C ++, car Red
n'est pas une expression assignable. Ceci est résolu en convertissant chaque constante en un type T
qui a un opérateur d'affectation, mais supprimera l'affectation: {(T)Red = 1, (T)Green, (T)Blue}
. {"Red = 1", "Green", "Blue"}
comme initialiseur pour le tableau de noms. Nous devrons couper le " = 1"
. Je ne suis pas au courant d'un excellent moyen de le faire au moment de la compilation, nous allons donc reporter cela à l'exécution. Par conséquent, _to_ssortingng
ne sera pas constexpr
, mais _from_ssortingng
peut toujours être constexpr
, car nous pouvons traiter les espaces et les signes comme des terminateurs lorsque nous comparons avec des chaînes non limitées. __VA_ARGS__
. C'est assez standard. Cette réponse comprend une version simple pouvant gérer jusqu'à 8 éléments. constexpr
(ou simplement const
) à la scope de l'espace de noms, ou des tableaux réguliers dans des fonctions inline statiques non constexpr
. Le code dans cette réponse est pour C ++ 11 et prend la première approche. L'article CodeProject est pour C ++ 98 et prend ce dernier. #include // For size_t. #include // For strcspn, strncpy. #include // For runtime_error. // A "typical" mapping macro. MAP(macro, a, b, c, ...) expands to // macro(a) macro(b) macro(c) ... // The helper macro COUNT(a, b, c, ...) expands to the number of // arguments, and IDENTITY(x) is needed to control the order of // expansion of __VA_ARGS__ on Visual C++ comstackrs. #define MAP(macro, ...) \ IDENTITY( \ APPLY(CHOOSE_MAP_START, COUNT(__VA_ARGS__)) \ (macro, __VA_ARGS__)) #define CHOOSE_MAP_START(count) MAP ## count #define APPLY(macro, ...) IDENTITY(macro(__VA_ARGS__)) #define IDENTITY(x) x #define MAP1(m, x) m(x) #define MAP2(m, x, ...) m(x) IDENTITY(MAP1(m, __VA_ARGS__)) #define MAP3(m, x, ...) m(x) IDENTITY(MAP2(m, __VA_ARGS__)) #define MAP4(m, x, ...) m(x) IDENTITY(MAP3(m, __VA_ARGS__)) #define MAP5(m, x, ...) m(x) IDENTITY(MAP4(m, __VA_ARGS__)) #define MAP6(m, x, ...) m(x) IDENTITY(MAP5(m, __VA_ARGS__)) #define MAP7(m, x, ...) m(x) IDENTITY(MAP6(m, __VA_ARGS__)) #define MAP8(m, x, ...) m(x) IDENTITY(MAP7(m, __VA_ARGS__)) #define EVALUATE_COUNT(_1, _2, _3, _4, _5, _6, _7, _8, count, ...) \ count #define COUNT(...) \ IDENTITY(EVALUATE_COUNT(__VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1)) // The type "T" mentioned above that drops assignment operations. template struct ignore_assign { constexpr explicit ignore_assign(U value) : _value(value) { } constexpr operator U() const { return _value; } constexpr const ignore_assign& operator =(int dummy) const { return *this; } U _value; }; // Prepends "(ignore_assign<_underlying>)" to each argument. #define IGNORE_ASSIGN_SINGLE(e) (ignore_assign<_underlying>)e, #define IGNORE_ASSIGN(...) \ IDENTITY(MAP(IGNORE_ASSIGN_SINGLE, __VA_ARGS__)) // Ssortingngizes each argument. #define STRINGIZE_SINGLE(e) #e, #define STRINGIZE(...) IDENTITY(MAP(STRINGIZE_SINGLE, __VA_ARGS__)) // Some helpers needed for _from_ssortingng. constexpr const char terminators[] = " =\t\r\n"; // The size of terminators includes the implicit '\0'. constexpr bool is_terminator(char c, size_t index = 0) { return index >= sizeof(terminators) ? false : c == terminators[index] ? true : is_terminator(c, index + 1); } constexpr bool matches_unsortingmmed(const char *unsortingmmed, const char *s, size_t index = 0) { return is_terminator(unsortingmmed[index]) ? s[index] == '\0' : s[index] != unsortingmmed[index] ? false : matches_unsortingmmed(unsortingmmed, s, index + 1); } // The macro proper. // // There are several "simplifications" in this implementation, for the // sake of brevity. First, we have only one viable option for declaring // constexpr arrays: at namespace scope. This probably should be done // two namespaces deep: one namespace that is likely to be unique for // our little enum "library", then inside it a namespace whose name is // based on the name of the enum to avoid collisions with other enums. // I am using only one level of nesting. // // Declaring constexpr arrays inside the struct is not viable because // they will need out-of-line definitions, which will result in // duplicate symbols when linking. This can be solved with weak // symbols, but that is comstackr- and system-specific. It is not // possible to declare constexpr arrays as static variables in // constexpr functions due to the ressortingctions on such functions. // // Note that this prevents the use of this macro anywhere except at // namespace scope. Ironically, the C++98 version of this, which can // declare static arrays inside static member functions, is actually // more flexible in this regard. It is shown in the CodeProject // article. // // Second, for compilation performance reasons, it is best to separate // the macro into a "paramesortingc" portion, and the portion that depends // on knowing __VA_ARGS__, and factor the former out into a template. // // Third, this code uses a default parameter in _from_ssortingng that may // be better not exposed in the public interface. #define ENUM(EnumName, Underlying, ...) \ namespace data_ ## EnumName { \ using _underlying = Underlying; \ enum { __VA_ARGS__ }; \ \ constexpr const size_t _size = \ IDENTITY(COUNT(__VA_ARGS__)); \ \ constexpr const _underlying _values[] = \ { IDENTITY(IGNORE_ASSIGN(__VA_ARGS__)) }; \ \ constexpr const char * const _raw_names[] = \ { IDENTITY(STRINGIZE(__VA_ARGS__)) }; \ } \ \ struct EnumName { \ using _underlying = Underlying; \ enum _enum : _underlying { __VA_ARGS__ }; \ \ const char * _to_ssortingng() const \ { \ for (size_t index = 0; index < data_ ## EnumName::_size; \ ++index) { \ \ if (data_ ## EnumName::_values[index] == _value) \ return _trimmed_names()[index]; \ } \ \ throw std::runtime_error("invalid value"); \ } \ \ constexpr static EnumName _from_string(const char *s, \ size_t index = 0) \ { \ return \ index >= data_ ## EnumName::_size ? \ throw std::runtime_error("invalid identifier") : \ matches_unsortingmmed( \ data_ ## EnumName::_raw_names[index], s) ? \ (EnumName)(_enum)data_ ## EnumName::_values[ \ index] : \ _from_ssortingng(s, index + 1); \ } \ \ EnumName() = delete; \ constexpr EnumName(_enum value) : _value(value) { } \ constexpr operator _enum() const { return (_enum)_value; } \ \ private: \ _underlying _value; \ \ static const char * const * _sortingmmed_names() \ { \ static char *the_names[data_ ## EnumName::_size]; \ static bool initialized = false; \ \ if (!initialized) { \ for (size_t index = 0; index < data_ ## EnumName::_size; \ ++index) { \ \ size_t length = \ std::strcspn(data_ ## EnumName::_raw_names[index],\ terminators); \ \ the_names[index] = new char[length + 1]; \ \ std::strncpy(the_names[index], \ data_ ## EnumName::_raw_names[index], \ length); \ the_names[index][length] = '\0'; \ } \ \ initialized = true; \ } \ \ return the_names; \ } \ };
et
// The code above was a "header file". This is a program that uses it. #include #include "the_file_above.h" ENUM(Channel, char, Red = 1, Green, Blue) constexpr Channel channel = Channel::_from_ssortingng("Red"); int main() { std::cout << channel._to_string() << std::endl; switch (channel) { case Channel::Red: return 0; case Channel::Green: return 1; case Channel::Blue: return 2; } } static_assert(sizeof(Channel) == sizeof(char), "");
Le programme ci-dessus imprime en Red
, comme prévu. Il y a un certain degré de sécurité du type, car vous ne pouvez pas créer une énumération sans l'initialiser, et la suppression de l'une des cases du switch
entraînera un avertissement du compilateur (en fonction de votre compilateur et de vos indicateurs). Notez également que "Red"
été converti en enum lors de la compilation.
Pour C ++ 17 C ++ 20, vous serez intéressé par les travaux du groupe d’étude de reflection (SG7). Il existe une série de documents parallèles couvrant la formulation ( P0194 ) et la justification, la conception et l’évolution ( P0385 ). (Les liens se résument au dernier article de chaque série.)
A partir de P0194r2 (2016-10-15), la syntaxe utiliserait le mot-clé reflexpr
proposé:
meta::get_base_name_v< meta::get_element_m< meta::get_enumerators_m, 0> >
Par exemple (adapté de la twig reflexpr de Matus Choclik ):
#include #include enum MyEnum { AAA = 1, BBB, CCC = 99 }; int main() { auto name_of_MyEnum_0 = std::meta::get_base_name_v< std::meta::get_element_m< std::meta::get_enumerators_m, 0> >; // prints "AAA" std::cout << name_of_MyEnum_0 << std::endl; }
La reflection statique n’a pas réussi à entrer dans C ++ 17 (plutôt dans le projet probablement final présenté lors de la réunion de normalisation de novembre 2016 à Issaquah), mais il est certain qu’elle sera intégrée à C ++ 20; du rapport de voyage de Herb Sutter :
En particulier, le groupe d'étude Réflexion a examiné la dernière proposition de reflection statique fusionnée et l'a trouvé prêt à entrer dans les principaux groupes Évolution lors de notre prochaine réunion pour commencer à examiner la proposition de reflection statique unifiée pour un TS ou la prochaine norme.
En 2011, j’ai passé un week-end à peaufiner une solution basée sur les macros et je ne l’ai jamais utilisée.
Ma procédure actuelle consiste à démarrer Vim, à copier les énumérateurs dans un corps de commutateur vide, à démarrer une nouvelle macro, à transformer le premier énumérateur en instruction de casse, à déplacer le curseur au début de la ligne suivante, à arrêter la macro instructions en exécutant la macro sur les autres énumérateurs.
Les macros Vim sont plus amusantes que les macros C ++.
Exemple réel:
enum class EtherType : uint16_t { ARP = 0x0806, IPv4 = 0x0800, VLAN = 0x8100, IPv6 = 0x86DD };
Je vais créer ceci:
std::ostream& operator<< (std::ostream& os, EtherType ethertype) { switch (ethertype) { case EtherType::ARP : return os << "ARP" ; case EtherType::IPv4: return os << "IPv4"; case EtherType::VLAN: return os << "VLAN"; case EtherType::IPv6: return os << "IPv6"; // omit default case to trigger compiler warning for missing cases }; return os << static_cast(ethertype); }
Et c’est comme ça que je me débrouille.
La prise en charge native de la énumération enum serait cependant bien meilleure. Je suis très intéressé par les résultats du groupe de travail de reflection en C ++ 17.
Une autre façon de le faire a été publiée par @sehe dans les commentaires .
Je ne sais pas si vous allez aimer ça ou pas, je ne suis pas très satisfait de cette solution, mais c’est une approche C ++ 14 conviviale car elle utilise des variables de modèle et abuse de la spécialisation des modèles:
enum class MyEnum : std::uint_fast8_t { AAA, BBB, CCC, }; template const char MyEnumName[] = "Invalid MyEnum value"; template<> const char MyEnumName[] = "AAA"; template<> const char MyEnumName[] = "BBB"; template<> const char MyEnumName[] = "CCC"; int main() { // Prints "AAA" std::cout << MyEnumName << '\n'; // Prints "Invalid MyEnum value" std::cout << MyEnumName(0x12345678)> << '\n'; // Well... in fact it prints "Invalid MyEnum value" for any value // different of MyEnum::AAA, MyEnum::BBB or MyEnum::CCC. return 0; }
Le pire à propos de cette approche est que c'est une douleur à maintenir, mais c'est aussi pénible de maintenir certains de ces approches similaires, n'est-ce pas?
Bons points sur cette approche:
Exemple en direct
Misterieux user673679 vous avez raison; l'approche du modèle de variable C ++ 14 ne gère pas le cas d'exécution, c'était ma faute de l'oublier 🙁
Mais nous pouvons toujours utiliser certaines fonctionnalités modernes du C ++ et des modèles de variables ainsi que des astuces de modèles variadiques pour obtenir une traduction à l'exécution de la valeur enum en chaîne ... c'est aussi gênant que les autres mais qui mérite d'être mentionné.
Commençons à utiliser un alias de modèle pour raccourcir l'access à un mappage d'énumération à chaîne:
// enum_map contains pairs of enum value and value ssortingng for each enum // this shortcut allows us to use enum_map. template using enum_map = std::map; // This variable template will create a map for each enum type which is // instantiated with. template enum_map enum_values{};
Ensuite, la ruse des modèles variadiques:
template void initialize() {} template void initialize(const ENUM value, const char *name, args ... tail) { enum_values.emplace(value, name); initialize (tail ...); }
Le " meilleur truc " ici est l'utilisation d'un modèle de variable pour la carte qui contient les valeurs et les noms de chaque entrée enum; cette carte sera la même dans chaque unité de traduction et aura le même nom partout, donc c'est simple et direct, si nous appelons la fonction d' initialize
comme ceci:
initialize ( MyEnum::AAA, "AAA", MyEnum::BBB, "BBB", MyEnum::CCC, "CCC" );
Nous atsortingbuons des noms à chaque entrée MyEnum
et nous pouvons les utiliser lors de l'exécution:
std::cout << enum_values[MyEnum::AAA] << '\n';
Mais peut être amélioré avec SFINAE et surcharge "opérateur:
template::value>::type> std::ostream &operator <<(std::ostream &o, const ENUM value) { static const std::string Unknown{std::string{typeid(ENUM).name()} + " unknown value"}; auto found = enum_values.find(value); return o << (found == enum_values .end() ? Unknown : found->second); }
Avec l' operator <<
correct operator <<
maintenant nous pouvons utiliser l'énumère de cette façon:
std::cout << MyEnum::AAA << '\n';
Cela est également gênant à maintenir et peut être amélioré, mais j'espère que vous en aurez l’idée.
Exemple en direct
J’ai eu le même problème il y a quelques jours. Je n’ai pas pu trouver de solution C ++ sans une étrange magie macro, alors j’ai décidé d’écrire un générateur de code CMake pour générer des instructions simples.
Usage:
enum2str_generate( PATH CLASS_NAME FUNC_NAME NAMESPACE INCLUDES ENUMS
BLACKLIST
USE_CONSTEXPR USE_C_STRINGS )
La fonction recherche les fichiers à inclure dans le système de fichiers (utilise les répertoires include fournis avec la commande include_directories), les lit et effectue des regex pour générer la classe et la ou les fonctions.
REMARQUE: constexpr implique en ligne C ++, l’utilisation de l’option USE_CONSTEXPR générera donc une classe d’en-tête uniquement!
Exemple:
./include/ah:
enum AAA : char { A1, A2 }; typedef enum { VAL1 = 0, VAL2 = 1, VAL3 = 2, VAL_FIRST = VAL1, // Ignored VAL_LAST = VAL3, // Ignored VAL_DUPLICATE = 1, // Ignored VAL_STRANGE = VAL2 + 1 // Must be blacklisted } BBB;
./CMakeLists.txt:
include_directories( ${PROJECT_SOURCE_DIR}/includes ...) enum2str_generate( PATH "${PROJECT_SOURCE_DIR}" CLASS_NAME "enum2Str" NAMESPACE "abc" FUNC_NAME "toStr" INCLUDES "ah" # WITHOUT directory ENUMS "AAA" "BBB" BLACKLIST "VAL_STRANGE")
Génère:
./enum2Str.hpp:
/*! * \file enum2Str.hpp * \warning This is an automatically generated file! */ #ifndef ENUM2STR_HPP #define ENUM2STR_HPP #include #include namespace abc { class enum2Str { public: static std::ssortingng toStr( AAA _var ) noexcept; static std::ssortingng toStr( BBB _var ) noexcept; }; } #endif // ENUM2STR_HPP
./enum2Str.cpp:
/*! * \file enum2Str.cpp * \warning This is an automatically generated file! */ #include "enum2Str.hpp" namespace abc { /*! * \brief Converts the enum AAA to a std::ssortingng * \param _var The enum value to convert * \returns _var converted to a std::ssortingng */ std::ssortingng enum2Str::toStr( AAA _var ) noexcept { switch ( _var ) { case A1: return "A1"; case A2: return "A2"; default: return ""; } } /*! * \brief Converts the enum BBB to a std::ssortingng * \param _var The enum value to convert * \returns _var converted to a std::ssortingng */ std::ssortingng enum2Str::toStr( BBB _var ) noexcept { switch ( _var ) { case VAL1: return "VAL1"; case VAL2: return "VAL2"; case VAL3: return "VAL3"; default: return " "; } } }
Le script supporte maintenant aussi les énumérations de scope (enum class | struct) et je l’ai déplacé vers un repository séparé avec d’autres scripts que j’utilise souvent: https://github.com/mensinda/cmakeBuildTools
Si votre enum
ressemble
enum MyEnum { AAA = -8, BBB = '8', CCC = AAA + BBB };
Vous pouvez déplacer le contenu de l’ enum
vers un nouveau fichier:
AAA = -8, BBB = '8', CCC = AAA + BBB
Et puis les valeurs peuvent être entourées d’une macro:
// default definition #ifned ITEM(X,Y) #define ITEM(X,Y) #endif // Items list ITEM(AAA,-8) ITEM(BBB,'8') ITEM(CCC,AAA+BBB) // clean up #undef ITEM
La prochaine étape peut consister à inclure à nouveau les éléments dans l’ enum
:
enum MyEnum { #define ITEM(X,Y) X=Y, #include "enum_definition_file" };
Et enfin, vous pouvez générer des fonctions utilitaires à propos de cette enum
:
std::ssortingng ToSsortingng(MyEnum value) { switch( value ) { #define ITEM(X,Y) case X: return #X; #include "enum_definition_file" } return ""; } MyEnum FromSsortingng(std::ssortingng const& value) { static std::map converter { #define ITEM(X,Y) { #X, X }, #include "enum_definition_file" }; auto it = converter.find(value); if( it != converter.end() ) return it->second; else throw std::runtime_error("Value is missing"); }
The solution can be applied to older C++ standards and it does not use modern C++ elements but it can be used to generate lot of code without too much effort and maintenance.
As per request from the OP, here a ssortingpped down version of the ugly macro solution based on Boost Preprosessor and Variadic Macros .
It allows for a simple list like syntax of the enumerator elements along with setting values for specific elements so that
XXX_ENUM(foo,(a,b,(c,42)));
expands to
enum foo { a, b, c=42 };
Alongside with the necessary functions to output and do some conversion back. This macro has been around here for ages, and I am not totally sure that its the most efficient way, or that it is a conforming way, but it has ever since been working
The complete code can be seen in action at both Ideone and Coliru .
Its gargantuan ugliness is above; I would have put it behind spoilers to protect your eyes, if I knew how, but markdown doesn’t like me.
#include #include #include namespace xxx { template struct enum_cast_adl_helper { }; template E enum_cast( const std::ssortingng& s ) { return do_enum_cast(s,enum_cast_adl_helper()); } template E enum_cast( const char* cs ) { std::ssortingng s(cs); return enum_cast(s); } } // namespace xxx #define XXX_PP_ARG_N( \ _1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \ _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \ _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \ _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \ _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \ _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \ _61,_62,_63,N,...) N #define XXX_PP_RSEQ_N() \ 63,62,61,60, \ 59,58,57,56,55,54,53,52,51,50, \ 49,48,47,46,45,44,43,42,41,40, \ 39,38,37,36,35,34,33,32,31,30, \ 29,28,27,26,25,24,23,22,21,20, \ 19,18,17,16,15,14,13,12,11,10, \ 9,8,7,6,5,4,3,2,1,0 #define XXX_PP_NARG_(...) XXX_PP_ARG_N(__VA_ARGS__) #define XXX_PP_NARG(...) XXX_PP_NARG_(__VA_ARGS__,XXX_PP_RSEQ_N()) #define XXX_TUPLE_SIZE_INTERNAL(TUPLE) XXX_PP_NARG TUPLE #define XXX_TUPLE_CHOICE(i) \ BOOST_PP_APPLY( \ BOOST_PP_TUPLE_ELEM( \ 25, i, ( \ (0), (1), (2), (3), (4), (5), (6), (7), (8), \ (9), (10), (11), (12), (13), (14), (15), (16), \ (17), (18), (19), (20), (21), (22), (23), (24) \ ) ) ) #define BOOST_PP_BOOL_00 BOOST_PP_BOOL_0 #define BOOST_PP_BOOL_01 BOOST_PP_BOOL_1 #define BOOST_PP_BOOL_02 BOOST_PP_BOOL_2 #define BOOST_PP_BOOL_03 BOOST_PP_BOOL_3 #define BOOST_PP_BOOL_04 BOOST_PP_BOOL_4 #define BOOST_PP_BOOL_05 BOOST_PP_BOOL_5 #define BOOST_PP_BOOL_06 BOOST_PP_BOOL_6 #define BOOST_PP_BOOL_07 BOOST_PP_BOOL_7 #define BOOST_PP_BOOL_08 BOOST_PP_BOOL_8 #define BOOST_PP_BOOL_09 BOOST_PP_BOOL_9 #define BOOST_PP_BOOL_010 BOOST_PP_BOOL_10 #define BOOST_PP_BOOL_011 BOOST_PP_BOOL_11 #define BOOST_PP_BOOL_012 BOOST_PP_BOOL_12 #define BOOST_PP_BOOL_013 BOOST_PP_BOOL_13 #define BOOST_PP_BOOL_014 BOOST_PP_BOOL_14 #define BOOST_PP_BOOL_015 BOOST_PP_BOOL_15 #define BOOST_PP_BOOL_016 BOOST_PP_BOOL_16 #define BOOST_PP_BOOL_017 BOOST_PP_BOOL_17 #define BOOST_PP_BOOL_018 BOOST_PP_BOOL_18 #define BOOST_PP_BOOL_019 BOOST_PP_BOOL_19 #define BOOST_PP_BOOL_020 BOOST_PP_BOOL_20 #define BOOST_PP_BOOL_021 BOOST_PP_BOOL_21 #define BOOST_PP_BOOL_022 BOOST_PP_BOOL_22 #define BOOST_PP_BOOL_023 BOOST_PP_BOOL_23 #define BOOST_PP_BOOL_024 BOOST_PP_BOOL_24 #define BOOST_PP_BOOL_025 BOOST_PP_BOOL_25 #define BOOST_PP_BOOL_026 BOOST_PP_BOOL_26 #define BOOST_PP_BOOL_027 BOOST_PP_BOOL_27 #define BOOST_PP_BOOL_028 BOOST_PP_BOOL_28 #define BOOST_PP_BOOL_029 BOOST_PP_BOOL_29 #define BOOST_PP_BOOL_030 BOOST_PP_BOOL_30 #define BOOST_PP_BOOL_031 BOOST_PP_BOOL_31 #define BOOST_PP_BOOL_032 BOOST_PP_BOOL_32 #define BOOST_PP_BOOL_033 BOOST_PP_BOOL_33 #define BOOST_PP_BOOL_034 BOOST_PP_BOOL_34 #define BOOST_PP_BOOL_035 BOOST_PP_BOOL_35 #define BOOST_PP_BOOL_036 BOOST_PP_BOOL_36 #define BOOST_PP_BOOL_037 BOOST_PP_BOOL_37 #define BOOST_PP_BOOL_038 BOOST_PP_BOOL_38 #define BOOST_PP_BOOL_039 BOOST_PP_BOOL_39 #define BOOST_PP_BOOL_040 BOOST_PP_BOOL_40 #define BOOST_PP_BOOL_041 BOOST_PP_BOOL_41 #define BOOST_PP_BOOL_042 BOOST_PP_BOOL_42 #define BOOST_PP_BOOL_043 BOOST_PP_BOOL_43 #define BOOST_PP_BOOL_044 BOOST_PP_BOOL_44 #define BOOST_PP_BOOL_045 BOOST_PP_BOOL_45 #define BOOST_PP_BOOL_046 BOOST_PP_BOOL_46 #define BOOST_PP_BOOL_047 BOOST_PP_BOOL_47 #define BOOST_PP_BOOL_048 BOOST_PP_BOOL_48 #define BOOST_PP_BOOL_049 BOOST_PP_BOOL_49 #define BOOST_PP_BOOL_050 BOOST_PP_BOOL_50 #define BOOST_PP_BOOL_051 BOOST_PP_BOOL_51 #define BOOST_PP_BOOL_052 BOOST_PP_BOOL_52 #define BOOST_PP_BOOL_053 BOOST_PP_BOOL_53 #define BOOST_PP_BOOL_054 BOOST_PP_BOOL_54 #define BOOST_PP_BOOL_055 BOOST_PP_BOOL_55 #define BOOST_PP_BOOL_056 BOOST_PP_BOOL_56 #define BOOST_PP_BOOL_057 BOOST_PP_BOOL_57 #define BOOST_PP_BOOL_058 BOOST_PP_BOOL_58 #define BOOST_PP_BOOL_059 BOOST_PP_BOOL_59 #define BOOST_PP_BOOL_060 BOOST_PP_BOOL_60 #define BOOST_PP_BOOL_061 BOOST_PP_BOOL_61 #define BOOST_PP_BOOL_062 BOOST_PP_BOOL_62 #define BOOST_PP_BOOL_063 BOOST_PP_BOOL_63 #define BOOST_PP_DEC_00 BOOST_PP_DEC_0 #define BOOST_PP_DEC_01 BOOST_PP_DEC_1 #define BOOST_PP_DEC_02 BOOST_PP_DEC_2 #define BOOST_PP_DEC_03 BOOST_PP_DEC_3 #define BOOST_PP_DEC_04 BOOST_PP_DEC_4 #define BOOST_PP_DEC_05 BOOST_PP_DEC_5 #define BOOST_PP_DEC_06 BOOST_PP_DEC_6 #define BOOST_PP_DEC_07 BOOST_PP_DEC_7 #define BOOST_PP_DEC_08 BOOST_PP_DEC_8 #define BOOST_PP_DEC_09 BOOST_PP_DEC_9 #define BOOST_PP_DEC_010 BOOST_PP_DEC_10 #define BOOST_PP_DEC_011 BOOST_PP_DEC_11 #define BOOST_PP_DEC_012 BOOST_PP_DEC_12 #define BOOST_PP_DEC_013 BOOST_PP_DEC_13 #define BOOST_PP_DEC_014 BOOST_PP_DEC_14 #define BOOST_PP_DEC_015 BOOST_PP_DEC_15 #define BOOST_PP_DEC_016 BOOST_PP_DEC_16 #define BOOST_PP_DEC_017 BOOST_PP_DEC_17 #define BOOST_PP_DEC_018 BOOST_PP_DEC_18 #define BOOST_PP_DEC_019 BOOST_PP_DEC_19 #define BOOST_PP_DEC_020 BOOST_PP_DEC_20 #define BOOST_PP_DEC_021 BOOST_PP_DEC_21 #define BOOST_PP_DEC_022 BOOST_PP_DEC_22 #define BOOST_PP_DEC_023 BOOST_PP_DEC_23 #define BOOST_PP_DEC_024 BOOST_PP_DEC_24 #define BOOST_PP_DEC_025 BOOST_PP_DEC_25 #define BOOST_PP_DEC_026 BOOST_PP_DEC_26 #define BOOST_PP_DEC_027 BOOST_PP_DEC_27 #define BOOST_PP_DEC_028 BOOST_PP_DEC_28 #define BOOST_PP_DEC_029 BOOST_PP_DEC_29 #define BOOST_PP_DEC_030 BOOST_PP_DEC_30 #define BOOST_PP_DEC_031 BOOST_PP_DEC_31 #define BOOST_PP_DEC_032 BOOST_PP_DEC_32 #define BOOST_PP_DEC_033 BOOST_PP_DEC_33 #define BOOST_PP_DEC_034 BOOST_PP_DEC_34 #define BOOST_PP_DEC_035 BOOST_PP_DEC_35 #define BOOST_PP_DEC_036 BOOST_PP_DEC_36 #define BOOST_PP_DEC_037 BOOST_PP_DEC_37 #define BOOST_PP_DEC_038 BOOST_PP_DEC_38 #define BOOST_PP_DEC_039 BOOST_PP_DEC_39 #define BOOST_PP_DEC_040 BOOST_PP_DEC_40 #define BOOST_PP_DEC_041 BOOST_PP_DEC_41 #define BOOST_PP_DEC_042 BOOST_PP_DEC_42 #define BOOST_PP_DEC_043 BOOST_PP_DEC_43 #define BOOST_PP_DEC_044 BOOST_PP_DEC_44 #define BOOST_PP_DEC_045 BOOST_PP_DEC_45 #define BOOST_PP_DEC_046 BOOST_PP_DEC_46 #define BOOST_PP_DEC_047 BOOST_PP_DEC_47 #define BOOST_PP_DEC_048 BOOST_PP_DEC_48 #define BOOST_PP_DEC_049 BOOST_PP_DEC_49 #define BOOST_PP_DEC_050 BOOST_PP_DEC_50 #define BOOST_PP_DEC_051 BOOST_PP_DEC_51 #define BOOST_PP_DEC_052 BOOST_PP_DEC_52 #define BOOST_PP_DEC_053 BOOST_PP_DEC_53 #define BOOST_PP_DEC_054 BOOST_PP_DEC_54 #define BOOST_PP_DEC_055 BOOST_PP_DEC_55 #define BOOST_PP_DEC_056 BOOST_PP_DEC_56 #define BOOST_PP_DEC_057 BOOST_PP_DEC_57 #define BOOST_PP_DEC_058 BOOST_PP_DEC_58 #define BOOST_PP_DEC_059 BOOST_PP_DEC_59 #define BOOST_PP_DEC_060 BOOST_PP_DEC_60 #define BOOST_PP_DEC_061 BOOST_PP_DEC_61 #define BOOST_PP_DEC_062 BOOST_PP_DEC_62 #define BOOST_PP_DEC_063 BOOST_PP_DEC_63 #define XXX_TO_NUMx(x) 0 ## x #define XXX_TO_NUM(x) BOOST_PP_ADD(0,XXX_TO_NUMx(x)) #define XXX_STRINGIZEX(x) # x #define XXX_VSTRINGIZE_SINGLE(a,b,x) XXX_STRINGIZE(x) #define XXX_VSTRINGIZE_TUPLE(tpl) XXX_TUPLE_FOR_EACH(XXX_VSTRINGIZE_SINGLE,,tpl) #define XXX_TUPLE_SIZE(TUPLE) XXX_TO_NUM(XXX_TUPLE_CHOICE(XXX_TUPLE_SIZE_INTERNAL(TUPLE))) #define XXX_TUPLE_FOR_EACH(MACRO,DATA,TUPLE) BOOST_PP_LIST_FOR_EACH(MACRO,DATA,BOOST_PP_TUPLE_TO_LIST(XXX_TUPLE_SIZE(TUPLE),TUPLE)) #define XXX_STRINGIZE(x) XXX_STRINGIZEX(x) #define XXX_VSTRINGIZE(...) XXX_VSTRINGIZE_TUPLE((__VA_ARGS__)) #define XXX_CAST_TO_VOID_ELEMENT(r,data,elem) (void)(elem); #define XXX_CAST_TO_VOID_INTERNAL(TUPLE) XXX_TUPLE_FOR_EACH(XXX_CAST_TO_VOID_ELEMENT,,TUPLE) #define XXX_CAST_TO_VOID(...) XXX_CAST_TO_VOID_INTERNAL((__VA_ARGS__)) #define XXX_ENUM_EXTRACT_SP(en) BOOST_PP_TUPLE_ELEM(XXX_TUPLE_SIZE(en),0,en) = BOOST_PP_TUPLE_ELEM(XXX_TUPLE_SIZE(en),1,en) #define XXX_ENUM_ELEMENT(r,data,elem) BOOST_PP_IF( XXX_TUPLE_SIZE(elem), XXX_ENUM_EXTRACT_SP(elem), elem) , #define XXX_ENUM_EXTRACT_ELEMENT(en) BOOST_PP_TUPLE_ELEM(XXX_TUPLE_SIZE(en),0,en) #define XXX_ENUM_CASE_ELEMENT(en) BOOST_PP_IF( XXX_TUPLE_SIZE(en), XXX_ENUM_EXTRACT_ELEMENT(en), en ) #define XXX_ENUM_CASE(r,data,elem) case data :: XXX_ENUM_CASE_ELEMENT(elem) : return #data "::" XXX_STRINGIZE(XXX_ENUM_CASE_ELEMENT(elem)); #define XXX_ENUM_IFELSE(r,data,elem) else if( en == data :: XXX_ENUM_CASE_ELEMENT(elem)) { return #data "::" XXX_STRINGIZE(XXX_ENUM_CASE_ELEMENT(elem)); } #define XXX_ENUM_CASTLIST(r,data,elem) { XXX_STRINGIZE(XXX_ENUM_CASE_ELEMENT(elem)), data :: XXX_ENUM_CASE_ELEMENT(elem) }, #define XXX_ENUM_QUALIFIED_CASTLIST(r,data,elem) { #data "::" XXX_STRINGIZE(XXX_ENUM_CASE_ELEMENT(elem)), data :: XXX_ENUM_CASE_ELEMENT(elem) }, #define XXX_ENUM_INTERNAL(TYPE,NAME,TUPLE) \ enum TYPE \ { \ XXX_TUPLE_FOR_EACH(XXX_ENUM_ELEMENT,,TUPLE) \ BOOST_PP_CAT(last_enum_,NAME) \ }; \ \ inline \ const char* to_ssortingng( NAME en ) \ { \ if(false) \ { \ } \ XXX_TUPLE_FOR_EACH(XXX_ENUM_IFELSE,NAME,TUPLE) \ else if( en == NAME :: BOOST_PP_CAT(last_enum_,NAME) ) \ { \ return XXX_VSTRINGIZE(NAME,::,BOOST_PP_CAT(last_enum_,NAME)); \ } \ else \ { \ return "Invalid enum value specified for " # NAME; \ } \ } \ \ inline \ std::ostream& operator<<( std::ostream& os, const NAME& en ) \ { \ os << to_string(en); \ return os; \ } \ \ inline \ NAME do_enum_cast( const std::string& s, const ::xxx::enum_cast_adl_helper& ) \ { \ static const std::unordered_map map = \ { \ XXX_TUPLE_FOR_EACH(XXX_ENUM_CASTLIST,NAME,TUPLE) \ XXX_TUPLE_FOR_EACH(XXX_ENUM_QUALIFIED_CASTLIST,NAME,TUPLE) \ }; \ \ auto cit = map.find(s); \ if( cit == map.end() ) \ { \ throw std::runtime_error("Invalid value to cast to enum"); \ } \ return cit->second; \ } #define XXX_ENUM(NAME,TUPLE) XXX_ENUM_INTERNAL(NAME,NAME,TUPLE) #define XXX_ENUM_CLASS(NAME,TUPLE) XXX_ENUM_INTERNAL(class NAME,NAME,TUPLE) #define XXX_ENUM_CLASS_TYPE(NAME,TYPE,TUPLE) XXX_ENUM_INTERNAL(class NAME : TYPE,NAME,TUPLE) #define XXX_ENUM_TYPE(NAME,TYPE,TUPLE) XXX_ENUM_INTERNAL(NAME : TYPE,NAME,TUPLE)
#include "xxx_enum.h" // the above lib #include XXX_ENUM(foo,(a,b,(c,42))); int main() { std::cout << "foo::a = " << foo::a <<'\n'; std::cout << "(int)foo::c = " << (int)foo::c <<'\n'; std::cout << "to_string(foo::b) = " << to_string(foo::b) <<'\n'; std::cout << "xxx::enum_cast(\"b\") = " << xxx::enum_cast ("b") <<'\n'; }
main.cpp
) > g++ --version | sed 1q g++ (GCC) 4.9.2 > g++ -std=c++14 -pedantic -Wall -Wextra main.cpp main.cpp:268:31: warning: extra ';' [-Wpedantic] XXX_ENUM(foo,(a,b,(c,42))); ^
foo::a = foo::a (int)foo::c = 42 to_ssortingng(foo::b) = foo::b xxx::enum_cast("b") = foo::b
Just generate your enums. Writing a generator for that purpose is about five minutes’ work.
Generator code in java and python, super easy to port to any language you like, including C++.
Also super easy to extend by whatever functionality you want.
example input:
First = 5 Second Third = 7 Fourth Fifth=11
generated header:
#include enum class Hallo { First = 5, Second = 6, Third = 7, Fourth = 8, Fifth = 11 }; std::ostream & operator << (std::ostream &, const Hallo&);
generated cpp file
#include #include "Hallo.h" std::ostream & operator << (std::ostream &out, const Hallo&value) { switch(value) { case Hallo::First: out << "First"; break; case Hallo::Second: out << "Second"; break; case Hallo::Third: out << "Third"; break; case Hallo::Fourth: out << "Fourth"; break; case Hallo::Fifth: out << "Fifth"; break; default: out << ""; } return out; }
And the generator, in a very terse form as a template for porting and extension. This example code really sortinges to avoid overwriting any files but still use it at your own risk.
package cppgen; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.nio.charset.Charset; import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; import java.util.regex.Matcher; import java.util.regex.Pattern; public class EnumGenerator { static void fail(Ssortingng message) { System.err.println(message); System.exit(1); } static void run(Ssortingng[] args) throws Exception { Pattern pattern = Pattern.comstack("\\s*(\\w+)\\s*(?:=\\s*(\\d+))?\\s*", Pattern.UNICODE_CHARACTER_CLASS); Charset charset = Charset.forName("UTF8"); Ssortingng tab = " "; if (args.length != 3) { fail("Required arguments:
And a port to Python 3.5 because different enough to be potentially helpful
import re import collections import sys import io import os def fail(*args): print(*args) exit(1) pattern = re.comstack(r'\s*(\w+)\s*(?:=\s*(\d+))?\s*') tab = " " if len(sys.argv) != 4: n=0 for arg in sys.argv: print("arg", n, ":", arg, " / ", sys.argv[n]) n += 1 fail("Required arguments:
I took the idea from @antron and implemented it differently: generating a true enum class .
This implementation meets all the requirements listed in original question but currently has only one real limitation : it assumes the enum values are either not provided or, if provided, must start with 0 and go up sequentially without gaps.
This is not an insortingnsic limitation – simply that I don’t use ad-hoc enum values. If this is needed, one can replace vector lookup with traditional switch/case implementation.
The solution uses some c++17 for inline variables but this can be easily avoided if needed. It also uses boost:sortingm because of simplicity.
Most importantly, it takes only 30 lines of code and no black magic macros. Le code est ci-dessous. It’s meant to be put in header and included in multiple compilation modules.
It can be used the same way as was suggested earlier in this thread:
ENUM(Channel, int, Red, Green = 1, Blue) std::out << "My name is " << Channel::Green; //prints My name is Green
Pls let me know if this is useful and how it can be improved further.
#include struct EnumSupportBase { static std::vector split(const std::ssortingng s, char delim) { std::ssortingngstream ss(s); std::ssortingng item; std::vector tokens; while (std::getline(ss, item, delim)) { auto pos = item.find_first_of ('='); if (pos != std::ssortingng::npos) item.erase (pos); boost::sortingm (item); tokens.push_back(item); } return tokens; } }; #define ENUM(EnumName, Underlying, ...) \ enum class EnumName : Underlying { __VA_ARGS__, _count }; \ struct EnumName ## Support : EnumSupportBase { \ static inline std::vector _token_names = split(#__VA_ARGS__, ','); \ static constexpr const char* get_name(EnumName enum_value) { \ int index = (int)enum_value; \ if (index >= (int)EnumName::_count || index < 0) \ return "???"; \ else \ return _token_names[index].c_str(); \ } \ }; \ inline std::ostream& operator<<(std::ostream& os, const EnumName & es) { \ return os << EnumName##Support::get_name(es); \ }
I have been frustrated by this problem for a long time too, along with the problem of getting a type converted to ssortingng in a proper way. However, for the last problem, I was surprised by the solution explained in Is it possible to print a variable’s type in standard C++? , using the idea from Can I obtain C++ type names in a constexpr way? . Using this technique, an analogous function can be constructed for getting an enum value as ssortingng:
#include using namespace std; class static_ssortingng { const char* const p_; const std::size_t sz_; public: typedef const char* const_iterator; template constexpr static_ssortingng(const char(&a)[N]) noexcept : p_(a) , sz_(N - 1) {} constexpr static_ssortingng(const char* p, std::size_t N) noexcept : p_(p) , sz_(N) {} constexpr const char* data() const noexcept { return p_; } constexpr std::size_t size() const noexcept { return sz_; } constexpr const_iterator begin() const noexcept { return p_; } constexpr const_iterator end() const noexcept { return p_ + sz_; } constexpr char operator[](std::size_t n) const { return n < sz_ ? p_[n] : throw std::out_of_range("static_string"); } }; inline std::ostream& operator<<(std::ostream& os, static_string const& s) { return os.write(s.data(), s.size()); } /// \brief Get the name of a type template static_ssortingng typeName() { #ifdef __clang__ static_ssortingng p = __PRETTY_FUNCTION__; return static_ssortingng(p.data() + 30, p.size() - 30 - 1); #elif defined(_MSC_VER) static_ssortingng p = __FUNCSIG__; return static_ssortingng(p.data() + 37, p.size() - 37 - 7); #endif } namespace details { template struct EnumWrapper { template < Enum enu > static static_ssortingng name() { #ifdef __clang__ static_ssortingng p = __PRETTY_FUNCTION__; static_ssortingng enumType = typeName(); return static_ssortingng(p.data() + 73 + enumType.size(), p.size() - 73 - enumType.size() - 1); #elif defined(_MSC_VER) static_ssortingng p = __FUNCSIG__; static_ssortingng enumType = typeName (); return static_ssortingng(p.data() + 57 + enumType.size(), p.size() - 57 - enumType.size() - 7); #endif } }; } /// \brief Get the name of an enum value template static_ssortingng enumName() { return details::EnumWrapper::template name(); } enum class Color { Blue = 0, Yellow = 1 }; int main() { std::cout << "_" << typeName() << "_" << std::endl; std::cout << "_" << enumName() << "_" << std::endl; return 0; }
The code above has only been tested on Clang (see https://ideone.com/je5Quv ) and VS2015, but should be adaptable to other comstackrs by fiddling a bit with the integer constants. Of course, it still uses macros under the hood, but at least one doesn't need access to the enum implementation.
The following solution is based on a std::array
for a given enum.
For enum
to std::ssortingng
conversion we can just cast the enum to size_t
and lookup the ssortingng from the array. The operation is O(1) and requires no heap allocation.
#include #include #include #include #include #include #define STRINGIZE(s, data, elem) BOOST_PP_STRINGIZE(elem) // ENUM // ============================================================================ #define ENUM(X, SEQ) \ struct X { \ enum Enum {BOOST_PP_SEQ_ENUM(SEQ)}; \ static const std::array array_of_ssortingngs() { \ return {{BOOST_PP_SEQ_ENUM(BOOST_PP_SEQ_TRANSFORM(STRINGIZE, 0, SEQ))}}; \ } \ static std::ssortingng to_ssortingng(Enum e) { \ auto a = array_of_ssortingngs(); \ return a[static_cast(e)]; \ } \ }
For std::ssortingng
to enum
conversion we would have to make a linear search over the array and cast the array index to enum
.
Try it here with usage examples: http://coliru.stacked-crooked.com/a/e4212f93bee65076
Edit: Reworked my solution so the custom Enum can be used inside a class.
Solutions using enum within class/struct (struct defaults with public members) and overloaded operators:
struct Color { enum Enum { RED, GREEN, BLUE }; Enum e; Color() {} Color(Enum e) : e(e) {} Color operator=(Enum o) { e = o; return *this; } Color operator=(Color o) { e = oe; return *this; } bool operator==(Enum o) { return e == o; } bool operator==(Color o) { return e == oe; } operator Enum() const { return e; } std::ssortingng toSsortingng() const { switch (e) { case Color::RED: return "red"; case Color::GREEN: return "green"; case Color::BLUE: return "blue"; default: return "unknown"; } } };
From the outside it looks nearly exactly like a class enum:
Color red; red = Color::RED; Color blue = Color::BLUE; cout << red.toString() << " " << Color::GREEN << " " << blue << endl;
This will output "red 1 2". You could possibly overload << to make blue output a string (although it might cause ambiguity so not possible), but it wouldn't work with Color::GREEN since it doesn't automatically convert to Color.
The purpose of having an implicit convert to Enum (which implicitly converts to int or type given) is to be able to do:
Color color; switch (color) ...
This works, but it also means that this work too:
int i = color;
With an enum class it wouldn't comstack. You ought to be careful if you overload two functions taking the enum and an integer, or remove the implicit conversion...
Another solution would involve using an actual enum class and static members:
struct Color { enum class Enum { RED, GREEN, BLUE }; static const Enum RED = Enum::RED, GREEN = Enum::GREEN, BLUE = Enum::BLUE; //same as previous... };
It possibly takes more space, and is longer to make, but causes a comstack error for implicit int conversions. I'd use this one because of that!
There's surely overhead with this though, but I think it's just simpler and looks better than other code I've seen. There's also potential for adding functionality, which could all be scoped within the class.
Edit : this works and most can be comstackd before execution:
class Color { public: enum class Enum { RED, GREEN, BLUE }; static const Enum RED = Enum::RED, GREEN = Enum::GREEN, BLUE = Enum::BLUE; constexpr Color() : e(Enum::RED) {} constexpr Color(Enum e) : e(e) {} constexpr bool operator==(Enum o) const { return e == o; } constexpr bool operator==(Color o) const { return e == oe; } constexpr operator Enum() const { return e; } Color& operator=(Enum o) { const_cast(this->e) = o; return *this; } Color& operator=(Color o) { const_cast (this->e) = oe; return *this; } std::ssortingng toSsortingng() const { switch (e) { case Enum::RED: return "red"; case Enum::GREEN: return "green"; case Enum::BLUE: return "blue"; default: return "unknown"; } } private: const Enum e; };
This gist provides a simple mapping based on C++ variadic templates.
This is a C++17-simplified version of the type-based map from the gist :
#include // http://stackoverflow.com/q/24520781 template struct map { static constexpr typename KeyValue::key_t get(const char* val) noexcept { if constexpr (sizeof...(RestOfKeyValues)==0) // C++17 if constexpr return KeyValue::key; // Returns last element else { static_assert(KeyValue::val != nullptr, "Only last element may have null name"); return strcmp(val, KeyValue::val()) ? map::get(val) : KeyValue::key; } } static constexpr const char* get(typename KeyValue::key_t key) noexcept { if constexpr (sizeof...(RestOfKeyValues)==0) return (KeyValue::val != nullptr) && (key == KeyValue::key) ? KeyValue::val() : ""; else return (key == KeyValue::key) ? KeyValue::val() : map::get(key); } }; template class names { typedef map Map; public: static constexpr Enum get(const char* nam) noexcept { return Map::get(nam); } static constexpr const char* get(Enum key) noexcept { return Map::get(key); } };
An example usage:
enum class fasion { fancy, classic, sporty, emo, __last__ = emo, __unknown__ = -1 }; #define NAME(s) static inline constexpr const char* s() noexcept {return #s;} namespace name { NAME(fancy) NAME(classic) NAME(sporty) NAME(emo) } template // C++17 template struct _ { typedef decltype(K) key_t; typedef decltype(V) name_t; static constexpr key_t key = K; // enum id value static constexpr name_t val = V; // enum id name }; typedef names, _, _, _, _ > fasion_names;
The map
can be used in both directions:
fasion_names::get(fasion::emo)
fasion_names::get("emo")
This example is available on godbolt.org
int main () { constexpr auto str = fasion_names::get(fasion::emo); constexpr auto fsn = fasion_names::get(str); return (int) fsn; }
Result from gcc-7 -std=c++1z -Ofast -S
main: mov eax, 3 ret
Very simple solution with one big constraint: you can’t assign custom values to enum
values, but with the right regex, you could. you could also add a map to translate them back to enum
values without much more effort:
#include #include #include #include std::vector split(const std::ssortingng& s, const std::regex& delim = std::regex(",\\s*")) { using namespace std; vector cont; copy(regex_token_iterator(s.begin(), s.end(), delim, -1), regex_token_iterator (), back_inserter(cont)); return cont; } #define EnumType(Type, ...) enum class Type { __VA_ARGS__ } #define EnumSsortingngs(Type, ...) static const std::vector \ Type##Ssortingngs = split(#__VA_ARGS__); #define EnumToSsortingng(Type, ...) EnumType(Type, __VA_ARGS__); \ EnumSsortingngs(Type, __VA_ARGS__)
Exemple d’utilisation:
EnumToSsortingng(MyEnum, Red, Green, Blue);
As mentioned above, N4113 is the final solution to this matter, but we’ll have to wait more than a year to see it coming out.
Meanwhile, if you want such feature, you’ll need to resort to “simple” templates and some preprocessor magic.
template class Enum final { const char* m_name; const T m_value; static T m_counter; public: Enum(const char* str, T init = m_counter) : m_name(str), m_value(init) {m_counter = (init + 1);} const T value() const {return m_value;} const char* name() const {return m_name;} }; template T Enum::m_counter = 0; #define ENUM_TYPE(x) using Enum = Enum; #define ENUM_DECL(x,...) x(#x,##__VA_ARGS__) #define ENUM(...) const Enum ENUM_DECL(__VA_ARGS__);
#include //the initialization order should be correct in all scenarios namespace Level { ENUM_TYPE(std::uint8) ENUM(OFF) ENUM(SEVERE) ENUM(WARNING) ENUM(INFO, 10) ENUM(DEBUG) ENUM(ALL) } namespace Example { ENUM_TYPE(long) ENUM(A) ENUM(B) ENUM(C, 20) ENUM(D) ENUM(E) ENUM(F) } int main(int argc, char** argv) { Level::Enum lvl = Level::WARNING; Example::Enum ex = Example::C; std::cout << lvl.value() << std::endl; //2 std::cout << ex.value() << std::endl; //20 }
Enum
is set to 0 inside each namespace declaration.
( Could someone point me out where ^^this behaviour^^ is mentioned on the standard? )
The preprocessor magic automates the declaration of enumerators.
enum
type, therefore not promotable to int This one sacrifices line numbering (not really) but can be used on switch cases .
#define ENUM_TYPE(x) using type = Enum #define ENUM(x) constexpr type x{__LINE__,#x} template struct Enum final { const T value; const char* name; constexpr operator const T() const noexcept {return value;} constexpr const char* operator&() const noexcept {return name;} };
#line 0
conflicts with -pedantic
on GCC and clang.
Either start at #line 1
and subtract 1 from __LINE__
.
Or, don't use -pedantic
.
And while we're at it, avoid VC++ at all costs, it has always been a joke of a comstackr.
#include namespace Level { ENUM_TYPE(short); #line 0 ENUM(OFF); ENUM(SEVERE); ENUM(WARNING); #line 10 ENUM(INFO); ENUM(DEBUG); ENUM(ALL); #line //restore the line numbering }; int main(int argc, char** argv) { std::cout << Level::OFF << std::endl; // 0 std::cout << &Level::OFF << std::endl; // OFF std::cout << Level::INFO << std::endl; // 10 std::cout << &Level::INFO << std::endl; // INFO switch(/* any integer or integer-convertible type */) { case Level::OFF: //... break; case Level::SEVERE: //... break; //... } return 0; }
r3dVoxel - Enum
r3dVoxel - ELoggingLevel
#line lineno -- cppreference.com
As long as you are okay with writing a separate .h/.cpp
pair for each queryable enum, this solution works with nearly the same syntax and capabilities as a regular c++ enum:
// MyEnum.h #include #ifndef ENUM_INCLUDE_MULTI #pragma once #end if enum MyEnum : int ETRAITS { EDECL(AAA) = -8, EDECL(BBB) = '8', EDECL(CCC) = AAA + BBB };
The .cpp
file is 3 lines of boilerplate:
// MyEnum.cpp #define ENUM_DEFINE MyEnum #define ENUM_INCLUDE #include
Exemple d’utilisation:
for (MyEnum value : EnumTraits::GetValues()) std::cout << EnumTraits ::GetName(value) << std::endl;
This solution requires 2 source files:
// EnumTraits.h #pragma once #include #include #include #define ETRAITS #define EDECL(x) x template class EnumTraits { public: static const std::vector& GetValues() { return values; } static ENUM GetValue(const char* name) { auto match = valueMap.find(name); return (match == valueMap.end() ? ENUM() : match->second); } static const char* GetName(ENUM value) { auto match = nameMap.find(value); return (match == nameMap.end() ? nullptr : match->second); } public: EnumTraits() = delete; using vector_type = std::vector ; using name_map_type = std::unordered_map; using value_map_type = std::unordered_map; private: static const vector_type values; static const name_map_type nameMap; static const value_map_type valueMap; }; struct EnumInitGuard{ constexpr const EnumInitGuard& operator=(int) const { return *this; } }; template constexpr T& operator<<=(T&& x, const EnumInitGuard&) { return x; }
...and
// EnumTraits.inl #define ENUM_INCLUDE_MULTI #include ENUM_INCLUDE #undef ETRAITS #undef EDECL using EnumType = ENUM_DEFINE; using TraitsType = EnumTraits; using VectorType = typename TraitsType::vector_type; using NameMapType = typename TraitsType::name_map_type; using ValueMapType = typename TraitsType::value_map_type; using NamePairType = typename NameMapType::value_type; using ValuePairType = typename ValueMapType::value_type; #define ETRAITS ; const VectorType TraitsType::values #define EDECL(x) EnumType::x <<= EnumInitGuard() #include ENUM_INCLUDE #undef ETRAITS #undef EDECL #define ETRAITS ; const NameMapType TraitsType::nameMap #define EDECL(x) NamePairType(EnumType::x, #x) <<= EnumInitGuard() #include ENUM_INCLUDE #undef ETRAITS #undef EDECL #define ETRAITS ; const ValueMapType TraitsType::valueMap #define EDECL(x) ValuePairType(#x, EnumType::x) <<= EnumInitGuard() #include ENUM_INCLUDE #undef ETRAITS #undef EDECL
This implementation exploits the fact that the braced list of elements of an enum definition can also be used as a braced initializer list for class member initialization.
When ETRAITS
is evaluated in the context of EnumTraits.inl
, it expands out to a static member definition for the EnumTraits<>
class.
The EDECL
macro transforms each enum member into initializer list values which subsequently get passed into the member constructor in order to populate the enum info.
The EnumInitGuard
class is designed to consume the enum initializer values and then collapse - leaving a pure list of enum data.
c++
-like syntax enum
and enum class
(*almost) enum
types with any numeric underlying type enum
types with automatic, explicit, and fragmented initializer values *
In contrast to enums
, initializers in enum class
types that reference other values from the same enum must have those values fully qualified
.h/.cpp
pair for each queryable enum
macro
and include
magic class
or namespace
scoped enums is nonsortingvial Intellisense will complain a bit about private member access when opening up EnumTraits.inl
, but since the expanded macros are actually defining class members, that isn't actually a problem.
The #ifndef ENUM_INCLUDE_MULTI
block at the top of the header file is a minor annoyance that could probably be shrunken down into a macro or something, but it's small enough to live with at its current size.
Declaring a namespace scoped enum requires that the enum first be forward declared inside its namespace scope, then defined in the global namespace. Additionally, any enum initializers using values of the same enum must have those values fully qualified.
namespace ns { enum MyEnum : int; } enum ns::MyEnum : int ETRAITS { EDECL(AAA) = -8, EDECL(BBB) = '8', EDECL(CCC) = ns::MyEnum::AAA + ns::MyEnum::BBB }
I wrote a library for solving this problem, everything happens in compiling time, except for getting the message.
Use macro DEF_MSG
to define a macro and message pair:
DEF_MSG(CODE_OK, "OK!") DEF_MSG(CODE_FAIL, "Fail!")
CODE_OK
is the macro to use, and "OK!"
is the corresponding message.
Use get_message()
or just gm()
to get the message:
get_message(CODE_FAIL); // will return "Fail!" gm(CODE_FAIL); // works exactly the same as above
Use MSG_NUM
to find out how many macros have been defined. This will automatically increse, you don’t need to do anything.
Predefined messages:
MSG_OK: OK MSG_BOTTOM: Message bottom
Project: libcodemsg
The library doesn’t create extra data. Everything happens in compiling time. In message_def.h
, it generates an enum
called MSG_CODE
; in message_def.c
, it generates a variable holds all the ssortingngs in static const char* _g_messages[]
.
In such case, the library is limited to create one enum
only. This is ideal for return values, for example:
MSG_CODE foo(void) { return MSG_OK; // or something else } MSG_CODE ret = foo(); if (MSG_OK != ret) { printf("%s\n", gm(ret);); }
Another thing I like this design is you can manage message definitions in different files.
I found the solution to this question looks much better.
#define ENUM_MAKE(TYPE, ...) \ enum class TYPE {__VA_ARGS__};\ struct Helper_ ## TYPE { \ static const Ssortingng& toName(TYPE type) {\ int index = static_cast(type);\ return splitSsortingngVec()[index];}\ static const TYPE toType(const Ssortingng& name){\ static std::unordered_map typeNameMap;\ if( typeNameMap.empty() )\ {\ const SsortingngVector& ssVec = splitSsortingngVec();\ for (size_t i = 0; i < ssVec.size(); ++i)\ typeNameMap.insert(std::make_pair(ssVec[i], static_cast(i)));\ }\ return typeNameMap[name];}\ static const SsortingngVector& splitSsortingngVec() {\ static SsortingngVector typeNameVector;\ if(typeNameVector.empty()) \ {\ typeNameVector = SsortingngUtil::split(#__VA_ARGS__, ",");\ for (auto& name : typeNameVector)\ {\ name.erase(std::remove(name.begin(), name.end(), ' '),name.end()); \ name = Ssortingng(#TYPE) + "::" + name;\ }\ }\ return typeNameVector;\ }\ }; using Ssortingng = std::ssortingng; using SsortingngVector = std::vector; SsortingngVector SsortingngUtil::split( const Ssortingng& str, const Ssortingng& delims, unsigned int maxSplits, bool preserveDelims) { SsortingngVector ret; // Pre-allocate some space for performance ret.reserve(maxSplits ? maxSplits+1 : 10); // 10 is guessed capacity for most case unsigned int numSplits = 0; // Use STL methods size_t start, pos; start = 0; do { pos = str.find_first_of(delims, start); if (pos == start) { // Do nothing start = pos + 1; } else if (pos == Ssortingng::npos || (maxSplits && numSplits == maxSplits)) { // Copy the rest of the ssortingng ret.push_back( str.substr(start) ); break; } else { // Copy up to delimiter ret.push_back( str.substr(start, pos - start) ); if(preserveDelims) { // Sometimes there could be more than one delimiter in a row. // Loop until we don't find any more delims size_t delimStart = pos, delimPos; delimPos = str.find_first_not_of(delims, delimStart); if (delimPos == Ssortingng::npos) { // Copy the rest of the ssortingng ret.push_back( str.substr(delimStart) ); } else { ret.push_back( str.substr(delimStart, delimPos - delimStart) ); } } start = pos + 1; } // parse up to next real data start = str.find_first_not_of(delims, start); ++numSplits; } while (pos != Ssortingng::npos); return ret; }
Exemple
ENUM_MAKE(MY_TEST, MY_1, MY_2, MY_3) MY_TEST s1 = MY_TEST::MY_1; MY_TEST s2 = MY_TEST::MY_2; MY_TEST s3 = MY_TEST::MY_3; Ssortingng z1 = Helper_MY_TEST::toName(s1); Ssortingng z2 = Helper_MY_TEST::toName(s2); Ssortingng z3 = Helper_MY_TEST::toName(s3); MY_TEST q1 = Helper_MY_TEST::toType(z1); MY_TEST q2 = Helper_MY_TEST::toType(z2); MY_TEST q3 = Helper_MY_TEST::toType(z3);
automatically ENUM_MAKE macro generate ‘enum class’ and helper class with ‘enum reflection function’.
In order to reduce mistakes, at once Everything is defined with only one ENUM_MAKE.
The advantage of this code is automatically created for reflection and a close look at macro code ,easy-to-understand code. ‘enum to ssortingng’ , ‘ssortingng to enum’ performance both is algorithm O(1).
Disadvantages is when first use , helper class for enum relection ‘s ssortingng vector and map is initialized. but If you want you’ll also be pre-initialized. –
my solution is without macro usage.
advantages:
disadvantages:
so… until the day that C++ implements the C# Enum.Parse functionality, I will be stuck with this:
#include enum class Language { unknown, Chinese, English, French, German // etc etc }; class Enumerations { public: static void fnInit(void); static std::unordered_map m_Language; static std::unordered_map m_invLanguage; private: static void fnClear(); static void fnSetValues(void); static void fnInvertValues(void); static bool m_init_done; }; std::unordered_map Enumerations::m_Language = std::unordered_map (); std::unordered_map Enumerations::m_invLanguage = std::unordered_map (); void Enumerations::fnInit() { fnClear(); fnSetValues(); fnInvertValues(); } void Enumerations::fnClear() { m_Language.clear(); m_invLanguage.clear(); } void Enumerations::fnSetValues(void) { m_Language[L"unknown"] = Language::unknown; m_Language[L"Chinese"] = Language::Chinese; m_Language[L"English"] = Language::English; m_Language[L"French"] = Language::French; m_Language[L"German"] = Language::German; // and more etc etc } void Enumerations::fnInvertValues(void) { for (auto it = m_Language.begin(); it != m_Language.end(); it++) { m_invLanguage[it->second] = it->first; } } // usage - //Language aLanguage = Language::English; //wssortingng sLanguage = Enumerations::m_invLanguage[aLanguage]; //wssortingng sLanguage = L"French" ; //Language aLanguage = Enumerations::m_Language[sLanguage];
Well, yet another option. A typical use case is where you need constants for the HTTP verbs as well as using its ssortingng version values.
The example:
int main () { VERB a = VERB::GET; VERB b = VERB::GET; VERB c = VERB::POST; VERB d = VERB::PUT; VERB e = VERB::DELETE; std::cout << a.toString() << std::endl; std::cout << a << std::endl; if ( a == VERB::GET ) { std::cout << "yes" << std::endl; } if ( a == b ) { std::cout << "yes" << std::endl; } if ( a != c ) { std::cout << "no" << std::endl; } }
The VERB class:
// ----------------------------------------------------------- // ----------------------------------------------------------- class VERB { private: // private constants enum Verb {GET_=0, POST_, PUT_, DELETE_}; // private ssortingng values static const std::ssortingng theSsortingngs[]; // private value const Verb value; const std::ssortingng text; // private constructor VERB (Verb v) : value(v), text (theSsortingngs[v]) { // std::cout << " constructor \n"; } public: operator const char * () const { return text.c_str(); } operator const std::string () const { return text; } const std::string toString () const { return text; } bool operator == (const VERB & other) const { return (*this).value == other.value; } bool operator != (const VERB & other) const { return ! ( (*this) == other); } // --- static const VERB GET; static const VERB POST; static const VERB PUT; static const VERB DELETE; }; const std::string VERB::theStrings[] = {"GET", "POST", "PUT", "DELETE"}; const VERB VERB::GET = VERB ( VERB::Verb::GET_ ); const VERB VERB::POST = VERB ( VERB::Verb::POST_ ); const VERB VERB::PUT = VERB ( VERB::Verb::PUT_ ); const VERB VERB::DELETE = VERB ( VERB::Verb::DELETE_ ); // end of file
What about a simple streaming overload? You still have to maintain the mapping if you don’t want to do some macro magic, but I find it cleaner than your original solution.
#include // for std::uint_fast8_t #include #include #include enum class MyEnum : std::uint_fast8_t { AAA, BBB, CCC, }; std::ostream& operator<<(std::ostream& str, MyEnum type) { switch(type) { case MyEnum::AAA: str << "AAA"; break; case MyEnum::BBB: str << "BBB"; break; case MyEnum::CCC: str << "CCC"; break; default: break; } return str; } int main() { std::cout << MyEnum::AAA <<'\n'; }