Main Page | Directories | File List | File Members

EnumerationMap.h File Reference

Defines macros for enumeration mapping. More...


Defines

#define BEGIN_ENUMERATION_MAP(Type, FunctionName)
 Open the map.
#define ENUM_ENTRY(value, mapped)
 Create an entry in the map.
#define ENUM_DEFAULT(mapped)
 Create an default mapped value for the map.


Detailed Description

Defines macros for enumeration mapping.

Defines the following macros;


Define Documentation

#define BEGIN_ENUMERATION_MAP Type,
FunctionName   ) 
 

Value:

static const Type FunctionName(const int nEnum)     \
    {                                                   \
        switch(nEnum)                                   \
        {
Open the map.

Creates a function with one parameter of type int. The client code specifies via BEGIN_ENUMERATION_MAP what the function name should be and what type the mapped values are (i.e. the function return type).

The use of this macro must be followed by a matching END_ENUMERATION_MAP. Entries in the map are optional, but must reside between one BEGIN_ENUMERATION_MAP and a closing END_ENUMERATION_MAP.

Maps can be declared on the file scope or on class scope (in class declaration). That means that templated classes and (other) inline classes can also apply these macros.

example:

    // create a map where a bus state enumeration is mapped to textual descriptions
    // via function GetStateDesc(enum);
    BEGIN_ENUMERATION_MAP(char*, GetStateDesc)
        ENUM_ENTRY(CAN::BusStateOff,            "Offline")
        ENUM_ENTRY(CAN::BusStateErrorPassive,   "Error")
        ENUM_ENTRY(CAN::BusStateErrorWarning,   "Warning")
        ENUM_ENTRY(CAN::BusStateErrorActive,    "Online")
        ENUM_DEFAULT("Offline");
    END_ENUMERATION_MAP()
   
    // multiple maps can be created in the same file and on the same scope
    // providing that the function names are unique.
    BEGIN_ENUMERATION_MAP(COLORREF, GetStateColor)
        ENUM_DEFAULT(0x000000)
        ENUM_ENTRY(CAN::BusStateOff,            0x000000)
        ENUM_ENTRY(CAN::BusStateErrorPassive,   0x0000ff)
        ENUM_ENTRY(CAN::BusStateErrorWarning,   0x007fff)
        ENUM_ENTRY(CAN::BusStateErrorActive,    0x007f00)
    END_ENUMERATION_MAP()
   
    void CBusDialog::OnUpdateBusState(const int nBusState)
    {
        if (m_nBusState != nBusState)
        {
            m_nBusState = nBusState;
   
            CString strBusState = GetStateDesc(m_nBusState);    // get mapped description
            CDataExchange DX(this, FALSE);
            DDX_Text(&DX, IDC_BUS_STATE, strBusState);
        }
    }
   
    //and...
    void CBusDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
        HBRUSH hbr = _BaseClass::OnCtlColor(pDC, pWnd, nCtlColor);
        if (pWnd->GetDlgCtrlID() == IDC_BUS_STATE)
        {
            pDC->SetTextColor(GetStateColor(m_nNodeState));     // get mapped (text) color
        }
        return hbr;
    }

#define ENUM_DEFAULT mapped   ) 
 

Value:

default:                                        \
            return mapped;                              \
            break;
Create an default mapped value for the map.

If the client code queries for a mapped value using an enumeration value that is not explicitly mapped using ENUM_ENTRY the map will return this default value. The client code must ensure that the mapped value is 'compatible' with the type as specified in the BEGIN_ENUMERATION_MAP statement.

The use of the macro is optional, but if one is used it must reside between BEGIN_ENUMERATION_MAP and a closing END_ENUMERATION_MAP.

ENUM_DEFAULT can be placed anywhere in the map, be it first in front of all ENUM_ENTRY's or as last at the back.

If the map does not make use of this macro to catch unmapped enumerations, then the map will return a (cast-) value of 0x0.

see example in BEGIN_ENUMERATION_MAP

#define ENUM_ENTRY value,
mapped   ) 
 

Value:

case value:                                     \
            return mapped;                              \
            break;
Create an entry in the map.

Creates a mapped entry in the map. The client code must ensure that the mapped value is 'compatible' with the map as specified in the BEGIN_ENUMERATION_MAP statement.

The use of the macro is optional, but if one is used it must reside between BEGIN_ENUMERATION_MAP and a closing END_ENUMERATION_MAP.

see example in BEGIN_ENUMERATION_MAP


Generated on Fri Aug 12 16:59:03 2005 for EnumerationMap by  doxygen 1.4.4