OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
SDeferred.h
Go to the documentation of this file.
1#ifndef OPAL_SDeferred_HH
2#define OPAL_SDeferred_HH
3
4// ------------------------------------------------------------------------
5// $RCSfile: SDeferred.h,v $
6// ------------------------------------------------------------------------
7// $Revision: 1.2.4.1 $
8// ------------------------------------------------------------------------
9// Copyright: see Copyright.readme
10// ------------------------------------------------------------------------
11//
12// Template class: SDeferred<T>
13//
14// ------------------------------------------------------------------------
15//
16// $Date: 2004/11/18 22:52:40 $
17// $Author: jsberg $
18//
19// ------------------------------------------------------------------------
20
21#include <exception>
22#include <iosfwd>
23#include <vector>
26#include "Expressions/SValue.h"
29
30namespace Expressions {
31
32 // Template class SDeferred
33 // ----------------------------------------------------------------------
35 // An deferred expression is always re-evaluated when its value is
36 // required. This is notably needed to implement random generators.
37
38 template <class T>
39 class SDeferred : public SValue<T> {
40 public:
42 // From scalar expression.
43 explicit SDeferred(PtrToScalar<T> expr);
44
45 SDeferred(const SDeferred<T>&);
46 virtual ~SDeferred();
47
49 virtual SDeferred<T>* clone() const;
50
52 // The expression is always re-evaluated.
53 virtual T evaluate();
54
56 // Always true.
57 virtual bool isExpression() const;
58
60 virtual void print(std::ostream&) const;
61
62 protected:
65
66 private:
67 // Not implemented.
69 void operator=(const SDeferred<T>&);
70
71 // Guard against recursive definition.
72 mutable bool in_evaluation;
73 };
74
75 // Implementation
76 // ------------------------------------------------------------------------
77
78 template <class T>
80 : SValue<T>(), expr_ptr(rhs.expr_ptr->clone()), in_evaluation(false) {}
81
82 template <class T>
84 : SValue<T>(), expr_ptr(expr), in_evaluation(false) {}
85
86 template <class T>
88
89 template <class T>
91 return new SDeferred<T>(*this);
92 }
93
94 template <class T>
96 if (in_evaluation) {
97 throw LogicalError("SDeferred::evaluate()", "Recursive expression definitions found.");
98 } else {
99 in_evaluation = true;
100 try {
101 this->value = expr_ptr->evaluate();
102 in_evaluation = false;
103 } catch (OpalException& ex) {
104 in_evaluation = false;
105 throw LogicalError(
106 ex.where(),
107 "Evaluating expression \"" + this->getImage() + "\": " + ex.what());
108 } catch (std::exception& ex) {
109 in_evaluation = false;
110 throw LogicalError(
111 "SDeferred::evaluate()", "Standard C++ exception while evaluating \""
112 + this->getImage() + "\": " + ex.what());
113 } catch (...) {
114 in_evaluation = false;
115 throw LogicalError(
116 "SDeferred::evaluate()",
117 "Unknown exception while evaluating \"" + this->getImage() + "\": ");
118 }
119 }
120
121 return this->value;
122 }
123
124 template <class T>
126 return true;
127 }
128
129 template <class T>
130 void SDeferred<T>::print(std::ostream& stream) const {
131 expr_ptr->print(stream, 0);
132 return;
133 }
134
135} // namespace Expressions
136
137#endif // OPAL_SDeferred_HH
double T
Definition OPALTypes.h:8
A pointer to a scalar expression.
Object attribute with a `‘deferred’' scalar value.
Definition SDeferred.h:39
virtual T evaluate()
Evaluate.
Definition SDeferred.h:95
virtual SDeferred< T > * clone() const
Make clone.
Definition SDeferred.h:90
PtrToScalar< T > expr_ptr
Pointer to expression.
Definition SDeferred.h:64
void operator=(const SDeferred< T > &)
virtual void print(std::ostream &) const
Print the attribute value.
Definition SDeferred.h:130
virtual bool isExpression() const
Return expression flag.
Definition SDeferred.h:125
Object attribute with a constant scalar value.
Definition SValue.h:34
Logical error exception.
virtual const std::string & what() const
Return the message string for the exception.
virtual const std::string & where() const
Return the name of the method or function which detected the exception.
Representation objects and parsers for attribute expressions.