OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
SRefExpr.h
Go to the documentation of this file.
1#ifndef OPAL_SRefExpr_HH
2#define OPAL_SRefExpr_HH
3
4// ------------------------------------------------------------------------
5// $RCSfile: SRefExpr.h,v $
6// ------------------------------------------------------------------------
7// $Revision: 1.4.4.1 $
8// ------------------------------------------------------------------------
9// Copyright: see Copyright.readme
10// ------------------------------------------------------------------------
11//
12// Class SRefExpr:
13//
14// ------------------------------------------------------------------------
15//
16// $Date: 2002/12/09 15:06:08 $
17// $Author: jsberg $
18//
19// ------------------------------------------------------------------------
20
21#include <iosfwd>
22#include <sstream>
26#include "Expressions/SValue.h"
27#include "Utilities/Options.h"
29
30namespace Expressions {
31
32 // Class SRefExpr
33 // ----------------------------------------------------------------------
35 // The referred attribute may have values of type real, logical or string.
36 // Reference expressions are (re)evaluated as needed as part of the
37 // containing expression.
38
39 template <class T>
40 class SRefExpr : public Scalar<T>, public Invalidator {
41 public:
43 // Use [b]objName[/b] to identify the object containg the array, and
44 // [b]attName[/b] to identify the array itself.
45 SRefExpr(const std::string& objName, const std::string& attName);
46
47 SRefExpr(const SRefExpr<T>& rhs);
48 virtual ~SRefExpr();
49
51 virtual Scalar<T>* clone() const;
52
54 virtual T evaluate() const;
55
57 // Force re-evaluation of the reference.
58 virtual void invalidate();
59
61 virtual void print(std::ostream& os, int precedence = 99) const;
62
63 private:
64 // Not implemented.
65 void operator=(const SRefExpr&);
66
67 // Fill in the reference.
68 void fill() const;
69
70 // Make print image.
71 const std::string getImage() const;
72
73 // The referred object and attribute names.
74 const std::string obj_name;
75 const std::string att_name;
76
77 // The object and attribute referred to.
78 mutable Object* itsObject;
80 };
81
82 // Implementation
83 // ------------------------------------------------------------------------
84
85 template <class T>
86 SRefExpr<T>::SRefExpr(const std::string& objName, const std::string& attName)
87 : obj_name(objName), att_name(attName), itsObject(0), itsAttr(0) {}
88
89 template <class T>
91 : Scalar<T>(rhs),
92 Invalidator(rhs),
93 obj_name(rhs.obj_name),
94 att_name(rhs.att_name),
95 itsObject(rhs.itsObject),
96 itsAttr(rhs.itsAttr) {}
97
98 template <class T>
100 if (itsObject) itsObject->unregisterReference(this);
101 }
102
103 template <class T>
105 return new SRefExpr<T>(*this);
106 }
107
108 template <class T>
109 inline T SRefExpr<T>::evaluate() const {
110 fill();
111
112 if (AttributeBase* base = &itsAttr->getBase()) {
113 if (SValue<T>* value = dynamic_cast<SValue<T>*>(base)) {
114 return value->evaluate();
115 } else {
116 throw ParseError(
117 "SRefExpr::evaluate()",
118 "Reference \"" + getImage() + "\" is not a variable.");
119 }
120 }
121
122 return T(0);
123 }
124
125 template <class T>
126 const std::string SRefExpr<T>::getImage() const {
127 std::ostringstream os;
128 print(os);
129 os << std::ends;
130 return os.str();
131 }
132
133 template <class T>
135 itsObject = 0;
136 itsAttr = 0;
137 }
138
139 template <class T>
140 void SRefExpr<T>::print(std::ostream& os, int) const {
141 os << obj_name;
142 if (!att_name.empty()) os << "->" << att_name;
143 return;
144 }
145
146 template <class T>
147 void SRefExpr<T>::fill() const {
148 if (itsObject == 0) {
149 itsObject = OpalData::getInstance()->find(obj_name);
150 if (itsObject == 0) {
151 if (att_name.empty()) {
152 throw ParseError(
153 "SRefExpr::fill()",
154 "\nThe <variable> \"" + obj_name + "\" is unknown.\n");
155 } else {
156 throw ParseError("SRefExpr::fill()", "Object \"" + obj_name + "\" is unknown.");
157 }
158 }
159
160 // Register the reference with the object, to allow invalidation
161 // when the object is deleted.
162 itsObject->registerReference(const_cast<SRefExpr<T>*>(this));
163
164 if (att_name.empty()) {
165 itsAttr = itsObject->findAttribute("VALUE");
166 if (itsAttr == 0) {
167 throw ParseError(
168 "SRefExpr::fill()",
169 "Object \"" + obj_name + "\" is not a variable, constant or vector.");
170 }
171 } else {
172 itsAttr = itsObject->findAttribute(att_name);
173 if (itsAttr == 0) {
174 throw ParseError(
175 "SRefExpr::fill()",
176 "Attribute \"" + obj_name + "->" + att_name + "\" is unknown.");
177 }
178 }
179 }
180 }
181
182} // namespace Expressions
183
184#endif // OPAL_SRefExpr_HH
double T
Definition OPALTypes.h:8
Abstract base class for attribute values of different types.
A representation of an Object attribute.
Definition Attribute.h:52
An expression defined as a reference to a scalar.
Definition SRefExpr.h:40
SRefExpr(const std::string &objName, const std::string &attName)
Constructor.
Definition SRefExpr.h:86
const std::string att_name
Definition SRefExpr.h:75
const std::string obj_name
Definition SRefExpr.h:74
const std::string getImage() const
Definition SRefExpr.h:126
virtual T evaluate() const
Evaluate.
Definition SRefExpr.h:109
virtual void print(std::ostream &os, int precedence=99) const
Print expression.
Definition SRefExpr.h:140
virtual Scalar< T > * clone() const
Make clone.
Definition SRefExpr.h:104
virtual void invalidate()
Invalidate.
Definition SRefExpr.h:134
Attribute * itsAttr
Definition SRefExpr.h:79
void operator=(const SRefExpr &)
void fill() const
Definition SRefExpr.h:147
virtual ~SRefExpr()
Definition SRefExpr.h:99
Object attribute with a constant scalar value.
Definition SValue.h:34
Abstract base class for references which must be invalidated when an.
Definition Invalidator.h:26
The base class for all OPAL objects.
Definition Object.h:45
Object * find(const std::string &name)
Find entry.
Definition OpalData.cpp:477
static OpalData * getInstance()
Definition OpalData.cpp:193
Parse exception.
Definition ParseError.h:31
Representation objects and parsers for attribute expressions.