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