enum to string

S

surapong

Is there any easy way to extract the string from enum variable??

e.g. I have

enum TheEnum {ONE, TWO, THREE};

I would like to generate the array of string containing

{"ONE", "TWO", "THREE"}

Thanks in advance !!
Surapong
 
C

Clem.Dickey


Along the same lines, you can in general include a file multiple times
to get different views of the same data. Here's how I do it for the
enum problem. File t.h is a "database" of enum names and enum values:

t.h:

E(ONE,1)
E(TWO,2)
E(FOUR,4)
#undef E

t.C:

#include <map>
#include <string>
#include <iostream>

// Make the enum
#define E(x,y) x = y,
enum MyEnum {
#include "t.h"
};

typedef std::map< MyEnum, std::string> EnumMap;

// Make the initializer for the map of enums to strings
#define E(x,y) EnumMap::value_type(x,#x),
EnumMap::value_type init[] = {
#include "t.h"
};

// Initialize the map
EnumMap em( init, &init[sizeof init/sizeof init[0]]);

// Demonstrate
int main(void)
{
std::cout << em[ONE] << "\n";
std::cout << em[TWO] << "\n";
std::cout << em[FOUR] << "\n";
return 0;
}
 
G

Gianni Mariani

surapong said:
Is there any easy way to extract the string from enum variable??

e.g. I have

enum TheEnum {ONE, TWO, THREE};

I would like to generate the array of string containing

{"ONE", "TWO", "THREE"}

This is one of the reasons NOT to use enums

Another way is to declare a class.

// header file:
class TheEnum
{
const char * const m_name;

// private
TheEnum & operator=( const TheEnum & );
TheEnum( const TheEnum & );

protected:
TheEnum( const char * i_name )
: m_name( i_name )
{
}

public:
const char * str() const
{
return m_name;
}

// equality is comparing addresses.
bool operator==( const TheEnum & i_rhs ) const
{
return this == & i_rhs
}

// equality is comparing addresses.
bool operator<( const TheEnum & i_rhs ) const
{
// *warning* this one may be UB...
// if ordering is really needed, you might need
// to do something special
return this < & i_rhs
}
};

extern const TheEnum & ONE;
extern const TheEnum & TWO;
extern const TheEnum & THREE;


//............................

//define them in a cpp file.

struct TheEnum_Real : TheEnum
{
TheEnum_Real( const char * i_name )
: TheEnum( i_name )
{
}
};

static const TheEnum_Real sONE( "ONE" );
const TheEnum & ONE = sONE;
static const TheEnum_Real sTWO( "TWO" );
const TheEnum & TWO =TWO;
static const TheEnum_Real sTHREE( "THREE" );
const TheEnum & THREE = sTHREE;


// use as
#include <iostream>

int main()
{
std::cout << ONE.str() << "\n";
}

Anything else associated with TheEnum should be placed in the
TheEnum_Real class or other classes complete with virtual const methods
for dealing with any kind of operation needed.

It is not allways practical to do this for various reasons but this does
make maintaining the code much easier.
 
R

rssoftware

Is there any easy way to extract the string from enum variable??

e.g. I have

enum TheEnum {ONE, TWO, THREE};

I would like to generate the array of string containing

{"ONE", "TWO", "THREE"}
If you need something like that:
const char * TheEnumToString(TheEnum enumerator)
{
switch(enumerator)
{
case ONE:
return "ONE";
case TWO:
return "TWO";
case THREE:
return "THREE";
default:
return "UNKNOWN_ENUM_VALUE";;
}
};

Try this http://rssoftware.home.pl/index.php?id=20,0,0,1,0,0
 
L

Lance Diduck

Is there any easy way to extract the string from enum variable??

e.g. I have

enum TheEnum {ONE, TWO, THREE};

I would like to generate the array of string containing

{"ONE", "TWO", "THREE"}

Thanks in advance !!
Surapong

Here is yet another way -- this one uses iostreams and locales
enum Numbers{ One,Two,Three};
struct Numbershelper:std::locale::facet{
static std::locale::id id;
Numbershelper(int refs=0): std::locale::facet(refs){
std::vector<std::string > scratch;
scratch.push_back("One");
scratch.push_back("Two");
scratch.push_back("Three");
labels.insert(label_map::value_type("C",scratch));
scratch.clear();
scratch.push_back("Ein");
scratch.push_back("Zwei");
scratch.push_back("Drei");
labels.insert(label_map::value_type("German",scratch));
scratch.clear();
scratch.push_back("Un");
scratch.push_back("Deux");
scratch.push_back("Tres");
labels.insert(label_map::value_type("French",scratch));
}
std::string get(std::string lang,Numbers idx)const{
label_map::const_iterator it=labels.find(lang);
if(it==labels.end())
it=labels.find("C");
return it->second.at(idx);
}
typedef std::map<std::string,std::vector<std::string > > label_map;
label_map labels;
};
std::locale::id Numbershelper::id;

std::eek:stream&operator<<(std::eek:stream&os, Numbers n){
std::locale loc=os.getloc();
if(std::has_facet<Numbershelper>(loc)){
Numbershelper const &h=std::use_facet<Numbershelper>(loc);
os<<h.get(loc.name(),n);
}else
os<<n;
return os;
}

int main(int ,char**)
{
std::eek:stringstream str;
std::locale loc(str.getloc(),new Numbershelper);
str.imbue(loc);
str<<One;
std::string label=str.str();//your string
return 0;
}
This works, except that there are no portable locale names, like I
used here. So "German" and "French" would have to be replaced with
your systems names. The "C" locale is the default for all systems.

And this is the crux of why there really isn't a standard "enum to
string" facility. Anything standardized by ISO (Like C++) has to be
internationalized, and as you can see, this can be quite a chore.

Lance
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,600
Members
45,180
Latest member
CryptoTax Software
Top