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