Ok, latest version is the last one (I think).
I made a few minor changes to the implementation, as well as adding the map functionality in (which I guess I should have done from the beginning).
Now, its easy to get all enumerators' values (both explicit and implicit) from an enum, using the string names of the enumerators. Since I provide the methods to get a vector<string> namesOfEnumerators and a vector<int>valuesOfEnumerators as well as a map <string, int> enumeratorsMap, its quite easy to iterate through the enum, and retrieve a value without hand-coding all the enumerator names of the enum (which was the whole objective all along anyway).
A few notes:
1. The program is designed to operate on C++ enum source code alone. Hence it cannot determine the value of a variable at runtime in your program, so you cannot use any variable names in enumerator assignments, or it wont work.
2. You obviously should make sure your enum is valid first before passing the filepath to the file containing it to ConvertEnumToStrings.
3. Formatting shouldn't be a issue now. It should handle single-line enums and multi-line enums with ease, and anything in between.
4. Multiple enums with the same name in a single file are not handled that well. I wrote it so it'll handle the first enum in a file with a name, and then ignore later enums with the same name. This shouldn't be a problem though, as you really shouldn't be using multiple enums with the same name in a single file anyway.
5. Thanks to everyone at the boards here who helped me achieve my goal. I really appreciate it.
Here's a listing of the functions generated to file by ConvertEnumToStrings:
Code:
const vector<string>& getNamesOfEnumerators();
const vector<int>& getValuesOfEnumerators();
const string& getNameOfEnum();
const int& getNumOfEnumerators();
const map<string, int>& getEnumeratorsMap();
const int& getEnumeratorValue(enumName enumerator);
Note that all these functions are members of the relevant class generated for a specific enum. Each enum found in a file has its own class with its own functions generated for it. The classes' names will always look like this:
where "enumName" is the actual name of a given enum. So its easy to remember what the name of a generated enum class is, so you can create an object of it, and call the provided functions. So, say you have a file with the following enums in it:
Code:
enum Work {
hard = 1,
lazy = -1
};
//A little later in the file...
enum Day {
Sunday = 1,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
}currentDay;
ConvertEnumToStrings will generate a class called "CWorkToStrings" with the afore-mentioned member functions for the first enum it finds. And then it will generate a separate class for enum "Day" called "CDayToStrings" with its own functions which are given the same names as those functions in the other enum's class. This keeps everything nicely grouped and organized. So you can then just include the results file in your program, create an object of either class, and call its functions, and do whatever you want with the results after.
Cheers.
Oh, and I almost forgot...
Here's the new download link:
http://dl.dropbox.com/u/8029532/Conv...mToStrings.zip