Version: 9.15.0
Dispatcher.cxx
Go to the documentation of this file.
1 // Copyright (C) 2006-2025 CEA, EDF
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 
20 #include "Dispatcher.hxx"
21 #include "Node.hxx"
22 #include <iostream>
23 
24 //#define _DEVDEBUG_
25 #include "YacsTrace.hxx"
26 
27 using namespace YACS::ENGINE;
28 
30 {
31 }
32 
33 void Observer::notifyObserver(Node* object, const std::string& event)
34 {
35  DEBTRACE("notifyObserver " << event << object );
36 }
37 
38 void Observer::notifyObserver2(Node* object,const std::string& event, void *something)
39 {
40  DEBTRACE("notifyObserver2 " << event << object );
41 }
42 
43 void Observer::notifyObserverFromClone(Node *originalInstance, const std::string& event, Node *clonedInstanceGeneratingEvent)
44 {
45  DEBTRACE("notifyObserverFromClone " << event << originalInstance );
46 }
47 
49 
51 {
53 }
54 
56 {
58  {
60  }
62 }
63 
65 {
67 }
68 
70 {
71  std::cerr << "Dispatcher::printObservers " << std::endl;
72  typedef std::map< std::pair<Node*,std::string> , std::set<Observer*> >::iterator it;
73  typedef std::set<Observer*>::iterator jt;
74 
75  for(it i=_observers.begin();i!=_observers.end();i++)
76  {
77  std::cerr << "Node*: " << (*i).first.first << " event: " << (*i).first.second << std::endl;
78  for(jt j=(*i).second.begin();j!=(*i).second.end();j++)
79  {
80  std::cerr << "observer: " << *j << std::endl;
81  }
82  }
83 }
84 
85 void Dispatcher::dispatch(Node* object, const std::string& event)
86 {
87  DEBTRACE("Dispatcher::dispatch " << event );
88  std::pair<Node*,std::string> key(object,event);
89  std::map< std::pair<Node*,std::string> , std::set<Observer*> >::const_iterator it(_observers.find(key));
90  if(it!=_observers.end())
91  {
92  for(std::set<Observer*>::const_iterator iter=(*it).second.begin();iter!=(*it).second.end();iter++)
93  {
94  (*iter)->notifyObserver(object,event);
95  }
96  }
97 }
98 
99 void Dispatcher::dispatch2(Node* object,const std::string& event, void *something)
100 {
101  std::pair<Node*,std::string> key(object,event);
102  std::map< std::pair<Node*,std::string> , std::set<Observer*> >::const_iterator it(_observers.find(key));
103  if(it!=_observers.end())
104  {
105  for(std::set<Observer*>::const_iterator iter=(*it).second.begin();iter!=(*it).second.end();iter++)
106  {
107  (*iter)->notifyObserver2(object,event,something);
108  }
109  }
110 }
111 
112 void Dispatcher::dispatchFromClone(Node *originalInstance, const std::string& event, Node *clonedInstanceGeneratingEvent)
113 {
114  std::pair<Node*,std::string> key(originalInstance,event);
115  std::map< std::pair<Node*,std::string> , std::set<Observer*> >::const_iterator it(_observers.find(key));
116  if(it!=_observers.end())
117  {
118  for(std::set<Observer*>::const_iterator iter=(*it).second.begin();iter!=(*it).second.end();iter++)
119  {
120  (*iter)->notifyObserverFromClone(originalInstance,event,clonedInstanceGeneratingEvent);
121  }
122  }
123 }
124 
125 void Dispatcher::addObserver(Observer* observer,Node* object, const std::string& event)
126 {
127  _observers[std::pair<Node*,std::string>(object,event)].insert(observer);
128 // printObservers();
129 }
130 
131 void Dispatcher::removeObserver(Observer* observer,Node* object, const std::string& event)
132 {
133  _observers[std::pair<Node*,std::string>(object,event)].erase(observer);
134 // printObservers();
135 }
#define DEBTRACE(msg)
Definition: YacsTrace.hxx:31
Base class for dispatcher in observer pattern.
Definition: Dispatcher.hxx:74
virtual void dispatchFromClone(Node *originalInstance, const std::string &event, Node *clonedInstanceGeneratingEvent)
Definition: Dispatcher.cxx:112
virtual void printObservers()
Definition: Dispatcher.cxx:69
virtual void addObserver(Observer *observer, Node *object, const std::string &event)
Definition: Dispatcher.cxx:125
static Dispatcher * _singleton
Definition: Dispatcher.hxx:87
virtual void dispatch(Node *object, const std::string &event)
Definition: Dispatcher.cxx:85
std::map< std::pair< Node *, std::string >, std::set< Observer * > > _observers
Definition: Dispatcher.hxx:86
virtual void removeObserver(Observer *observer, Node *object, const std::string &event)
Definition: Dispatcher.cxx:131
static Dispatcher * getDispatcher()
Definition: Dispatcher.cxx:55
virtual void dispatch2(Node *object, const std::string &event, void *something)
Definition: Dispatcher.cxx:99
static void setDispatcher(Dispatcher *dispatcher)
Definition: Dispatcher.cxx:64
Base class for all nodes.
Definition: Node.hxx:70
Base class for observer in observer pattern.
Definition: Dispatcher.hxx:46
virtual void notifyObserverFromClone(Node *originalInstance, const std::string &event, Node *clonedInstanceGeneratingEvent)
Definition: Dispatcher.cxx:43
virtual void notifyObserver(Node *object, const std::string &event)
Definition: Dispatcher.cxx:33
virtual void notifyObserver2(Node *object, const std::string &event, void *something)
Definition: Dispatcher.cxx:38
dispatcher
Definition: CItems.py:28