~ubuntu-branches/ubuntu/wily/opencollada/wily-proposed

« back to all changes in this revision

Viewing changes to Externals/MathMLSolver/include/AST/MathMLASTConstantExpression.h

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2015-05-14 17:23:27 UTC
  • Revision ID: package-import@ubuntu.com-20150514172327-f862u8envms01fra
Tags: upstream-0.1.0~20140703.ddf8f47+dfsg1
ImportĀ upstreamĀ versionĀ 0.1.0~20140703.ddf8f47+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************************************
 
2
Copyright (c) 2007 netAllied GmbH, Tettnang
 
3
 
 
4
Permission is hereby granted, free of charge, to any person
 
5
obtaining a copy of this software and associated documentation
 
6
files (the "Software"), to deal in the Software without
 
7
restriction, including without limitation the rights to use,
 
8
copy, modify, merge, publish, distribute, sublicense, and/or sell
 
9
copies of the Software, and to permit persons to whom the
 
10
Software is furnished to do so, subject to the following
 
11
conditions:
 
12
 
 
13
The above copyright notice and this permission notice shall be
 
14
included in all copies or substantial portions of the Software.
 
15
 
 
16
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
17
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 
18
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
19
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 
20
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 
21
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
22
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 
23
OTHER DEALINGS IN THE SOFTWARE.
 
24
******************************************************************************/
 
25
 
 
26
#ifndef __MATHML_AST_CONSTANT_EXPRESSION_H___
 
27
#define __MATHML_AST_CONSTANT_EXPRESSION_H___
 
28
 
 
29
#include "MathMLSolverPrerequisites.h"
 
30
#include "MathMLASTNode.h"
 
31
#include "MathMLASTArithmeticExpression.h"
 
32
#include "MathMLASTUnaryArithmeticExpression.h"
 
33
#include "MathMLASTBinaryComparisionExpression.h"
 
34
#include "MathMLASTLogicExpression.h"
 
35
#include "MathMLError.h"
 
36
 
 
37
 
 
38
namespace MathML
 
