1 #ifndef MUP_IVALUE_H
2 #define MUP_IVALUE_H
3 
4 /** \file
5     \brief Definition of the virtual base class used for all parser values.
6 
7 <pre>
8                __________                                 ____  ___
9     _____  __ _\______   \_____ _______  ______ __________\   \/  /
10    /     \|  |  \     ___/\__  \\_  __ \/  ___// __ \_  __ \     /
11   |  Y Y  \  |  /    |     / __ \|  | \/\___ \\  ___/|  | \/     \
12   |__|_|  /____/|____|    (____  /__|  /____  >\___  >__| /___/\  \
13         \/                     \/           \/     \/           \_/
14   Copyright (C) 2021 Ingo Berg, et al.
15   All rights reserved.
16 
17   Redistribution and use in source and binary forms, with or without
18   modification, are permitted provided that the following conditions are met:
19 
20    * Redistributions of source code must retain the above copyright notice,
21      this list of conditions and the following disclaimer.
22    * Redistributions in binary form must reproduce the above copyright notice,
23      this list of conditions and the following disclaimer in the documentation
24      and/or other materials provided with the distribution.
25 
26   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
27   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29   IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
30   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35   POSSIBILITY OF SUCH DAMAGE.
36 </pre>
37 */
38 #include "mpIToken.h"
39 #include "mpFwdDecl.h"
40 
41 MUP_NAMESPACE_START
42 
43   //------------------------------------------------------------------------------
44   /** \brief Interface to be implemented by all classes representing values.
45 
46     IValue is the common base class of both the Value and Variable classes.
47   */
48   class IValue : public IToken
49   {
50   friend std::ostream& operator<<(std::ostream &a_Stream, const IValue &a_Val);
51   friend std::wostream& operator<<(std::wostream &a_Stream, const IValue &a_Val);
52 
53   public:
54 
55     explicit IValue(ECmdCode a_iCode);
56     IValue(ECmdCode a_iCode, const string_type &a_sIdent);
57 
58     bool operator==(const IValue &a_Val) const;
59     bool operator!=(const IValue &a_Val) const;
60     bool operator< (const IValue &a_Val) const;
61     bool operator> (const IValue &a_Val) const;
62     bool operator<=(const IValue &a_Val) const;
63     bool operator>=(const IValue &a_Val) const;
64 
65     virtual ICallback* AsICallback();
66     virtual IValue* AsIValue();
67     virtual Value* AsValue() = 0;
68 
69     virtual IValue& operator=(int_type val) = 0;
70     virtual IValue& operator=(float_type val) = 0;
71     virtual IValue& operator=(string_type val) = 0;
72     virtual IValue& operator=(bool_type val) = 0;
73     virtual IValue& operator=(const cmplx_type &val) = 0;
74     virtual IValue& operator=(const matrix_type &val) = 0;
75             IValue& operator=(const IValue &ref);
76 
77     virtual IValue& operator+=(const IValue &ref) = 0;
78     virtual IValue& operator-=(const IValue &ref) = 0;
79     virtual IValue& operator*=(const IValue &ref) = 0;
80 
81     virtual IValue& At(int nRow, int nCol = 0) = 0;
82     virtual IValue& At(const IValue &nRows, const IValue &nCols) = 0;
83 
84     virtual int_type GetInteger() const = 0;
85     virtual float_type GetFloat() const = 0;
86     virtual float_type GetImag() const = 0;
87     virtual bool GetBool() const = 0;
88     virtual const cmplx_type& GetComplex() const = 0;
89     virtual const string_type&  GetString() const = 0;
90     virtual const matrix_type& GetArray() const = 0;
91     virtual char_type GetType() const = 0;
92     virtual int GetRows() const = 0;
93     virtual int GetCols() const = 0;
94 
95     virtual string_type ToString() const;
96 
97     //---------------------------------------------------------------------------
98     /** \brief Returns the dimension of the value represented by a value object.
99 
100         The value represents the dimension of the object. Possible value are:
101         <ul>
102           <li>0 - scalar</li>
103           <li>1 - vector</li>
104           <li>2 - matrix</li>
105         </ul>
106     */
GetDim()107     inline int GetDim() const
108     {
109       return (IsMatrix()) ? GetArray().GetDim() : 0;
110     }
111 
112     //---------------------------------------------------------------------------
113     virtual bool  IsVariable() const = 0;
114 
115     //---------------------------------------------------------------------------
116     /** \brief Returns true if the type is either floating point or interger.
117         \throw nothrow
118     */
IsNonComplexScalar()119     inline bool IsNonComplexScalar() const
120     {
121       char_type t = GetType();
122       return t=='f' || t=='i';
123     }
124 
125     //---------------------------------------------------------------------------
126     /** \brief Returns true if the type is not a vector and not a string.
127         \throw nothrow
128     */
IsScalar()129     inline bool IsScalar() const
130     {
131       char_type t = GetType();
132       return t=='f' || t=='i' || t=='c';
133     }
134 
135     //---------------------------------------------------------------------------
136     /** \brief Returns true if this value is a noncomplex integer.
137         \throw nothrow
138     */
IsInteger()139     inline bool IsInteger() const
140     {
141       // checking the type is is insufficient. The integer could be disguised
142       // as a float or a complex value
143       return IsScalar() && GetImag()==0 && GetFloat()==(int_type)GetFloat();
144     }
145 
146     //---------------------------------------------------------------------------
147     /** \brief Returns true if this value is an array.
148         \throw nothrow
149     */
IsMatrix()150     inline bool IsMatrix() const
151     {
152       return GetType() == 'm';
153     }
154 
155     //---------------------------------------------------------------------------
156     /** \brief Returns true if this value is a complex value.
157         \throw nothrow
158     */
IsComplex()159     inline bool IsComplex() const
160     {
161       return GetType() == 'c' && GetImag()!=0;
162     }
163 
164     //---------------------------------------------------------------------------
165     /** \brief Returns true if this value is a string value.
166         \throw nothrow
167     */
IsString()168     inline bool IsString() const
169     {
170       return GetType() == 's';
171     }
172 
173   protected:
174     virtual ~IValue();
175   }; // class IValue
176 
177   //---------------------------------------------------------------------------------------------
178   Value operator*(const IValue& lhs, const IValue& rhs);
179 }  // namespace mu
180 
181 #endif
182 
183 
184