CustusX  2021.04.21-dev+develop.337f9
An IGT application
EnumConverter< ENUM, COUNT > Class Template Reference

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
 

Detailed Description

template<class ENUM, unsigned COUNT>
class EnumConverter< ENUM, COUNT >

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.

Member Function Documentation

template<class ENUM , unsigned COUNT>
static QString EnumConverter< ENUM, COUNT >::enum2string ( const ENUM &  val)
inlinestatic

Definition at line 120 of file cxEnumConverter.h.

template<class ENUM , unsigned COUNT>
static ENUM EnumConverter< ENUM, COUNT >::string2enum ( const QString &  val)
inlinestatic

Definition at line 127 of file cxEnumConverter.h.

Member Data Documentation

template<class ENUM , unsigned COUNT>
boost::array<QString, COUNT> EnumConverter< ENUM, COUNT >::mText
static

Definition at line 118 of file cxEnumConverter.h.


The documentation for this class was generated from the following file: