OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
SUnary.h
Go to the documentation of this file.
1#ifndef OPAL_SUnary_HH
2#define OPAL_SUnary_HH
3
4// ------------------------------------------------------------------------
5// $RCSfile: SUnary.h,v $
6// ------------------------------------------------------------------------
7// $Revision: 1.1.1.1 $
8// ------------------------------------------------------------------------
9// Copyright: see Copyright.readme
10// ------------------------------------------------------------------------
11//
12// Template class: SUnary<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 <iostream>
28
29namespace Expressions {
30
31 // Template Class SUnary
32 // ----------------------------------------------------------------------
34 // This expression evaluates a scalar operand, and then returns the
35 // result of applying a scalar function to the result.
36
37 template <class T, class U>
38 class SUnary : public Scalar<T> {
39 public:
41 // Use scalar function of one operand and a scalar operand.
42 SUnary(const TFunction1<T, U>& function, PtrToScalar<U> operand);
43
44 SUnary(const SUnary<T, U>&);
45 virtual ~SUnary();
46
48 virtual Scalar<T>* clone() const;
49
51 virtual T evaluate() const;
52
54 // If the operand is constant 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 TFunction1<T, U>& function, PtrToScalar<U> operand);
58
60 virtual void print(std::ostream&, int precedence = 99) const;
61
62 private:
63 // Not implemented.
65 void operator=(const SUnary&);
66
67 // The operation object.
69
70 // The two operands.
72 };
73
74 // Implementation
75 // ------------------------------------------------------------------------
76
77 template <class T, class U>
79 : Scalar<T>(rhs), fun(rhs.fun), opr(rhs.opr->clone()) {}
80
81 template <class T, class U>
83 : fun(function), opr(oper) {}
84
85 template <class T, class U>
87
88 template <class T, class U>
90 return new SUnary<T, U>(*this);
91 }
92
93 template <class T, class U>
94 inline T SUnary<T, U>::evaluate() const {
95 errno = 0;
96 U arg = opr->evaluate();
97 T result = (*fun.function)(arg);
98
99 // Test for run-time evaluation errors.
100 switch (errno) {
101 case EDOM:
102 throw DomainError("SUnary::evaluate()");
103
104 case ERANGE:
105 // Ignore underflow.
106 if (result == T(0)) return result;
107 throw OverflowError("SUnary::evaluate()");
108
109 default:;
110 }
111
112 return result;
113 }
114
115 template <class T, class U>
117 // We must pick up the constant flag before the ownership of "operand"
118 // is transferred to "result".
119 bool isConst = operand->isConstant();
120 PtrToScalar<T> result = new SUnary<T, U>(function, operand);
121 ;
122
123 if (function.precedence != -2) {
124 if (isConst) {
125 // Constant expression.
126 double value = result->evaluate();
127 result = new SConstant<T>(value);
128 }
129 }
130
131 // Other expression.
132 return result.release();
133 }
134
135 template <class T, class U>
136 inline void SUnary<T, U>::print(std::ostream& os, int precedence) const {
137 if (fun.precedence >= 0) {
138 if (fun.precedence <= precedence) os << "(";
139 os << fun.name;
140 opr->print(os, fun.precedence);
141 if (fun.precedence <= precedence) os << ")";
142 } else {
143 os << fun.name << "(";
144 opr->print(os, 0);
145 os << ")";
146 }
147 }
148
149} // namespace Expressions
150
151#endif // OPAL_SUnary_HH
double T
Definition OPALTypes.h:8
Domain error exception.
Definition DomainError.h:32
A pointer to a scalar expression.
A scalar constant expression.
Definition SConstant.h:35
A scalar expression with one scalar operand.
Definition SUnary.h:38
virtual Scalar< T > * clone() const
Make clone.
Definition SUnary.h:89
PtrToScalar< U > opr
Definition SUnary.h:71
virtual T evaluate() const
Evaluate.
Definition SUnary.h:94
void operator=(const SUnary &)
const TFunction1< T, U > & fun
Definition SUnary.h:68
virtual void print(std::ostream &, int precedence=99) const
Print expression.
Definition SUnary.h:136
virtual ~SUnary()
Definition SUnary.h:86
static Scalar< T > * make(const TFunction1< T, U > &function, PtrToScalar< U > operand)
Make a new expression.
Definition SUnary.h:116
Overflow exception.
Representation objects and parsers for attribute expressions.
A function of one U, returning a T.
Definition TFunction1.h:31
int precedence
The operator precedence.
Definition TFunction1.h:41