OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
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 
22 #include "Expressions/TFunction0.h"
23 #include "Utilities/DomainError.h"
25 #include <cerrno>
26 #include <iostream>
27 
28 
29 namespace Expressions {
30 
31  // Class SNull
32  // ----------------------------------------------------------------------
34  // This expression evaluates a scalar function withoud operands
35  // (e.g. a random generator) and returns the result.
36 
37  template <class T>
38  class SNull: public Scalar<T> {
39 
40  public:
41 
43  // Use an operand-less scalar function.
44  explicit SNull(const TFunction0<T> &function);
45 
46  SNull(const SNull<T> &);
47  virtual ~SNull();
48 
50  virtual Scalar<T> *clone() const;
51 
53  virtual T evaluate() const;
54 
56  // If the function is not a random generator, evaluate it and
57  // store the result as a constant.
58  static Scalar<T> *make(const TFunction0<T> &function);
59 
61  virtual void print(std::ostream &str, int precedence = 99) const;
62 
63  private:
64 
65  // Not implemented.
66  SNull();
67  void operator=(const SNull &);
68 
69  // The operation function.
71  };
72 
73 
74  // Implementation
75  // ------------------------------------------------------------------------
76 
77  template <class T> inline
79  Scalar<T>(rhs),
80  fun(rhs.fun)
81  {}
82 
83 
84  template <class T> inline
85  SNull<T>::SNull(const TFunction0<T> &function):
86  fun(function)
87  {}
88 
89 
90  template <class T> inline
92  {}
93 
94 
95  template <class T> inline
97  return new SNull<T>(*this);
98  }
99 
100 
101  template <class T> inline
103  errno = 0;
104  T result = (*fun.function)();
105 
106  // Test for run-time evaluation errors.
107  switch(errno) {
108 
109  case EDOM:
110  throw DomainError("SNull:evaluate()");
111 
112  case ERANGE:
113  // Ignore underflow.
114  if(result == T(0)) return result;
115  throw OverflowError("SNull:evaluate()");
116 
117  default:
118  return result;
119  }
120  }
121 
122 
123  template <class T> inline
125  return new SNull<T>(fun);
126  }
127 
128 
129  template <class T> inline
130  void SNull<T>::print(std::ostream &stream, int) const {
131  stream << fun.name << "()";
132  }
133 
134 }
135 
136 #endif // OPAL_SNull_HH
A scalar expression.
Definition: Expressions.h:79
A scalar expression without operands.
Definition: SNull.h:38
Definition: rbendmap.h:8
virtual T evaluate() const
Evaluate.
Definition: SNull.h:102
static Scalar< T > * make(const TFunction0< T > &function)
Make expression.
Definition: SNull.h:124
Overflow exception.
Definition: OverflowError.h:32
Domain error exception.
Definition: DomainError.h:32
virtual void print(std::ostream &str, int precedence=99) const
Print expression.
Definition: SNull.h:130
void operator=(const SNull &)
virtual Scalar< T > * clone() const
Make clone.
Definition: SNull.h:96
virtual ~SNull()
Definition: SNull.h:91
An operand-less function returning a T.
Definition: TFunction0.h:31
const TFunction0< T > & fun
Definition: SNull.h:70