OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
Object.cpp
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2// $RCSfile: Object.cpp,v $
3// ------------------------------------------------------------------------
4// $Revision: 1.4 $
5// ------------------------------------------------------------------------
6// Copyright: see Copyright.readme
7// ------------------------------------------------------------------------
8//
9// Class: Object
10// This abstract base class defines the common interface for all
11// objects defined in OPAL.
12//
13// ------------------------------------------------------------------------
14//
15// $Date: 2000/12/15 10:04:08 $
16// $Author: opal $
17//
18// ------------------------------------------------------------------------
19
24#include "Parser/Statement.h"
25#include "Utilities/Options.h"
27#include "Utilities/Util.h"
28
29#include <cmath>
30#include <iostream>
31#include <vector>
32
33extern 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
54void Object::copyAttributes(const Object &source) {
55 itsAttr = source.itsAttr;
56}
57
58
60 // Default action: do nothing.
61}
62
63
64Attribute *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
74const 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
91Object *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
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)));
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
129void 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
161void 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
201void Object::printHelp(std::ostream &/*os*/) const {
202 *gmsg << endl << itsHelp << endl;
203
204 if(!itsAttr.empty()) {
205 *gmsg << "Attributes:" << endl;
206
207 size_t maxNameLength = 16;
208 size_t maxTypeLength = 16;
209 std::vector<Attribute>::const_iterator it;
210 for (it = itsAttr.begin(); it != itsAttr.end(); ++ it) {
211 std::string name = it->getName();
212 maxNameLength = std::max(maxNameLength, name.length() + 1);
213 std::string type = it->getType();
214 maxTypeLength = std::max(maxTypeLength, type.length() + 1);
215 }
216
217 for (it = itsAttr.begin(); it != itsAttr.end(); ++ it) {
218 std::string type = it->getType();
219 std::string name = it->getName();
220 std::istringstream help(it->getHelp());
221 std::vector<std::string> words;
222 std::copy(std::istream_iterator<std::string>(help),
223 std::istream_iterator<std::string>(),
224 std::back_inserter(words));
225 unsigned int columnWidth = 40;
226 if (maxNameLength + maxTypeLength < 40u) {
227 columnWidth = 80 - maxNameLength - maxTypeLength;
228 }
229
230 auto wordsIt = words.begin();
231 auto wordsEnd = words.end();
232 while (wordsIt != wordsEnd) {
233 *gmsg << '\t' << type << std::string(maxTypeLength - type.length(), ' ');
234 *gmsg << name << std::string(maxNameLength - name.length(), ' ');
235 unsigned int totalLength = 0;
236 do {
237 totalLength += wordsIt->length();
238 *gmsg << *wordsIt << " ";
239 ++ wordsIt;
240 } while (wordsIt != wordsEnd && totalLength + wordsIt->length() < columnWidth);
241 if (wordsIt != wordsEnd) {
242 *gmsg << endl;
243 }
244
245 type = "";
246 name = "";
247 }
248
249 if(it->isReadOnly()) *gmsg << " (read only)";
250 *gmsg << endl;
251 }
252 }
253
254 *gmsg << endl;
255}
256
257
259 // Default action: do nothing.
260}
261
262
264 // Default action: do nothing.
265}
266
267
268bool Object::isBuiltin() const {
269 return builtin;
270}
271
272
273bool Object::isShared() const {
274 return sharedFlag;
275}
276
277
278void Object::setShared(bool flag) {
279 sharedFlag = flag;
280}
281
282
283void Object::setDirty(bool dirty) {
284 // The object is now different from the data base.
285 modified = dirty;
286}
287
288
289bool Object::isDirty() const {
290 return modified;
291}
292
293
294void Object::setFlag(bool flag) {
295 flagged = flag;
296}
297
298
299bool Object::isFlagged() const {
300 return flagged;
301}
302
304 const Object *base = this;
305 while(base->itsParent != 0) base = base->itsParent;
306 return base;
307}
308
309
310const std::string &Object::getOpalName() const {
311 return itsName;
312}
313
314
316 return itsParent;
317}
318
319
320bool Object::isTreeMember(const Object *classObject) const {
321 const Object *object = this;
322
323 while(object != 0 && object != classObject) {
324 object = object->itsParent;
325 }
326
327 return object != 0;
328}
329
330
331void Object::setOpalName(const std::string &name) {
332 itsName = name;
333}
334
335
337 itsParent = parent;
338}
339
340
342 occurrence = 0;
343}
344
345
347 return ++occurrence;
348}
349
350
352 return occurrence;
353}
354
355
356Object::Object(int size, const char *name, const char *help):
357 RCObject(), itsAttr(size), itsParent(0),
358 itsName(name), itsHelp(help), occurrence(0), sharedFlag(false) {
359 // NOTE: The derived classes must define the attribute handlers and
360 // any initial values for the attributes.
361
362 // The object is an exemplar and must not be saved in the data base.
363 builtin = true;
364 flagged = modified = false;
365}
366
367
368Object::Object(const std::string &name, Object *parent):
369 RCObject(), itsAttr(parent->itsAttr), itsParent(parent),
370 itsName(name), itsHelp(parent->itsHelp), occurrence(0), sharedFlag(false) {
371 // The object is now different from the data base.
372 builtin = flagged = false;
373 modified = true;
374}
375
376std::ostream &operator<<(std::ostream &os, const Object &object) {
377 object.print(os);
378 return os;
379}
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
std::ostream & operator<<(std::ostream &os, const Object &object)
Definition: Object.cpp:376
Inform * gmsg
Definition: Main.cpp:61
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:336
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:299
Object * itsParent
Definition: Object.h:252
bool isDirty() const
True, if the [b]modified[/b] flag is set.
Definition: Object.cpp:289
std::string itsHelp
Definition: Object.h:258
Object * getParent() const
Return parent pointer.
Definition: Object.cpp:315
virtual void update()
Update this object.
Definition: Object.cpp:263
std::string itsName
Definition: Object.h:255
const Object * getBaseObject() const
Return the object's base type object.
Definition: Object.cpp:303
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:310
virtual void print(std::ostream &) const
Print the object.
Definition: Object.cpp:161
int occurrenceCount()
Return the occurrence counter.
Definition: Object.cpp:351
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:283
int increment()
Increment and return the occurrence counter.
Definition: Object.cpp:346
virtual void setShared(bool)
Set/reset shared flag.
Definition: Object.cpp:278
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:341
void setOpalName(const std::string &name)
Set object name.
Definition: Object.cpp:331
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:320
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:268
virtual ~Object()
Definition: Object.cpp:39
virtual bool isShared() const
Shared flag.
Definition: Object.cpp:273
void setFlag(bool)
Flag/unflag this object, e. g. to control output of objects for.
Definition: Object.cpp:294
bool builtin
Built-in flag.
Definition: Object.h:233
virtual void replace(Object *oldObject, Object *newObject)
Replace references.
Definition: Object.cpp:258
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