Salome HOME
Copyright update: 2016
[modules/yacs.git] / src / engine / Dispatcher.cxx
1 // Copyright (C) 2006-2016  CEA/DEN, EDF R&D
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
29 Observer::~Observer()
30 {
31 }
32
33 void Observer::notifyObserver(Node* object, const std::string& event)
34 {
35   DEBTRACE("notifyObserver " << event << object );
36 }
37
38 Dispatcher* Dispatcher::_singleton = 0;
39
40 Dispatcher::~Dispatcher()
41 {
42   Dispatcher::_singleton =0;
43 }
44
45 Dispatcher* Dispatcher::getDispatcher()
46 {
47   if ( !Dispatcher::_singleton )
48     {
49        Dispatcher::_singleton =new Dispatcher;
50     }
51   return Dispatcher::_singleton;
52 }
53
54 void Dispatcher::setDispatcher(Dispatcher* dispatcher)
55 {
56   Dispatcher::_singleton=dispatcher;
57 }
58
59 void Dispatcher::printObservers()
60 {
61   std::cerr << "Dispatcher::printObservers " << std::endl;
62   typedef std::map< std::pair<Node*,std::string> , std::set<Observer*> >::iterator it;
63   typedef std::set<Observer*>::iterator jt;
64
65   for(it i=_observers.begin();i!=_observers.end();i++)
66     {
67       std::cerr << "Node*: " << (*i).first.first << " event: " << (*i).first.second << std::endl;
68       for(jt j=(*i).second.begin();j!=(*i).second.end();j++)
69         {
70           std::cerr << "observer: " << *j << std::endl;
71         }
72     }
73 }
74
75 void Dispatcher::dispatch(Node* object, const std::string& event)
76 {
77   DEBTRACE("Dispatcher::dispatch " << event );
78   typedef std::set<Observer*>::iterator jt;
79   std::pair<Node*,std::string> key(object,event);
80   if(_observers.count(key) != 0)
81     {
82       for(jt iter=_observers[key].begin();iter!=_observers[key].end();iter++)
83         {
84           (*iter)->notifyObserver(object,event);
85         }
86     }
87 }
88
89 void Dispatcher::addObserver(Observer* observer,Node* object, const std::string& event)
90 {
91   _observers[std::pair<Node*,std::string>(object,event)].insert(observer);
92 //  printObservers();
93 }
94
95 void Dispatcher::removeObserver(Observer* observer,Node* object, const std::string& event)
96 {
97   _observers[std::pair<Node*,std::string>(object,event)].erase(observer);
98 //  printObservers();
99 }