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