Psyllid  v1.12.4
Project 8 Data Acquisisition Software
node_builder.hh
Go to the documentation of this file.
1 /*
2  * node_builder.hh
3  *
4  * Created on: Feb 18, 2016
5  * Author: nsoblath
6  */
7 
8 #ifndef PSYLLID_NODE_BUILDER_HH_
9 #define PSYLLID_NODE_BUILDER_HH_
10 
11 #include "psyllid_error.hh"
12 
13 #include "member_variables.hh"
14 #include "param.hh"
15 
16 namespace midge
17 {
18  class node;
19 }
20 
21 namespace psyllid
22 {
23  //****************
24  // node_binding
25  //****************
26 
39  {
40  public:
41  node_binding();
42  virtual ~node_binding();
43 
44  node_binding& operator=( const node_binding& a_rhs );
45 
46  virtual node_binding* clone() const = 0;
47 
48  public:
51  virtual void apply_config( midge::node* a_node, const scarab::param_node& a_config ) const = 0;
54  virtual void dump_config( const midge::node* a_node, scarab::param_node& a_config ) const = 0;
55 
58  virtual bool run_command( midge::node* a_node, const std::string& a_cmd, const scarab::param_node& a_args ) const = 0;
59 
60  };
61 
62 
63  //*****************
64  // _node_binding
65  //*****************
66 
67  template< class x_node_type, class x_binding_type >
68  class _node_binding : public node_binding
69  {
70  public:
71  _node_binding();
72  virtual ~_node_binding();
73 
75 
76  virtual node_binding* clone() const;
77 
78  public:
79  virtual void apply_config( midge::node* a_node, const scarab::param_node& a_config ) const;
80  virtual void dump_config( const midge::node* a_node, scarab::param_node& a_config ) const;
81 
82  virtual bool run_command( midge::node* a_node, const std::string& a_cmd, const scarab::param_node& a_args ) const;
83 
84  private:
85  virtual void do_apply_config( x_node_type* a_node, const scarab::param_node& a_config ) const = 0;
86  virtual void do_dump_config( const x_node_type* a_node, scarab::param_node& a_config ) const = 0;
87 
89  virtual bool do_run_command( x_node_type* a_node, const std::string& a_cmd, const scarab::param_node& a_args ) const;
90 
91  };
92 
93 
94  //****************
95  // node_builder
96  //****************
107  class node_builder : public node_binding
108  {
109  public:
110  node_builder( node_binding* a_binding );
111  virtual ~node_builder();
112 
113  node_builder& operator=( const node_builder& );
114 
115  const node_binding& binding() const;
116 
117  protected:
119 
120  public:
121  virtual midge::node* build() = 0;
122 
123  void configure_builder( const scarab::param_node& a_config );
124  void replace_builder_config( const scarab::param_node& a_config );
125  void dump_builder_config( scarab::param_node& a_config );
126 
127  protected:
128  scarab::param_node f_config;
129 
130  mv_referrable( std::string, name );
131 
132  public:
133  virtual void apply_config( midge::node* a_node, const scarab::param_node& a_config ) const;
134  virtual void dump_config( const midge::node* a_node, scarab::param_node& a_config ) const;
135 
136  virtual bool run_command( midge::node* a_node, const std::string& a_cmd, const scarab::param_node& a_args ) const;
137 
138  };
139 
140 
141  //*****************
142  // _node_builder
143  //*****************
144 
145  template< class x_node_type, class x_binding_type >
147  {
148  public:
149  _node_builder();
150  _node_builder( x_binding_type* a_binding );
151  virtual ~_node_builder();
152 
154 
155  node_binding* clone() const;
156 
157  public:
159  virtual midge::node* build();
160 
161  };
162 
163 
164  //*******************
165  //*******************
166  // Implementations
167  //*******************
168  //*******************
169 
170 
171  //*****************
172  // _node_binding
173  //*****************
174 
175  template< class x_node_type, class x_node_binding >
177  node_binding()
178  {}
179 
180  template< class x_node_type, class x_node_binding >
182  {}
183 
184  template< class x_node_type, class x_node_binding >
186  {
187  this->node_binding::operator=( a_rhs );
188  return *this;
189  }
190 
191  template< class x_node_type, class x_node_binding >
193  {
194  x_node_binding* t_new_binding = new x_node_binding();
195  t_new_binding->operator=( *static_cast< const x_node_binding* >(this) );
196  return t_new_binding;
197  }
198 
199  template< class x_node_type, class x_node_binding >
200  void _node_binding< x_node_type, x_node_binding >::apply_config( midge::node* a_node, const scarab::param_node& a_config ) const
201  {
202  x_node_type* t_derived_node = dynamic_cast< x_node_type* >( a_node );
203  if( t_derived_node == nullptr )
204  {
205  throw error() << "Node type does not match builder type (apply_config(node*))";
206  }
207  try
208  {
209  do_apply_config( t_derived_node, a_config );
210  }
211  catch( std::exception& e )
212  {
213  throw psyllid::error() << e.what();
214  }
215  return;
216  }
217 
218  template< class x_node_type, class x_node_binding >
219  void _node_binding< x_node_type, x_node_binding >::dump_config( const midge::node* a_node, scarab::param_node& a_config ) const
220  {
221  const x_node_type* t_derived_node = dynamic_cast< const x_node_type* >( a_node );
222  if( t_derived_node == nullptr )
223  {
224  throw error() << "Node type does not match builder type (extract_config(node*, param_node&))";
225  }
226  try
227  {
228  do_dump_config( t_derived_node, a_config );
229  }
230  catch( std::exception& e )
231  {
232  throw psyllid::error() << e.what();
233  }
234  return;
235  }
236 
237  template< class x_node_type, class x_node_binding >
238  bool _node_binding< x_node_type, x_node_binding >::run_command( midge::node* a_node, const std::string& a_cmd, const scarab::param_node& a_args ) const
239  {
240  x_node_type* t_derived_node = dynamic_cast< x_node_type* >( a_node );
241  if( t_derived_node == nullptr )
242  {
243  throw error() << "Node type does not match builder type (extract_config(node*, param_node&))";
244  }
245  try
246  {
247  return do_run_command( t_derived_node, a_cmd, a_args );
248  }
249  catch( std::exception& e )
250  {
251  throw error() << e.what();
252  }
253  }
254 
255  template< class x_node_type, class x_node_binding >
256  bool _node_binding< x_node_type, x_node_binding >::do_run_command( x_node_type*, const std::string&, const scarab::param_node& ) const
257  {
258  return false;
259  }
260 
261 
262  //****************
263  // node_builder
264  //****************
265 
266  inline const node_binding& node_builder::binding() const
267  {
268  return *f_binding;
269  }
270 
271  inline void node_builder::configure_builder( const scarab::param_node& a_config )
272  {
273  f_config.merge( a_config );
274  return;
275  }
276 
277  inline void node_builder::replace_builder_config( const scarab::param_node& a_config )
278  {
279  f_config.clear();
280  f_config.merge( a_config );
281  return;
282  }
283 
284  inline void node_builder::dump_builder_config( scarab::param_node& a_config )
285  {
286  a_config.clear();
287  a_config.merge( f_config );
288  return;
289  }
290 
291  inline void node_builder::apply_config( midge::node* a_node, const scarab::param_node& a_config ) const
292  {
293  f_binding->apply_config( a_node, a_config );
294  return;
295  }
296 
297  inline void node_builder::dump_config( const midge::node* a_node, scarab::param_node& a_config ) const
298  {
299  f_binding->dump_config( a_node, a_config );
300  return;
301  }
302 
303  inline bool node_builder::run_command( midge::node* a_node, const std::string& a_cmd, const scarab::param_node& a_args ) const
304  {
305  return f_binding->run_command( a_node, a_cmd, a_args );
306  }
307 
308 
309  //*****************
310  // _node_builder
311  //*****************
312 
313  template< class x_node_type, class x_binding_type >
315  node_builder( new x_binding_type() )
316  {}
317 
318  template< class x_node_type, class x_binding_type >
320  node_builder( a_binding )
321  {}
322 
323  template< class x_node_type, class x_binding_type >
325  {}
326 
327  template< class x_node_type, class x_binding_type >
329  {
330  this->node_builder::operator=( a_rhs );
331  return *this;
332  }
333 
334  template< class x_node_type, class x_binding_type >
336  {
338  t_new_builder->operator=( *this );
339  return t_new_builder;
340  }
341 
342  template< class x_node_type, class x_binding_type >
344  {
345  x_node_type* t_node = new x_node_type();
346 
347  // before we do anything else, get the default configuration and merge anything in f_config with it
348  scarab::param_node t_temp_config( f_config );
349  f_config.clear();
350  dump_config( t_node, f_config );
351  f_config.merge( t_temp_config );
352 
353  apply_config( t_node, f_config );
354  t_node->set_name( f_name );
355 
356  return t_node;
357  }
358 
359 
360 
361 #define REGISTER_NODE_AND_BUILDER( node_class, node_name, node_binding ) \
362  static ::scarab::registrar< ::midge::node, node_class > s_node_##node_class##_registrar( node_name ); \
363  static ::scarab::registrar< ::psyllid::node_builder, _node_builder< node_class, node_binding > > s_node_builder_##node_class##_registrar( node_name );
364 
365 
366 } /* namespace psyllid */
367 
368 #endif /* PSYLLID_NODE_BUILDER_HH_ */
node_binding * clone() const
virtual void apply_config(midge::node *a_node, const scarab::param_node &a_config) const
const char * what() const
scarab::param_node f_config
node_binding * f_binding
Holds node configuration and can be used to create node classes and node binding classes.
_node_builder< x_node_type, x_binding_type > & operator=(const _node_builder< x_node_type, x_binding_type > &a_rhs)
virtual midge::node * build()
Builds a new node and applies the builder&#39;s configuration information.
node_builder & operator=(const node_builder &)
Definition: node_builder.cc:45
virtual void dump_config(const midge::node *a_node, scarab::param_node &a_config) const
virtual void apply_config(midge::node *a_node, const scarab::param_node &a_config) const =0
Allows access to midge nodes.
Definition: node_builder.hh:38