Message Class Macros

Description

For C++ programmers, SwitchKit provides macros that work with message classes instead of structures.

Syntax

The C++ syntax for a basic message is:

SKC_Message *SK_msg(const MsgStruct *, bool fromSwitch, int bufSz = 0);

Switch Statement

The received message class includes information describing what kind of message it is. You handle this information with the SKC_MSG_SWITCH macros. These macros mimic a switch statement, and allow different branches of code to be executed, depending on the type of message. Within the branch, a cast is automatically performed for you, allowing you to operate on a variable of the correct type. The switch statement must end with SKC_END_SWITCH.

Example for C++ with Predefined Macros

SK_Event *incomingEvent = getEvent();

SKC_Message *msg = incomingEvent->IncomingCMsg;

SKC_MSG_SWITCH(msg)

{

CASEC_ConnectAck(ca)

return processStatus(ca->getStatus());

CASEC_ChannelReleased(cr)

{

CleanUpChan(cr->getSpan(), cr->getChannel());

break;

}

CASEC_default

{

/*Unexpected msg!*/

break;

}

} SKC_END_SWITCH

 

In the SKC_MSG_SWITCH statement, "break" can be used normally, and the lack of a break statement causes execution to fall through to the next branch, just as a normal switch statement would. However, the default branch must be labeled with CASEC_default, as in the above example. The other important syntax difference from a normal switch statement is the lack of colons after the case label. The macro inserts the colons for you.

You can find the definitions of the marcros in the included file API_Funcs.h. Those macros are defined in terms of more primitive operations, which you can use instead of an SKC_MSG_SWITCH statement.

Steps to Translate Example to Straight C++ Code

1. Copy the message class.

2. Do the switch statement on the tag of the message.

3. For the first case, use TAG_ConnectAck for the tag of the acknowledgment to the connect message. There is no XLC_ConnectAck class because its acknowledgement is generic, so the message is cast to an XLC_AcknowledgeMessage. It is then stored in the ca variable, which is used in the body of the case clause.

4. Use the next case for the XLC_ChannelReleased message. This time, the message is cast to a class that is put in cr.

5. You don’t use the default clause for anything in this example.

Example for C without Predefined macros

SKC_Message *sk_saved_msg = msg;

switch(sk_saved_msg -> getMsgTag())

{

case(TAG_ConnectAck):

{

XLC_AcknowledgeMessage *ca =
(XLC_AcknowledgeMessage *)sk_saved_msg;

return processStatus(ca->getStatus());

}

case(TAG_ChannelReleased):

{

XLC_ChannelReleased *cr =

(XLC_ChannelReleased *)sk_saved_msg;

cleanUpChan(cr->getSpan, cr->getChannel);

break;

}

default:

break;

}