File indexing completed on 2024-05-12 05:55:15
0001 // This file is part of the SpeedCrunch project 0002 // Copyright (C) 2004 Ariya Hidayat <ariya@kde.org> 0003 // Copyright (C) 2008-2009, 2013 @heldercorreia 0004 // 0005 // This program is free software; you can redistribute it and/or 0006 // modify it under the terms of the GNU General Public License 0007 // as published by the Free Software Foundation; either version 2 0008 // of the License, or (at your option) any later version. 0009 // 0010 // This program is distributed in the hope that it will be useful, 0011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 // GNU General Public License for more details. 0014 // 0015 // You should have received a copy of the GNU General Public License 0016 // along with this program; see the file COPYING. If not, write to 0017 // the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0018 // Boston, MA 02110-1301, USA. 0019 0020 #ifndef CORE_FUNCTION_H 0021 #define CORE_FUNCTION_H 0022 0023 #include "errors.h" 0024 #include "quantity.h" 0025 0026 #include <QHash> 0027 #include <QObject> 0028 #include <QStringList> 0029 #include <QVector> 0030 0031 class Function; 0032 0033 class Function : public QObject { 0034 Q_OBJECT 0035 public: 0036 typedef QVector<Quantity> ArgumentList; 0037 typedef Quantity (*FunctionImpl)(Function*, const ArgumentList&); 0038 0039 Function(const QString& identifier, FunctionImpl ptr, QObject* parent = 0) 0040 : QObject(parent) 0041 , m_identifier(identifier) 0042 , m_ptr(ptr) 0043 { } 0044 0045 const QString& identifier() const { return m_identifier; } 0046 const QString& name() const { return m_name; } 0047 const QString& usage() const { return m_usage; } 0048 Error error() const { return m_error; } 0049 Quantity exec(const ArgumentList&); 0050 0051 void setName(const QString& name) { m_name = name; } 0052 void setUsage(const QString& usage) { m_usage = usage; } 0053 void setError(Error error) { m_error = error; } 0054 0055 private: 0056 Q_DISABLE_COPY(Function) 0057 Function(); 0058 0059 QString m_identifier; 0060 QString m_name; 0061 QString m_usage; 0062 Error m_error; 0063 FunctionImpl m_ptr; 0064 }; 0065 0066 class FunctionRepo : public QObject { 0067 Q_OBJECT 0068 public: 0069 static FunctionRepo* instance(); 0070 0071 void insert(Function*); 0072 QStringList getIdentifiers() const; 0073 Function* find(const QString& identifier) const; 0074 0075 public slots: 0076 void retranslateText(); 0077 0078 private: 0079 Q_DISABLE_COPY(FunctionRepo) 0080 FunctionRepo(); 0081 0082 void createFunctions(); 0083 void setFunctionNames(); 0084 void setNonTranslatableFunctionUsages(); 0085 void setTranslatableFunctionUsages(); 0086 0087 QHash<QString, Function*> m_functions; 0088 }; 0089 0090 #endif // CORE_FUNCTION_H