SBinary.h 5.05 KB
Newer Older
gsell's avatar
gsell committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
#ifndef OPAL_SBinary_HH
#define OPAL_SBinary_HH

// ------------------------------------------------------------------------
// $RCSfile: SBinary.h,v $
// ------------------------------------------------------------------------
// $Revision: 1.1.1.1 $
// ------------------------------------------------------------------------
// Copyright: see Copyright.readme
// ------------------------------------------------------------------------
//
// Template class: SBinary<T,U>
//
// ------------------------------------------------------------------------
//
// $Date: 2000/03/27 09:33:42 $
// $Author: Andreas Adelmann $
//
// ------------------------------------------------------------------------

#include "AbstractObjects/Expressions.h"
#include "Expressions/SConstant.h"
#include "Expressions/TFunction2.h"
#include "Utilities/DomainError.h"
#include "Utilities/OverflowError.h"
#include <cerrno>
#include <iosfwd>


namespace Expressions {

    // Template class SBinary
    // ----------------------------------------------------------------------
    /// A scalar expression with two scalar operands.
    //  The expression first evaluates the two scalar operands, and then
    //  it applies the given function to the operands and returns the result.

    template <class T, class U>
    class SBinary: public Scalar<T> {

    public:

        /// Constructor.
        //  Use scalar function with two operands and two scalars.
        SBinary(const TFunction2<T, U> &function,
                PtrToScalar<U> left, PtrToScalar<U> right);

        SBinary(const SBinary<T, U> &);
        virtual ~SBinary();

        /// Make clone.
        virtual Scalar<T> *clone() const;

        /// Evaluate.
        virtual T evaluate() const;

        /// Make a new expression.
        //  If both operands are constants and the function is not a random
        //  generator, evaluate the expression and  store the result as a
        //  constant.
        static Scalar<T> *make(const TFunction2<T, U> &,
                               PtrToScalar<U> left, PtrToScalar<U> right);

        /// Print expression.
        virtual void print(std::ostream &str, int precedence = 99) const;

    private:

        // Not implemented.
        SBinary();
        void operator=(const SBinary &);

        // The operation object.
        const TFunction2<T, U> &fun;

        // The two operands.
        PtrToScalar<U> lft;
        PtrToScalar<U> rgt;
    };


    // Implementation
    // ------------------------------------------------------------------------

    template <class T, class U> inline
    SBinary<T, U>::SBinary(const SBinary &rhs):
87
        Scalar<T>(rhs),
gsell's avatar
gsell committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
        fun(rhs.fun), lft(rhs.lft->clone()), rgt(rhs.rgt->clone())
    {}


    template <class T, class U> inline
    SBinary<T, U>::SBinary(const TFunction2<T, U> &function,
                           PtrToScalar<U> left, PtrToScalar<U> right):
        fun(function), lft(left), rgt(right)
    {}


    template <class T, class U> inline
    SBinary<T, U>::~SBinary()
    {}


    template <class T, class U> inline
    Scalar<T> *SBinary<T, U>::clone() const {
        return new SBinary<T, U>(*this);
    }


    template <class T, class U> inline
    T SBinary<T, U>::evaluate() const {
        errno = 0;
        U op1 = lft->evaluate();
        U op2 = rgt->evaluate();
        T result = (*fun.function)(op1, op2);

        // Test for run-time evaluation errors.
        switch(errno) {

            case EDOM:
                throw DomainError("SBinary::evaluate()");

            case ERANGE:
                // Ignore underflow.
                if(result == T(0)) return result;
                throw OverflowError("SBinary::evaluate()");

            default:
                return result;
        }
    }


    template <class T, class U> inline
    Scalar<T> *SBinary<T, U>::make
    (const TFunction2<T, U> &function,
     PtrToScalar<U> left, PtrToScalar<U> right) {
        // We must pick up the constant flag before the ownerships of "left" and
        // "right" are transferred to "result".
        bool isConst = left->isConstant() && right->isConstant();
        PtrToScalar<T> result = new SBinary<T, U>(function, left, right);

        if(function.precedence != -2) {
            if(isConst) {
                // Replace constant expression by its value.
                result = new SConstant<T>(result->evaluate());
            }
        }

        // Other expression.
        return result.release();
    }


    template <class T, class U> inline
    void SBinary<T, U>::print(std::ostream &os, int precedence) const {
        if(fun.precedence >= 0) {
            // Binary operation.
            if(fun.precedence <= precedence) os << "(";
            lft->print(os, fun.precedence - 1);
            os << fun.name;
            rgt->print(os, fun.precedence);
            if(fun.precedence <= precedence) os << ")";
        } else {
            // Function.
            os << fun.name << '(';
            lft->print(os, 0);
            os << ',';
            rgt->print(os, 0);
            os << ')';
        }

        return;
    }

}

#endif // OPAL_SBinary_HH