OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
Object.cpp
Go to the documentation of this file.
1 
2 // ------------------------------------------------------------------------
3 // $RCSfile: Object.cpp,v $
4 // ------------------------------------------------------------------------
5 // $Revision: 1.4 $
6 // ------------------------------------------------------------------------
7 // Copyright: see Copyright.readme
8 // ------------------------------------------------------------------------
9 //
10 // Class: Object
11 // This abstract base class defines the common interface for all
12 // objects defined in OPAL.
13 //
14 // ------------------------------------------------------------------------
15 //
16 // $Date: 2000/12/15 10:04:08 $
17 // $Author: opal $
18 //
19 // ------------------------------------------------------------------------
20 
21 #include "AbstractObjects/Object.h"
25 #include "Parser/Statement.h"
26 #include "Utilities/Options.h"
27 #include "Utilities/ParseError.h"
28 
29 #include <cmath>
30 #include <iostream>
31 #include <vector>
32 
33 extern Inform *gmsg;
34 
35 
36 // Class Object
37 // ------------------------------------------------------------------------
38 
40  // Invalidate all references to this object.
42  i != references.end(); ++i) {
43  (*i)->invalidate();
44  }
45 }
46 
47 
49  // Default action: no replacement allowed.
50  return false;
51 }
52 
53 
54 void Object::copyAttributes(const Object &source) {
55  itsAttr = source.itsAttr;
56 }
57 
58 
60  // Default action: do nothing.
61 }
62 
63 
64 Attribute *Object::findAttribute(const std::string &name) {
66  i != itsAttr.end(); ++i) {
67  if(i->getName() == name) return &(*i);
68  }
69 
70  return 0;
71 }
72 
73 
74 const Attribute *Object::findAttribute(const std::string &name) const {
75  for(std::vector<Attribute>::const_iterator i = itsAttr.begin();
76  i != itsAttr.end(); ++i) {
77  if(i->getName() == name) return &(*i);
78  }
79 
80  return 0;
81 }
82 
83 
85 (const std::string &name, TokenStream &, Statement &) {
86  throw ParseError("Object::makeTemplate()", "Object \"" + name +
87  "\" cannot be used to define a macro.");
88 }
89 
90 
91 Object *Object::makeInstance(const std::string &/*name*/, Statement &, const Parser *) {
92  throw ParseError("Object::makeInstance()", "Object \"" + getOpalName() +
93  "\" cannot be called as a macro.");
94 }
95 
96 
97 void Object::parse(Statement &stat) {
98  while(stat.delimiter(',')) {
99  std::string name = Expressions::parseString(stat, "Attribute name expected.");
100 
101  if(Attribute *attr = findAttribute(name)) {
102  if(stat.delimiter('[')) {
103  int index = int(std::round(Expressions::parseRealConst(stat)));
104  Expressions::parseDelimiter(stat, ']');
105 
106  if(stat.delimiter('=')) {
107  attr->parseComponent(stat, true, index);
108  } else if(stat.delimiter(":=")) {
109  attr->parseComponent(stat, false, index);
110  } else {
111  throw ParseError("Object::parse()",
112  "Delimiter \"=\" or \":=\" expected.");
113  }
114  } else if(stat.delimiter('=')) {
115  attr->parse(stat, true);
116  } else if(stat.delimiter(":=")) {
117  attr->parse(stat, false);
118  } else {
119  attr->setDefault();
120  }
121  } else {
122  throw ParseError("Object::parse()", "Object \"" + getOpalName() +
123  "\" has no attribute \"" + name + "\".");
124  }
125  }
126 }
127 
128 
129 void Object::parseShortcut(Statement &stat, bool eval) {
130  // Only one attribute.
131  if(stat.delimiter(',')) {
132  stat.mark();
133  std::string name;
134 
135  if(stat.word(name)) {
136  if(stat.delimiter('=')) {
137  if(Attribute *attr = findAttribute(name)) {
138  attr->parse(stat, eval);
139  return;
140  } else {
141  throw ParseError("Object::parseShortcut()", "Object \"" + getOpalName() +
142  "\" has no attribute \"" + name + "\".");
143  }
144  } else if(stat.delimiter(":=")) {
145  if(Attribute *attr = findAttribute(name)) {
146  attr->parse(stat, false);
147  return;
148  } else {
149  throw ParseError("Object::parseShortcut()", "Object \"" + getOpalName() +
150  "\" has no attribute \"" + name + "\".");
151  }
152  }
153  }
154 
155  stat.restore();
156  itsAttr[0].parse(stat, false);
157  }
158 }
159 
160 
161 void Object::print(std::ostream & msg) const {
162  std::string head = getOpalName();
163  Object *parent = getParent();
164  if(parent != 0 && ! parent->getOpalName().empty()) {
165  if(! getOpalName().empty()) head += ':';
166  head += parent->getOpalName();
167  }
168 
169  msg << head;
170  int pos = head.length();
171 
172  for(std::vector<Attribute>::const_iterator i = itsAttr.begin();
173  i != itsAttr.end(); ++i) {
174  if(*i) i->print(pos);
175  }
176  msg << ';';
177  msg << std::endl;
178  return;
179 }
180 
181 
183  references.insert(ref);
184 }
185 
186 
188  references.erase(ref);
189 }
190 
192  if (getParent() != 0) return;
193 
194  const unsigned int end = itsAttr.size();
195  const std::string name = getOpalName();
196  for (unsigned int i = 0; i < end; ++ i) {
197  AttributeHandler::addAttributeOwner(name, itsClass, itsAttr[i].getName());
198  }
199 }
200 
201 void Object::printHelp(std::ostream &/*os*/) const {
202  *gmsg << endl << itsHelp << endl;
203 
204  if(itsAttr.size() > 0) {
205  *gmsg << "Attributes:" << endl;
206 
207  size_t maxLength = 16;
208  std::vector<Attribute>::const_iterator it;
209  for (it = itsAttr.begin(); it != itsAttr.end(); ++ it) {
210  std::string name = it->getName();
211  maxLength = std::max(maxLength, name.length() + 1);
212  }
213 
214  for (it = itsAttr.begin(); it != itsAttr.end(); ++ it) {
215  std::string type = it->getType();
216  std::string name = it->getName();
217  *gmsg << '\t' << type << std::string(16 - type.length(), ' ');
218  *gmsg << name << std::string(maxLength - name.length(), ' ');
219  *gmsg << it->getHelp();
220  if(it->isReadOnly()) *gmsg << " (read only)";
221  *gmsg << endl;
222  }
223  }
224 
225  *gmsg << endl;
226 }
227 
228 
230  // Default action: do nothing.
231 }
232 
233 
235  // Default action: do nothing.
236 }
237 
238 
239 bool Object::isBuiltin() const {
240  return builtin;
241 }
242 
243 
244 bool Object::isShared() const {
245  return sharedFlag;
246 }
247 
248 
249 void Object::setShared(bool flag) {
250  sharedFlag = flag;
251 }
252 
253 
254 void Object::setDirty(bool dirty) {
255  // The object is now different from the data base.
256  modified = dirty;
257 }
258 
259 
260 bool Object::isDirty() const {
261  return modified;
262 }
263 
264 
265 void Object::setFlag(bool flag) {
266  flagged = flag;
267 }
268 
269 
270 bool Object::isFlagged() const {
271  return flagged;
272 }
273 
275  const Object *base = this;
276  while(base->itsParent != 0) base = base->itsParent;
277  return base;
278 }
279 
280 
281 const std::string &Object::getOpalName() const {
282  return itsName;
283 }
284 
285 
287  return itsParent;
288 }
289 
290 
291 bool Object::isTreeMember(const Object *classObject) const {
292  const Object *object = this;
293 
294  while(object != 0 && object != classObject) {
295  object = object->itsParent;
296  }
297 
298  return object != 0;
299 }
300 
301 
302 void Object::setOpalName(const std::string &name) {
303  itsName = name;
304 }
305 
306 
307 void Object::setParent(Object *parent) {
308  itsParent = parent;
309 }
310 
311 
313  occurrence = 0;
314 }
315 
316 
318  return ++occurrence;
319 }
320 
321 
323  return occurrence;
324 }
325 
326 
327 Object::Object(int size, const char *name, const char *help):
328  RCObject(), itsAttr(size), itsParent(0),
329  itsName(name), itsHelp(help), occurrence(0), sharedFlag(false) {
330  // NOTE: The derived classes must define the attribute handlers and
331  // any initial values for the attributes.
332 
333  // The object is an exemplar and must not be saved in the data base.
334  builtin = true;
335  flagged = modified = false;
336 }
337 
338 
339 Object::Object(const std::string &name, Object *parent):
340  RCObject(), itsAttr(parent->itsAttr), itsParent(parent),
341  itsName(name), itsHelp(parent->itsHelp), occurrence(0), sharedFlag(false) {
342  // The object is now different from the data base.
343  builtin = flagged = false;
344  modified = true;
345 }
346 
347 std::ostream &operator<<(std::ostream &os, const Object &object) {
348  object.print(os);
349  return os;
350 }
std::ostream & operator<<(std::ostream &os, const Object &object)
Definition: Object.cpp:347
Inform * gmsg
Definition: Main.cpp:62
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
T::PETE_Expr_t::PETE_Return_t max(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Definition: ReductionLoc.h:84
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
const std::string name
std::string parseString(Statement &, const char msg[])
Parse string value.
void parseDelimiter(Statement &stat, char delim)
Test for one-character delimiter.
double parseRealConst(Statement &)
Parse real constant.
std::string::iterator iterator
Definition: MSLang.h:16
boost::function< boost::tuple< double, bool >arguments_t)> type
Definition: function.hpp:21
A representation of an Object attribute.
Definition: Attribute.h:52
static void addAttributeOwner(const std::string &owner, const OwnerType &type, const std::string &name)
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
void setParent(Object *)
Set parent object.
Definition: Object.cpp:307
virtual void parseShortcut(Statement &, bool eval=true)
Parser for single-attribute commands.
Definition: Object.cpp:129
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition: Object.cpp:191
virtual bool canReplaceBy(Object *object)
Test if replacement is allowed.
Definition: Object.cpp:48
virtual Object * makeTemplate(const std::string &, TokenStream &, Statement &)
Macro handler function.
Definition: Object.cpp:85
int occurrence
Definition: Object.h:261
bool isFlagged() const
True, if [b]this[/b] is flagged by setFlag(true).
Definition: Object.cpp:270
Object * itsParent
Definition: Object.h:252
bool isDirty() const
True, if the [b]modified[/b] flag is set.
Definition: Object.cpp:260
std::string itsHelp
Definition: Object.h:258
Object * getParent() const
Return parent pointer.
Definition: Object.cpp:286
virtual void update()
Update this object.
Definition: Object.cpp:234
std::string itsName
Definition: Object.h:255
const Object * getBaseObject() const
Return the object's base type object.
Definition: Object.cpp:274
bool sharedFlag
Definition: Object.h:271
bool flagged
Object flag.
Definition: Object.h:242
virtual Object * makeInstance(const std::string &name, Statement &, const Parser *)
Macro handler function.
Definition: Object.cpp:91
const std::string & getOpalName() const
Return object name.
Definition: Object.cpp:281
virtual void print(std::ostream &) const
Print the object.
Definition: Object.cpp:161
int occurrenceCount()
Return the occurrence counter.
Definition: Object.cpp:322
void copyAttributes(const Object &)
Copy attributes from another object.
Definition: Object.cpp:54
void setDirty(bool)
Set/reset the [b]modified[/b] flag.
Definition: Object.cpp:254
int increment()
Increment and return the occurrence counter.
Definition: Object.cpp:317
virtual void setShared(bool)
Set/reset shared flag.
Definition: Object.cpp:249
std::set< Invalidator * > references
Definition: Object.h:268
virtual Attribute * findAttribute(const std::string &name)
Find an attribute by name.
Definition: Object.cpp:64
void registerReference(Invalidator *a)
Register a reference to this object.
Definition: Object.cpp:182
bool modified
Dirty flag.
Definition: Object.h:238
virtual void execute()
Execute the command.
Definition: Object.cpp:59
void clear()
Clear the occurrence counter.
Definition: Object.cpp:312
void setOpalName(const std::string &name)
Set object name.
Definition: Object.cpp:302
virtual void printHelp(std::ostream &) const
Print help.
Definition: Object.cpp:201
std::vector< Attribute > itsAttr
The object attributes.
Definition: Object.h:216
bool isTreeMember(const Object *subTree) const
Test for tree membership.
Definition: Object.cpp:291
virtual void parse(Statement &)
Parse the object.
Definition: Object.cpp:97
bool isBuiltin() const
True, if [b]this[/b] is a built-in object.
Definition: Object.cpp:239
virtual ~Object()
Definition: Object.cpp:39
virtual bool isShared() const
Shared flag.
Definition: Object.cpp:244
void setFlag(bool)
Flag/unflag this object, e. g. to control output of objects for.
Definition: Object.cpp:265
bool builtin
Built-in flag.
Definition: Object.h:233
virtual void replace(Object *oldObject, Object *newObject)
Replace references.
Definition: Object.cpp:229
void unregisterReference(Invalidator *a)
Unegister a reference to this object.
Definition: Object.cpp:187
Abstract base class for reference counted objects.
Definition: RCObject.h:40
Interface for abstract language parser.
Definition: Parser.h:31
Interface for statements.
Definition: Statement.h:38
void restore()
Return to marked position.
Definition: Statement.cpp:175
void mark()
Mark position in command.
Definition: Statement.cpp:170
bool word(std::string &value)
Return word value.
Definition: Statement.cpp:159
bool delimiter(char c)
Test for delimiter.
Definition: Statement.cpp:101
Abstract interface for a stream of input tokens.
Definition: TokenStream.h:33
Parse exception.
Definition: ParseError.h:32
Definition: Inform.h:42