-
Notifications
You must be signed in to change notification settings - Fork 0
Home
This is an implementation of messaging design pattern that allows zero-overhead message passing between modules. Each messaging system is made up of an instance of messenger, a set of module instances, and a set of messages.
The messenger handles the message passing to and between modules. Each messaging system should have exactly one messenger. Messenger types is a generic type that should be specialized using the types of modules. The messenger handles the instantiation of each module and calls the default constructor for each module. Messenger is defined in messenger.inl file. The following example demonstrates an example of messenger type specialization.
#pragma once
// The definition of Messenger::Messenger
#include "messenger.inl"
// The definition of Test::ModuleA
#include "test-module-a.inl"
// The definition of Test::ModuleB
#include "test-module-b.inl"
namespace Test
{
// The specialization of Messenger for our use with proper modules.
typedef Messenger::Messenger<Test::ModuleA, Test::ModuleB> TestMessenger;
}The following example demonstrates the instantiation of a messenger and the process of passing a message to messenger. It also demonstrates an access to module. The access to a module is done via the index of the module using the get_module method as demonstrated below.
// Includes the Test::TestMessenger type for the messenger.
#include "test-messenger.inl"
// The entery point of the program.
int main()
{
// Instantiation of messenger.
Test::TestMessenger test_messenger;
// Pass an int message to messenger.
test_messenger.pass_message(12);
// Example of and accessing a module.
std::cout << "The last int message Module A recieved had value: " << test_messenger.get_module<0>().last_recieved_message << std::endl;
return 0;
}