artdaq_mfextensions  v1_06_02
ma_cond_test_primary.cpp
1 #include "ErrorHandler/MessageAnalyzer/ma_cond_test_primary.h"
2 #include "ErrorHandler/MessageAnalyzer/ma_cond_test_expr.h"
3 
4 using namespace novadaq::errorhandler;
5 
6 template<typename T>
7 bool compare(compare_op_t op, T v, T rhv)
8 {
9  switch (op)
10  {
11  case CO_L:
12  return (v < rhv);
13  case CO_LE:
14  return (v <= rhv);
15  case CO_E:
16  return (v == rhv);
17  case CO_NE:
18  return (v != rhv);
19  case CO_GE:
20  return (v >= rhv);
21  case CO_G:
22  return (v > rhv);
23  default:
24  return false;
25  }
26 }
27 
28 void ma_cond_test_primary::insert_expr(ma_cond_test_expr const& e)
29 {
30  expr.reset(new ma_cond_test_expr(e));
31  cond_type = EXPR;
32 }
33 
34 void ma_cond_test_primary::insert_func(std::string const& name, anys_t const& args)
35 {
36  func.reset(ma_test_function_factory::create_instance(name));
37 
38  try
39  {
40  if (!func->parse_arguments(args))
41  throw std::runtime_error("arguments rejected by test function " + name);
42  }
43  catch (std::exception& e)
44  {
45  throw std::runtime_error("arguments rejected by test function " + name + "() with an exception:\n" + e.what());
46  }
47 
48  cond_type = FUNCTION;
49 }
50 
51 void ma_cond_test_primary::insert_compare_op(compare_op_t cop, any_t const& v)
52 {
53  op = cop;
54 
55  if (v.type() == typeid(bool))
56  {
57  rhv_b = boost::any_cast<bool>(v);
58  cond_type = FUNCTION_BOOL;
59  }
60  else if (v.type() == typeid(int))
61  {
62  rhv_d = boost::any_cast<int>(v);
63  cond_type = FUNCTION_DOUBLE;
64  }
65  else if (v.type() == typeid(double))
66  {
67  rhv_d = boost::any_cast<double>(v);
68  cond_type = FUNCTION_DOUBLE;
69  }
70  else
71  {
72  rhv_s = boost::any_cast<std::string>(v);
73  cond_type = FUNCTION_STRING;
74  }
75 }
76 
77 bool ma_cond_test_primary::evaluate(ma_condition const* cond) const
78 {
79  if (cond_type == EXPR)
80  {
81  assert(expr.get() != NULL);
82  return expr->evaluate(cond);
83  }
84  else
85  {
86  any_t v = func->evaluate(*cond);
87 
88  bool b;
89  double d;
90  std::string s;
91 
92  switch (cond_type)
93  {
94  case FUNCTION:
95  return boost::any_cast<bool>(v);
96 
97  case FUNCTION_BOOL:
98  b = boost::any_cast<bool>(v);
99  return compare(op, b, rhv_b);
100 
101  case FUNCTION_STRING:
102  s = boost::any_cast<std::string>(v);
103  return compare(op, s, rhv_s);
104 
105  case FUNCTION_DOUBLE:
106  if (v.type() == typeid(int))
107  d = boost::any_cast<int>(v);
108  else if (v.type() == typeid(unsigned int))
109  d = boost::any_cast<unsigned int>(v);
110  else if (v.type() == typeid(long))
111  d = boost::any_cast<long>(v);
112  else if (v.type() == typeid(float))
113  d = boost::any_cast<float>(v);
114  else
115  d = boost::any_cast<double>(v);
116 
117  return compare(op, d, rhv_d);
118 
119  default:
120  throw std::runtime_error("Unkonwn test primary type");
121  }
122  }
123 }