SciDAVis  1.D4
MuParserScript.h
Go to the documentation of this file.
1 /***************************************************************************
2  File : MuParserScript.h
3  Project : SciDAVis
4  --------------------------------------------------------------------
5  Copyright : (C) 2009 Knut Franke
6  Email (use @ for *) : Knut.Franke*gmx.net
7  Description : Evaluate mathematical expressions using muParser
8 
9  ***************************************************************************/
10 
11 /***************************************************************************
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * *
18  * This program is distributed in the hope that it will be useful, *
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
21  * GNU General Public License for more details. *
22  * *
23  * You should have received a copy of the GNU General Public License *
24  * along with this program; if not, write to the Free Software *
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
26  * Boston, MA 02110-1301 USA *
27  * *
28  ***************************************************************************/
29 
30 #ifndef MU_PARSER_SCRIPT_H
31 #define MU_PARSER_SCRIPT_H
32 
33 #include "Script.h"
34 #include <QtCore/QMap>
35 #include <muParser.h>
36 
37 class QByteArray;
38 class Column;
39 
40 class MuParserScript : public Script {
41  Q_OBJECT
42 
43  public:
44  MuParserScript(ScriptingEnv *environment, const QString &code, QObject *context=0, const QString &name="<input>");
45 
46  public slots:
47  bool compile(bool asFunction = true);
48  QVariant eval();
49  bool exec() { return eval().isValid(); }
50  bool setQObject(QObject *value, const char *name) { Q_UNUSED(value); Q_UNUSED(name); return false; }
51  bool setDouble(double value, const char *name);
52  bool setInt(int value, const char *name) { return setDouble(static_cast<double>(value), name); }
53 
54  private:
55  static double *variableFactory(const char *name, void *self);
56  static double statementSeparator(double a, double b);
57  static double tableColumnFunction(const char *columnPath);
58  static double tableColumn_Function(double columnIndex);
59  static double tableColumn__Function(const char *tableName, double columnIndex);
60  static double tableCellFunction(const char *columnPath, double rowIndex);
61  static double tableCell_Function(double columnIndex, double rowIndex);
62  static double matrixCellFunction(double rowIndex, double columnIndex);
63 
64  protected:
65  Column *resolveColumnPath(const QString &path);
66  bool translateLegacyFunctions(QString &input);
67 
68  private:
69  mu::Parser m_parser;
70  QMap<QByteArray, double> m_variables;
71 
73 };
74 
75 #endif // ifndef MU_PARSER_SCRIPT_H