39
{
 
40
 
 
41
    /** Forward Declaration. */
 
42
    class ErrorHandler;
 
43
 
 
44
    namespace AST
 
45
    {
 
46
 
 
47
        /** Specialized constant node implementing INode. */
 
48
 
 
49
        class _MATHML_SOLVER_EXPORT ConstantExpression : public INode
 
50
        {
 
51
 
 
52
        public:
 
53
            /** The type of scalar value. */
 
54
            enum Type
 
55
            {
 
56
                SCALAR_INVALID,
 
57
                SCALAR_BOOL,
 
58
                SCALAR_LONG,
 
59
                SCALAR_DOUBLE
 
60
            };
 
61
 
 
62
        private:
 
63
            /** Type of scalar. */
 
64
            Type mType;
 
65
 
 
66
            /** Value of scalar. */
 
67
            double mValue;
 
68
 
 
69
            /** The string value... */
 
70
            String mStringValue;
 
71
 
 
72
            /** Error handler. */
 
73
            ErrorHandler* mErrorHandler;
 
74
 
 
75
        public:
 
76
            /** Enable copy construction to avoid expensive deep copies of big trees.
 
77
            */
 
78
            ConstantExpression( const ConstantExpression& ref );
 
79
 
 
80
            // see INode::accept(IVisitor* )
 
81
            virtual void accept( IVisitor* visitor ) const;
 
82
 
 
83
                        // see INode::getType()
 
84
                        virtual NodeType getNodeType() const { return CONSTANT; }
 
85
 
 
86
            /** @return a copy of this node. */
 
87
            virtual INode* clone(CloneFlags cloneFlags) const;
 
88
 
 
89
            /** Default C-tor. */
 
90
            ConstantExpression();
 
91
 
 
92
            /** D-tor. */
 
93
            virtual ~ConstantExpression();
 
94
 
 
95
            /** Assignment from the string received from the parser.
 
96
            @param str The string containing the constant value.
 
97
            @param type The scalar type to set.
 
98
            */
 
99
            ConstantExpression( const MathML::String& str, Type type );
 
100
 
 
101
            /** constructor for folded constant values.
 
102
            @param str The string containing the string value (NOT for scalar value).
 
103
            */
 
104
            ConstantExpression( const MathML::String& str );
 
105
 
 
106
            /** Initializing constructor.
 
107
            @param val The double value to initialize with.
 
108
            */
 
109
            ConstantExpression( double val );
 
110
 
 
111
            /** Initializing constructor.
 
112
            @param val The long value to initialize with.
 
113
            */
 
114
            ConstantExpression( long val );
 
115
 
 
116
            /** Initializing constructor.
 
117
            @param val The bool value to initialize with.
 
118
            */
 
119
            ConstantExpression( bool val );
 
120
 
 
121
            /** Setter for double value.
 
122
            @param str The string to set as value.
 
123
            @param type The type of scalar value.
 
124
            */
 
125
            virtual void setValue( const String& str, Type type );
 
126
 
 
127
            /** Setter for double value.
 
128
            @param val The double value to set.
 
129
            */
 
130
            virtual void setValue( double val );
 
131
 
 
132
            /** Setter for double value.
 
133
            @param val The double value to set.
 
134
            */
 
135
            virtual void setValue( long val );
 
136
 
 
137
            /** Setter for bool value.
 
138
            @param val The bool value to set.
 
139
            */
 
140
            virtual void setValue( bool val );
 
141
 
 
142
            /** Getting the type of the scalar.
 
143
            @return The type of the scalar value.
 
144
            */
 
145
            virtual Type getType() const;
 
146
 
 
147
            /** Getting the double value.
 
148
            @return The double value of scalar.
 
149
            */
 
150
            virtual double getDoubleValue() const;
 
151
 
 
152
            /** Getting the long value.
 
153
            @return The long value of scalar.
 
154
            */
 
155
            virtual long getLongValue() const;
 
156
 
 
157
            /** Getting the unsigned long value.
 
158
            @return The unsigned long value of scalar.
 
159
            */
 
160
            virtual unsigned long getUnsignedLongValue() const;
 
161
 
 
162
            /** Getting the bool value.
 
163
            @return The bool value of scalar.
 
164
            */
 
165
            virtual bool getBoolValue() const;
 
166
 
 
167
            /** Returns the string value. */
 
168
            virtual const MathML::String& getStringValue() const;
 
169
 
 
170
            /** Sets the string value. */
 
171
            virtual void setStringValue( const MathML::String& );
 
172
 
 
173
            /** Returns the ConstantExpression string. */
 
174
            virtual MathML::String toString() const;
 
175
 
 
176
 
 
177
            /** @return Currently set error handler. */
 
178
            virtual ErrorHandler* getErrorHandler() {return mErrorHandler;}
 
179
            /** Sets error handler to be used. */
 
180
            virtual void setErrorHandler( ErrorHandler* errorHandler){mErrorHandler = errorHandler;}
 
181
 
 
182
            inline ConstantExpression& operator=( const ConstantExpression &a )
 
183
            {
 
184
                // check if we are to assign ourself
 
185
 
 
186
                if ( this == &a )
 
187
                    return * this;
 
188
 
 
189
                mType = a.mType;
 
190
 
 
191
                switch ( mType )
 
192
                {
 
193
 
 
194
                case SCALAR_INVALID:
 
195
                    //assert(0);
 
196
                    break;
 
197
 
 
198
                default:
 
199
                    mValue = a.mValue;
 
200
                    break;
 
201
                }
 
202
 
 
203
                return *this;
 
204
            }
 
205
 
 
206
            inline ConstantExpression operator==( const ConstantExpression &a ) const
 
207
            {
 
208
                ConstantExpression result;
 
209
                relationalBinaryOperation( result, a, AST::BinaryComparisonExpression::EQ );
 
210
                return result;
 
211
            }
 
212
 
 
213
            inline ConstantExpression operator!=( const ConstantExpression &a ) const
 
214
            {
 
215
                ConstantExpression result;
 
216
                relationalBinaryOperation( result, a, AST::BinaryComparisonExpression::NEQ );
 
217
                return result;
 
218
            }
 
219
 
 
220
            inline ConstantExpression operator<( const ConstantExpression &a ) const
 
221
            {
 
222
                ConstantExpression result;
 
223
                relationalBinaryOperation( result, a, AST::BinaryComparisonExpression::LT );
 
224
                return result;
 
225
            }
 
226
 
 
227
            inline ConstantExpression operator>( const ConstantExpression &a ) const
 
228
            {
 
229
                ConstantExpression result;
 
230
                relationalBinaryOperation( result, a, AST::BinaryComparisonExpression::GT );
 
231
                return result;
 
232
            }
 
233
 
 
234
            inline ConstantExpression operator<=( const ConstantExpression &a ) const
 
235
            {
 
236
                ConstantExpression result;
 
237
                relationalBinaryOperation( result, a, AST::BinaryComparisonExpression::LTE );
 
238
                return result;
 
239
            }
 
240
 
 
241
            inline ConstantExpression operator>=( const ConstantExpression &a ) const
 
242
            {
 
243
                ConstantExpression result;
 
244
                relationalBinaryOperation( result, a, AST::BinaryComparisonExpression::GTE );
 
245
                return result;
 
246
            }
 
247
 
 
248
            inline ConstantExpression operator+() const
 
249
            {
 
250
                ConstantExpression result;
 
251
                unaryOperation( result, AST::UnaryExpression::ADD );
 
252
                return result;
 
253
            }
 
254
 
 
255
            inline ConstantExpression operator-() const
 
256
            {
 
257
                ConstantExpression result;
 
258
                unaryOperation( result, AST::UnaryExpression::SUB );
 
259
                return result;
 
260
            }
 
261
 
 
262
            inline ConstantExpression operator!() const
 
263
            {
 
264
                ConstantExpression result;
 
265
                unaryOperation( result, AST::UnaryExpression::NOT );
 
266
                return result;
 
267
            }
 
268
 
 
269
            inline ConstantExpression operator+( const ConstantExpression &b ) const
 
270
            {
 
271
                ConstantExpression result;
 
272
                arithmeticalBinaryOperation( result, b, AST::ArithmeticExpression::ADD );
 
273
                return result;
 
274
            }
 
275
 
 
276
            inline ConstantExpression operator-( const ConstantExpression &b ) const
 
277
            {
 
278
                ConstantExpression result;
 
279
                arithmeticalBinaryOperation( result, b, AST::ArithmeticExpression::SUB );
 
280
                return result;
 
281
            }
 
282
 
 
283
            inline ConstantExpression operator*( const ConstantExpression &b ) const
 
284
            {
 
285
                ConstantExpression result;
 
286
                arithmeticalBinaryOperation( result, b, AST::ArithmeticExpression::MUL );
 
287
                return result;
 
288
            }
 
289
 
 
290
            inline ConstantExpression operator/( const ConstantExpression &b ) const
 
291
            {
 
292
                ConstantExpression result;
 
293
                arithmeticalBinaryOperation( result, b, AST::ArithmeticExpression::DIV );
 
294
                return result;
 
295
            }
 
296
 
 
297
            inline ConstantExpression operator&&( const ConstantExpression &b ) const
 
298
            {
 
299
                ConstantExpression result;
 
300
                logicalBinaryOperation( result, b, AST::LogicExpression::AND );
 
301
                return result;
 
302
            }
 
303
 
 
304
            inline ConstantExpression operator||( const ConstantExpression &b ) const
 
305
            {
 
306
                ConstantExpression result;
 
307
                logicalBinaryOperation( result, b, AST::LogicExpression::OR );
 
308
                return result;
 
309
            }
 
310
 
 
311
            inline ConstantExpression operator^( const ConstantExpression &b ) const
 
312
            {
 
313
                ConstantExpression result;
 
314
                logicalBinaryOperation( result, b, AST::LogicExpression::XOR );
 
315
                return result;
 
316
            }
 
317
 
 
318
 
 
319
        private:
 
320
            /** Called by operators to perform operations: +,-,*,/.
 
321
            Does type checking and calls template method to perform the operation.
 
322
            @param result The ConstantExpression reference for storing the result of the operation.
 
323
            @param rhs The second operand (the first is 'this').
 
324
            @param op The operator to use.
 
325
            */
 
326
            void arithmeticalBinaryOperation( ConstantExpression& result, const ConstantExpression& rhs, ArithmeticExpression::Operator op ) const;
 
327
 
 
328
            /** Called by operators to perform operations: +,-,*,/.
 
329
            Performs the operation without type checking.
 
330
            Intended type parameters are long and double.
 
331
            @param result The ConstantExpression reference for storing the result of the operation.
 
332
            @param lhs The first operand.
 
333
            @param rhs The second operand.
 
334
            @param op The operator to use.
 
335
            */
 
336
 
 
337
            template < typename T >
 
338
            void arithmeticalBinaryOperation( ConstantExpression& result, const T& lhs, const T& rhs, ArithmeticExpression::Operator op ) const;
 
339
 
 
340
            /** Called by operators to perform unary operations: +,-,!.
 
341
            Does type checking and calls template method to perform the operation.
 
342
            Logical not is performed directly.
 
343
            @param result The ConstantExpression reference for storing the result of the operation.
 
344
            @param op The operator to use.
 
345
            */
 
346
            void unaryOperation( ConstantExpression& result, UnaryExpression::Operator op ) const;
 
347
 
 
348
            /** Called by operators to perform unary operations: +,-.
 
349
            Performs the operation without type checking.
 
350
            Intended type parameters are long and double.
 
351
            @param operand The operand.
 
352
            @param op The operator to use.
 
353
            @return Returns the result of the operation as primitive type.
 
354
            */
 
355
            template < typename T >
 
356
            T unaryOperation( const T& operand, UnaryExpression::Operator op ) const;
 
357
 
 
358
            /** Called by operators to perform operations: ==,!=,<,>,<=,>=.
 
359
            Does type checking and performs the operation.
 
360
            @param result The ConstantExpression reference for storing the result of the operation.
 
361
            @param rhs The second operand (the first is 'this').
 
362
            @param op The operator to use.
 
363
            */
 
364
            void relationalBinaryOperation( ConstantExpression& result, const ConstantExpression& rhs, BinaryComparisonExpression::Operator op ) const;
 
365
 
 
366
            /** Called by operators to perform operations: &&,||,^.
 
367
            Does type checking and performs the operation.
 
368
            @param result The ConstantExpression reference for storing the result of the operation.
 
369
            @param rhs The second operand (the first is 'this').
 
370
            @param op The operator to use.
 
371
            */
 
372
            void logicalBinaryOperation( ConstantExpression& result, const ConstantExpression& rhs, LogicExpression::Operator op ) const;
 
373
        };
 
374
 
 
375
 
 
376
        //-------------------------------------------------------------------------
 
377
 
 
378
        template < typename T >
 
379
        void ConstantExpression::arithmeticalBinaryOperation( ConstantExpression& result, const T& lhs, const T& rhs, ArithmeticExpression::Operator op ) const
 
380
        {
 
381
            switch ( op )
 
382
            {
 
383
 
 
384
            case AST::ArithmeticExpression::ADD:
 
385
                result.setValue( lhs + rhs );
 
386
                return ;
 
387
 
 
388
            case AST::ArithmeticExpression::SUB:
 
389
                result.setValue( lhs - rhs );
 
390
                break;
 
391
 
 
392
            case AST::ArithmeticExpression::MUL:
 
393
                result.setValue( lhs * rhs );
 
394
                break;
 
395
 
 
396
            case AST::ArithmeticExpression::DIV:
 
397
 
 
398
                if ( rhs == 0 )
 
399
                {
 
400
                    if ( mErrorHandler )
 
401
                    {
 
402
                        Error error( Error::ERR_INVALIDPARAMS, "division by zero" );
 
403
                        mErrorHandler->handleError( &error );
 
404
                        result.setValue( 0. );
 
405
                        return;
 
406
                    }
 
407
                }
 
408
 
 
409
                result.setValue( lhs / rhs );
 
410
                break;
 
411
 
 
412
            default:
 
413
                if ( mErrorHandler )
 
414
                {
 
415
                    Error error( Error::ERR_INVALIDPARAMS, "invalid operator: " + ArithmeticExpression::operatorString( op ) );
 
416
                    mErrorHandler->handleError( &error );
 
417
                    result.setValue( 0. );
 
418
                    return;
 
419
                }
 
420
            }
 
421
        }
 
422
 
 
423
        //-------------------------------------------------------------------------
 
424
        template < typename T >
 
425
        T ConstantExpression::unaryOperation( const T& operand, UnaryExpression::Operator op ) const
 
426
        {
 
427
            switch ( op )
 
428
            {
 
429
 
 
430
            case AST::UnaryExpression::ADD:
 
431
                //do nothing
 
432
                return operand;
 
433
 
 
434
            case AST::UnaryExpression::SUB:
 
435
                return ( -operand );
 
436
 
 
437
            case AST::UnaryExpression::NOT:
 
438
                return ( !operand );
 
439
 
 
440
            default:
 
441
                std::ostringstream oss;
 
442
                oss << "invalid operator: " << AST::UnaryExpression::operatorString( op ) << ", cause operand not of type 'bool' [f, t]";
 
443
                if ( mErrorHandler )
 
444
                {
 
445
                    Error error( Error::ERR_INVALIDPARAMS, oss.str() );
 
446
                    mErrorHandler->handleError( &error );
 
447
                    return operand;
 
448
                }
 
449
            }
 
450
            return operand;
 
451
        }
 
452
 
 
453
    } //namespace AST
 
454
 
 
455
} //namespace MathML
 
456
 
 
457
#endif //__MATHML_AST_CONSTANT_EXPRESSION_H___