CustusX
2023.01.05-dev+develop.0da12
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): DEFINE_ENUM_STRING_CONVERTERS_BEGIN(vm, VIEW_ZONE_TYPE, vtCOUNT) { "ACS_3D_LAYOUT", "ANYPLANE_LAYOUT", "DUALANYPLANE_LAYOUT", } 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 115 of file cxEnumConverter.h.
|
inlinestatic |
Definition at line 120 of file cxEnumConverter.h.
|
inlinestatic |
Definition at line 127 of file cxEnumConverter.h.
|
static |
Definition at line 118 of file cxEnumConverter.h.