Salome HOME
Update copyrights
[modules/yacs.git] / src / yacsloader / ExampleOfObserversPluginForDriver.cxx
1 // Copyright (C) 2016-2019  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 // Author : Anthony Geay (EDF R&D)
20
21
22 // This is an example of plugin implementation you can write to intercept events sent by YACS engine during execution
23 // 1 - Build your shared library with 2 symboles defined : DefineCustomObservers and CleanUpObservers (warning respect the API !).
24 // 2 - At run time set YACS_DRIVER_PLUGIN_PATH in your environement to the shared library
25
26 #include "Dispatcher.hxx"
27 #include "ForEachLoop.hxx"
28
29 #include <iostream>
30 #include <sstream>
31
32 class PluginObserver : public YACS::ENGINE::Observer
33 {
34 public:
35   PluginObserver(YACS::ENGINE::ForEachLoop *fe):_fe(fe) { }
36 private:
37   void notifyObserver(YACS::ENGINE::Node* object,const std::string& event);
38   void notifyObserver2(YACS::ENGINE::Node* object,const std::string& event, void *something);
39 private:
40   YACS::ENGINE::ForEachLoop *_fe;
41 };
42
43 void PluginObserver::notifyObserver(YACS::ENGINE::Node* object,const std::string& event)
44 {// Customize here !
45   std::cerr << "------------" << event << std::endl;
46 }
47
48 void PluginObserver::notifyObserver2(YACS::ENGINE::Node* object,const std::string& event, void *something)
49 {// Customize here !
50   std::ostringstream oss;
51   if(event=="progress_ok")
52     {
53       int itemOk(*reinterpret_cast<int *>(something));
54       oss << event << " " << itemOk;
55       std::cerr << oss.str() << std::endl;
56     }
57 }
58
59 class PluginObserverKeeper
60 {
61 public:
62   ~PluginObserverKeeper() { clean(); }
63   void clean() { for(std::vector<YACS::ENGINE::Observer *>::iterator it=_observers.begin();it!=_observers.end();it++) { delete *it; _disp->removeObserver(*it,_nc,_what); }  _observers.clear(); }
64   void registerObserver(YACS::ENGINE::Observer *newObs, YACS::ENGINE::ForEachLoop *nc, const std::string& what, YACS::ENGINE::Dispatcher *disp) { _what=what; _nc=nc; _disp=disp; _observers.push_back(newObs); std::cerr << "register @@@@@@@@@@@ " << _observers.size() << std::endl; }
65 private:
66   std::string _what;
67   YACS::ENGINE::ForEachLoop *_nc = nullptr;
68   std::vector<YACS::ENGINE::Observer *> _observers;
69   YACS::ENGINE::Dispatcher *_disp = nullptr;
70 };
71
72 PluginObserverKeeper pok;
73
74 #include "ForEachLoop.hxx"
75
76 extern "C"
77 {
78   void DefineCustomObservers(YACS::ENGINE::Dispatcher *disp, YACS::ENGINE::ComposedNode *rootNode, YACS::ENGINE::Executor *executor)
79   {// Customize here !
80     YACS::ENGINE::Node *n(rootNode->getChildByName("ForEachLoop_pyobj1"));
81     YACS::ENGINE::ForEachLoop *nc(dynamic_cast<YACS::ENGINE::ForEachLoop *>(n));
82     if(!nc)
83       throw YACS::Exception("Expect to have a ForEach node called ForEachLoop_pyobj1 !");
84     PluginObserver *myCustomObsever(new PluginObserver(nc));
85     constexpr char WHAT[]="progress_ok";
86     pok.registerObserver(myCustomObsever,nc,WHAT,disp);
87     disp->addObserver(myCustomObsever,nc,WHAT);
88   }
89   
90   void CleanUpObservers()
91   {// Customize here !
92   }
93 }
94