"Weiming Liu" <wl**@email.byu.edu> wrote in message news:<bk***********@acs2.byu.edu>...
Suppose to declare a enum type:
enum ImageType
{
Binary,
ByteGray,
ShortGray,
...
}
Is it possible to get the enum constant name like "Binary" from a
configuration file so that
I can change it later?
Or can we solve it using other way instead of enum?
Thanks.
Weiming
If i understood what you wanting to do, i would:
<code>
#include <iostream>
#include <string>
enum ImageType
{
Unknown,
Binary,
ByteGray,
ShortGray
};
typedef struct {
const char *image_type_text;
ImageType image_type;
} ImageTypeTable_t;
static ImageTypeTable_t ImageTypeTable[] = {
{ "Binary", Binary },
{ "ByteGray", ByteGray },
{ "ShortGray", ShortGray },
{ 0, Unknown },
};
ImageType ImageTypeFromText(std::string image_type_text) {
int i;
for (i=0; ImageTypeTable[i].image_type_text != 0; i++) {
if (ImageTypeTable[i].image_type_text == image_type_text)
return ImageTypeTable[i].image_type;
}
return Unknown;
}
int main(void)
{
std::cout << ImageTypeFromText ("Binary") << std::endl;
std::cout << ImageTypeFromText ("ByteGray") << std::endl;
std::cout << ImageTypeFromText ("ShortGray") << std::endl;
std::cout << ImageTypeFromText ("XyzXyz123") << std::endl;
return 0;
}
</code>
Diego Giagio