This example shows the most common use case: the class Choices has an
enumerated type and the TYPE_MAP static data member will store textual
representations of each enumerated value. Note that we ensure the
names line up properly by forcing the first value of the enumeration
to be zero.
class Choices {
// public constants
//
enum TYPES {FULL = 0, TRIANGULAR, SPARSE, SYMMETRIC, DIAGONAL};
static NameMap TYPE_MAP;
};
// the instantiation is outside the header file, probably at the
// bottom of the default constructor's file
//
NameMap Choices::TYPE_MAP(L"FULL, TRIANGULAR, SPARSE, SYMMETRIC, DIAGONAL");
boolean OtherClass::doSomething() {
Choices::TYPES type = Choices::FULL;
SysString output(L"they selected choice ");
// cast the enum to a long integer to get the index
//
output.concat(Choices::TYPE_MAP.getName((long)type);
Console::put(output);
}
The writeElementData and readElementData exist to simplify the i/o
methods of classes that have enums as internal data.
AudioFile {
enum FILE_TYPE { RAW, WAV, SPHERE, DEF_FILE_TYPE = RAW };
enum COMP_TYPE { LINEAR, ULAW, ALAW, DEF_COMP_TYPE = LINEAR };
static NameMap FILE_TYPE_MAP;
static NameMap COMP_TYPE_MAP;
static const SysString PARAM_FILE_TYPE;
static const SysString PARAM_COMP_TYPE;
static const SysString PARAM_SAMPLE_FREQUENCY;
FILE_TYPE file_type_d; // file type
COMP_TYPE comp_type_d; // compression type
Double sample_freq_d; // sample frequency
}
boolean AudioFile::writeData(...) {
FILE_TYPE_MAP.writeElementData(sof_a, PARAM_FILE_TYPE, (long)file_type_d);
COMP_TYPE_MAP.writeElementData(sof_a, PARAM_COMP_TYPE, (long)comp_type_d);
sample_freq_d.writeData(sof_a, PARAM_SAMPLE_FREQUENCY);
...
}
boolean AudioFile::readData(...) {
SofParser parser;
parser.load(sof_a, size_a);
// read the file type
//
FILE_TYPE ftype;
if (parser.isPresent(sof_a, PARAM_FILE_TYPE)) {
if (!FILE_TYPE_MAP.readElementData((long&)ftype, sof_a,
PARAM_FILE_TYPE,
parser.getEntry(sof_a, PARAM_FILE_TYPE))) {
return Error::handle(name(), L"readData", Error::READ,
__FILE__, __LINE__, Error::WARNING);
}
}
else {
ftype = DEF_FILE_TYPE;
}
// read the sample frequency
//
if (parser.isPresent(sof_a, PARAM_SAMPLE_FREQUENCY)) {
if (!sample_freq_d.readData(sof_a,
parser.getEntry(sof_a,
PARAM_SAMPLE_FREQUENCY))) {
return Error::handle(name(), L"readData", Error::READ,
__FILE__, __LINE__, Error::WARNING);
}
}
else {
sample_freq_d.assign(DEF_SAMPLE_FREQ);
}
...
}
Through the NameMap's methods you can simulate an enumeration's
I/O as though it were part of a normal object.