Version: 8.3.0
YACS::ENGINE::Dispatcher Class Reference

Base class for dispatcher in observer pattern. More...

#include <Dispatcher.hxx>

Inheritance diagram for YACS::ENGINE::Dispatcher:
Collaboration diagram for YACS::ENGINE::Dispatcher:

Public Member Functions

virtual void dispatch (Node *object, const std::string &event)
 
virtual void dispatch2 (Node *object, const std::string &event, void *something)
 
virtual void addObserver (Observer *observer, Node *object, const std::string &event)
 
virtual void removeObserver (Observer *observer, Node *object, const std::string &event)
 
virtual void printObservers ()
 
virtual ~Dispatcher ()
 

Static Public Member Functions

static DispatchergetDispatcher ()
 
static void setDispatcher (Dispatcher *dispatcher)
 

Protected Attributes

std::map< std::pair< Node
*, std::string >, std::set
< Observer * > > 
_observers
 

Static Protected Attributes

static Dispatcher_singleton = 0
 

Detailed Description

Base class for dispatcher in observer pattern.

Dispatcher and Observer objects can be used to be notified about events that occurs in editing or executing a calculation schema.

When an object wants to notify an event, it calls the dispatch method of the dispatcher with 2 arguments : the object reference and an event type. The dispatcher which is a singleton is obtained by calling the class method getDispatcher. The dispatcher notifies all the registered observers by calling their notifyObserver method.

Observers can be registered by calling the addObserver method with two arguments : an object reference and an event type. This observer will be notify with events coming only from this object.

Limitation : emitting objects can be only Node

Definition at line 72 of file Dispatcher.hxx.

Constructor & Destructor Documentation

Dispatcher::~Dispatcher ( )
virtual

Definition at line 45 of file Dispatcher.cxx.

References _singleton.

46 {
48 }

Member Function Documentation

void Dispatcher::addObserver ( Observer observer,
Node object,
const std::string &  event 
)
virtual

Definition at line 107 of file Dispatcher.cxx.

References _observers.

Referenced by DefineCustomObservers(), and YACS::HMI::SubjectNode::SubjectNode().

108 {
109  _observers[std::pair<Node*,std::string>(object,event)].insert(observer);
110 // printObservers();
111 }
void Dispatcher::dispatch ( Node object,
const std::string &  event 
)
virtual

Reimplemented in MyDispatcher, and YACS::ENGINE::SALOMEDispatcher.

Definition at line 80 of file Dispatcher.cxx.

References _observers, and DEBTRACE.

Referenced by YACS::ENGINE::Executor::sendEvent(), and YACS::ENGINE::Node::sendEvent().

81 {
82  DEBTRACE("Dispatcher::dispatch " << event );
83  std::pair<Node*,std::string> key(object,event);
84  std::map< std::pair<Node*,std::string> , std::set<Observer*> >::const_iterator it(_observers.find(key));
85  if(it!=_observers.end())
86  {
87  for(std::set<Observer*>::const_iterator iter=(*it).second.begin();iter!=(*it).second.end();iter++)
88  {
89  (*iter)->notifyObserver(object,event);
90  }
91  }
92 }
void Dispatcher::dispatch2 ( Node object,
const std::string &  event,
void *  something 
)
virtual

Definition at line 94 of file Dispatcher.cxx.

References _observers.

Referenced by YACS::ENGINE::Node::sendEvent2().

95 {
96  std::pair<Node*,std::string> key(object,event);
97  std::map< std::pair<Node*,std::string> , std::set<Observer*> >::const_iterator it(_observers.find(key));
98  if(it!=_observers.end())
99  {
100  for(std::set<Observer*>::const_iterator iter=(*it).second.begin();iter!=(*it).second.end();iter++)
101  {
102  (*iter)->notifyObserver2(object,event,something);
103  }
104  }
105 }
Dispatcher * Dispatcher::getDispatcher ( )
static
void Dispatcher::printObservers ( )
virtual

Definition at line 64 of file Dispatcher.cxx.

References _observers, and CORBAEngineTest::i.

65 {
66  std::cerr << "Dispatcher::printObservers " << std::endl;
67  typedef std::map< std::pair<Node*,std::string> , std::set<Observer*> >::iterator it;
68  typedef std::set<Observer*>::iterator jt;
69 
70  for(it i=_observers.begin();i!=_observers.end();i++)
71  {
72  std::cerr << "Node*: " << (*i).first.first << " event: " << (*i).first.second << std::endl;
73  for(jt j=(*i).second.begin();j!=(*i).second.end();j++)
74  {
75  std::cerr << "observer: " << *j << std::endl;
76  }
77  }
78 }
void Dispatcher::removeObserver ( Observer observer,
Node object,
const std::string &  event 
)
virtual

Definition at line 113 of file Dispatcher.cxx.

References _observers.

114 {
115  _observers[std::pair<Node*,std::string>(object,event)].erase(observer);
116 // printObservers();
117 }
void Dispatcher::setDispatcher ( Dispatcher dispatcher)
static

Member Data Documentation

std::map< std::pair<Node*,std::string> , std::set<Observer*> > YACS::ENGINE::Dispatcher::_observers
protected

Definition at line 84 of file Dispatcher.hxx.

Referenced by addObserver(), dispatch(), dispatch2(), printObservers(), and removeObserver().

Dispatcher * Dispatcher::_singleton = 0
staticprotected

Definition at line 85 of file Dispatcher.hxx.

Referenced by getDispatcher(), setDispatcher(), and ~Dispatcher().


The documentation for this class was generated from the following files: