artdaq_mfextensions  v1_06_02
ma_function.h
1 #ifndef ERROR_HANDLER_MA_FUNCTION_H
2 #define ERROR_HANDLER_MA_FUNCTION_H
3 
4 #include <map>
5 #include <string>
6 #include <vector>
7 
8 #include "ErrorHandler/MessageAnalyzer/ma_types.h"
9 
10 #include <boost/any.hpp>
11 #include <boost/function.hpp>
12 
13 namespace novadaq {
14 namespace errorhandler {
15 
16 class ma_condition;
17 
18 typedef std::vector<boost::any> anys_t;
19 
20 // base class - all customized fucntions are inherited from it
22 {
23 public:
24  ma_function() {}
25  virtual ~ma_function() {}
26 
27  // evaluate() function gets called when the boolean expression
28  // of the rule is being evaluated.
29  //
30  // param 'cond':
31  // 'cond' is the reference to the basic condition to which
32  // this user defined function is related. Though this object
33  // the function can have access to the most recent message
34  // that triggeres the condition, as well as other historical
35  // information
36  //
37  // param 'dom':
38  // when using complex conditions and domain selection clauses
39  // in the rule expression, a user define function might get
40  // evaluated multiple times each for a possible source/targets
41  // domain when a new message comes in. 'dom' contains the domain
42  // of possible source/target combinations in this evaluation.
43  // an example would be, cond.get_alarm_count(dom, cond_type)
44  // returns with the number of triggered source/target pairs
45  // WITHIN the given domain of a condition.
46  //
47  // return value:
48  // the return value could be a boolean, an integer, a double,
49  // or a string object. all of them need to be wrapped in a
50  // boost::any object before returning to the caller
51  virtual boost::any
52  evaluate(ma_condition const& cond, ma_cond_domain dom) = 0;
53 
54  // a user function is internally implemented by a class, which allows
55  // the function to have states. not surprisingly would the funciton
56  // writers also like to implement a reset method for the purpose of
57  // resetting the state of the user-defined function into its ground
58  // state
59  virtual void
60  reset() {}
61 
62  // a user function is allowed to take multiple arguments
63  // other than the condition name that it applies on
64  virtual bool
65  parse_arguments(anys_t const& /*args*/) { return true; }
66 
67  // whether the funciton triggers a grouped alarm or
68  // individual alarms with respect to the condition's
69  // source / targets.
70  //
71  // e.g.: a user function COUNT() usually triggers a grouped
72  // alarm, as we dont necessary need to make distinguish
73  // between whether it was source 1,2,5, or source 3,7,8
74  // who trigger the alarm
75  //
76  // an example of non-grouped alarm is the function of
77  // OUT_OF_SYNC(). we are interested in who was out of
78  // sync, and raises alarms for each source
79  virtual bool
80  grouped_alarm() { return true; }
81 
82 protected:
83 private:
84 };
85 
86 typedef boost::function<ma_function*()> gen_func_t;
87 
89 {
90  typedef std::map<std::string, gen_func_t> gen_map_t;
91 
92 public:
93  static void
94  reg(std::string const& func_name, gen_func_t f);
95 
96  static ma_function*
97  create_instance(std::string const& func_name);
98 
99 private:
101 
102  static gen_map_t&
103  get_map()
104  {
105  static gen_map_t map;
106  return map;
107  }
108 };
109 
111 {
112  ma_function_maker(std::string const& func_name, gen_func_t f)
113  {
114  ma_function_factory::reg(func_name, f);
115  }
116 };
117 
118 } // end of namespace errorhandler
119 } // end of namespace novadaq
120 
121 // -------------------------------------------------
122 // Macro for registering the custom function
123 
124 #define REG_MA_FUNCTION(func_name, class_name) \
125  ma_function* \
126  class_name##_maker_func() { return new class_name(); } \
127  ma_function_maker \
128  class_name##_maker_func_global_var(#func_name, class_name##_maker_func);
129 
130 #endif