1 // Copyright (C) 2006-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "Dispatcher.hxx"
25 #include "YacsTrace.hxx"
27 using namespace YACS::ENGINE;
33 void Observer::notifyObserver(Node* object, const std::string& event)
35 DEBTRACE("notifyObserver " << event << object );
38 void Observer::notifyObserver2(Node* object,const std::string& event, void *something)
40 DEBTRACE("notifyObserver2 " << event << object );
43 Dispatcher* Dispatcher::_singleton = 0;
45 Dispatcher::~Dispatcher()
47 Dispatcher::_singleton =0;
50 Dispatcher* Dispatcher::getDispatcher()
52 if ( !Dispatcher::_singleton )
54 Dispatcher::_singleton =new Dispatcher;
56 return Dispatcher::_singleton;
59 void Dispatcher::setDispatcher(Dispatcher* dispatcher)
61 Dispatcher::_singleton=dispatcher;
64 void Dispatcher::printObservers()
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;
70 for(it i=_observers.begin();i!=_observers.end();i++)
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++)
75 std::cerr << "observer: " << *j << std::endl;
80 void Dispatcher::dispatch(Node* object, const std::string& event)
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())
87 for(std::set<Observer*>::const_iterator iter=(*it).second.begin();iter!=(*it).second.end();iter++)
89 (*iter)->notifyObserver(object,event);
94 void Dispatcher::dispatch2(Node* object,const std::string& event, void *something)
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())
100 for(std::set<Observer*>::const_iterator iter=(*it).second.begin();iter!=(*it).second.end();iter++)
102 (*iter)->notifyObserver2(object,event,something);
107 void Dispatcher::addObserver(Observer* observer,Node* object, const std::string& event)
109 _observers[std::pair<Node*,std::string>(object,event)].insert(observer);
113 void Dispatcher::removeObserver(Observer* observer,Node* object, const std::string& event)
115 _observers[std::pair<Node*,std::string>(object,event)].erase(observer);