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

0001 /*************************************************************************************
0002  *  Copyright (C) 2007 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 OBJECT_H
0020 #define OBJECT_H
0021 
0022 #include <QDebug>
0023 
0024 #include "analitzaexport.h"
0025 
0026 namespace Analitza
0027 {
0028 
0029 class AbstractExpressionVisitor;
0030 
0031 /**
0032  *    \internal
0033  *    Abstract expression tree node
0034  *    @author Aleix Pol <aleixpol@kde.org>  
0035  */
0036 
0037 //FIXME: Check for public -> protected on some members
0038 class ANALITZA_EXPORT Object
0039 {
0040     Q_GADGET
0041 public:
0042     /** ObjectType is used to describe objects. */
0043     enum ObjectType {
0044         none=0,        /**< No object type, usually means an error. */
0045         value,        /**< Describes an object as a value. */
0046         variable,    /**< Describes an object as a variable. */
0047         vector,        /**< Describes an object as a vector. */
0048         list,        /**< Describes an object as a list. */
0049         apply,        /**< Describes an object as an application. */
0050         oper,        /**< Describes an object as an operator. */
0051         container,    /**< Describes an object as a container. */
0052         matrix,        /**< Describes an object as a matrix. */
0053         matrixrow,    /**< Describes an object as a matrix row. */
0054         custom        /**< Describes a custom object */
0055     };
0056     Q_ENUM(ObjectType)
0057     
0058     /** Object destructor. Does nothing. */
0059     virtual ~Object() { /*qDebug() << "Destroying " << this;*/}
0060     
0061     /** Returns the object type of the object */
0062     enum ObjectType type() const { return m_type; }
0063     
0064     /** Returns whether it is an apply or not. */
0065     bool isApply() const { return m_type==apply; }
0066     
0067     /** Returns whether it is a container or not. */
0068     bool isContainer() const { return m_type==container; }
0069     
0070     /** Returns whether it is a none object. Useful for checking for errors */
0071     bool isNone() const { return m_type==none; }
0072 
0073     /** Returns the string representation of the object. */
0074     QString toString() const;
0075     
0076     /** Returns some string depending on the visior */
0077     virtual QVariant accept(AbstractExpressionVisitor* exp) const = 0;
0078     
0079     virtual bool isZero() const { return false; }
0080     
0081     /** 
0082         @p exp is the tree that we will compare to,
0083         @p found is where we will pass the variables store the results.
0084         
0085         It will return whether the object follows the @p pattern structure.
0086     */
0087     virtual bool matches(const Object* exp, QMap<QString, const Object*>* found) const=0;
0088     
0089     /** @returns a new and equal instance of the tree. */
0090     virtual Object* copy() const =0;
0091     
0092 protected:
0093     /** Creates an object with a @p t type */
0094     Object(enum ObjectType t) : m_type(t) {}
0095     
0096     /** Specifies the Object type. */
0097     const ObjectType m_type;
0098 };
0099 
0100 
0101 /**
0102  * \class None
0103  * 
0104  * \ingroup AnalitzaModule
0105  *
0106  * \brief A empty object that indicates usually an error in the expression tree. 
0107  * 
0108  * The default convention for an error value is None and the default text to print is 
0109  * an empty string. This class does not hold an error string, since any error string will 
0110  * be manage mainly in Analyzer, Expression and ExpressionTypeChecker. This class is just a 
0111  * flag in memory to indicate that something went wrong in the expression tree.
0112  */
0113 
0114 class ANALITZA_EXPORT None : public Object
0115 {
0116     public:
0117         explicit None() : Object(Object::none) {}
0118         ~None() override {}
0119         
0120         bool operator==(const None& ) const { return true; }
0121         
0122         QString toMathML() const { return QString(); }
0123         QString toHtml() const { return QString(); }
0124         virtual QVariant accept(Analitza::AbstractExpressionVisitor* visitor) const override;
0125         virtual bool matches(const Object* pattern, QMap<QString, const Object*>* found) const override;
0126         None* copy() const override;
0127 };
0128 
0129 }
0130 #endif