muParser API -  1.35
muParserCallback.cpp
Go to the documentation of this file.
1 /*
2 
3  _____ __ _____________ _______ ______ ___________
4  / \| | \____ \__ \\_ __ \/ ___// __ \_ __ \
5  | Y Y \ | / |_> > __ \| | \/\___ \\ ___/| | \/
6  |__|_| /____/| __(____ /__| /____ >\___ >__|
7  \/ |__| \/ \/ \/
8  Copyright (C) 2004 - 2020 Ingo Berg
9 
10  Redistribution and use in source and binary forms, with or without modification, are permitted
11  provided that the following conditions are met:
12 
13  * Redistributions of source code must retain the above copyright notice, this list of
14  conditions and the following disclaimer.
15  * Redistributions in binary form must reproduce the above copyright notice, this list of
16  conditions and the following disclaimer in the documentation and/or other materials provided
17  with the distribution.
18 
19  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
20  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 
29 #include "muParserCallback.h"
30 
31 #if defined(_MSC_VER)
32  #pragma warning(push)
33  #pragma warning(disable : 26812)
34 #endif
35 
36 /** \file
37  \brief Implementation of the parser callback class.
38 */
39 
40 
41 namespace mu
42 {
43  ParserCallback::ParserCallback(fun_type0 a_pFun, bool a_bAllowOpti)
44  :m_pFun((void*)a_pFun)
45  , m_iArgc(0)
46  , m_iPri(-1)
47  , m_eOprtAsct(oaNONE)
48  , m_iCode(cmFUNC)
49  , m_iType(tpDBL)
50  , m_bAllowOpti(a_bAllowOpti)
51  {}
52 
53 
54  ParserCallback::ParserCallback(fun_type1 a_pFun, bool a_bAllowOpti, int a_iPrec, ECmdCode a_iCode)
55  :m_pFun((void*)a_pFun)
56  , m_iArgc(1)
57  , m_iPri(a_iPrec)
58  , m_eOprtAsct(oaNONE)
59  , m_iCode(a_iCode)
60  , m_iType(tpDBL)
61  , m_bAllowOpti(a_bAllowOpti)
62  {}
63 
64 
65 
66  /** \brief Constructor for constructing function callbacks taking two arguments.
67  \throw nothrow
68  */
69  ParserCallback::ParserCallback(fun_type2 a_pFun, bool a_bAllowOpti)
70  :m_pFun((void*)a_pFun)
71  , m_iArgc(2)
72  , m_iPri(-1)
73  , m_eOprtAsct(oaNONE)
74  , m_iCode(cmFUNC)
75  , m_iType(tpDBL)
76  , m_bAllowOpti(a_bAllowOpti)
77  {}
78 
79 
80  /** \brief Constructor for constructing binary operator callbacks.
81  \param a_pFun Pointer to a static function taking two arguments
82  \param a_bAllowOpti A flag indicating this function can be optimized
83  \param a_iPrec The operator precedence
84  \param a_eOprtAsct The operators associativity
85  \throw nothrow
86  */
88  bool a_bAllowOpti,
89  int a_iPrec,
90  EOprtAssociativity a_eOprtAsct)
91  :m_pFun((void*)a_pFun)
92  , m_iArgc(2)
93  , m_iPri(a_iPrec)
94  , m_eOprtAsct(a_eOprtAsct)
95  , m_iCode(cmOPRT_BIN)
96  , m_iType(tpDBL)
97  , m_bAllowOpti(a_bAllowOpti)
98  {}
99 
100 
101  ParserCallback::ParserCallback(fun_type3 a_pFun, bool a_bAllowOpti)
102  :m_pFun((void*)a_pFun)
103  , m_iArgc(3)
104  , m_iPri(-1)
105  , m_eOprtAsct(oaNONE)
106  , m_iCode(cmFUNC)
107  , m_iType(tpDBL)
108  , m_bAllowOpti(a_bAllowOpti)
109  {}
110 
111 
112  ParserCallback::ParserCallback(fun_type4 a_pFun, bool a_bAllowOpti)
113  :m_pFun((void*)a_pFun)
114  , m_iArgc(4)
115  , m_iPri(-1)
116  , m_eOprtAsct(oaNONE)
117  , m_iCode(cmFUNC)
118  , m_iType(tpDBL)
119  , m_bAllowOpti(a_bAllowOpti)
120  {}
121 
122 
123  ParserCallback::ParserCallback(fun_type5 a_pFun, bool a_bAllowOpti)
124  :m_pFun((void*)a_pFun)
125  , m_iArgc(5)
126  , m_iPri(-1)
127  , m_eOprtAsct(oaNONE)
128  , m_iCode(cmFUNC)
129  , m_iType(tpDBL)
130  , m_bAllowOpti(a_bAllowOpti)
131  {}
132 
133 
134  ParserCallback::ParserCallback(fun_type6 a_pFun, bool a_bAllowOpti)
135  :m_pFun((void*)a_pFun)
136  , m_iArgc(6)
137  , m_iPri(-1)
138  , m_eOprtAsct(oaNONE)
139  , m_iCode(cmFUNC)
140  , m_iType(tpDBL)
141  , m_bAllowOpti(a_bAllowOpti)
142  {}
143 
144 
145  ParserCallback::ParserCallback(fun_type7 a_pFun, bool a_bAllowOpti)
146  :m_pFun((void*)a_pFun)
147  , m_iArgc(7)
148  , m_iPri(-1)
149  , m_eOprtAsct(oaNONE)
150  , m_iCode(cmFUNC)
151  , m_iType(tpDBL)
152  , m_bAllowOpti(a_bAllowOpti)
153  {}
154 
155 
156  ParserCallback::ParserCallback(fun_type8 a_pFun, bool a_bAllowOpti)
157  :m_pFun((void*)a_pFun)
158  , m_iArgc(8)
159  , m_iPri(-1)
160  , m_eOprtAsct(oaNONE)
161  , m_iCode(cmFUNC)
162  , m_iType(tpDBL)
163  , m_bAllowOpti(a_bAllowOpti)
164  {}
165 
166 
167  ParserCallback::ParserCallback(fun_type9 a_pFun, bool a_bAllowOpti)
168  :m_pFun((void*)a_pFun)
169  , m_iArgc(9)
170  , m_iPri(-1)
171  , m_eOprtAsct(oaNONE)
172  , m_iCode(cmFUNC)
173  , m_iType(tpDBL)
174  , m_bAllowOpti(a_bAllowOpti)
175  {}
176 
177 
178  ParserCallback::ParserCallback(fun_type10 a_pFun, bool a_bAllowOpti)
179  :m_pFun((void*)a_pFun)
180  , m_iArgc(10)
181  , m_iPri(-1)
182  , m_eOprtAsct(oaNONE)
183  , m_iCode(cmFUNC)
184  , m_iType(tpDBL)
185  , m_bAllowOpti(a_bAllowOpti)
186  {}
187 
188 
189  ParserCallback::ParserCallback(bulkfun_type0 a_pFun, bool a_bAllowOpti)
190  :m_pFun((void*)a_pFun)
191  , m_iArgc(0)
192  , m_iPri(-1)
193  , m_eOprtAsct(oaNONE)
194  , m_iCode(cmFUNC_BULK)
195  , m_iType(tpDBL)
196  , m_bAllowOpti(a_bAllowOpti)
197  {}
198 
199 
200  ParserCallback::ParserCallback(bulkfun_type1 a_pFun, bool a_bAllowOpti)
201  :m_pFun((void*)a_pFun)
202  , m_iArgc(1)
203  , m_iPri(-1)
204  , m_eOprtAsct(oaNONE)
205  , m_iCode(cmFUNC_BULK)
206  , m_iType(tpDBL)
207  , m_bAllowOpti(a_bAllowOpti)
208  {}
209 
210 
211  /** \brief Constructor for constructing function callbacks taking two arguments.
212  \throw nothrow
213  */
215  :m_pFun((void*)a_pFun)
216  , m_iArgc(2)
217  , m_iPri(-1)
218  , m_eOprtAsct(oaNONE)
219  , m_iCode(cmFUNC_BULK)
220  , m_iType(tpDBL)
221  , m_bAllowOpti(a_bAllowOpti)
222  {}
223 
224 
225  ParserCallback::ParserCallback(bulkfun_type3 a_pFun, bool a_bAllowOpti)
226  :m_pFun((void*)a_pFun)
227  , m_iArgc(3)
228  , m_iPri(-1)
229  , m_eOprtAsct(oaNONE)
230  , m_iCode(cmFUNC_BULK)
231  , m_iType(tpDBL)
232  , m_bAllowOpti(a_bAllowOpti)
233  {}
234 
235 
236  ParserCallback::ParserCallback(bulkfun_type4 a_pFun, bool a_bAllowOpti)
237  :m_pFun((void*)a_pFun)
238  , m_iArgc(4)
239  , m_iPri(-1)
240  , m_eOprtAsct(oaNONE)
241  , m_iCode(cmFUNC_BULK)
242  , m_iType(tpDBL)
243  , m_bAllowOpti(a_bAllowOpti)
244  {}
245 
246 
247  ParserCallback::ParserCallback(bulkfun_type5 a_pFun, bool a_bAllowOpti)
248  :m_pFun((void*)a_pFun)
249  , m_iArgc(5)
250  , m_iPri(-1)
251  , m_eOprtAsct(oaNONE)
252  , m_iCode(cmFUNC_BULK)
253  , m_iType(tpDBL)
254  , m_bAllowOpti(a_bAllowOpti)
255  {}
256 
257 
258  ParserCallback::ParserCallback(bulkfun_type6 a_pFun, bool a_bAllowOpti)
259  :m_pFun((void*)a_pFun)
260  , m_iArgc(6)
261  , m_iPri(-1)
262  , m_eOprtAsct(oaNONE)
263  , m_iCode(cmFUNC_BULK)
264  , m_iType(tpDBL)
265  , m_bAllowOpti(a_bAllowOpti)
266  {}
267 
268 
269  ParserCallback::ParserCallback(bulkfun_type7 a_pFun, bool a_bAllowOpti)
270  :m_pFun((void*)a_pFun)
271  , m_iArgc(7)
272  , m_iPri(-1)
273  , m_eOprtAsct(oaNONE)
274  , m_iCode(cmFUNC_BULK)
275  , m_iType(tpDBL)
276  , m_bAllowOpti(a_bAllowOpti)
277  {}
278 
279 
280  ParserCallback::ParserCallback(bulkfun_type8 a_pFun, bool a_bAllowOpti)
281  :m_pFun((void*)a_pFun)
282  , m_iArgc(8)
283  , m_iPri(-1)
284  , m_eOprtAsct(oaNONE)
285  , m_iCode(cmFUNC_BULK)
286  , m_iType(tpDBL)
287  , m_bAllowOpti(a_bAllowOpti)
288  {}
289 
290 
291  ParserCallback::ParserCallback(bulkfun_type9 a_pFun, bool a_bAllowOpti)
292  :m_pFun((void*)a_pFun)
293  , m_iArgc(9)
294  , m_iPri(-1)
295  , m_eOprtAsct(oaNONE)
296  , m_iCode(cmFUNC_BULK)
297  , m_iType(tpDBL)
298  , m_bAllowOpti(a_bAllowOpti)
299  {}
300 
301 
302  ParserCallback::ParserCallback(bulkfun_type10 a_pFun, bool a_bAllowOpti)
303  :m_pFun((void*)a_pFun)
304  , m_iArgc(10)
305  , m_iPri(-1)
306  , m_eOprtAsct(oaNONE)
307  , m_iCode(cmFUNC_BULK)
308  , m_iType(tpDBL)
309  , m_bAllowOpti(a_bAllowOpti)
310  {}
311 
312 
313  ParserCallback::ParserCallback(multfun_type a_pFun, bool a_bAllowOpti)
314  :m_pFun((void*)a_pFun)
315  , m_iArgc(-1)
316  , m_iPri(-1)
317  , m_eOprtAsct(oaNONE)
318  , m_iCode(cmFUNC)
319  , m_iType(tpDBL)
320  , m_bAllowOpti(a_bAllowOpti)
321  {}
322 
323 
324  ParserCallback::ParserCallback(strfun_type1 a_pFun, bool a_bAllowOpti)
325  :m_pFun((void*)a_pFun)
326  , m_iArgc(0)
327  , m_iPri(-1)
328  , m_eOprtAsct(oaNONE)
329  , m_iCode(cmFUNC_STR)
330  , m_iType(tpSTR)
331  , m_bAllowOpti(a_bAllowOpti)
332  {}
333 
334 
335  ParserCallback::ParserCallback(strfun_type2 a_pFun, bool a_bAllowOpti)
336  :m_pFun((void*)a_pFun)
337  , m_iArgc(1)
338  , m_iPri(-1)
339  , m_eOprtAsct(oaNONE)
340  , m_iCode(cmFUNC_STR)
341  , m_iType(tpSTR)
342  , m_bAllowOpti(a_bAllowOpti)
343  {}
344 
345 
346  ParserCallback::ParserCallback(strfun_type3 a_pFun, bool a_bAllowOpti)
347  :m_pFun((void*)a_pFun)
348  , m_iArgc(2)
349  , m_iPri(-1)
350  , m_eOprtAsct(oaNONE)
351  , m_iCode(cmFUNC_STR)
352  , m_iType(tpSTR)
353  , m_bAllowOpti(a_bAllowOpti)
354  {}
355 
356 
357  ParserCallback::ParserCallback(strfun_type4 a_pFun, bool a_bAllowOpti)
358  :m_pFun((void*)a_pFun)
359  , m_iArgc(3)
360  , m_iPri(-1)
361  , m_eOprtAsct(oaNONE)
362  , m_iCode(cmFUNC_STR)
363  , m_iType(tpSTR)
364  , m_bAllowOpti(a_bAllowOpti)
365  {}
366 
367 
368  ParserCallback::ParserCallback(strfun_type5 a_pFun, bool a_bAllowOpti)
369  :m_pFun((void*)a_pFun)
370  , m_iArgc(4)
371  , m_iPri(-1)
372  , m_eOprtAsct(oaNONE)
373  , m_iCode(cmFUNC_STR)
374  , m_iType(tpSTR)
375  , m_bAllowOpti(a_bAllowOpti)
376  {}
377 
378 
379  /** \brief Default constructor.
380  \throw nothrow
381  */
383  :m_pFun(0)
384  , m_iArgc(0)
385  , m_iPri(-1)
386  , m_eOprtAsct(oaNONE)
387  , m_iCode(cmUNKNOWN)
388  , m_iType(tpVOID)
389  , m_bAllowOpti(0)
390  {}
391 
392 
393  /** \brief Copy constructor.
394  \throw nothrow
395  */
397  {
398  m_pFun = ref.m_pFun;
399  m_iArgc = ref.m_iArgc;
400  m_bAllowOpti = ref.m_bAllowOpti;
401  m_iCode = ref.m_iCode;
402  m_iType = ref.m_iType;
403  m_iPri = ref.m_iPri;
404  m_eOprtAsct = ref.m_eOprtAsct;
405  }
406 
407  ParserCallback & ParserCallback::operator=(const ParserCallback & ref)
408  {
409  if (this != &ref)
410  {
411  m_pFun = ref.m_pFun;
412  m_iArgc = ref.m_iArgc;
413  m_bAllowOpti = ref.m_bAllowOpti;
414  m_iCode = ref.m_iCode;
415  m_iType = ref.m_iType;
416  m_iPri = ref.m_iPri;
417  m_eOprtAsct = ref.m_eOprtAsct;
418  }
419  return *this;
420  }
421 
422  /** \brief Clone this instance and return a pointer to the new instance. */
424  {
425  return new ParserCallback(*this);
426  }
427 
428 
429  /** \brief Return tru if the function is conservative.
430 
431  Conservative functions return always the same result for the same argument.
432  \throw nothrow
433  */
435  {
436  return m_bAllowOpti;
437  }
438 
439 
440  /** \brief Get the callback address for the parser function.
441 
442  The type of the address is void. It needs to be recasted according to the
443  argument number to the right type.
444 
445  \throw nothrow
446  \return #pFun
447  */
449  {
450  return m_pFun;
451  }
452 
453 
454  /** \brief Return the callback code. */
456  {
457  return m_iCode;
458  }
459 
460 
461  ETypeCode ParserCallback::GetType() const
462  {
463  return m_iType;
464  }
465 
466 
467  /** \brief Return the operator precedence.
468  \throw nothrown
469 
470  Only valid if the callback token is an operator token (binary or infix).
471  */
473  {
474  return m_iPri;
475  }
476 
477 
478  /** \brief Return the operators associativity.
479  \throw nothrown
480 
481  Only valid if the callback token is a binary operator token.
482  */
484  {
485  return m_eOprtAsct;
486  }
487 
488 
489  /** \brief Returns the number of function Arguments. */
491  {
492  return m_iArgc;
493  }
494 } // namespace mu
495 
496 #if defined(_MSC_VER)
497  #pragma warning(pop)
498 #endif
ParserCallback()
Default constructor.
value_type(* strfun_type1)(const char_type *)
Callback type used for functions taking a string as an argument.
Definition: muParserDef.h:397
user defined binary operator
Definition: muParserDef.h:174
value_type(* bulkfun_type1)(int, int, value_type)
Callback type used for functions with a single arguments.
Definition: muParserDef.h:364
value_type(* bulkfun_type8)(int, int, value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type)
Callback type used for functions with eight arguments.
Definition: muParserDef.h:385
value_type(* fun_type5)(value_type, value_type, value_type, value_type, value_type)
Callback type used for functions with five arguments.
Definition: muParserDef.h:343
value_type(* bulkfun_type2)(int, int, value_type, value_type)
Callback type used for functions with two arguments.
Definition: muParserDef.h:367
value_type(* fun_type1)(value_type)
Callback type used for functions with a single arguments.
Definition: muParserDef.h:331
value_type(* strfun_type4)(const char_type *, value_type, value_type, value_type)
Callback type used for functions taking a string and a value as arguments.
Definition: muParserDef.h:406
value_type(* bulkfun_type9)(int, int, value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type)
Callback type used for functions with nine arguments.
Definition: muParserDef.h:388
value_type(* strfun_type3)(const char_type *, value_type, value_type)
Callback type used for functions taking a string and two values as arguments.
Definition: muParserDef.h:403
ECmdCode GetCode() const
Return the callback code.
bool IsOptimizable() const
Return tru if the function is conservative.
value_type(* multfun_type)(const value_type *, int)
Callback type used for functions with a variable argument list.
Definition: muParserDef.h:394
Code for a generic function item.
Definition: muParserDef.h:170
EOprtAssociativity
Parser operator precedence values.
Definition: muParserDef.h:199
value_type(* fun_type8)(value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type)
Callback type used for functions with eight arguments.
Definition: muParserDef.h:352
value_type(* bulkfun_type5)(int, int, value_type, value_type, value_type, value_type, value_type)
Callback type used for functions with five arguments.
Definition: muParserDef.h:376
value_type(* bulkfun_type6)(int, int, value_type, value_type, value_type, value_type, value_type, value_type)
Callback type used for functions with six arguments.
Definition: muParserDef.h:379
value_type(* strfun_type5)(const char_type *, value_type, value_type, value_type, value_type)
Callback type used for functions taking a string and two values as arguments.
Definition: muParserDef.h:409
ParserCallback * Clone() const
Clone this instance and return a pointer to the new instance.
EOprtAssociativity GetAssociativity() const
Return the operators associativity.
value_type(* fun_type10)(value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type)
Callback type used for functions with ten arguments.
Definition: muParserDef.h:358
value_type(* fun_type9)(value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type)
Callback type used for functions with nine arguments.
Definition: muParserDef.h:355
ETypeCode
Types internally used by the parser.
Definition: muParserDef.h:183
value_type(* fun_type2)(value_type, value_type)
Callback type used for functions with two arguments.
Definition: muParserDef.h:334
ECmdCode
Bytecode values.
Definition: muParserDef.h:135
String type (Function arguments and constants only, no string variables)
Definition: muParserDef.h:185
value_type(* strfun_type2)(const char_type *, value_type)
Callback type used for functions taking a string and a value as arguments.
Definition: muParserDef.h:400
Namespace for mathematical applications.
Definition: muParser.cpp:46
value_type(* fun_type3)(value_type, value_type, value_type)
Callback type used for functions with three arguments.
Definition: muParserDef.h:337
value_type(* fun_type0)()
Callback type used for functions without arguments.
Definition: muParserDef.h:328
Undefined type.
Definition: muParserDef.h:187
Special callbacks for Bulk mode with an additional parameter for the bulk index.
Definition: muParserDef.h:172
Definition of the parser callback class.
Code for a function with a string parameter.
Definition: muParserDef.h:171
void * GetAddr() const
Get the callback address for the parser function.
value_type(* bulkfun_type7)(int, int, value_type, value_type, value_type, value_type, value_type, value_type, value_type)
Callback type used for functions with seven arguments.
Definition: muParserDef.h:382
uninitialized item
Definition: muParserDef.h:178
value_type(* fun_type4)(value_type, value_type, value_type, value_type)
Callback type used for functions with four arguments.
Definition: muParserDef.h:340
value_type(* fun_type7)(value_type, value_type, value_type, value_type, value_type, value_type, value_type)
Callback type used for functions with seven arguments.
Definition: muParserDef.h:349
int GetArgc() const
Returns the number of function Arguments.
value_type(* bulkfun_type4)(int, int, value_type, value_type, value_type, value_type)
Callback type used for functions with four arguments.
Definition: muParserDef.h:373
value_type(* bulkfun_type0)(int, int)
Callback type used for functions without arguments.
Definition: muParserDef.h:361
Floating point variables.
Definition: muParserDef.h:186
value_type(* bulkfun_type3)(int, int, value_type, value_type, value_type)
Callback type used for functions with three arguments.
Definition: muParserDef.h:370
Encapsulation of prototypes for a numerical parser function.
int GetPri() const
Return the operator precedence.
value_type(* fun_type6)(value_type, value_type, value_type, value_type, value_type, value_type)
Callback type used for functions with six arguments.
Definition: muParserDef.h:346
value_type(* bulkfun_type10)(int, int, value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type)
Callback type used for functions with ten arguments.
Definition: muParserDef.h:391