GranOO  3.0
A robust and versatile workbench to build 3D dynamic simulations based on the Discrete Element Method
PlugIn.hpp
Go to the documentation of this file.
1 // This file is part of GranOO, a workbench for DEM simulation.
2 //
3 // Author(s) : - Damien Andre IRCER/UNILIM, Limoges France
4 // <damien.andre@unilim.fr>
5 // - Jean-luc Charles Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
6 // <jean-luc.charles@ensam.eu>
7 // - Jeremie Girardot Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
8 // <jeremie.girardot@ensam.eu>
9 // - Cedric Hubert LAMIH/UPHF, Valenciennes France
10 // <cedric.hubert@uphf.fr>
11 // - Ivan Iordanoff Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
12 // <ivan.iordanoff@ensam.eu>
13 //
14 // Copyright (C) 2008-2019 D. Andre, JL. Charles, J. Girardot, C. Hubert, I. Iordanoff
15 //
16 // This program is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 // This program is distributed in the hope that it will be useful,
22 // but WITHOUT ANY WARRANTY; without even the implied warranty of
23 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 // GNU General Public License for more details.
25 //
26 // You should have received a copy of the GNU General Public License
27 // along with this program. If not, see <http://www.gnu.org/licenses/>.
28 
29 
30 #ifndef _GranOO_libCore_PlugIn_hpp_
31 #define _GranOO_libCore_PlugIn_hpp_
32 
33 #include <vector>
34 #include <map>
35 #include <functional>
36 #include <chrono>
37 
40 #include "GranOO3/Core/Macro.hpp"
41 
42 //
43 // 1- PlugIn is used as a base class for all plugins.
44 // * The static map "all_" contains all PlugIn
45 // * The first time the the PlugIn is called by the run_and_init() method,
46 // the virtual method init() is called. It could be used to initialize the PlugIn.
47 //
48 // 2- PlugInInterface<T> is an interface between base class "PlugIn" and child class "T".
49 // Static inheritance is introduced to store PlugIn with its real type, see :
50 // * The static array "all_" contains all instance
51 // * The static method "get()" allow to grab an PlugIn instance.
52 //
53 
54 
55 // Macros that declare and register a standard or custom GranOO plugin
56 #define DECLARE_STD_GRANOO_PLUGIN(T) \
57 public: \
58  static const bool register_granoo_plugin_; \
59  static const std::string class_ID() {return std::string(#T);}
60 
61 #define DECLARE_STD_GRANOO_PLUGIN_ID(CLASSID) \
62 public: \
63  static const bool register_granoo_plugin_; \
64  static const std::string class_ID() {return std::string(CLASSID);}
65 
66 #define DECLARE_CUSTOM_GRANOO_PLUGIN(T) \
67 public: \
68  static const bool register_granoo_plugin_; \
69  static const std::string class_ID() {return std::string(#T);}
70 
71 #define DECLARE_CUSTOM_GRANOO_PLUGIN_ID(CLASSID) \
72 public: \
73  static const bool register_granoo_plugin_; \
74  static const std::string class_ID() {return std::string(CLASSID);}
75 
76 // Macros that register plugin, with the possibility to add a description (*_DESC* macro)
77 #define REGISTER_GRANOO_PLUGIN(T) \
78  const bool T::register_granoo_plugin_ = Core::PlugInInterface<T>::record(__FILE__);
79 
80 #define REGISTER_GRANOO_PLUGIN_DESC(T, desc) \
81  const bool T::register_granoo_plugin_ = Core::PlugInInterface<T>::record(__FILE__, desc);
82 
83 #define REGISTER_GRANOO_PLUGIN_T(T) \
84  template<> const bool T::register_granoo_plugin_ = Core::PlugInInterface<T>::record(__FILE__);
85 
86 #define REGISTER_GRANOO_PLUGIN_DESC_T(T, desc) \
87  template<> const bool T::register_granoo_plugin_ = Core::PlugInInterface<T>::record(__FILE__, desc);
88 
89 
90 
91 namespace GranOO3
92 {
93  namespace Math
94  {
95  class Expression;
96  }
97  namespace Core
98  {
99 
100  class PlugIn
101  {
102  public:
103  static PlugIn& get(const std::string& id, unsigned int rank = 0);
104  static std::map< std::string, std::vector<PlugIn* > > get_all_plugin();
105  static std::vector<PlugIn* > get_all_plugin_vector();
106  static bool exist(const std::string& id, unsigned int rank = 0);
107  static const std::string class_ID() {return std::string("PlugIn");}
108 
109 
110  PlugIn(const std::string& id);
111  virtual ~PlugIn();
112  const std::string & get_ID() const {return _ID;}
113 
114  void verbose();
115  void run_and_init();
116  void trigger_signal_run();
117  double get_elapsed_time();
118  int get_instance_number();
119  void trigger_signal(const std::string& sigID);
120  virtual void run();
121  virtual void init();
122  virtual void read_xml_element (const TiXmlElement* el, bool verbose = false);
123  virtual void parse_xml();
124 
125  bool is_multi_thread() const;
126  bool is_trigger() const;
127  bool is_stop() const;
128 
129 
130  void stop();
131  void unstop();
132 
133  GRANOO_ACCESS (every_iter , unsigned int , _every_iter );
134  GRANOO_ACCESS_GET_COPY(parent , PlugIn* , _parent );
135  GRANOO_ACCESS_GET_COPY(current_subplugin , PlugIn* , _current_subplugin );
136  GRANOO_ACCESS_GET_COPY(current_iter_number, unsigned int , _current_iter_number);
137 
138  void set_tot_iter_number(unsigned int);
139 
140  private:
142  PlugIn(const PlugIn &);
143  void operator=(const PlugIn &);
144 
145  protected:
146  std::vector<PlugIn*> _subplugin;
148 
149  private:
150  static std::map< std::string, std::vector<PlugIn* > > _all;
151 
152 
153  protected:
154  const std::string _ID;
155  const std::string _description;
157  bool _init;
158  bool _can_run;
159  bool _verbose;
160  unsigned int _current_iter_number;
161  unsigned int _iter_number;
162  double _total_time;
163  bool _stop;
164  unsigned int _every_iter;
165  double _step_time;
166  unsigned int _nb_outputs;
169  std::chrono::microseconds _elapsed_time;
170  };
171 
172 
173 
174 
175  template<class T>
176  class PlugInInterface : public PlugIn, public Core::ObjectFactoryInterface<PlugIn, T>
177  {
178  public:
179  static PlugIn * build();
180  static T& get(unsigned int = 0);
181  static bool exist(unsigned int rank = 0);
182  static bool record(const std::string file, const std::string desc = "");
183 
184  virtual ~PlugInInterface();
185 
186  protected:
188 
189  private:
192 
193  private:
194  static std::vector< PlugInInterface<T>* > _all;
195  };
196 
197  extern template class ObjectFactoryRegistered<PlugIn>;
198  extern template class ObjectFactory<PlugIn>;
199 
200  //------------------- PlugIn -------------------// -> see .cpp
201 
202 
203 
204 
205  //------------------- PlugInInterface -------------------
206 
207  template<class T>
208  std::vector< PlugInInterface<T>* > PlugInInterface<T>::_all = std::vector< PlugInInterface<T>* >();
209 
210 
211  template<class T>
212  T&
213  PlugInInterface<T>::get(unsigned int rank) {
214  UserAssert( rank < _all.size(), "Can't find the '" + std::string(typeid(T).name()) +
215  "' with rank=" + std::to_string(rank));
216  return *static_cast<T*>(_all[rank]);
217  }
218 
219  template<class T>
220  bool
221  PlugInInterface<T>::exist(unsigned int rank) {
222  return (rank < _all.size());
223  }
224 
225  template<class T>
226 
228  : PlugIn(T::class_ID()) {
229  _all.push_back(this);
230  }
231 
232  template<class T>
234  bool find = false;
235  for (unsigned int i = 0; i < _all.size(); ++i) {
236  if (_all[i]==this) {
237  _all.erase(_all.begin()+i);
238  find = true;
239  break;
240  }
241  }
242  UserAssert(find==true, "Can't find plugin");
243  }
244 
245  template<class T>
246  Core::PlugIn *
248  PlugIn* p = new T();
249  return p;
250  }
251 
252  template<class T>
253  bool
254  PlugInInterface<T>::record(const std::string file, const std::string desc) {
255  const std::string pluginId = T::class_ID();
256  std::function<PlugIn * ()> f;
257  f = std::bind(&PlugInInterface<T>::build);
258  PlugInManager::get().record(pluginId,f);
259 
260  ObjectFactoryInterface<PlugIn,T>::record(T::class_ID(), file, desc);
261  return true;
262  }
263 
264  }
265 }
266 
267 #endif
#define UserAssert(condition, message)
Definition: Macro.hpp:54
Definition: ObjectFactory.hpp:99
Definition: ObjectFactory.hpp:235
static bool record(const std::string &id, std::string file_name="", std::string desc="")
Definition: ObjectFactory.hpp:276
Definition: ObjectFactory.hpp:167
Definition: PlugIn.hpp:101
bool _can_run
Definition: PlugIn.hpp:158
virtual ~PlugIn()
Definition: PlugIn.cpp:105
static std::vector< PlugIn * > get_all_plugin_vector()
Definition: PlugIn.cpp:62
bool _verbose
Definition: PlugIn.hpp:159
bool _init
Definition: PlugIn.hpp:157
virtual void run()
Definition: PlugIn.cpp:123
static PlugIn & get(const std::string &id, unsigned int rank=0)
Definition: PlugIn.cpp:50
virtual void read_xml_element(const TiXmlElement *el, bool verbose=false)
Definition: PlugIn.cpp:301
static const std::string class_ID()
Definition: PlugIn.hpp:107
PlugIn * _parent
Definition: PlugIn.hpp:156
std::chrono::microseconds _elapsed_time
Definition: PlugIn.hpp:169
void trigger_signal_run()
Definition: PlugIn.cpp:285
Math::Expression * _init_if_expr
Definition: PlugIn.hpp:168
bool is_stop() const
Definition: PlugIn.cpp:142
static std::map< std::string, std::vector< PlugIn * > > get_all_plugin()
Definition: PlugIn.cpp:57
double _step_time
Definition: PlugIn.hpp:165
void operator=(const PlugIn &)
void verbose()
Definition: PlugIn.cpp:295
virtual void init()
Definition: PlugIn.cpp:365
static bool exist(const std::string &id, unsigned int rank=0)
Definition: PlugIn.cpp:73
void unstop()
Definition: PlugIn.cpp:135
unsigned int _iter_number
Definition: PlugIn.hpp:161
Math::Expression * _trigger_if_expr
Definition: PlugIn.hpp:167
void run_and_init()
Definition: PlugIn.cpp:161
std::vector< PlugIn * > _subplugin
Definition: PlugIn.hpp:146
PlugIn(const PlugIn &)
const std::string _ID
Definition: PlugIn.hpp:154
static std::map< std::string, std::vector< PlugIn * > > _all
Definition: PlugIn.hpp:150
bool is_trigger() const
Definition: PlugIn.cpp:147
void stop()
Definition: PlugIn.cpp:130
unsigned int _nb_outputs
Definition: PlugIn.hpp:166
unsigned int _every_iter
Definition: PlugIn.hpp:164
int get_instance_number()
Definition: PlugIn.cpp:266
double get_elapsed_time()
Definition: PlugIn.cpp:261
PlugIn * _current_subplugin
Definition: PlugIn.hpp:147
void set_tot_iter_number(unsigned int)
Definition: PlugIn.cpp:255
const std::string _description
Definition: PlugIn.hpp:155
virtual void parse_xml()
Definition: PlugIn.cpp:360
unsigned int _current_iter_number
Definition: PlugIn.hpp:160
bool _stop
Definition: PlugIn.hpp:163
double _total_time
Definition: PlugIn.hpp:162
bool is_multi_thread() const
Definition: PlugIn.cpp:376
const std::string & get_ID() const
Definition: PlugIn.hpp:112
void trigger_signal(const std::string &sigID)
Definition: PlugIn.cpp:277
Definition: PlugIn.hpp:177
static bool record(const std::string file, const std::string desc="")
Definition: PlugIn.hpp:254
static T & get(unsigned int=0)
Definition: PlugIn.hpp:213
virtual ~PlugInInterface()
Definition: PlugIn.hpp:233
void operator=(const PlugInInterface< T > &)
static bool exist(unsigned int rank=0)
Definition: PlugIn.hpp:221
PlugInInterface()
Definition: PlugIn.hpp:227
PlugInInterface(const PlugInInterface< T > &)
static PlugIn * build()
Definition: PlugIn.hpp:247
static std::vector< PlugInInterface< T > * > _all
Definition: PlugIn.hpp:194
static PlugInManager & get()
Definition: PlugInManager.cpp:45
bool record(const std::string &id, std::function< PlugIn *()>)
Definition: PlugInManager.cpp:133
Definition: Expression.hpp:46
static const char * desc
Definition: Between2SetOf.cpp:37
Definition: Common.hpp:198