OpcUaCanOpen
CANopen OPC-UA server
TMath.h
Go to the documentation of this file.
1 #ifndef _tmath
2 #define _tmath
3 
4 //#include "CompileMathExpression.h"
5 #include <cmath>
6 #include <string>
7 #include <vector>
8 
9 enum tokenids {
17  SIF,
19  SOR,
21  SNE,
22  SGT,
23  SGE,
24  SEQ,
25  SLE,
26  SLT,
28 };
29 
30 union ptr_func {
31  double (*ptr0Func)();
32  double (*ptr1Func)(double);
33  double (*ptr2Func)(double,double);
34  double (*ptr3Func)(double,double,double);
35  double(*ptr3IFFunc)(bool, double, double);
36  bool (*ptr0LFunc)();
37  bool (*ptr1LDFunc)(double);
38  bool (*ptr2LDFunc)(double,double);
39  bool (*ptr1LFunc)(bool);
40  bool (*ptr2LFunc)(bool,bool);
41 
42 };
43 
44 struct funcTable {
45  unsigned int iden;
47  int num_param;
49 
50  funcTable() { iden = IDANY; name = ""; num_param = 0; p_func.ptr0Func = NULL; }
51  funcTable ( const struct funcTable &ft)
52  {
53  name = ft.name; iden = ft.iden; num_param = ft.num_param; p_func = ft.p_func;
54  };
55  funcTable(unsigned int id, std::string n, int n_p, double (p_f)())
56  {
57  iden = id; name = n; num_param = n_p; p_func.ptr0Func = p_f;
58  };
59  funcTable(unsigned int id, std::string n, int n_p, double (p_f)(double) )
60  {
61  iden = id; name = n; num_param = n_p; p_func.ptr1Func = p_f;
62  };
63  funcTable(unsigned int id, std::string n, int n_p, double (p_f)(double,double) )
64  {
65  iden = id; name = n; num_param = n_p; p_func.ptr2Func = p_f;
66  };
67  funcTable(unsigned int id, std::string n, int n_p, double (p_f)(double,double,double))
68  {
69  iden = id; name = n; num_param = n_p; p_func.ptr3Func = p_f;
70  };
71  funcTable(unsigned int id, std::string n, int n_p, double (p_f)(bool, double, double))
72  {
73  iden = id; name = n; num_param = n_p; p_func.ptr3IFFunc = p_f;
74  };
75  funcTable(unsigned int id, std::string n, int n_p, bool (p_f)())
76  {
77  iden = id; name = n; num_param = n_p; p_func.ptr0LFunc = p_f;
78  };
79  funcTable(unsigned int id, std::string n, int n_p, bool (p_f)(double) )
80  {
81  iden = id; name = n; num_param = n_p; p_func.ptr1LDFunc = p_f;
82  };
83  funcTable(unsigned int id, std::string n, int n_p, bool (p_f)(double,double) )
84  {
85  iden = id; name = n; num_param = n_p; p_func.ptr2LDFunc = p_f;
86  };
87  funcTable(unsigned int id, std::string n, int n_p, bool (p_f)(bool) )
88  {
89  iden = id; name = n; num_param = n_p; p_func.ptr1LFunc = p_f;
90  };
91  funcTable(unsigned int id, std::string n, int n_p, bool (p_f)(bool,bool) )
92  {
93  iden = id; name = n; num_param = n_p; p_func.ptr2LFunc = p_f;
94  };
95 };
96 
97 class TMath
98 {
99 public:
100  class isName {
101  public:
102  isName(std::string sn): n(sn) {}
103  inline bool operator()(const struct funcTable &in) { return in.name == n; }
104  private:
106  };
107 
108  TMath() {}
109 
110  static std::vector<funcTable> functionSet;
111 
112  inline double static Pi() { return M_PI; }
113  inline double static E() { return M_E; }
114  inline double static PLUS(double d1,double d2) { return d1+d2; }
115  inline double static MINUS(double d1,double d2 ) { return d1-d2; }
116  inline double static MINUS2(double d1 ) { return -d1; }
117  inline double static MUL(double d1,double d2) { return d1*d2; }
118  inline double static DIV(double d1,double d2) { return d1/d2; }
119  inline double static EXP2(double d1,double d2) { return pow(d1,d2); }
120  inline bool static ETRE(bool d1) { return d1; }
121  inline bool static ETRE(double d1) { (void)(d1); return true; }
122  inline double static IFF(bool d1, double d2, double d3) { return ETRE(d1) ? d2 : d3; }
123  inline bool static NOT(bool d1) { return !d1; }
124  inline bool static NE(double d1,double d2) { return d1 != d2; }
125  inline bool static OR(bool d1, bool d2) { return d1 || d2; }
126  inline bool static AND(bool d1, bool d2) { return d1 && d2; }
127  inline bool static GT(double d1,double d2) { return d1 > d2; }
128  inline bool static EQ(double d1,double d2) { return d1 == d2; }
129  inline bool static GE(double d1,double d2) { return d1 >= d2; }
130  inline bool static LE(double d1,double d2) { return d1 <= d2; }
131  inline bool static LT(double d1,double d2) { return d1 < d2; }
132 
133  static void init()
134  {
138  functionSet[SMUL] = funcTable(SMUL,"*",2,MUL);
139  functionSet[SDIV] = funcTable(SDIV,"/",2,DIV);
140  functionSet[SEXP] = funcTable(SEXP,"^",2,EXP2);
141  functionSet[SNOT] = funcTable(SNOT,"!",1,NOT);
142  functionSet[SNE] = funcTable(SNE,"!=",2,NE);
143  functionSet[SOR] = funcTable(SOR,"||",2,OR);
144  functionSet[SAND] = funcTable(SAND,"&&",2,AND);
145  functionSet[SGT] = funcTable(SGT,">=",2,GT);
146  functionSet[SGE] = funcTable(SGE,">",2,GE);
147  functionSet[SEQ] = funcTable(SEQ,"=",2,EQ);
148  functionSet[SLE] = funcTable(SLE,"<",2,LE);
149  functionSet[SLT] = funcTable(SLT,"<=",2,LT);
150  functionSet[SIF] = funcTable(SIF, "?", 3, IFF);
151  functionSet[SFUNC+1] = funcTable(SFUNC+1,"pi(",0,Pi);
152  functionSet[SFUNC+2] = funcTable(SFUNC+2,"e(",0,E);
153  functionSet[SFUNC+3] = funcTable(SFUNC+3,"sin(",1,sin);
154  functionSet[SFUNC+4] = funcTable(SFUNC+4,"cos(",1,cos);
155  functionSet[SFUNC+5] = funcTable(SFUNC+5,"tan(",1,tan);
156  functionSet[SFUNC+6] = funcTable(SFUNC+6,"asin(",1,asin);
157  functionSet[SFUNC+7] = funcTable(SFUNC+7,"acos(",1,acos);
158  functionSet[SFUNC+8] = funcTable(SFUNC+8,"atan(",1,atan);
159  functionSet[SFUNC+9] = funcTable(SFUNC+9,"atan2(",2,atan2);
160  functionSet[SFUNC+10] = funcTable(SFUNC+10,"sinh(",1,sinh);
161  functionSet[SFUNC+11] = funcTable(SFUNC+11,"cosh(",1,cosh);
162  functionSet[SFUNC+12] = funcTable(SFUNC+12,"tanh(",1,tanh);
163  functionSet[SFUNC+13] = funcTable(SFUNC+13,"exp(",1,exp);
164  functionSet[SFUNC+14] = funcTable(SFUNC+14,"log(",1,log);
165  functionSet[SFUNC+15] = funcTable(SFUNC+15,"log10(",1,log10);
166  functionSet[SFUNC+16] = funcTable(SFUNC+16,"pow(",2,pow);
167  functionSet[SFUNC+17] = funcTable(SFUNC+17,"sqrt(",1,sqrt);
168  functionSet[SFUNC+18] = funcTable(SFUNC+18,"ceil(",1,ceil);
169  functionSet[SFUNC+19] = funcTable(SFUNC+19,"fabs(",1,fabs);
170  functionSet[SFUNC+20] = funcTable(SFUNC+20,"floor(",1,floor);
171  functionSet[SFUNC+21] = funcTable(SFUNC+21,"fmod(",1,fmod);
172 
173  };
174 
175  static unsigned int getFunctionId(std::string nFunc)
176  {
177  std::vector<struct funcTable>::iterator ift = find_if(functionSet.begin(), functionSet.end(), isName(nFunc));
178  if (ift == functionSet.end()) {
179  std::cout << "The function " << nFunc << "did not implement" << std::endl;
180  exit(-1);
181  }
182 
183  return ift->iden;
184  };
185  static int getNumParam(unsigned int nFunc)
186  {
187  return functionSet[nFunc].num_param;
188  };
189  static const struct funcTable &getFuncTable(unsigned int nFunc)
190  {
191  return functionSet[nFunc];
192  };
193  static struct funcTable &getFuncTableStr(std::string nFunc)
194  {
195  return functionSet[getFunctionId(nFunc)];
196  };
197 
198 
199 };
200 #endif