Seamly2D
Code documentation
qmuparserbytecode.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 QMUPARSERBYTECODE_H
23 #define QMUPARSERBYTECODE_H
24 
25 #include <QVector>
26 #include <QtGlobal>
27 
28 #include "qmudef.h"
29 #include "qmuparserdef.h"
30 #include "qmuparsertoken.h"
31 
32 /**
33  * @file
34  * @brief Definition of the parser bytecode class.
35  */
36 
37 QT_WARNING_PUSH
38 QT_WARNING_DISABLE_CLANG("-Wnested-anon-types")
39 namespace qmu
40 {
41 struct SToken
42 {
44  int StackPos;
45 
46  union //
47  {
48  struct //SValData
49  {
50  qreal *ptr;
51  qreal data;
52  qreal data2;
53  } Val;
54 
55  struct //SFunData
56  {
57  // Note: generic_fun_type is merely a placeholder. The real type could be
58  // anything between gun_type1 and fun_type9. I can't use a void
59  // pointer due to constraints in the ANSI standard which allows
60  // data pointers and function pointers to differ in size.
62  int argc;
63  int idx;
64  } Fun;
65 
66  struct //SOprtData
67  {
68  qreal *ptr;
69  int offset;
70  } Oprt;
71  };
72 };
73 
75 
76 /**
77  * @brief Bytecode implementation of the Math Parser.
78  *
79  * The bytecode contains the formula converted to revers polish notation stored in a continious
80  * memory area. Associated with this data are operator codes, variable pointers, constant
81  * values and function pointers. Those are necessary in order to calculate the result.
82  * All those data items will be casted to the underlying datatype of the bytecode.
83  *
84  * @author (C) 2004-2013 Ingo Berg
85  */
87 {
88 public:
90  QmuParserByteCode(const QmuParserByteCode &a_ByteCode);
91  QmuParserByteCode& operator=(const QmuParserByteCode &a_ByteCode);
92  void Assign(const QmuParserByteCode &a_ByteCode);
93  void AddVar(qreal *a_pVar);
94  void AddVal(qreal a_fVal);
95  void AddOp(ECmdCode a_Oprt);
96  void AddIfElse(ECmdCode a_Oprt);
97  void AddAssignOp(qreal *a_pVar);
98  void AddFun(generic_fun_type a_pFun, int a_iArgc);
99  void AddBulkFun(generic_fun_type a_pFun, int a_iArgc);
100  void AddStrFun(generic_fun_type a_pFun, int a_iArgc, int a_iIdx);
101  void EnableOptimizer(bool bStat);
102  void Finalize();
103  void clear();
104  int GetMaxStackSize() const;
105  int GetSize() const;
106  const SToken* GetBase() const;
107  void AsciiDump();
108 private:
109  /** @brief Token type for internal use only. */
111 
112  /** @brief Token vector for storing the RPN. */
114 
115  /** @brief Position in the Calculation array. */
116  unsigned m_iStackPos;
117 
118  /** @brief Maximum size needed for the stack. */
119  unsigned m_iMaxStackSize;
120 
121  /** @brief The actual rpn storage. */
123 
125 
126  void ConstantFolding(ECmdCode a_Oprt);
127 };
128 
129 //---------------------------------------------------------------------------------------------------------------------
130 inline void QmuParserByteCode::EnableOptimizer(bool bStat)
131 {
132  m_bEnableOptimizer = bStat;
133 }
134 
135 //---------------------------------------------------------------------------------------------------------------------
136 inline int QmuParserByteCode::GetMaxStackSize() const
137 {
138  return static_cast<int>(m_iMaxStackSize+1);
139 }
140 
141 //---------------------------------------------------------------------------------------------------------------------
142 /**
143  * @brief Returns the number of entries in the bytecode.
144  */
145 // cppcheck-suppress unusedFunction
146 inline int QmuParserByteCode::GetSize() const
147 {
148  return m_vRPN.size();
149 }
150 
151 } // namespace qmu
152 #endif
Bytecode implementation of the Math Parser.
unsigned m_iMaxStackSize
Maximum size needed for the stack.
unsigned m_iStackPos
Position in the Calculation array.
rpn_type m_vRPN
The actual rpn storage.
QVector< SToken > rpn_type
Token vector for storing the RPN.
QmuParserToken< qreal, string_type > token_type
Token type for internal use only.
Encapsulation of the data for a single formula token.
Namespace for mathematical applications.
ECmdCode
Bytecode values.
Definition: qmuparserdef.h:99
qreal(* generic_fun_type)()
Callback type used for functions without arguments.
Definition: qmuparserdef.h:221
This file contains standard definitions used by the parser.
This file contains the parser token definition.
generic_fun_type ptr