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