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