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