Seamly2D
Code documentation
qmuparsercallback.h
Go to the documentation of this file.
1 /***************************************************************************************************
2  **
3  ** Copyright (C) 2013 Ingo Berg
4  **
5  ** Permission is hereby granted, free of charge, to any person obtaining a copy of this
6  ** software and associated documentation files (the "Software"), to deal in the Software
7  ** without restriction, including without limitation the rights to use, copy, modify,
8  ** merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
9  ** permit persons to whom the Software is furnished to do so, subject to the following conditions:
10  **
11  ** The above copyright notice and this permission notice shall be included in all copies or
12  ** substantial portions of the Software.
13  **
14  ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
15  ** NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16  ** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17  ** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  ** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  **
20  ******************************************************************************************************/
21 
22 #ifndef QMUPARSERCALLBACK_H
23 #define QMUPARSERCALLBACK_H
24 
25 #include <qcompilerdetection.h>
26 #include <QString>
27 #include <map>
28 
29 #include "qmuparserdef.h"
30 
31 /**
32  * @file
33  * @brief Definition of the parser callback class.
34  */
35 
36 namespace qmu
37 {
38 
39 /**
40  * @brief Encapsulation of prototypes for a numerical parser function.
41  *
42  * Encapsulates the prototyp for numerical parser functions. The class stores the number of arguments for parser
43  * functions as well as additional flags indication the function is non optimizeable. The pointer to the callback
44  * function pointer is stored as void* and needs to be casted according to the argument count. Negative argument counts
45  * indicate a parser function with a variable number of arguments.
46  *
47  * @author (C) 2004-2011 Ingo Berg
48  */
50 {
51 public:
52  QmuParserCallback(fun_type0 a_pFun, bool a_bAllowOpti);
53  QmuParserCallback(fun_type1 a_pFun, bool a_bAllowOpti, int a_iPrec = -1, ECmdCode a_iCode=cmFUNC);
54  QmuParserCallback(fun_type2 a_pFun, bool a_bAllowOpti, int a_iPrec, EOprtAssociativity a_eOprtAsct);
55  QmuParserCallback(fun_type2 a_pFun, bool a_bAllowOpti);
56  QmuParserCallback(fun_type3 a_pFun, bool a_bAllowOpti);
57  QmuParserCallback(fun_type4 a_pFun, bool a_bAllowOpti);
58  QmuParserCallback(fun_type5 a_pFun, bool a_bAllowOpti);
59  QmuParserCallback(fun_type6 a_pFun, bool a_bAllowOpti);
60  QmuParserCallback(fun_type7 a_pFun, bool a_bAllowOpti);
61  QmuParserCallback(fun_type8 a_pFun, bool a_bAllowOpti);
62  QmuParserCallback(fun_type9 a_pFun, bool a_bAllowOpti);
63  QmuParserCallback(fun_type10 a_pFun, bool a_bAllowOpti);
64 
65  QmuParserCallback(bulkfun_type0 a_pFun, bool a_bAllowOpti);
66  QmuParserCallback(bulkfun_type1 a_pFun, bool a_bAllowOpti);
67  QmuParserCallback(bulkfun_type2 a_pFun, bool a_bAllowOpti);
68  QmuParserCallback(bulkfun_type3 a_pFun, bool a_bAllowOpti);
69  QmuParserCallback(bulkfun_type4 a_pFun, bool a_bAllowOpti);
70  QmuParserCallback(bulkfun_type5 a_pFun, bool a_bAllowOpti);
71  QmuParserCallback(bulkfun_type6 a_pFun, bool a_bAllowOpti);
72  QmuParserCallback(bulkfun_type7 a_pFun, bool a_bAllowOpti);
73  QmuParserCallback(bulkfun_type8 a_pFun, bool a_bAllowOpti);
74  QmuParserCallback(bulkfun_type9 a_pFun, bool a_bAllowOpti);
75  QmuParserCallback(bulkfun_type10 a_pFun, bool a_bAllowOpti);
76 
77  QmuParserCallback(multfun_type a_pFun, bool a_bAllowOpti);
78  QmuParserCallback(strfun_type1 a_pFun, bool a_bAllowOpti);
79  QmuParserCallback(strfun_type2 a_pFun, bool a_bAllowOpti);
80  QmuParserCallback(strfun_type3 a_pFun, bool a_bAllowOpti);
84 
85  Q_REQUIRED_RESULT QmuParserCallback* Clone() const;
86 
87  bool IsOptimizable() const;
88  void* GetAddr() const;
89  ECmdCode GetCode() const;
90  ETypeCode GetType() const;
91  int GetPri() const;
93  int GetArgc() const;
94 private:
95  void *m_pFun; ///< Pointer to the callback function, casted to void
96 
97  /**
98  * @brief Number of numeric function arguments
99  *
100  * This number is negative for functions with variable number of arguments. in this cases
101  * they represent the actual number of arguments found.
102  */
103  int m_iArgc;
104  int m_iPri; ///< Valid only for binary and infix operators; Operator precedence.
105  EOprtAssociativity m_eOprtAsct; ///< Operator associativity; Valid only for binary operators
108  bool m_bAllowOpti; ///< Flag indication optimizeability
109 };
110 
111 //---------------------------------------------------------------------------------------------------------------------
112 /**
113  * @brief Container for Callback objects.
114  */
115 typedef std::map<QString, QmuParserCallback> funmap_type;
116 
117 //---------------------------------------------------------------------------------------------------------------------
118 /**
119  * @brief Clone this instance and return a pointer to the new instance.
120  */
122 {
123  return new QmuParserCallback ( *this );
124 }
125 
126 //---------------------------------------------------------------------------------------------------------------------
127 /**
128  * @brief Return true if the function is conservative.
129  *
130  * Conservative functions return always the same result for the same argument.
131  * @throw nothrow
132  */
133 // cppcheck-suppress unusedFunction
135 {
136  return m_bAllowOpti;
137 }
138 
139 //---------------------------------------------------------------------------------------------------------------------
140 /**
141  * @brief Get the callback address for the parser function.
142  *
143  * The type of the address is void. It needs to be recasted according to the argument number to the right type.
144  *
145  * @throw nothrow
146  * @return #pFun
147  */
148 inline void* QmuParserCallback::GetAddr() const
149 {
150  return m_pFun;
151 }
152 
153 //---------------------------------------------------------------------------------------------------------------------
154 /**
155  * @brief Return the callback code.
156 */
158 {
159  return m_iCode;
160 }
161 
162 //---------------------------------------------------------------------------------------------------------------------
164 {
165  return m_iType;
166 }
167 
168 //---------------------------------------------------------------------------------------------------------------------
169 /**
170  * @brief Return the operator precedence.
171  * @throw nothrown
172  *
173  * Only valid if the callback token is an operator token (binary or infix).
174  */
175 inline int QmuParserCallback::GetPri() const
176 {
177  return m_iPri;
178 }
179 
180 //---------------------------------------------------------------------------------------------------------------------
181 /**
182  * @brief Return the operators associativity.
183  * @throw nothrown
184  *
185  * Only valid if the callback token is a binary operator token.
186  */
188 {
189  return m_eOprtAsct;
190 }
191 
192 //---------------------------------------------------------------------------------------------------------------------
193 /**
194  * @brief Returns the number of function Arguments.
195  */
196 inline int QmuParserCallback::GetArgc() const
197 {
198  return m_iArgc;
199 }
200 
201 } // namespace qmu
202 
203 #endif
Encapsulation of prototypes for a numerical parser function.
EOprtAssociativity m_eOprtAsct
Operator associativity; Valid only for binary operators.
bool IsOptimizable() const
Return true if the function is conservative.
int GetArgc() const
Returns the number of function Arguments.
int m_iPri
Valid only for binary and infix operators; Operator precedence.
EOprtAssociativity GetAssociativity() const
Return the operators associativity.
QmuParserCallback()
Default constructor.
Q_REQUIRED_RESULT QmuParserCallback * Clone() const
Clone this instance and return a pointer to the new instance.
void * m_pFun
Pointer to the callback function, casted to void.
bool m_bAllowOpti
Flag indication optimizeability.
int GetPri() const
Return the operator precedence.
ECmdCode GetCode() const
Return the callback code.
int m_iArgc
Number of numeric function arguments.
void * GetAddr() const
Get the callback address for the parser function.
QmuParserCallback & operator=(const QmuParserCallback &a_Fun)
ETypeCode GetType() const
Namespace for mathematical applications.
qreal(* bulkfun_type10)(int, int, qreal, qreal, qreal, qreal, qreal, qreal, qreal, qreal, qreal, qreal)
Callback type used for functions with five arguments.
Definition: qmuparserdef.h:287
std::map< QString, QmuParserCallback > funmap_type
Container for Callback objects.
qreal(* bulkfun_type4)(int, int, qreal, qreal, qreal, qreal)
Callback type used for functions with four arguments.
Definition: qmuparserdef.h:269
qreal(* fun_type7)(qreal, qreal, qreal, qreal, qreal, qreal, qreal)
Callback type used for functions with five arguments.
Definition: qmuparserdef.h:245
qreal(* fun_type10)(qreal, qreal, qreal, qreal, qreal, qreal, qreal, qreal, qreal, qreal)
Callback type used for functions with five arguments.
Definition: qmuparserdef.h:254
qreal(* fun_type3)(qreal, qreal, qreal)
Callback type used for functions with three arguments.
Definition: qmuparserdef.h:233
qreal(* fun_type8)(qreal, qreal, qreal, qreal, qreal, qreal, qreal, qreal)
Callback type used for functions with five arguments.
Definition: qmuparserdef.h:248
qreal(* bulkfun_type5)(int, int, qreal, qreal, qreal, qreal, qreal)
Callback type used for functions with five arguments.
Definition: qmuparserdef.h:272
ECmdCode
Bytecode values.
Definition: qmuparserdef.h:99
@ cmFUNC
Code for a generic function item.
Definition: qmuparserdef.h:134
qreal(* fun_type6)(qreal, qreal, qreal, qreal, qreal, qreal)
Callback type used for functions with five arguments.
Definition: qmuparserdef.h:242
qreal(* fun_type4)(qreal, qreal, qreal, qreal)
Callback type used for functions with four arguments.
Definition: qmuparserdef.h:236
qreal(* fun_type9)(qreal, qreal, qreal, qreal, qreal, qreal, qreal, qreal, qreal)
Callback type used for functions with five arguments.
Definition: qmuparserdef.h:251
qreal(* bulkfun_type1)(int, int, qreal)
Callback type used for functions with a single arguments.
Definition: qmuparserdef.h:260
qreal(* fun_type2)(qreal, qreal)
Callback type used for functions with two arguments.
Definition: qmuparserdef.h:230
qreal(* bulkfun_type2)(int, int, qreal, qreal)
Callback type used for functions with two arguments.
Definition: qmuparserdef.h:263
qreal(* bulkfun_type0)(int, int)
Callback type used for functions without arguments.
Definition: qmuparserdef.h:257
qreal(* strfun_type3)(const QString &, qreal, qreal)
Callback type used for functions taking a string and two values as arguments.
Definition: qmuparserdef.h:299
EOprtAssociativity
Parser operator precedence values.
Definition: qmuparserdef.h:165
qreal(* fun_type1)(qreal)
Callback type used for functions with a single arguments.
Definition: qmuparserdef.h:227
qreal(* bulkfun_type7)(int, int, qreal, qreal, qreal, qreal, qreal, qreal, qreal)
Callback type used for functions with five arguments.
Definition: qmuparserdef.h:278
qreal(* multfun_type)(const qreal *, int)
Callback type used for functions with a variable argument list.
Definition: qmuparserdef.h:290
qreal(* strfun_type2)(const QString &, qreal)
Callback type used for functions taking a string and a value as arguments.
Definition: qmuparserdef.h:296
qreal(* fun_type0)()
Callback type used for functions without arguments.
Definition: qmuparserdef.h:224
qreal(* bulkfun_type6)(int, int, qreal, qreal, qreal, qreal, qreal, qreal)
Callback type used for functions with five arguments.
Definition: qmuparserdef.h:275
qreal(* bulkfun_type8)(int, int, qreal, qreal, qreal, qreal, qreal, qreal, qreal, qreal)
Callback type used for functions with five arguments.
Definition: qmuparserdef.h:281
qreal(* bulkfun_type3)(int, int, qreal, qreal, qreal)
Callback type used for functions with three arguments.
Definition: qmuparserdef.h:266
qreal(* bulkfun_type9)(int, int, qreal, qreal, qreal, qreal, qreal, qreal, qreal, qreal, qreal)
Callback type used for functions with five arguments.
Definition: qmuparserdef.h:284
ETypeCode
Types internally used by the parser.
Definition: qmuparserdef.h:149
qreal(* fun_type5)(qreal, qreal, qreal, qreal, qreal)
Callback type used for functions with five arguments.
Definition: qmuparserdef.h:239
qreal(* strfun_type1)(const QString &)
Callback type used for functions taking a string as an argument.
Definition: qmuparserdef.h:293
This file contains standard definitions used by the parser.