Is it possibleto configure enumator constants in C++? (hard problem)

W

Weiming Liu

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
 
W

White Wolf

Weiming said:
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?
Nope

Or can we solve it using other way instead of enum?

Yep. A class with member functions returning the values. But please
remember: those values will not be compile time constants anymore, so they
cannot be case labels etc.
 
J

Jerry Coffin

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?

Yes and no -- it's trivial to invent a file format something like:

Binary = 1
ByteGray = 2
ShortGray = 3

and write a bit of code that can read this and know that "Binary" has
the value 1 afterwards.

The problem is with what you can do with it afterwards -- or more
accurately, with what you CAN'T do with it. You can't use it as a
constant afterwards, so code like:

switch(image_type) {
case Binary:

Because as far as the program cares, Binary is a string that was read in
from a file, and the compiler doesn't know anything about the fact that
you're trying to use it as a constant with the value 1.

OTOH, there are alternatives to that -- for example, you can use an
std::map to associate the value with something like a pointer to an
object or a function, and when you read that value, you can look it up
in the map and find (and execute) what you've associated with the value.
Or can we solve it using other way instead of enum?

The question is what exact problem you're trying to solve.
 
D

Diego Giagio

Weiming Liu said:
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.image_type_text != 0; i++) {
if (ImageTypeTable.image_type_text == image_type_text)
return ImageTypeTable.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
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top