How can I avoid repeating myself when creating a C++ enum and a dependent data structure? [duplicate]

倖福魔咒の 提交于 2019-12-03 02:55:48

There is an old pre-processor trick for this:

Gadget.data

DEFINE_GADGET(First)
DEFINE_GADGET(Second)

Gadget.**

#define QUOTE_VAL(X)  #X

enum Gadget
{
#define DEFINE_GADGET(X)   X,
#include "Gadget.data"
#undef DEFINE_GADGET(X)
}; 

const char* gadget_debug_names[] = {
#define DEFINE_GADGET(X)   QUOTE_VAL(X),
#include "Gadget.data"
#undef DEFINE_GADGET(X)
};

Something that you might or might not want to do, first define what is to be defined, in this case enums and char arrays:

#define DEFENUM(v) v,
#define DEFENUM_last(v) v
#define DEFINE_ENUM(n, LIST) enum n { LIST(DEFENUM) }

#define DEFARR(v) #v,
#define DEFARR_last(v) #v
#define DEFINE_ARRAY(n, LIST) const char *n[] = { LIST(DEFARR) }

Then make your list with this format:

#define LETTERS(GEN) \
        GEN(aaa) \
        GEN(bbb) \
        GEN(ccc) \
        GEN(ddd) \
        GEN##_last(eee)

Or this one:

#define LETTERS(GEN) \
        GEN(aaa) GEN(bbb) GEN(ccc) GEN(ddd) GEN##_last(eee)

Finally create what you want to create:

DEFINE_ENUM(LettersEnum, LETTERS);
DEFINE_ARRAY(letters_array, LETTERS);

And this will be converted to:

enum LettersEnum { aaa, bbb, ccc, ddd, eee };
const char *letters_array[] = { "aaa", "bbb", "ccc", "ddd", "eee" };

Short example of what I use:

#include <iostream>
#include <cassert>
#include "boost/preprocessor.hpp"
#include "boost/algorithm/string/predicate.hpp"
#define ENUMIFY_FOREACH( r, data, elem ) \
    BOOST_PP_STRINGIZE( elem ) BOOST_PP_COMMA()


#define ENUMIFY( name, values ) \
struct name \
{ \
    static const unsigned int Size = BOOST_PP_SEQ_SIZE( values ); \
    typedef enum{ \
    BOOST_PP_SEQ_ENUM( values ) \
    } Values; \
    static const char* (&Mappings())[ Size ] \
    { \
    static const char* mappings[] = \
        { \
        BOOST_PP_SEQ_FOR_EACH( ENUMIFY_FOREACH, _, values ) \
        }; \
        return mappings; \
    }; \
    static const char* String( Values a_Val ) \
    { \
    return Mappings()[ static_cast< unsigned int >( a_Val ) ]; \
    } \
    static Values Value( const char* a_Key ) \
    { \
    for( unsigned int i = 0; i < Size; ++i ) \
    if( boost::iequals( a_Key, Mappings()[i] ) ) return static_cast< Values >( i ); \
    assert( 0 && "Didn't find the value of string " ); \
    return static_cast< Values >( 0 ); \
    } \
    \
};

ENUMIFY( SomeEnum, (Long)(Short)(Etc) );
int main()
{
    std::cout << SomeEnum::String( SomeEnum::Long ) << std::endl; // Outputs Long
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!