1 // Copyright (C) 2016-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
19 // Author : Anthony Geay (EDF R&D)
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
26 #include "Dispatcher.hxx"
27 #include "ForEachLoop.hxx"
32 class PluginObserver : public YACS::ENGINE::Observer
35 PluginObserver(YACS::ENGINE::ForEachLoop *fe):_fe(fe) { }
37 void notifyObserver(YACS::ENGINE::Node* object,const std::string& event);
38 void notifyObserver2(YACS::ENGINE::Node* object,const std::string& event, void *something);
40 YACS::ENGINE::ForEachLoop *_fe;
43 void PluginObserver::notifyObserver(YACS::ENGINE::Node* object,const std::string& event)
45 std::cerr << "------------" << event << std::endl;
48 void PluginObserver::notifyObserver2(YACS::ENGINE::Node* object,const std::string& event, void *something)
50 std::ostringstream oss;
51 if(event=="progress_ok")
53 int itemOk(*reinterpret_cast<int *>(something));
54 oss << event << " " << itemOk;
55 std::cerr << oss.str() << std::endl;
59 class PluginObserverKeeper
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; }
67 YACS::ENGINE::ForEachLoop *_nc = nullptr;
68 std::vector<YACS::ENGINE::Observer *> _observers;
69 YACS::ENGINE::Dispatcher *_disp = nullptr;
72 PluginObserverKeeper pok;
74 #include "ForEachLoop.hxx"
78 void DefineCustomObservers(YACS::ENGINE::Dispatcher *disp, YACS::ENGINE::ComposedNode *rootNode, YACS::ENGINE::Executor *executor)
80 YACS::ENGINE::Node *n(rootNode->getChildByName("ForEachLoop_pyobj1"));
81 YACS::ENGINE::ForEachLoop *nc(dynamic_cast<YACS::ENGINE::ForEachLoop *>(n));
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);
90 void CleanUpObservers()