OPAL (Object Oriented Parallel Accelerator Library)  2024.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 
20 #include "AbstractObjects/Object.h"
24 #include "Parser/Statement.h"
25 #include "Utilities/Options.h"
26 #include "Utilities/ParseError.h"
27 #include "Utilities/Util.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.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 
268 bool Object::isBuiltin() const {
269  return builtin;
270 }
271 
272 
273 bool Object::isShared() const {
274  return sharedFlag;
275 }
276 
277 
278 void Object::setShared(bool flag) {
279  sharedFlag = flag;
280 }
281 
282 
283 void Object::setDirty(bool dirty) {
284  // The object is now different from the data base.
285  modified = dirty;
286 }
287 
288 
289 bool Object::isDirty() const {
290  return modified;
291 }
292 
293 
294 void Object::setFlag(bool flag) {
295  flagged = flag;
296 }
297 
298 
299 bool 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 
310 const std::string &Object::getOpalName() const {
311  return itsName;
312 }
313 
314 
316  return itsParent;
317 }
318 
319 
320 bool 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 
331 void Object::setOpalName(const std::string &name) {
332  itsName = name;
333 }
334 
335 
336 void Object::setParent(Object *parent) {
337  itsParent = parent;
338 }
339 
340 
342  occurrence = 0;
343 }
344 
345 
347  return ++occurrence;
348 }
349 
350 
352  return occurrence;
353 }
354 
355 
356 Object::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 
368 Object::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 
376 std::ostream &operator<<(std::ostream &os, const Object &object) {
377  object.print(os);
378  return os;
379 }
virtual void execute()
Execute the command.
Definition: Object.cpp:59
void registerReference(Invalidator *a)
Register a reference to this object.
Definition: Object.cpp:182
virtual void parse(Statement &)
Parse the object.
Definition: Object.cpp:97
bool builtin
Built-in flag.
Definition: Object.h:233
bool modified
Dirty flag.
Definition: Object.h:238
and give any other recipients of the Program a copy of this License along with the Program You may charge a fee for the physical act of transferring a copy
Definition: LICENSE:87
Abstract base class for reference counted objects.
Definition: RCObject.h:40
void setOpalName(const std::string &name)
Set object name.
Definition: Object.cpp:331
The base class for all OPAL objects.
Definition: Object.h:48
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two distribute and or modify the software for each author s protection and we want to make certain that everyone understands that there is no warranty for this free software If the software is modified by someone else and passed we want its recipients to know that what they have is not the so that any problems introduced by others will not reflect on the original authors reputations any free program is threatened constantly by software patents We wish to avoid the danger that redistributors of a free program will individually obtain patent in effect making the program proprietary To prevent we have made it clear that any patent must be licensed for everyone s free use or not licensed at all The precise terms and conditions for distribution and modification follow GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR DISTRIBUTION AND MODIFICATION This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License The refers to any such program or and a work based on the Program means either the Program or any derivative work under copyright a work containing the Program or a portion of it
Definition: LICENSE:43
void setFlag(bool)
Flag/unflag this object, e. g. to control output of objects for.
Definition: Object.cpp:294
virtual Object * makeTemplate(const std::string &, TokenStream &, Statement &)
Macro handler function.
Definition: Object.cpp:85
void copyAttributes(const Object &)
Copy attributes from another object.
Definition: Object.cpp:54
void parseDelimiter(Statement &stat, char delim)
Test for one-character delimiter.
bool flagged
Object flag.
Definition: Object.h:242
bool isBuiltin() const
True, if [b]this[/b] is a built-in object.
Definition: Object.cpp:268
virtual void printHelp(std::ostream &) const
Print help.
Definition: Object.cpp:201
std::ostream & operator<<(std::ostream &os, const Attribute &attr)
Definition: Attribute.cpp:169
bool sharedFlag
Definition: Object.h:271
Parse exception.
Definition: ParseError.h:32
virtual ~Object()
Definition: Object.cpp:39
void setParent(Object *)
Set parent object.
Definition: Object.cpp:336
void unregisterReference(Invalidator *a)
Unegister a reference to this object.
Definition: Object.cpp:187
void mark()
Mark position in command.
Definition: Statement.cpp:170
void restore()
Return to marked position.
Definition: Statement.cpp:175
virtual void update()
Update this object.
Definition: Object.cpp:263
virtual void replace(Object *oldObject, Object *newObject)
Replace references.
Definition: Object.cpp:258
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
T::PETE_Expr_t::PETE_Return_t max(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Definition: ReductionLoc.h:84
int occurrence
Definition: Object.h:261
std::string::iterator iterator
Definition: MSLang.h:15
bool isDirty() const
True, if the [b]modified[/b] flag is set.
Definition: Object.cpp:289
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition: Object.cpp:191
bool isFlagged() const
True, if [b]this[/b] is flagged by setFlag(true).
Definition: Object.cpp:299
Abstract interface for a stream of input tokens.
Definition: TokenStream.h:33
virtual void setShared(bool)
Set/reset shared flag.
Definition: Object.cpp:278
virtual void parseShortcut(Statement &, bool eval=true)
Parser for single-attribute commands.
Definition: Object.cpp:129
Object * getParent() const
Return parent pointer.
Definition: Object.cpp:315
Definition: Inform.h:42
int occurrenceCount()
Return the occurrence counter.
Definition: Object.cpp:351
bool word(std::string &value)
Return word value.
Definition: Statement.cpp:159
Interface for statements.
Definition: Statement.h:38
std::set< Invalidator * > references
Definition: Object.h:268
bool delimiter(char c)
Test for delimiter.
Definition: Statement.cpp:101
std::string parseString(Statement &, const char msg[])
Parse string value.
const std::string & getOpalName() const
Return object name.
Definition: Object.cpp:310
std::vector< Attribute > itsAttr
The object attributes.
Definition: Object.h:216
virtual Object * makeInstance(const std::string &name, Statement &, const Parser *)
Macro handler function.
Definition: Object.cpp:91
std::string itsName
Definition: Object.h:255
std::string itsHelp
Definition: Object.h:258
Abstract base class for references which must be invalidated when an.
Definition: Invalidator.h:27
const std::string name
virtual void print(std::ostream &) const
Print the object.
Definition: Object.cpp:161
double parseRealConst(Statement &)
Parse real constant.
virtual bool isShared() const
Shared flag.
Definition: Object.cpp:273
bool isTreeMember(const Object *subTree) const
Test for tree membership.
Definition: Object.cpp:320
virtual Attribute * findAttribute(const std::string &name)
Find an attribute by name.
Definition: Object.cpp:64
const Object * getBaseObject() const
Return the object&#39;s base type object.
Definition: Object.cpp:303
void setDirty(bool)
Set/reset the [b]modified[/b] flag.
Definition: Object.cpp:283
Interface for abstract language parser.
Definition: Parser.h:31
int increment()
Increment and return the occurrence counter.
Definition: Object.cpp:346
void clear()
Clear the occurrence counter.
Definition: Object.cpp:341
Object * itsParent
Definition: Object.h:252
SDDS1 &description type
Definition: test.stat:4
Inform * gmsg
Definition: Main.cpp:70
end
Definition: multipole_t.tex:9
static void addAttributeOwner(const std::string &owner, const OwnerType &type, const std::string &name)
virtual bool canReplaceBy(Object *object)
Test if replacement is allowed.
Definition: Object.cpp:48
A representation of an Object attribute.
Definition: Attribute.h:52