OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
SRefAttr.h
Go to the documentation of this file.
1#ifndef OPAL_SRefAttr_HH
2#define OPAL_SRefAttr_HH
3
4// ------------------------------------------------------------------------
5// $RCSfile: SRefAttr.h,v $
6// ------------------------------------------------------------------------
7// $Revision: 1.2 $
8// ------------------------------------------------------------------------
9// Copyright: see Copyright.readme
10// ------------------------------------------------------------------------
11//
12// Template class SRefAttr<T>
13//
14// ------------------------------------------------------------------------
15//
16// $Date: 2000/04/07 12:02:54 $
17// $Author: opal $
18//
19// ------------------------------------------------------------------------
20
25#include "Expressions/SValue.h"
27// #include "Utilities/Options.h"
28#include <vector>
29#include "Utilities/Options.h"
30
31namespace Expressions {
32
33 // Class SRefAttr
34 // ----------------------------------------------------------------------
35
37 // The referred attribute may have values of type real, logical or string.
38 // When a reference is seen, the pointers to the relevant object and
39 // attribute are left zero. When the expression value is required,
40 // the object and the attribute are searched, and the pointers cached.
41 // The reference is registered with the object. If the object referred
42 // to is deleted, it calls the invalidate() method of all reference
43 // expressions referring to it. This resets the pointers to zero, so
44 // that the next evaluation forces to search for a replacement object.
45
46 template <class T>
47 class SRefAttr : public AttributeBase {
48 public:
50 // Use object name [b]oName[/b] to identify the object containing
51 // the scalar, and [b]aName[/b] to identify the scalar itself.
52 SRefAttr(const std::string& oName, const std::string& aName, int index);
53
54 SRefAttr(const SRefAttr&);
55 virtual ~SRefAttr();
56
58 virtual SRefAttr<T>* clone() const;
59
61 // Evaluate the reference and return the value.
62 virtual T evaluate() const;
63
65 // This function has been added for speed of access.
66 virtual double getReal();
67
69 // Force re-evaluation of the reference.
70 virtual void invalidate();
71
73 virtual void print(std::ostream&) const;
74
76 // Evaluate the reference and assign to the scalar referred to.
77 virtual void set(const T&) const;
78
79 private:
80 // Not implemented.
82 void operator=(const SRefAttr&);
83
84 // Fill in the reference.
85 void fill() const;
86
87 // The name of the type referred to.
88 static const std::string typeName;
89
90 // The referred object, attribute and index.
91 const std::string obj_name;
92 const std::string att_name;
93 const int itsIndex;
94
95 // The object and attribute referred to.
96 mutable Object* itsObject;
98 };
99
100 template <class T>
101 inline std::ostream& operator<<(std::ostream& os, const SRefAttr<T>& a) {
102 a.print(os);
103 return os;
104 }
105
106 // Implementation of class SRefAttr<T>.
107 // ------------------------------------------------------------------------
108
109 template <class T>
110 SRefAttr<T>::SRefAttr(const std::string& oName, const std::string& aName, int index)
111 : obj_name(oName), att_name(aName), itsIndex(index), itsObject(0), itsAttr(0) {}
112
113 template <class T>
115 : AttributeBase(),
116 obj_name(rhs.obj_name),
117 att_name(rhs.att_name),
118 itsIndex(rhs.itsIndex),
119 itsObject(rhs.itsObject),
120 itsAttr(rhs.itsAttr) {}
121
122 template <class T>
124 if (itsObject) itsObject->unregisterReference(this);
125 }
126
127 template <class T>
129 return new SRefAttr<T>(*this);
130 }
131
132 template <class T>
134 fill();
135
136 if (AttributeBase* base = &itsAttr->getBase()) {
137 if (itsIndex) {
138 if (ADeferred<T>* value = dynamic_cast<ADeferred<T>*>(base)) {
139 std::vector<T> array = value->evaluate();
140 if (itsIndex > int(array.size())) {
141 throw ParseError(
142 "SRefAttr::evaluate()",
143 "Reference \"" + getImage() + "\" has index out of range.");
144 } else {
145 return array[itsIndex - 1];
146 }
147 } else {
148 throw ParseError(
149 "SRefAttr::evaluate()",
150 "Reference \"" + getImage() + "\" is not an array.");
151 }
152 } else {
153 if (SValue<T>* value = dynamic_cast<SValue<T>*>(base)) {
154 return value->evaluate();
155 } else {
156 throw ParseError(
157 "SRefAttr::evaluate()", getImage() + "\" is of the wrong type.");
158 }
159 }
160 }
161
162 return T(0);
163 }
164
165 template <class T>
167 throw ParseError("SRefAttr<T>::getReal()", "Attribute is not of real type.");
168 }
169
170 template <>
172 return evaluate();
173 }
174
175 template <class T>
176 void SRefAttr<T>::print(std::ostream& os) const {
177 os << obj_name;
178 if (!att_name.empty()) os << "->" << att_name;
179 if (itsIndex != 0) os << '[' << itsIndex << ']';
180 return;
181 }
182
183 template <class T>
185 itsObject = 0;
186 itsAttr = 0;
187 }
188
189 template <class T>
190 void SRefAttr<T>::set(const T& value) const {
191 fill();
192
193 if (AttributeBase* base = &itsAttr->getBase()) {
194 if (dynamic_cast<SValue<T>*>(base)) {
195 return itsAttr->set(new SValue<T>(value));
196 } else {
197 throw ParseError(
198 "Real::get()",
199 "Attribute \"" + itsAttr->getName() + "\" is of the wrong type.");
200 }
201 }
202 }
203
204 template <class T>
205 void SRefAttr<T>::fill() const {
206 if (itsObject == 0) {
207 itsObject = OpalData::getInstance()->find(obj_name);
208 if (itsObject == 0) {
209 if (att_name.empty() && itsIndex <= 0) {
210 throw ParseError(
211 "SRefAttr::fill()",
212 "\nThe <variable> \"" + obj_name + "\" is unknown.\n");
213 } else {
214 throw ParseError("SRefAttr::fill()", "Object \"" + obj_name + "\" is unknown.");
215 }
216 }
217
218 // Register the reference with the object, to allow invalidation
219 // when the object is deleted.
220 itsObject->registerReference(const_cast<SRefAttr<T>*>(this));
221
222 if (att_name.empty()) {
223 itsAttr = itsObject->findAttribute("VALUE");
224 if (itsAttr == 0) {
225 throw ParseError(
226 "SRefAttr::fill()",
227 "Object \"" + obj_name + "\" is not a variable, constant or vector.");
228 }
229 } else {
230 itsAttr = itsObject->findAttribute(att_name);
231 if (itsAttr == 0) {
232 throw ParseError(
233 "SRefAttr::fill()",
234 "Attribute \"" + obj_name + "->" + att_name + "\" is unknown.");
235 }
236 }
237 }
238 }
239
240} // namespace Expressions
241
242#endif // OPAL_SRefAttr_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
Object attribute with a `‘deferred’' array value.
Definition ADeferred.h:40
An attribute defined as a reference to a scalar.
Definition SRefAttr.h:47
static const std::string typeName
Definition SRefAttr.h:88
void operator=(const SRefAttr &)
Attribute * itsAttr
Definition SRefAttr.h:97
const std::string att_name
Definition SRefAttr.h:92
virtual SRefAttr< T > * clone() const
Make clone.
Definition SRefAttr.h:128
const std::string obj_name
Definition SRefAttr.h:91
virtual void set(const T &) const
Store new value.
Definition SRefAttr.h:190
virtual void print(std::ostream &) const
Print the reference.
Definition SRefAttr.h:176
virtual void invalidate()
Invalidate.
Definition SRefAttr.h:184
void fill() const
Definition SRefAttr.h:205
virtual double getReal()
Return real value.
Definition SRefAttr.h:166
virtual T evaluate() const
Evaluate.
Definition SRefAttr.h:133
Object attribute with a constant scalar value.
Definition SValue.h:34
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.
std::ostream & operator<<(std::ostream &os, const SRefAttr< T > &a)
Definition SRefAttr.h:101