17 #ifndef SURGSIM_FRAMEWORK_MESSENGER_H 18 #define SURGSIM_FRAMEWORK_MESSENGER_H 21 #include "SurgSim/Framework/Timer.h" 22 #include "SurgSim/Framework/Component.h" 24 #include <boost/thread/mutex.hpp> 47 Event(
const std::string& name,
const std::string&
sender,
double time,
const boost::any&
data) :
57 typedef std::function<void(const Event&)> EventCallback;
69 void publish(
const std::string& event,
const std::string&
sender,
const boost::any&
data = boost::any());
75 void publish(
const std::string& event,
76 const std::shared_ptr<Component>& sender,
77 const boost::any&
data = boost::any());
84 void subscribe(
const std::string& event,
const std::shared_ptr<SurgSim::Framework::Component>& subscriber,
85 const EventCallback& callback);
90 void subscribe(
const std::shared_ptr<SurgSim::Framework::Component>& subscriber,
91 const EventCallback& callback);
96 void unsubscribe(
const std::string& event,
const std::shared_ptr<SurgSim::Framework::Component>& subscriber);
100 void unsubscribe(
const std::shared_ptr<SurgSim::Framework::Component>& subscriber);
107 typedef std::pair<std::weak_ptr<SurgSim::Framework::Component>, EventCallback> Subscriber;
110 std::unordered_map<std::string, std::vector<Subscriber>> m_subscribers;
113 std::vector<Subscriber> m_universalSubscribers;
116 boost::mutex m_subscriberMutex;
119 std::vector<Event> m_events;
122 boost::mutex m_eventMutex;
125 void sendEvent(
const Event& event,
const std::vector<Subscriber>& receivers);
Messenger()
To receive events this is the format of the callback.
Definition: Messenger.cpp:57
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
double time
Name of the sender.
Definition: Messenger.h:53
void subscribe(const std::string &event, const std::shared_ptr< SurgSim::Framework::Component > &subscriber, const EventCallback &callback)
Subscribe to receiving events, when an event occurs that matches the event the callback function will...
Definition: Messenger.cpp:104
void publish(const std::string &event, const std::string &sender, const boost::any &data=boost::any())
Put an event onto the queue to be sent to all subscribers.
Definition: Messenger.cpp:93
boost::any data
Time the event is received.
Definition: Messenger.h:54
Datastructure to contain basic event data.
Definition: Messenger.h:45
Timer class, measures execution times.
Definition: Timer.h:31
std::string sender
Name of the event.
Definition: Messenger.h:52
void update()
Execute all the queued up callbacks.
Definition: Messenger.cpp:62
Messenger implements asynchronous communication to OSS, components can add themselves as subscribers ...
Definition: Messenger.h:40
void unsubscribe(const std::string &event, const std::shared_ptr< SurgSim::Framework::Component > &subscriber)
Unsubscribe from receiving specific events, prevent subscriber from receiving events of type 'event'...
Definition: Messenger.cpp:135