OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
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
21#include <iosfwd>
22#include <sstream>
23#include <vector>
27#include "Expressions/AValue.h"
28#include "Expressions/SValue.h"
30
31namespace Expressions {
32
33 // Class ARefExpr
34 // ------------------------------------------------------------------------
36 // The components of the array may be 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 ARefExpr : public OArray<T>, public Invalidator {
42 public:
44 // Use [b]objName[/b] to identify the object containg the array, and
45 // [b]attName[/b] to identify the array itself.
46 ARefExpr(const std::string& objName, const std::string& attName);
47
48 ARefExpr(const ARefExpr<T>& rhs);
49 virtual ~ARefExpr();
50
52 virtual OArray<T>* clone() const;
53
55 virtual std::vector<T> evaluate() const;
56
58 virtual void print(std::ostream& os, int precedence = 99) const;
59
60 private:
61 // Not implemented.
62 void operator=(const ARefExpr&);
63
64 // Fill in the reference.
65 void fill() const;
66
67 // Make print image.
68 const std::string getImage() const;
69
70 // The referred object and attribute.
71 const std::string obj_name;
72 const std::string att_name;
73
74 // The object and attribute referred to.
75 mutable Object* itsObject;
77 };
78
79 // Implementation
80 // ------------------------------------------------------------------------
81
82 template <class T>
83 ARefExpr<T>::ARefExpr(const std::string& objName, const std::string& attName)
84 : obj_name(objName), att_name(attName), itsObject(0), itsAttr(0) {}
85
86 template <class T>
88 : OArray<T>(rhs),
89 Invalidator(rhs),
90 obj_name(rhs.obj_name),
91 att_name(rhs.att_name),
92 itsObject(rhs.itsObject),
93 itsAttr(rhs.itsAttr) {}
94
95 template <class T>
97 if (itsObject) itsObject->unregisterReference(this);
98 }
99
100 template <class T>
102 return new ARefExpr<T>(*this);
103 }
104
105 template <class T>
106 inline std::vector<T> ARefExpr<T>::evaluate() const {
107 fill();
108
109 if (AttributeBase* base = &itsAttr->getBase()) {
110 if (AValue<T>* value = dynamic_cast<AValue<T>*>(base)) {
111 return value->evaluate();
112 } else if (SValue<T>* value = dynamic_cast<SValue<T>*>(base)) {
113 return std::vector<T>(1, value->evaluate());
114 } else {
115 throw OpalException(
116 "ARefExpr::evaluate()",
117 "Reference \"" + getImage() + "\" is not an array.");
118 }
119 } else {
120 return std::vector<T>();
121 }
122 }
123
124 template <class T>
125 const std::string ARefExpr<T>::getImage() const {
126 std::ostringstream os;
127 print(os);
128 os << std::ends;
129 return os.str();
130 }
131
132 template <class T>
133 void ARefExpr<T>::print(std::ostream& os, int) const {
134 os << obj_name;
135 if (!att_name.empty()) os << "->" << att_name;
136 }
137
138 template <class T>
139 void ARefExpr<T>::fill() const {
140 if (itsObject == 0) {
141 itsObject = OpalData::getInstance()->find(obj_name);
142 if (itsObject == 0) {
143 throw OpalException("ARefExpr::fill()", "Object \"" + obj_name + "\" is unknown.");
144 }
145
146 // Register the reference with the object, to allow invalidation
147 // when the object is deleted.
148 itsObject->registerReference(const_cast<ARefExpr<T>*>(this));
149
150 if (att_name.empty()) {
151 itsAttr = itsObject->findAttribute("VALUE");
152 if (itsAttr == 0) {
153 throw OpalException(
154 "ARefExpr::fill()",
155 "Object \"" + obj_name + "\" is not a variable, constant or vector.");
156 }
157 } else {
158 itsAttr = itsObject->findAttribute(att_name);
159 if (itsAttr == 0) {
160 throw OpalException(
161 "ARefExpr::fill()",
162 "Attribute \"" + obj_name + "->" + att_name + "\" is unknown.");
163 }
164 }
165 }
166 }
167
168} // namespace Expressions
169
170#endif // OPAL_ARefExpr_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 an array.
Definition ARefExpr.h:41
const std::string getImage() const
Definition ARefExpr.h:125
virtual OArray< T > * clone() const
Make clone.
Definition ARefExpr.h:101
Attribute * itsAttr
Definition ARefExpr.h:76
ARefExpr(const std::string &objName, const std::string &attName)
Constructor.
Definition ARefExpr.h:83
void operator=(const ARefExpr &)
const std::string obj_name
Definition ARefExpr.h:71
const std::string att_name
Definition ARefExpr.h:72
virtual ~ARefExpr()
Definition ARefExpr.h:96
virtual std::vector< T > evaluate() const
Evaluate the reference and return the value referred to.
Definition ARefExpr.h:106
virtual void print(std::ostream &os, int precedence=99) const
Print expression.
Definition ARefExpr.h:133
void fill() const
Definition ARefExpr.h:139
Object attribute with a constant array value.
Definition AValue.h:35
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
Representation objects and parsers for attribute expressions.