OPALX (Object Oriented Parallel Accelerator Library for Exascal) MINIorX
OPALX
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
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.
41 for(std::set<Invalidator *>::iterator i = references.begin();
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) {
65 for(std::vector<Attribute>::iterator i = itsAttr.begin(); i != itsAttr.end(); ++i) {
66 if(i->getName() == name) return &(*i);
67 }
68 return nullptr;
69}
70
71
72const Attribute *Object::findAttribute(const std::string &name) const {
73 for(std::vector<Attribute>::const_iterator i = itsAttr.begin();
74 i != itsAttr.end(); ++i) {
75 if(i->getName() == name) return &(*i);
76 }
77
78 return 0;
79}
80
81
83(const std::string &name, TokenStream &, Statement &) {
84 throw ParseError("Object::makeTemplate()", "Object \"" + name +
85 "\" cannot be used to define a macro.");
86}
87
88
89Object *Object::makeInstance(const std::string &/*name*/, Statement &, const Parser *) {
90 throw ParseError("Object::makeInstance()", "Object \"" + getOpalName() +
91 "\" cannot be called as a macro.");
92}
93
94
96 while(stat.delimiter(',')) {
97 std::string name = Expressions::parseString(stat, "Attribute name expected.");
98
99 if(Attribute *attr = findAttribute(name)) {
100 if(stat.delimiter('[')) {
101 int index = int(std::round(Expressions::parseRealConst(stat)));
103
104 if(stat.delimiter('=')) {
105 attr->parseComponent(stat, true, index);
106 } else if(stat.delimiter(":=")) {
107 attr->parseComponent(stat, false, index);
108 } else {
109 throw ParseError("Object::parse()",
110 "Delimiter \"=\" or \":=\" expected.");
111 }
112 } else if(stat.delimiter('=')) {
113 attr->parse(stat, true);
114 } else if(stat.delimiter(":=")) {
115 attr->parse(stat, false);
116 } else {
117 attr->setDefault();
118 }
119 } else {
120 throw ParseError("Object::parse()", "Object \"" + getOpalName() +
121 "\" has no attribute \"" + name + "\".");
122 }
123 }
124}
125
126
127void Object::parseShortcut(Statement &stat, bool eval) {
128 // Only one attribute.
129 if(stat.delimiter(',')) {
130 stat.mark();
131 std::string name;
132
133 if(stat.word(name)) {
134 if(stat.delimiter('=')) {
135 if(Attribute *attr = findAttribute(name)) {
136 attr->parse(stat, eval);
137 return;
138 } else {
139 throw ParseError("Object::parseShortcut()", "Object \"" + getOpalName() +
140 "\" has no attribute \"" + name + "\".");
142 } else if(stat.delimiter(":=")) {
143 if(Attribute *attr = findAttribute(name)) {
144 attr->parse(stat, false);
145 return;
146 } else {
147 throw ParseError("Object::parseShortcut()", "Object \"" + getOpalName() +
148 "\" has no attribute \"" + name + "\".");
149 }
150 }
151 }
152
153 stat.restore();
154 itsAttr[0].parse(stat, false);
155 }
156}
157
158
159void Object::print(std::ostream & msg) const {
160 std::string head = getOpalName();
161 Object *parent = getParent();
162 if(parent != 0 && ! parent->getOpalName().empty()) {
163 if(! getOpalName().empty()) head += ':';
164 head += parent->getOpalName();
165 }
166
167 msg << head;
168 int pos = head.length();
169
170 for(std::vector<Attribute>::const_iterator i = itsAttr.begin();
171 i != itsAttr.end(); ++i) {
172 if(*i) i->print(pos);
173 }
174 msg << ';';
175 msg << std::endl;
176 return;
177}
178
179
181 references.insert(ref);
182}
183
184
186 references.erase(ref);
187}
188
190 if (getParent() != 0) return;
191
192 const unsigned int end = itsAttr.size();
193 const std::string name = getOpalName();
194 for (unsigned int i = 0; i < end; ++ i) {
195 AttributeHandler::addAttributeOwner(name, itsClass, itsAttr[i].getName());
196 }
197}
198
199void Object::printHelp(std::ostream &/*os*/) const {
200 *gmsg << endl << itsHelp << endl;
201
202 if(!itsAttr.empty()) {
203 *gmsg << "Attributes:" << endl;
204
205 size_t maxNameLength = 16;
206 size_t maxTypeLength = 16;
207 std::vector<Attribute>::const_iterator it;
208 for (it = itsAttr.begin(); it != itsAttr.end(); ++ it) {
209 std::string name = it->getName();
210 maxNameLength = std::max(maxNameLength, name.length() + 1);
211 std::string type = it->getType();
212 maxTypeLength = std::max(maxTypeLength, type.length() + 1);
213 }
214
215 for (it = itsAttr.begin(); it != itsAttr.end(); ++ it) {
216 std::string type = it->getType();
217 std::string name = it->getName();
218 std::istringstream help(it->getHelp());
219 std::vector<std::string> words;
220 std::copy(std::istream_iterator<std::string>(help),
221 std::istream_iterator<std::string>(),
222 std::back_inserter(words));
223 unsigned int columnWidth = 40;
224 if (maxNameLength + maxTypeLength < 40u) {
225 columnWidth = 80 - maxNameLength - maxTypeLength;
226 }
227
228 auto wordsIt = words.begin();
229 auto wordsEnd = words.end();
230 while (wordsIt != wordsEnd) {
231 *gmsg << '\t' << type << std::string(maxTypeLength - type.length(), ' ');
232 *gmsg << name << std::string(maxNameLength - name.length(), ' ');
233 unsigned int totalLength = 0;
234 do {
235 totalLength += wordsIt->length();
236 *gmsg << *wordsIt << " ";
237 ++ wordsIt;
238 } while (wordsIt != wordsEnd && totalLength + wordsIt->length() < columnWidth);
239 if (wordsIt != wordsEnd) {
240 *gmsg << endl;
241 }
242
243 type = "";
244 name = "";
245 }
246
247 if(it->isReadOnly()) *gmsg << " (read only)";
248 *gmsg << endl;
249 }
250 }
251
252 *gmsg << endl;
253}
254
255
257 // Default action: do nothing.
258}
259
260
262 // Default action: do nothing.
263}
264
265
266bool Object::isBuiltin() const {
267 return builtin;
268}
269
270
271bool Object::isShared() const {
272 return sharedFlag;
273}
274
275
276void Object::setShared(bool flag) {
277 sharedFlag = flag;
278}
279
280
281void Object::setDirty(bool dirty) {
282 // The object is now different from the data base.
283 modified = dirty;
284}
285
286
287bool Object::isDirty() const {
288 return modified;
289}
290
291
292void Object::setFlag(bool flag) {
293 flagged = flag;
294}
295
296
297bool Object::isFlagged() const {
298 return flagged;
299}
300
302 const Object *base = this;
303 while(base->itsParent != 0) base = base->itsParent;
304 return base;
305}
306
307
308const std::string &Object::getOpalName() const {
309 return itsName;
310}
311
312
314 return itsParent;
315}
316
317
318bool Object::isTreeMember(const Object *classObject) const {
319 const Object *object = this;
320
321 while(object != 0 && object != classObject) {
322 object = object->itsParent;
323 }
324
325 return object != 0;
326}
327
328
329void Object::setOpalName(const std::string &name) {
330 itsName = name;
331}
332
333
335 itsParent = parent;
336}
337
338
340 occurrence = 0;
341}
342
343
345 return ++occurrence;
346}
347
348
350 return occurrence;
351}
352
353
354Object::Object(int size, const char *name, const char *help):
355 RCObject(), itsAttr(size), itsParent(0),
356 itsName(name), itsHelp(help), occurrence(0), sharedFlag(false) {
357 // NOTE: The derived classes must define the attribute handlers and
358 // any initial values for the attributes.
359
360 // The object is an exemplar and must not be saved in the data base.
361 builtin = true;
362 flagged = modified = false;
363}
364
365
366Object::Object(const std::string &name, Object *parent):
367 RCObject(), itsAttr(parent->itsAttr), itsParent(parent),
368 itsName(name), itsHelp(parent->itsHelp), occurrence(0), sharedFlag(false) {
369 // The object is now different from the data base.
370 builtin = flagged = false;
371 modified = true;
372}
373
374std::ostream &operator<<(std::ostream &os, const Object &object) {
375 object.print(os);
376 return os;
377}
Inform * gmsg
Definition changes.cpp:7
PartBunch< T, Dim >::ConstIterator end(PartBunch< T, Dim > const &bunch)
std::ostream & operator<<(std::ostream &os, const Object &object)
Definition Object.cpp:374
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.
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:334
virtual void parseShortcut(Statement &, bool eval=true)
Parser for single-attribute commands.
Definition Object.cpp:127
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition Object.cpp:189
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:83
int occurrence
Definition Object.h:261
bool isFlagged() const
True, if [b]this[/b] is flagged by setFlag(true).
Definition Object.cpp:297
Object * itsParent
Definition Object.h:252
bool isDirty() const
True, if the [b]modified[/b] flag is set.
Definition Object.cpp:287
std::string itsHelp
Definition Object.h:258
Object * getParent() const
Return parent pointer.
Definition Object.cpp:313
virtual void update()
Update this object.
Definition Object.cpp:261
std::string itsName
Definition Object.h:255
const Object * getBaseObject() const
Return the object's base type object.
Definition Object.cpp:301
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:89
const std::string & getOpalName() const
Return object name.
Definition Object.cpp:308
virtual void print(std::ostream &) const
Print the object.
Definition Object.cpp:159
Object(int size, const char *name, const char *help)
Constructor for exemplars.
Definition Object.cpp:354
int occurrenceCount()
Return the occurrence counter.
Definition Object.cpp:349
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:281
int increment()
Increment and return the occurrence counter.
Definition Object.cpp:344
virtual void setShared(bool)
Set/reset shared flag.
Definition Object.cpp:276
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:180
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:339
void setOpalName(const std::string &name)
Set object name.
Definition Object.cpp:329
virtual void printHelp(std::ostream &) const
Print help.
Definition Object.cpp:199
std::vector< Attribute > itsAttr
The object attributes.
Definition Object.h:216
bool isTreeMember(const Object *subTree) const
Test for tree membership.
Definition Object.cpp:318
virtual void parse(Statement &)
Parse the object.
Definition Object.cpp:95
bool isBuiltin() const
True, if [b]this[/b] is a built-in object.
Definition Object.cpp:266
virtual ~Object()
Definition Object.cpp:39
virtual bool isShared() const
Shared flag.
Definition Object.cpp:271
void setFlag(bool)
Flag/unflag this object, e. g. to control output of objects for.
Definition Object.cpp:292
bool builtin
Built-in flag.
Definition Object.h:233
virtual void replace(Object *oldObject, Object *newObject)
Replace references.
Definition Object.cpp:256
void unregisterReference(Invalidator *a)
Unegister a reference to this object.
Definition Object.cpp:185
RCObject()
Default constructor.
Definition RCObject.h:98
Interface for abstract language parser.
Definition Parser.h:31
Interface for statements.
Definition Statement.h:38
void restore()
Return to marked position.
void mark()
Mark position in command.
bool word(std::string &value)
Return word value.
bool delimiter(char c)
Test for delimiter.
Abstract interface for a stream of input tokens.
Definition TokenStream.h:33
Parse exception.
Definition ParseError.h:32