OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
Object.h
Go to the documentation of this file.
1#ifndef OPAL_Object_HH
2#define OPAL_Object_HH
3
4// ------------------------------------------------------------------------
5// $RCSfile: Object.h,v $
6// ------------------------------------------------------------------------
7// $Revision: 1.2 $
8// ------------------------------------------------------------------------
9// Copyright: see Copyright.readme
10// ------------------------------------------------------------------------
11//
12// Class: Object
13//
14// ------------------------------------------------------------------------
15//
16// $Date: 2000/03/29 10:40:37 $
17// $Author: opal $
18//
19// ------------------------------------------------------------------------
20
23#include <iosfwd>
24#include <set>
25#include <string>
26#include <vector>
27
28class Invalidator;
29class Parser;
30class Statement;
31class TokenStream;
32
33// Class Object
34// ------------------------------------------------------------------------
36// Any OPAL command object which may be generated by parsing a command or
37// definition.
38// All known Objects are referred to via [b]Pointer<Object>[/b] to
39// make memory management easy. For this purpose, the class Object is
40// derived from the abstract class RCObject.
41//
42// Objects are linked by name to the OPAL directory, which resides in
43// the global object [b]OpalData[/b]. Each Object has a pointer pointing
44// to its parent object. This may be an exemplar object, or a previously
45// generated command or definition object. This mechanism allows to find
46// easily whether an Object belongs to a given class (see isTreeMember()).
47
48class Object: public RCObject {
49
50public:
51
52 virtual ~Object();
53
55 // By default redefinition of an existing Object is not allowed.
56 virtual bool canReplaceBy(Object *object);
57
59 // Call the [b]clone[/b] constructor to generate a copy of [b]this[/b]
60 // with a new name [b]name[/b].
61 virtual Object *clone(const std::string &name) = 0;
62
64 void copyAttributes(const Object &);
65
67 // For definitions check validity, for actions execute the action.
68 virtual void execute();
69
71 // Find the Object's attribute identified by [b]name[/b]
72 // (version for non-constant object).
73 virtual Attribute *findAttribute(const std::string &name);
74
76 // Find the Object's attribute identified by [b]name[/b]
77 // (version for constant object).
78 virtual const Attribute *findAttribute(const std::string &name) const;
79
81 // Is overridden by the (still abstract) sub-classes of Object.
82 virtual const std::string getCategory() const = 0;
83
85 // If true, the object's execute() function should be traced.
86 // For all commands for which OPAL should give tracing output this
87 // method must return true.
88 virtual bool shouldTrace() const = 0;
89
91 // If true, the data structure must be updated before calling execute().
92 // For all commands which require the OPAL data structure to be up to date
93 // for their execution this method must return true.
94 virtual bool shouldUpdate() const = 0;
95
97 // Construct an ``archetype'' object for a MACRO commands.
98 // The default version throws OpalException.
99 virtual Object *makeTemplate(const std::string &, TokenStream &, Statement &);
100
102 // Construct an instance object from an ``archetype'' object.
103 // The default version throws OpalException.
104 virtual Object *makeInstance
105 (const std::string &name, Statement &, const Parser *);
106
108 // Parse the statement and fill in the Object's attributes.
109 virtual void parse(Statement &);
110
112 // This parser allows to use unnamed attributes for command which
113 // have only one attribute.
114 // If [b]eval[/b] is false, then the attribute is not evaluated immediately
115 // even if the delimiter is ``=''.
116 virtual void parseShortcut(Statement &, bool eval = true);
117
119 // Print a OPAL-readable image of [b]this[/b] on the given output stream.
120 virtual void print(std::ostream &) const;
121
122 virtual void printValue(std::ostream &) const;
123
125 // Print help information for [b]this[/b] on the default output stream (argument not used).
126 virtual void printHelp(std::ostream &) const;
127
129 // Called for all defined objects, when an object [b]oldObject[/b] is
130 // replaced by a new definition [b]newObject[/b].
131 // This default version does nothing.
132 // Some derived classes may react to the redefinition by invalidating
133 // themselves or by replacing reference inside their data.
134 virtual void replace(Object *oldObject, Object *newObject);
135
137 // Called for all defined objects, before an action command is executed.
138 // This default version does nothing.
139 // Derived classes may use this call to update their internal state.
140 // The beam line elements can update their CLASSIC counterpart.
141 virtual void update();
142
144 bool isBuiltin() const;
145
147 // If true, this object cannot be cloned to create new elements.
148 // If it is an element or a line, all references are to the same object.
149 virtual bool isShared() const;
150
152 // If true, this object cannot be cloned to create new elements.
153 // If it is an element or a line, all references are to the same object.
154 virtual void setShared(bool);
155
157 // Set the flag when an object is created and modified,
158 // reset it when the object is saved in the DOOM data base.
159 void setDirty(bool);
160
162 bool isDirty() const;
163
165 // flagged objects only.
166 void setFlag(bool);
167
169 bool isFlagged() const;
170
172 // Return the top-most ancestor of this object, i. e. the built-in
173 // object from which [b]this[/b] is ultimately derived.
174 const Object *getBaseObject() const;
175
177 const std::string &getOpalName() const;
178
180 Object *getParent() const;
181
183 // Return true, if [b]this[/b] has been directly or indirectly derived
184 // from [b]subTree[/b].
185 bool isTreeMember(const Object *subTree) const;
186
188 void setOpalName(const std::string &name);
189
191 // This method should normally be called only from Directory.
192 void setParent(Object *);
193
195 void clear();
196
198 int increment();
199
201 int occurrenceCount();
202
204 // Place [b]a[/b] in the list of references. Whenever [b]this[/b]
205 // is erased or modified, [b]a[/b] will be notified of the change.
207
209 // Remove [b]a[/b] from the list of references. The object [b]a[/b]
210 // will no longer be notified of any change to [b]this[/b].
212
213 void registerOwnership(const AttributeHandler::OwnerType &itsClass) const;
214
216 std::vector<Attribute> itsAttr;
217
218protected:
219
221 // The exemplar object will have the name [b]name[/b], and the help
222 // text [b]help[/b]; it initially reserves [b]size[/b] attributes.
223 Object(int size, const char *name, const char *help);
224
226 // The clone object will have the name [b]name[/b]; it inherits its
227 // attributes (shared) from the object [b]*parent[/b].
228 Object(const std::string &name, Object *parent);
229
231 // True, if the object is built-in to OPAL-9, i. e. if it is an exemplar
232 // or a predefined definition.
234
236 // True when [b]this[/b] is new or modified and should be saved in the
237 // DOOM data base.
239
241 // This flag can be freely set and reset during the execution of a command.
243
244private:
245
246 // Not implemented.
248 Object(const Object &object);
249 void operator=(const Object &);
250
251 // Pointer to the parent object.
253
254 // The object name.
255 std::string itsName;
256
257 // The help text.
258 std::string itsHelp;
259
260 // The occurrence counter.
262
263 // The DOOM data base time stamp.
264 double timeStamp;
265
266 // The set of all references to this object.
267 // This allows to invalidate the references when [b]this[/b] is deleted.
268 std::set <Invalidator *> references;
269
270 // If true, the object is shared.
272};
273
274
275// Implementation.
276// ------------------------------------------------------------------------
277extern std::ostream &operator<<(std::ostream &os, const Object &object);
278
279inline
280void Object::printValue(std::ostream &os) const {
281 print(os);
282}
283
284#endif // OPAL_Object_HH
std::ostream & operator<<(std::ostream &os, const Object &object)
Definition: Object.cpp:376
std::complex< double > a
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:27
The base class for all OPAL objects.
Definition: Object.h:48
void setParent(Object *)
Set parent object.
Definition: Object.cpp:336
Object(const Object &object)
virtual bool shouldTrace() const =0
Trace flag.
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
void operator=(const Object &)
virtual Object * clone(const std::string &name)=0
Return a clone.
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
virtual bool shouldUpdate() const =0
Update flag.
virtual const std::string getCategory() const =0
Return the object category as a string.
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
virtual void printValue(std::ostream &) const
Definition: Object.h:280
double timeStamp
Definition: Object.h:264
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
Abstract interface for a stream of input tokens.
Definition: TokenStream.h:33