Fraxinus
16.5.0-fx-rc4
An IGT application
|
Class for easy conversion between an enum and a QString. More...
#include <cxEnumConverter.h>
Static Public Member Functions | |
static QString | enum2string (const ENUM &val) |
static ENUM | string2enum (const QString &val) |
Static Public Attributes | |
static boost::array< QString, COUNT > | mText |
Class for easy conversion between an enum and a QString.
NOTE: Use the helper macros instead of the description below.
Macro example:
namespace vm { enum VIEW_ZONE_TYPE { vtACS_3D_LAYOUT = 0, vtANYPLANE_LAYOUT = 1, vtDUALANYPLANE_LAYOUT = 2, vtCOUNT }; } // namespace vm Macro in header file (right after enum def, outside namespace): SNW_DECLARE_ENUM_STRING_CONVERTERS(vm, VIEW_ZONE_TYPE); Macro in cpp file (outside namespace): SNW_DEFINE_ENUM_STRING_CONVERTERS_BEGIN(vm, VIEW_ZONE_TYPE, vtCOUNT) { "ACS_3D_LAYOUT", "ANYPLANE_LAYOUT", "DUALANYPLANE_LAYOUT", } SNW_DEFINE_ENUM_STRING_CONVERTERS_END(vm, VIEW_ZONE_TYPE, vtCOUNT) End macro example.
Usage: Use the two nonmember functions enum2string() and string2enum() to convert between enum and string and back.
Implementation for a new enum: We use this one as an example: namespace vm { enum VIEW_ZONE_TYPE { vtACS_3D_LAYOUT = 0, vtANYPLANE_LAYOUT = 1, vtDUALANYPLANE_LAYOUT = 2, vtCOUNT }; } // vm
Define the mText array for your enum, for example
template<> boost::array<QString, vm::vtCOUNT> EnumConverter<vm::VIEW_ZONE_TYPE,vm::vtCOUNT>::mText = { { "ACS_3D_LAYOUT", "ANYPLANE_LAYOUT", "DUALANYPLANE_LAYOUT", }};
Then implement the two nonmember functions to call their respective member functions, as follows:
template<> QString enum2string<vm::VIEW_ZONE_TYPE>(const vm::VIEW_ZONE_TYPE& val) { return EnumConverter<vm::VIEW_ZONE_TYPE, vm::vtCOUNT>::enum2string(val); } template<> vm::VIEW_ZONE_TYPE string2enum<vm::VIEW_ZONE_TYPE>(const QString& val) { return EnumConverter<vm::VIEW_ZONE_TYPE, vm::vtCOUNT>::string2enum(val); }
Remember to do everything in the global namespace, and in a .cpp file.
You can also define operators << and >> based on these functions.
Definition at line 128 of file cxEnumConverter.h.
|
inlinestatic |
Definition at line 133 of file cxEnumConverter.h.
|
inlinestatic |
Definition at line 140 of file cxEnumConverter.h.
|
static |
Definition at line 131 of file cxEnumConverter.h.