31 #ifndef ONCE_FPARSER_TYPES_H_
32 #define ONCE_FPARSER_TYPES_H_
37 #ifdef ONCE_FPARSER_H_
41 namespace FUNCTIONPARSERTYPES
51 cAtan, cAtan2, cAtanh,
54 cCos, cCosh, cCot, cCsc,
55 cExp, cExp2, cFloor, cHypot,
58 cInt, cLog, cLog10, cLog2, cMax, cMin,
62 cSec, cSin, cSinh, cSqrt, cTan, cTanh,
69 cNeg, cAdd, cSub, cMul, cDiv, cMod,
70 cEqual, cNEqual, cLess, cLessOrEq, cGreater, cGreaterOrEq,
78 #ifdef FP_SUPPORT_OPTIMIZER
105 #ifdef ONCE_FPARSER_H_
106 struct FuncDefinition
117 #ifdef FUNCTIONPARSER_SUPPORT_DEBUGGING
120 struct name { } name;
125 inline bool okForInt()
const {
return (flags & OkForInt) != 0; }
126 inline bool complexOnly()
const {
return (flags & ComplexOnly) != 0; }
129 #ifdef FUNCTIONPARSER_SUPPORT_DEBUGGING
130 # define FP_FNAME(n) n
132 # define FP_FNAME(n) {}
137 const FuncDefinition Functions[]=
139 { FP_FNAME(
"abs"), 1, FuncDefinition::OkForInt },
140 { FP_FNAME(
"acos"), 1, FuncDefinition::AngleOut },
141 { FP_FNAME(
"acosh"), 1, FuncDefinition::AngleOut },
142 { FP_FNAME(
"arg"), 1, FuncDefinition::AngleOut | FuncDefinition::ComplexOnly },
143 { FP_FNAME(
"asin"), 1, FuncDefinition::AngleOut },
144 { FP_FNAME(
"asinh"), 1, FuncDefinition::AngleOut },
145 { FP_FNAME(
"atan"), 1, FuncDefinition::AngleOut },
146 { FP_FNAME(
"atan2"), 2, FuncDefinition::AngleOut },
147 { FP_FNAME(
"atanh"), 1, 0 },
148 { FP_FNAME(
"cbrt"), 1, 0 },
149 { FP_FNAME(
"ceil"), 1, 0 },
150 { FP_FNAME(
"conj"), 1, FuncDefinition::ComplexOnly },
151 { FP_FNAME(
"cos"), 1, FuncDefinition::AngleIn },
152 { FP_FNAME(
"cosh"), 1, FuncDefinition::AngleIn },
153 { FP_FNAME(
"cot"), 1, FuncDefinition::AngleIn },
154 { FP_FNAME(
"csc"), 1, FuncDefinition::AngleIn },
155 { FP_FNAME(
"exp"), 1, 0 },
156 { FP_FNAME(
"exp2"), 1, 0 },
157 { FP_FNAME(
"floor"), 1, 0 },
158 { FP_FNAME(
"hypot"), 2, 0 },
159 { FP_FNAME(
"if"), 0, FuncDefinition::OkForInt },
160 { FP_FNAME(
"imag"), 1, FuncDefinition::ComplexOnly },
161 { FP_FNAME(
"int"), 1, 0 },
162 { FP_FNAME(
"log"), 1, 0 },
163 { FP_FNAME(
"log10"), 1, 0 },
164 { FP_FNAME(
"log2"), 1, 0 },
165 { FP_FNAME(
"max"), 2, FuncDefinition::OkForInt },
166 { FP_FNAME(
"min"), 2, FuncDefinition::OkForInt },
167 { FP_FNAME(
"polar"), 2, FuncDefinition::ComplexOnly | FuncDefinition::AngleIn },
168 { FP_FNAME(
"pow"), 2, 0 },
169 { FP_FNAME(
"real"), 1, FuncDefinition::ComplexOnly },
170 { FP_FNAME(
"sec"), 1, FuncDefinition::AngleIn },
171 { FP_FNAME(
"sin"), 1, FuncDefinition::AngleIn },
172 { FP_FNAME(
"sinh"), 1, FuncDefinition::AngleIn },
173 { FP_FNAME(
"sqrt"), 1, 0 },
174 { FP_FNAME(
"tan"), 1, FuncDefinition::AngleIn },
175 { FP_FNAME(
"tanh"), 1, FuncDefinition::AngleIn },
176 { FP_FNAME(
"trunc"), 1, 0 }
185 NamePtr(
const char* n,
unsigned l): name(n), nameLength(l) {}
187 inline bool operator==(
const NamePtr& rhs)
const
189 return nameLength == rhs.nameLength
190 && std::memcmp(name, rhs.name, nameLength) == 0;
192 inline bool operator<(
const NamePtr& rhs)
const
194 for(
unsigned i = 0; i < nameLength; ++i)
196 if(i == rhs.nameLength)
return false;
197 const char c1 = name[i], c2 = rhs.name[i];
198 if(c1 < c2)
return true;
199 if(c2 < c1)
return false;
201 return nameLength < rhs.nameLength;
205 template<
typename Value_t>
208 enum DataType { CONSTANT, UNIT, FUNC_PTR, PARSER_PTR, VARIABLE };
213 NameData(DataType t,
unsigned v) : type(t), index(v), value() { }
214 NameData(DataType t, Value_t v) : type(t), index(), value(v) { }
218 template<
typename Value_t>
219 class NamePtrsMap:
public
220 std::map<FUNCTIONPARSERTYPES::NamePtr,
221 FUNCTIONPARSERTYPES::NameData<Value_t> >
225 const unsigned FUNC_AMOUNT =
sizeof(Functions)/
sizeof(Functions[0]);
226 #endif // ONCE_FPARSER_H_
229 #ifdef ONCE_FPARSER_H_
232 template<
typename Value_t>
233 struct FunctionParserBase<Value_t>::Data
235 unsigned mReferenceCounter;
238 ParseErrorType mParseErrorType;
240 bool mUseDegreeConversion;
241 bool mHasByteCodeFlags;
242 const char* mErrorLocation;
244 unsigned mVariablesAmount;
245 std::string mVariablesString;
246 FUNCTIONPARSERTYPES::NamePtrsMap<Value_t> mNamePtrs;
248 struct InlineVariable
250 FUNCTIONPARSERTYPES::NamePtr mName;
251 unsigned mFetchIndex;
254 typedef std::vector<InlineVariable> InlineVarNamesContainer;
255 InlineVarNamesContainer mInlineVarNames;
257 struct FuncWrapperPtrData
263 FunctionPtr mRawFuncPtr;
264 FunctionWrapper* mFuncWrapperPtr;
267 FuncWrapperPtrData();
268 ~FuncWrapperPtrData();
269 FuncWrapperPtrData(
const FuncWrapperPtrData&);
270 FuncWrapperPtrData& operator=(
const FuncWrapperPtrData&);
273 struct FuncParserPtrData
275 FunctionParserBase<Value_t>* mParserPtr;
279 std::vector<FuncWrapperPtrData> mFuncPtrs;
280 std::vector<FuncParserPtrData> mFuncParsers;
282 std::vector<unsigned> mByteCode;
283 std::vector<Value_t> mImmed;
285 #if !defined(FP_USE_THREAD_SAFE_EVAL) && \
286 !defined(FP_USE_THREAD_SAFE_EVAL_WITH_ALLOCA)
287 std::vector<Value_t> mStack;
296 Data& operator=(
const Data&);
Configuration header for the FunctionParser class.