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