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