| enum to string [message #11402] |
Fri, 07 September 2007 22:36  |
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 #11418 is a reply to message #11416 ] |
Sat, 08 September 2007 02:28   |
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 #17314 is a reply to message #11402 ] |
Fri, 05 October 2007 18:02   |
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  |
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
|
|
|