File indexing completed on 2024-04-28 03:40:40

0001 /*************************************************************************************
0002  *  Copyright (C) 2010 by Aleix Pol <aleixpol@kde.org>                               *
0003  *                                                                                   *
0004  *  This program is free software; you can redistribute it and/or                    *
0005  *  modify it under the terms of the GNU General Public License                      *
0006  *  as published by the Free Software Foundation; either version 2                   *
0007  *  of the License, or (at your option) any later version.                           *
0008  *                                                                                   *
0009  *  This program is distributed in the hope that it will be useful,                  *
0010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of                   *
0011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                    *
0012  *  GNU General Public License for more details.                                     *
0013  *                                                                                   *
0014  *  You should have received a copy of the GNU General Public License                *
0015  *  along with this program; if not, write to the Free Software                      *
0016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
0017  *************************************************************************************/
0018 
0019 #ifndef BUILTINMETHODS_H
0020 #define BUILTINMETHODS_H
0021 #include <QVariant>
0022 #include "analitzaexport.h"
0023 #include "expressiontype.h"
0024 #include <QStringList>
0025 
0026 namespace Analitza
0027 {
0028 
0029 class Expression;
0030 class ExpressionType;
0031 
0032 class FunctionDefinition
0033 {
0034     public:
0035         virtual ~FunctionDefinition() {}
0036         
0037         /** 
0038          * Lets the user specify a function to be injected. When called it should perform whatever
0039          * the function is supposed to do.
0040          * 
0041          * @param args: specifies the values passed as arguments.
0042          * @returns the resulting expression.
0043          */
0044         virtual Expression operator()(const QList<Expression>& args)=0;
0045 };
0046 
0047 class PointerFunctionDefinition : public FunctionDefinition
0048 {
0049     public:
0050         typedef Expression (*func)(const QList<Expression>& args);
0051         
0052         explicit PointerFunctionDefinition(func call);
0053         virtual Expression operator()(const QList<Expression>& args) override;
0054     private:
0055         func m_function;
0056 };
0057 
0058 /**
0059  * \class BuiltinMethods
0060  * 
0061  * \ingroup AnalitzaModule
0062  *
0063  * \brief Manage custom commands in analitza language.
0064  */
0065 
0066 class ANALITZA_EXPORT BuiltinMethods
0067 {
0068     public:
0069         ~BuiltinMethods();
0070         
0071         //TODO improve doc of variadic function ... and put some examples ...
0072         //TODO unit test of variadic function with ExpressionType/Checker 
0073         /**
0074             Adds a new function to the system identified @p id with @p type that can be called using @p f
0075             For variadic function use ExpressionType::Any as first lambda parameter. Also, if you use ExpressionType::Any
0076             with some type as contained, then it means the all arguments of variadic function must be of the same type, equals 
0077             to the contained type.
0078         */
0079         void insertFunction(const QString& id, const ExpressionType& type, FunctionDefinition* f);
0080         
0081         /** @returns whether it exists a builtin function named like @p id */
0082         bool contains(const QString& id) const { return m_functions.contains(id); }
0083         
0084         /** @returns a map that relates all functions with their specified type. */
0085         QMap<QString, ExpressionType> varTypes() const { return m_types; }
0086         
0087         /** @returns the builtin function identified by @p id to be called. */
0088         FunctionDefinition* function(const QString& id) const { return m_functions.value(id); }
0089         
0090         /** @returns a list with the name of all registered identifiers. */
0091         QStringList identifiers() const { return m_functions.keys(); }
0092     private:
0093         QMap<QString, ExpressionType> m_types;
0094         QHash<QString, FunctionDefinition*> m_functions;
0095 };
0096 
0097 }
0098 
0099 #endif