SpeedCrunch  0.11
/usr/src/RPM/BUILD/speedcrunch-0.11/src/core/functions.h
Go to the documentation of this file.
00001 // This file is part of the SpeedCrunch project
00002 // Copyright (C) 2004 Ariya Hidayat <ariya@kde.org>
00003 // Copyright (C) 2008-2009, 2013 Helder Correia <helder.pereira.correia@gmail.com>
00004 //
00005 // This program is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU General Public License
00007 // as published by the Free Software Foundation; either version 2
00008 // of the License, or (at your option) any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; see the file COPYING.  If not, write to
00017 // the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018 // Boston, MA 02110-1301, USA.
00019 
00020 #ifndef CORE_FUNCTION_H
00021 #define CORE_FUNCTION_H
00022 
00023 #include "core/errors.h"
00024 #include "math/hmath.h"
00025 
00026 #include <QHash>
00027 #include <QObject>
00028 #include <QStringList>
00029 #include <QVector>
00030 
00031 class Function;
00032 
00033 class Function : public QObject {
00034     Q_OBJECT
00035 public:
00036     typedef QVector<HNumber> ArgumentList;
00037     typedef HNumber (*FunctionImpl)(Function*, const ArgumentList&);
00038 
00039     Function(const QString& identifier, FunctionImpl ptr, QObject* parent = 0)
00040         : QObject(parent)
00041         , m_identifier(identifier)
00042         , m_ptr(ptr)
00043     { }
00044 
00045     const QString& identifier() const { return m_identifier; }
00046     const QString& name() const { return m_name; }
00047     const QString& usage() const { return m_usage; }
00048     Error error() const { return m_error; }
00049     HNumber exec(const ArgumentList&);
00050 
00051     void setName(const QString& name) { m_name = name; }
00052     void setUsage(const QString& usage) { m_usage = usage; }
00053     void setError(Error error) { m_error = error; }
00054 
00055 private:
00056     Q_DISABLE_COPY(Function)
00057     Function();
00058 
00059     QString m_identifier;
00060     QString m_name;
00061     QString m_usage;
00062     Error m_error;
00063     FunctionImpl m_ptr;
00064 };
00065 
00066 class FunctionRepo : public QObject {
00067     Q_OBJECT
00068 public:
00069     static FunctionRepo* instance();
00070 
00071     void insert(Function*);
00072     QStringList getIdentifiers() const;
00073     Function* find(const QString& identifier) const;
00074 
00075 public slots:
00076     void retranslateText();
00077 
00078 private:
00079     Q_DISABLE_COPY(FunctionRepo)
00080     FunctionRepo();
00081 
00082     void createFunctions();
00083     void setFunctionNames();
00084     void setNonTranslatableFunctionUsages();
00085     void setTranslatableFunctionUsages();
00086 
00087     QHash<QString, Function*> m_functions;
00088 };
00089 
00090 #endif // CORE_FUNCTION_H