Skip to content

Instantly share code, notes, and snippets.

@dublindan
Created June 24, 2010 06:05
Show Gist options
  • Save dublindan/451051 to your computer and use it in GitHub Desktop.
Save dublindan/451051 to your computer and use it in GitHub Desktop.
class TestModule : public MessageListener
{
private:
// Instance variables which are modified by message handlers must be explicityly marked as mutable, since message handlers are const functions - this makes it obvious what is accessed.
mutable int value;
mutable const Mailbox& mailbox;
StateMachineMessageFunctor stateMachine;
// Message handlers
static void testHandler (const Message* const message, const Conversation con)
{
}
void fooHandler (const Foo* const message, const Conversation con) const
{
value = message->value;
}
public:
TestModule (MessagingAPI* api) :
mailbox(api->registerMailbox("test mailbox")),
stateMachine("state1")
{
const MessageID& foo_id = api->lookupMessageIdByName("foo");
api->registerHandler(GenericMessageFunctor(&TestModule::testHandler), foo_id);
api->registerHandler(TypedMessageFunctor<Foo>(this, &TestModule::fooHandler), foo_id);
api->registerHandler(*this, foo_id);
// Set up a two state state-machine
stateMachine.addState("init", GenericMessageFunctor(&TestModule::testHandler));
stateMachine.addState("state2", TypedMessageFunctor<Foo>(this, &TestModule::fooHandler));
api->registerHandler(stateMachine, foo_id);
}
void operator()(const Message* const message, const Conversation conversation) const
{
}
};
int main(int argc, char* argv[])
{
HostAPI api("main");
const MessageID foo_id = api.registerMessageType("foo", new Factory<Foo>());
const MessageID bar_id = api.registerMessageType("bar", new Factory<Foo>());
TestModule test(&api);
const MessageID foo_id = api.lookupMessageIdByName("foo");
// Send a message.
{
MessageSender<Foo> m(api, foo_id);
m.msg->value = 9;
}
// Send a message to a mailbox.
{
MessageSender<Foo> m(api, foo_id);
m.msg->value = 10;
m.mailbox = "test mailbox";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment