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