OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
SBinary.h
Go to the documentation of this file.
1#ifndef OPAL_SBinary_HH
2#define OPAL_SBinary_HH
3
4// ------------------------------------------------------------------------
5// $RCSfile: SBinary.h,v $
6// ------------------------------------------------------------------------
7// $Revision: 1.1.1.1 $
8// ------------------------------------------------------------------------
9// Copyright: see Copyright.readme
10// ------------------------------------------------------------------------
11//
12// Template class: SBinary<T,U>
13//
14// ------------------------------------------------------------------------
15//
16// $Date: 2000/03/27 09:33:42 $
17// $Author: Andreas Adelmann $
18//
19// ------------------------------------------------------------------------
20
21#include <cerrno>
22#include <iosfwd>
28
29namespace Expressions {
30
31 // Template class SBinary
32 // ----------------------------------------------------------------------
34 // The expression first evaluates the two scalar operands, and then
35 // it applies the given function to the operands and returns the result.
36
37 template <class T, class U>
38 class SBinary : public Scalar<T> {
39 public:
41 // Use scalar function with two operands and two scalars.
42 SBinary(const TFunction2<T, U>& function, PtrToScalar<U> left, PtrToScalar<U> right);
43
45 virtual ~SBinary();
46
48 virtual Scalar<T>* clone() const;
49
51 virtual T evaluate() const;
52
54 // If both operands are constants and the function is not a random
55 // generator, evaluate the expression and store the result as a
56 // constant.
57 static Scalar<T>* make(const TFunction2<T, U>&, PtrToScalar<U> left, PtrToScalar<U> right);
58
60 virtual void print(std::ostream& str, int precedence = 99) const;
61
62 private:
63 // Not implemented.
65 void operator=(const SBinary&);
66
67 // The operation object.
69
70 // The two operands.
73 };
74
75 // Implementation
76 // ------------------------------------------------------------------------
77
78 template <class T, class U>
79 inline SBinary<T, U>::SBinary(const SBinary& rhs)
80 : Scalar<T>(rhs), fun(rhs.fun), lft(rhs.lft->clone()), rgt(rhs.rgt->clone()) {}
81
82 template <class T, class U>
84 const TFunction2<T, U>& function, PtrToScalar<U> left, PtrToScalar<U> right)
85 : fun(function), lft(left), rgt(right) {}
86
87 template <class T, class U>
89
90 template <class T, class U>
92 return new SBinary<T, U>(*this);
93 }
94
95 template <class T, class U>
96 inline T SBinary<T, U>::evaluate() const {
97 errno = 0;
98 U op1 = lft->evaluate();
99 U op2 = rgt->evaluate();
100 T result = (*fun.function)(op1, op2);
101
102 // Test for run-time evaluation errors.
103 switch (errno) {
104 case EDOM:
105 throw DomainError("SBinary::evaluate()");
106
107 case ERANGE:
108 // Ignore underflow.
109 if (result == T(0)) return result;
110 throw OverflowError("SBinary::evaluate()");
111
112 default:
113 return result;
114 }
115 }
116
117 template <class T, class U>
119 const TFunction2<T, U>& function, PtrToScalar<U> left, PtrToScalar<U> right) {
120 // We must pick up the constant flag before the ownerships of "left" and
121 // "right" are transferred to "result".
122 bool isConst = left->isConstant() && right->isConstant();
123 PtrToScalar<T> result = new SBinary<T, U>(function, left, right);
124
125 if (function.precedence != -2) {
126 if (isConst) {
127 // Replace constant expression by its value.
128 result = new SConstant<T>(result->evaluate());
129 }
130 }
131
132 // Other expression.
133 return result.release();
134 }
135
136 template <class T, class U>
137 inline void SBinary<T, U>::print(std::ostream& os, int precedence) const {
138 if (fun.precedence >= 0) {
139 // Binary operation.
140 if (fun.precedence <= precedence) os << "(";
141 lft->print(os, fun.precedence - 1);
142 os << fun.name;
143 rgt->print(os, fun.precedence);
144 if (fun.precedence <= precedence) os << ")";
145 } else {
146 // Function.
147 os << fun.name << '(';
148 lft->print(os, 0);
149 os << ',';
150 rgt->print(os, 0);
151 os << ')';
152 }
153
154 return;
155 }
156
157} // namespace Expressions
158
159#endif // OPAL_SBinary_HH
double T
Definition OPALTypes.h:8
Domain error exception.
Definition DomainError.h:32
A pointer to a scalar expression.
A scalar expression with two scalar operands.
Definition SBinary.h:38
static Scalar< T > * make(const TFunction2< T, U > &, PtrToScalar< U > left, PtrToScalar< U > right)
Make a new expression.
Definition SBinary.h:118
SBinary(const SBinary< T, U > &)
virtual ~SBinary()
Definition SBinary.h:88
virtual void print(std::ostream &str, int precedence=99) const
Print expression.
Definition SBinary.h:137
PtrToScalar< U > lft
Definition SBinary.h:71
void operator=(const SBinary &)
virtual Scalar< T > * clone() const
Make clone.
Definition SBinary.h:91
virtual T evaluate() const
Evaluate.
Definition SBinary.h:96
PtrToScalar< U > rgt
Definition SBinary.h:72
const TFunction2< T, U > & fun
Definition SBinary.h:68
A scalar constant expression.
Definition SConstant.h:35
Overflow exception.
Representation objects and parsers for attribute expressions.
A function of two U's returning a T.
Definition TFunction2.h:31
int precedence
The operator precedence.
Definition TFunction2.h:41