Version: 9.16.0
ExampleOfObserversPluginForDriver.cxx
Go to the documentation of this file.
1// Copyright (C) 2016-2026 CEA, EDF
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
33{
34public:
36private:
37 void notifyObserver(YACS::ENGINE::Node* object,const std::string& event);
38 void notifyObserver2(YACS::ENGINE::Node* object,const std::string& event, void *something);
39private:
41};
42
43void PluginObserver::notifyObserver(YACS::ENGINE::Node* object,const std::string& event)
44{// Customize here !
45 std::cerr << "------------" << event << std::endl;
46}
47
48void 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
60{
61public:
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; }
65private:
66 std::string _what;
68 std::vector<YACS::ENGINE::Observer *> _observers;
70};
71
73
74#include "ForEachLoop.hxx"
75
76extern "C"
77{
79 {// Customize here !
80 YACS::ENGINE::Node *n(rootNode->getChildByName("ForEachLoop_pyobj1"));
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
91 {// Customize here !
92 }
93}
94
PluginObserverKeeper pok
void DefineCustomObservers(YACS::ENGINE::Dispatcher *disp, YACS::ENGINE::ComposedNode *rootNode, YACS::ENGINE::Executor *executor)
std::vector< YACS::ENGINE::Observer * > _observers
void registerObserver(YACS::ENGINE::Observer *newObs, YACS::ENGINE::ForEachLoop *nc, const std::string &what, YACS::ENGINE::Dispatcher *disp)
PluginObserver(YACS::ENGINE::ForEachLoop *fe)
YACS::ENGINE::ForEachLoop * _fe
void notifyObserver(YACS::ENGINE::Node *object, const std::string &event)
void notifyObserver2(YACS::ENGINE::Node *object, const std::string &event, void *something)
Base class for all composed nodes.
Node * getChildByName(const std::string &name) const
Base class for dispatcher in observer pattern.
Definition: Dispatcher.hxx:74
virtual void addObserver(Observer *observer, Node *object, const std::string &event)
Definition: Dispatcher.cxx:125
virtual void removeObserver(Observer *observer, Node *object, const std::string &event)
Definition: Dispatcher.cxx:131
Threaded Executor.
Definition: Executor.hxx:63
Loop node for parametric calculation.
Base class for all nodes.
Definition: Node.hxx:70
Base class for observer in observer pattern.
Definition: Dispatcher.hxx:46