OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
SNull.h
Go to the documentation of this file.
1#ifndef OPAL_SNull_HH
2#define OPAL_SNull_HH
3
4// ------------------------------------------------------------------------
5// $RCSfile: SNull.h,v $
6// ------------------------------------------------------------------------
7// $Revision: 1.1.1.1 $
8// ------------------------------------------------------------------------
9// Copyright: see Copyright.readme
10// ------------------------------------------------------------------------
11//
12// Template class: SNull<T>
13//
14// ------------------------------------------------------------------------
15//
16// $Date: 2000/03/27 09:33:42 $
17// $Author: Andreas Adelmann $
18//
19// ------------------------------------------------------------------------
20
21#include <cerrno>
22#include <iostream>
27
28namespace Expressions {
29
30 // Class SNull
31 // ----------------------------------------------------------------------
33 // This expression evaluates a scalar function withoud operands
34 // (e.g. a random generator) and returns the result.
35
36 template <class T>
37 class SNull : public Scalar<T> {
38 public:
40 // Use an operand-less scalar function.
41 explicit SNull(const TFunction0<T>& function);
42
43 SNull(const SNull<T>&);
44 virtual ~SNull();
45
47 virtual Scalar<T>* clone() const;
48
50 virtual T evaluate() const;
51
53 // If the function is not a random generator, evaluate it and
54 // store the result as a constant.
55 static Scalar<T>* make(const TFunction0<T>& function);
56
58 virtual void print(std::ostream& str, int precedence = 99) const;
59
60 private:
61 // Not implemented.
63 void operator=(const SNull&);
64
65 // The operation function.
67 };
68
69 // Implementation
70 // ------------------------------------------------------------------------
71
72 template <class T>
73 inline SNull<T>::SNull(const SNull<T>& rhs) : Scalar<T>(rhs), fun(rhs.fun) {}
74
75 template <class T>
76 inline SNull<T>::SNull(const TFunction0<T>& function) : fun(function) {}
77
78 template <class T>
79 inline SNull<T>::~SNull() {}
80
81 template <class T>
82 inline Scalar<T>* SNull<T>::clone() const {
83 return new SNull<T>(*this);
84 }
85
86 template <class T>
87 inline T SNull<T>::evaluate() const {
88 errno = 0;
89 T result = (*fun.function)();
90
91 // Test for run-time evaluation errors.
92 switch (errno) {
93 case EDOM:
94 throw DomainError("SNull:evaluate()");
95
96 case ERANGE:
97 // Ignore underflow.
98 if (result == T(0)) return result;
99 throw OverflowError("SNull:evaluate()");
100
101 default:
102 return result;
103 }
104 }
105
106 template <class T>
108 return new SNull<T>(fun);
109 }
110
111 template <class T>
112 inline void SNull<T>::print(std::ostream& stream, int) const {
113 stream << fun.name << "()";
114 }
115
116} // namespace Expressions
117
118#endif // OPAL_SNull_HH
double T
Definition OPALTypes.h:8
Domain error exception.
Definition DomainError.h:32
A scalar expression without operands.
Definition SNull.h:37
virtual ~SNull()
Definition SNull.h:79
virtual T evaluate() const
Evaluate.
Definition SNull.h:87
const TFunction0< T > & fun
Definition SNull.h:66
virtual Scalar< T > * clone() const
Make clone.
Definition SNull.h:82
virtual void print(std::ostream &str, int precedence=99) const
Print expression.
Definition SNull.h:112
void operator=(const SNull &)
static Scalar< T > * make(const TFunction0< T > &function)
Make expression.
Definition SNull.h:107
Overflow exception.
Representation objects and parsers for attribute expressions.
An operand-less function returning a T.
Definition TFunction0.h:31