Gorgon Game Engine

Gorgon Library supports stream-able, parsable and enumerable enumerations.

A single macro call with enum type and enum value - name pairs is all that is required. There is no need to modify original enum definition. Additionally, this system allows multiple names for a single enum value which will be used while parsing a string to the enum type. If there are multiple names, first one is used while converting enum to string.

There are some restrictions to the use of macros supplied with this system. First restrictions is that the macro call should be done in the same namespace as the original enum. Both regular enum and enum class is supported. For a simple namespace enum or enum class, DefineEnumStrings macro should be used. This macro requires only the name of the enum along with the value - name pairs. Enum type supplied to the DefineEnumStrings macro cannot contain scope resolution operator. This eliminates the possibility of using class member enums with this macro. However, for this specific purpose, DefineEnumStringsCM macro is created. This macro allows class member enums to be used by suppling class name as the first parameter.

String::Parse() method requires name of the type, which is automatically deduced from the typename. If this is not desired DefineEnumStringsTN macro could be used. This macro has a parameter after the type which will be used as the type name. Additionally DefineEnumStringsCMTN is also defined to be used with class member enums.

Example

#include <iostream>
#include <Gorgon/Enum.h>
//...
enum class Days {
Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
};
{Days::Monday, "Monday"},
{Days::Tuesday, "Tuesday"},
{Days::Wednesday, "Wednesday"},
{Days::Thursday, "Thursday"},
{Days::Friday, "Friday"},
{Days::Saturday, "Saturday"},
{Days::Sunday, "Sunday"}
{Days::Monday, "Mon"},
//...
);
//...
Days day;
std::cin>>day; //user enters "mon"
std::cout<<"Today is "<<day<<std::endl; //output will be "Today is Monday"
std::getline(cin, day); //this is also possible
try {
Gorgon::String::Parse<Days>("not valid");
}
catch(const Gorgon::String::ParseError &ex) {
std::cout<<ex.what();<<std::endl;
std::cout<<"Days of week are: "<<std::endl;
for(auto e : Gorgon::Enumerate<Days>()) {
std::cout<<e<<std::endl;
}
}
//...
DefineEnumStrings
#define DefineEnumStrings(type,...)
This macro converts a regular C++ enumeration in to an enumerable, stream-able, and parsable enumerat...
Definition: Enum.h:190
Enum.h
contains Enum class that allows C++ enums to have string capabilities.
Gorgon::String::ParseError
This error will be thrown if a parsing function encounters with a general error.
Definition: Exceptions.h:10
std::getline
std::enable_if< decltype(gorgon__enum_tr_loc(T_()))::isupgradedenum, std::istream & >::type getline(std::istream &in, T_ &e)
Stream reader for upgraded enums.
Definition: Enum.h:350