Home » Coding » c++ » enum to string
enum to string [message #11402] Fri, 07 September 2007 22:36 Go to next message
surapong  is currently offline surapong
Messages: 2
Registered: September 2007
Junior Member
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
Re: enum to string [message #11416 is a reply to message #11402 ] Sat, 08 September 2007 02:03 Go to previous messageGo to next message
comeau  is currently offline comeau
Messages: 19
Registered: August 2007
Junior Member
In article <1189219005.343422.194990@o80g2000hse.googlegroups.com>,
surapong <boheman@gmail.com> wrote:
>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"}

Check out http://www.comeaucomputing.com/techtalk/#enumtostring
--
Greg Comeau / 4.3.9 with C++0xisms now in beta!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Re: enum to string [message #11418 is a reply to message #11416 ] Sat, 08 September 2007 02:28 Go to previous messageGo to next message
Clem.Dickey  is currently offline Clem.Dickey
Messages: 1
Registered: September 2007
Junior Member
On Sep 7, 11:03 pm, com...@panix.com (Greg Comeau) wrote:
> In article <1189219005.343422.194...@o80g2000hse.googlegroups.com>,
>
> surapong <bohe...@gmail.com> wrote:
> >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"}
>
> Check outhttp://www.comeaucomputing.com/techtalk/#enumtostring

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;
}
Re: enum to string [message #11438 is a reply to message #11402 ] Sat, 08 September 2007 07:33 Go to previous messageGo to next message
Gianni Mariani  is currently offline Gianni Mariani
Messages: 153
Registered: July 2007
Senior Member
surapong wrote:
> 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.
Re: enum to string [message #11992 is a reply to message #11438 ] Mon, 10 September 2007 14:31 Go to previous messageGo to next message
surapong  is currently offline surapong
Messages: 2
Registered: September 2007
Junior Member
Thank very much for both replies.. Really appreciated !!!

Surapong L.
Re: enum to string [message #17314 is a reply to message #11402 ] Fri, 05 October 2007 18:02 Go to previous messageGo to next message
rssoftware  is currently offline rssoftware
Messages: 1
Registered: October 2007
Junior Member
On 8 Wrz, 04:36, surapong <bohe...@gmail.com> wrote:
> 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
Re: enum to string [message #17352 is a reply to message #11402 ] Sat, 06 October 2007 11:35 Go to previous message
Lance Diduck  is currently offline Lance Diduck
Messages: 1
Registered: October 2007
Junior Member
On Sep 7, 10:36 pm, surapong <bohe...@gmail.com> wrote:
> 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::ostream&operator<<(std::ostream&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::ostringstream 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
Previous Topic:Elementary question on preprocessors and xlw
Next Topic:Re: class method static variable same across isntances?
Goto Forum:
  


Current Time: Fri May 16 02:59:43 EDT 2008

Total time taken to generate the page: 0.23260 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 2.7.7.
Copyright ©2001-2007 FUD Forum Bulletin Board Software