OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
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
23#include "Expressions/SValue.h"
26#include <iosfwd>
27#include <vector>
28#include <exception>
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
41 public:
42
44 // From scalar expression.
45 explicit SDeferred(PtrToScalar<T> expr);
46
47 SDeferred(const SDeferred<T> &);
48 virtual ~SDeferred();
49
51 virtual SDeferred<T> *clone() const;
52
54 // The expression is always re-evaluated.
55 virtual T evaluate();
56
58 // Always true.
59 virtual bool isExpression() const;
60
62 virtual void print(std::ostream &) const;
63
64 protected:
65
68
69 private:
70
71 // Not implemented.
73 void operator=(const SDeferred<T> &);
74
75 // Guard against recursive definition.
76 mutable bool in_evaluation;
77 };
78
79
80 // Implementation
81 // ------------------------------------------------------------------------
82
83 template <class T>
85 SValue<T>(), expr_ptr(rhs.expr_ptr->clone()), in_evaluation(false)
86 {}
87
88
89 template <class T>
91 SValue<T>(), expr_ptr(expr), in_evaluation(false)
92 {}
93
94
95 template <class T>
97 {}
98
99
100 template <class T>
102 return new SDeferred<T>(*this);
103 }
104
105
106 template <class T>
108 if(in_evaluation) {
109 throw LogicalError("SDeferred::evaluate()",
110 "Recursive expression definitions found.");
111 } else {
112 in_evaluation = true;
113 try {
114 this->value = expr_ptr->evaluate();
115 in_evaluation = false;
116 } catch(OpalException &ex) {
117 in_evaluation = false;
118 throw LogicalError(ex.where(),
119 "Evaluating expression \"" +
120 this->getImage() + "\": " + ex.what());
121 } catch(ClassicException &ex) {
122 in_evaluation = false;
123 throw LogicalError(ex.where(),
124 "Evaluating expression \"" +
125 this->getImage() + "\": " + ex.what());
126 } catch(std::exception &ex) {
127 in_evaluation = false;
128 throw LogicalError("SDeferred::evaluate()",
129 "Standard C++ exception while evaluating \"" +
130 this->getImage() + "\": " + ex.what());
131 } catch(...) {
132 in_evaluation = false;
133 throw LogicalError("SDeferred::evaluate()",
134 "Unknown exception while evaluating \"" +
135 this->getImage() + "\": ");
136 }
137 }
138
139 return this->value;
140 }
141
142
143 template <class T>
145 return true;
146 }
147
148
149 template <class T>
150 void SDeferred<T>::print(std::ostream &stream) const {
151 expr_ptr->print(stream, 0);
152 return;
153 }
154
155}
156
157#endif // OPAL_SDeferred_HH
Representation objects and parsers for attribute expressions.
Definition: Expressions.h:64
A pointer to a scalar expression.
Definition: Expressions.h:109
The abstract base class for all exceptions in CLASSIC.
Logical error exception.
Definition: LogicalError.h:33
Object attribute with a `‘deferred’' scalar value.
Definition: SDeferred.h:39
virtual T evaluate()
Evaluate.
Definition: SDeferred.h:107
virtual ~SDeferred()
Definition: SDeferred.h:96
virtual SDeferred< T > * clone() const
Make clone.
Definition: SDeferred.h:101
PtrToScalar< T > expr_ptr
Pointer to expression.
Definition: SDeferred.h:67
void operator=(const SDeferred< T > &)
virtual void print(std::ostream &) const
Print the attribute value.
Definition: SDeferred.h:150
virtual bool isExpression() const
Return expression flag.
Definition: SDeferred.h:144
Object attribute with a constant scalar value.
Definition: SValue.h:34
The base class for all OPAL exceptions.
Definition: OpalException.h:28
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.