\returns the result of the operation*/
typedef std::function<QVariant(const QVariant&op1,const QVariant&op2,Engine&engine)>BinaryOperatorCall;
+/** Helper type to select the correct overload from several variants.
+If your compiler complains that it cannot resolve an overloaded pointer when calling BinaryOperator::setCallback then cast to this type to help the compiler:
+\code
+myBinaryOp.setCallback(BinaryOperatorCall_FP(myFunction),type1,type2);
+\endcode
+*/
+typedef QVariant(*BinaryOperatorCall_FP)(const QVariant&op1,const QVariant&op2,Engine&engine);
+
/** \brief Wraps a particular binary operator.
You can use the methods of this class to change the routines that handle the operator and the types on which it operates. Instances of this class are implicitly shared - meaning calls on a copy of an instance are also visible on the original and all other copies.
*/
void setCallback(BinaryOperatorCall callback,int type1,int type2);
- ///convenience override to make the use of overloaded function pointers easier
- void setCallback(QVariant(*fp)(const QVariant&,const QVariant&,Engine&),QString type1,QString type2)
- {setCallback(BinaryOperatorCall(fp),type1,type2);}
- ///convenience override to make the use of overloaded function pointers easier
- void setCallback(QVariant(*fp)(const QVariant&,const QVariant&,Engine&),int type1,int type2)
- {setCallback(BinaryOperatorCall(fp),type1,type2);}
-
/**returns the callback function attached to the type or NULL if there is none*/
BinaryOperatorCall getCallback(QString type1,QString type2)const;
/**returns the callback function attached to the type or NULL if there is none*/
*/
typedef std::function<QVariant(const QList<QVariant>&args,Engine&engine)>Function;
+/** Helper type to select the correct overload from several variants.
+If your compiler complains that it cannot resolve an overloaded pointer when calling Engine::setFunction then cast to this type to help the compiler:
+\code
+myEngine.setFunction("hellofunc",Function_FP(myFunction));
+\endcode
+*/
+typedef QVariant(*Function_FP)(const QList<QVariant>&args,Engine&engine);
+
/**wraps the parser routine for a literal
\param expr the original expression string
*/
typedef std::function<QPair<QString,QVariant>(const QString&expr,Engine&engine,int start)>LiteralParser;
+/** Helper type to select the correct overload from several variants.
+If your compiler complains that it cannot resolve an overloaded pointer when calling Engine::setLiteralParser then cast to this type to help the compiler:
+\code
+myEngine.setLiteralParser(Function_FP(myDecimal),"123456789");
+\endcode
+*/
+typedef QPair<QString,QVariant>(*LiteralParser_FP)(const QString&expr,Engine&engine,int start);
+
/**Wraps a cast function to convert various types into another type.
\param orig the original value of any type
\param engine the engine calling the cast
\returns the converted value that conforms to the expected type*/
typedef std::function<QVariant(const QVariant&orig,const Engine&engine)>TypeCast;
+/** Helper type to select the correct overload from several variants.
+If your compiler complains that it cannot resolve an overloaded pointer when calling Engine::setAutoCast then cast to this type to help the compiler:
+\code
+myEngine.setAutoCast(targetType,QList<int>()<<origintype,TypeCast_FP(myCast));
+\endcode
+*/
+typedef QVariant(*TypeCast_FP)(const QVariant&orig,const Engine&engine);
+
/**The calculation engine of .
Instances of this class can be configured to represent a specific system of functions and operators.
DECLARE_DPTR(d);
public:
///instantiates an engine object
- Engine(QObject*parent=0);
+ explicit Engine(QObject*parent=0);
///true if the named variable exists in this engine
Q_INVOKABLE virtual bool hasVariable(QString)const;
IgnoredTokenMask=0xf0000000,
};
///creates an empty/invalid token
- Token(Position pos=Position(-1,-1));
+ explicit Token(Position pos=Position(-1,-1));
///creates a token from a parsed piece of string, only generic types can be used
Token(QString,Type,Position pos=Position(-1,-1));
///creates a literal token
int iid=QVariant::LongLong;
eng.setAutoCast(fid, QList<int>()<<QVariant::Int<<QVariant::LongLong<<QVariant::UInt<<QVariant::ULongLong, fltCast, 30);
//unary
- eng.unaryOperator("-").setCallback(floatMinus,fid);
+ eng.unaryOperator("-").setCallback(UnaryOperatorCall_FP(floatMinus),fid);
eng.unaryOperator("+").setCallback(floatPlus,fid);
//binary
eng.binaryOperator("==",60).setCallback(fltEq,fid,fid);
eng.binaryOperator(">",60).setCallback(fltGt,fid,fid);
eng.binaryOperator(">").setCallback(fltGt,iid,fid);
eng.binaryOperator(">").setCallback(fltGt,fid,iid);
- eng.binaryOperator("-",80).setCallback(floatMinus,fid,fid);
- eng.binaryOperator("-").setCallback(floatMinus,iid,fid);
- eng.binaryOperator("-").setCallback(floatMinus,fid,iid);
+ eng.binaryOperator("-",80).setCallback(BinaryOperatorCall_FP(floatMinus),fid,fid);
+ eng.binaryOperator("-").setCallback(BinaryOperatorCall_FP(floatMinus),iid,fid);
+ eng.binaryOperator("-").setCallback(BinaryOperatorCall_FP(floatMinus),fid,iid);
eng.binaryOperator("+",80).setCallback(floatAdd,fid,fid);
eng.binaryOperator("+").setCallback(floatAdd,iid,fid);
eng.binaryOperator("+").setCallback(floatAdd,fid,iid);
//cast
eng.setFunction("int",intFunc);
//unaries
- eng.unaryOperator("-").setCallback(intMinus,iid);
+ eng.unaryOperator("-").setCallback(UnaryOperatorCall_FP(intMinus),iid);
eng.unaryOperator("+").setCallback(intPlus,iid);
eng.unaryOperator("~").setCallback(intNot,iid);
//binaries
eng.binaryOperator(">=",60).setCallback(intGe,iid,iid);
eng.binaryOperator("<",60).setCallback(intLt,iid,iid);
eng.binaryOperator(">",60).setCallback(intGt,iid,iid);
- eng.binaryOperator("-",80).setCallback(intMinus,iid,iid);
+ eng.binaryOperator("-",80).setCallback(BinaryOperatorCall_FP(intMinus),iid,iid);
eng.binaryOperator("+",80).setCallback(intAdd,iid,iid);
eng.binaryOperator("*",90).setCallback(intMult,iid,iid);
eng.binaryOperator("/",90).setCallback(intDiv,iid,iid);
\returns the result of the operation*/
typedef std::function<QVariant(const QVariant&op,Engine&engine)>UnaryOperatorCall;
+/** Helper type to select the correct overload from several variants.
+If your compiler complains that it cannot resolve an overloaded pointer when calling UnaryOperator::setCallback then cast to this type to help the compiler:
+\code
+myUnaryOp.setCallback(UnaryOperatorCall_FP(myFunction),type);
+\endcode
+*/
+typedef QVariant(*UnaryOperatorCall_FP)(const QVariant&op,Engine&engine);
+
/** \brief Wraps a particular unary operator.
You can use the methods of this class to change the routines that handle the operator and the types on which it operates. Instances of this class are implicitly shared - meaning calls on a copy of an instance are also visible on the original and all other copies.
*/
void setCallback(UnaryOperatorCall callback,int type);
- ///convenience overload to make the use of overloaded function pointers easier
- void setCallback(QVariant(*fp)(const QVariant&,Engine&),int type)
- {setCallback(UnaryOperatorCall(fp),type);}
- ///convenience overload to make the use of overloaded function pointers easier
- void setCallback(QVariant(*fp)(const QVariant&,Engine&),QString type)
- {setCallback(UnaryOperatorCall(fp),type);}
/**returns the callback function attached to the type or NULL if there is none*/
UnaryOperatorCall getCallback(QString type)const;
/**returns the callback function attached to the type or NULL if there is none*/