OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
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 
28 class Invalidator;
29 class Parser;
30 class Statement;
31 class 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 
48 class Object: public RCObject {
49 
50 public:
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  virtual void parseShortcut(Statement &);
115 
117  // Print a OPAL-readable image of [b]this[/b] on the given output stream.
118  virtual void print(std::ostream &) const;
119 
120  virtual void printValue(std::ostream &) const;
121 
123  // Print help information for [b]this[/b] on the default output stream (argument not used).
124  virtual void printHelp(std::ostream &) const;
125 
127  // Called for all defined objects, when an object [b]oldObject[/b] is
128  // replaced by a new definition [b]newObject[/b].
129  // This default version does nothing.
130  // Some derived classes may react to the redefinition by invalidating
131  // themselves or by replacing reference inside their data.
132  virtual void replace(Object *oldObject, Object *newObject);
133 
135  // Called for all defined objects, before an action command is executed.
136  // This default version does nothing.
137  // Derived classes may use this call to update their internal state.
138  // The beam line elements can update their CLASSIC counterpart.
139  virtual void update();
140 
142  bool isBuiltin() const;
143 
145  // If true, this object cannot be cloned to create new elements.
146  // If it is an element or a line, all references are to the same object.
147  virtual bool isShared() const;
148 
150  // If true, this object cannot be cloned to create new elements.
151  // If it is an element or a line, all references are to the same object.
152  virtual void setShared(bool);
153 
155  // Set the flag when an object is created and modified,
156  // reset it when the object is saved in the DOOM data base.
157  void setDirty(bool);
158 
160  bool isDirty() const;
161 
163  // flagged objects only.
164  void setFlag(bool);
165 
167  bool isFlagged() const;
168 
170  // Return the top-most ancestor of this object, i. e. the built-in
171  // object from which [b]this[/b] is ultimately derived.
172  const Object *getBaseObject() const;
173 
175  const std::string &getOpalName() const;
176 
178  Object *getParent() const;
179 
181  // Return true, if [b]this[/b] has been directly or indirectly derived
182  // from [b]subTree[/b].
183  bool isTreeMember(const Object *subTree) const;
184 
186  void setOpalName(const std::string &name);
187 
189  // This method should normally be called only from Directory.
190  void setParent(Object *);
191 
193  void clear();
194 
196  int increment();
197 
199  int occurrenceCount();
200 
202  // Place [b]a[/b] in the list of references. Whenever [b]this[/b]
203  // is erased or modified, [b]a[/b] will be notified of the change.
205 
207  // Remove [b]a[/b] from the list of references. The object [b]a[/b]
208  // will no longer be notified of any change to [b]this[/b].
210 
211  void registerOwnership(const AttributeHandler::OwnerType &itsClass) const;
212 
214  std::vector<Attribute> itsAttr;
215 
216 protected:
217 
219  // The exemplar object will have the name [b]name[/b], and the help
220  // text [b]help[/b]; it initially reserves [b]size[/b] attributes.
221  Object(int size, const char *name, const char *help);
222 
224  // The clone object will have the name [b]name[/b]; it inherits its
225  // attributes (shared) from the object [b]*parent[/b].
226  Object(const std::string &name, Object *parent);
227 
229  // True, if the object is built-in to OPAL-9, i. e. if it is an exemplar
230  // or a predefined definition.
231  bool builtin;
232 
234  // True when [b]this[/b] is new or modified and should be saved in the
235  // DOOM data base.
236  bool modified;
237 
239  // This flag can be freely set and reset during the execution of a command.
240  bool flagged;
241 
242 private:
243 
244  // Not implemented.
245  Object();
246  Object(const Object &object);
247  void operator=(const Object &);
248 
249  // Pointer to the parent object.
251 
252  // The object name.
253  std::string itsName;
254 
255  // The help text.
256  std::string itsHelp;
257 
258  // The occurrence counter.
260 
261  // The DOOM data base time stamp.
262  double timeStamp;
263 
264  // The set of all references to this object.
265  // This allows to invalidate the references when [b]this[/b] is deleted.
266  std::set <Invalidator *> references;
267 
268  // If true, the object is shared.
270 };
271 
272 
273 // Implementation.
274 // ------------------------------------------------------------------------
275 extern std::ostream &operator<<(std::ostream &os, const Object &object);
276 
277 inline
278 void Object::printValue(std::ostream &os) const {
279  print(os);
280 }
281 
282 #endif // OPAL_Object_HH
virtual void print(std::ostream &) const
Print the object.
Definition: Object.cpp:164
int occurrence
Definition: Object.h:259
bool isFlagged() const
True, if [b]this[/b] is flagged by setFlag(true).
Definition: Object.cpp:273
std::ostream & operator<<(std::ostream &os, const Attribute &attr)
Definition: Attribute.cpp:167
void setParent(Object *)
Set parent object.
Definition: Object.cpp:310
virtual bool isShared() const
Shared flag.
Definition: Object.cpp:247
virtual const std::string getCategory() const =0
Return the object category as a string.
virtual void parseShortcut(Statement &)
Parser for single-attribute commands.
Definition: Object.cpp:132
bool modified
Dirty flag.
Definition: Object.h:236
void copyAttributes(const Object &)
Copy attributes from another object.
Definition: Object.cpp:57
double timeStamp
Definition: Object.h:262
virtual void setShared(bool)
Set/reset shared flag.
Definition: Object.cpp:252
std::string itsHelp
Definition: Object.h:256
bool isDirty() const
True, if the [b]modified[/b] flag is set.
Definition: Object.cpp:263
virtual bool shouldUpdate() const =0
Update flag.
bool isBuiltin() const
True, if [b]this[/b] is a built-in object.
Definition: Object.cpp:242
Abstract interface for a stream of input tokens.
Definition: TokenStream.h:33
virtual Attribute * findAttribute(const std::string &name)
Find an attribute by name.
virtual bool shouldTrace() const =0
Trace flag.
virtual Object * makeTemplate(const std::string &, TokenStream &, Statement &)
Macro handler function.
Definition: Object.cpp:88
Object * getParent() const
Return parent pointer.
Definition: Object.cpp:289
void setDirty(bool)
Set/reset the [b]modified[/b] flag.
Definition: Object.cpp:257
bool flagged
Object flag.
Definition: Object.h:240
std::vector< Attribute > itsAttr
The object attributes (see Attribute.hh).
Definition: Object.h:214
virtual void execute()
Execute the command.
Definition: Object.cpp:62
virtual ~Object()
Definition: Object.cpp:42
virtual void replace(Object *oldObject, Object *newObject)
Replace references.
Definition: Object.cpp:232
Interface for abstract language parser.
Definition: Parser.h:31
const Object * getBaseObject() const
Return the object&#39;s base type object.
Definition: Object.cpp:277
const std::string & getOpalName() const
Return object name.
Definition: Object.cpp:284
A representation of an Object attribute.
Definition: Attribute.h:55
void operator=(const Object &)
std::set< Invalidator * > references
Definition: Object.h:266
Interface for statements.
Definition: Statement.h:38
Abstract base class for references which must be invalidated when an.
Definition: Invalidator.h:27
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition: Object.cpp:194
Abstract base class for reference counted objects.
Definition: RCObject.h:42
virtual void printHelp(std::ostream &) const
Print help.
Definition: Object.cpp:204
virtual Object * makeInstance(const std::string &name, Statement &, const Parser *)
Macro handler function.
Definition: Object.cpp:94
Object * itsParent
Definition: Object.h:250
virtual void printValue(std::ostream &) const
Definition: Object.h:278
virtual void update()
Update this object.
Definition: Object.cpp:237
void unregisterReference(Invalidator *a)
Unegister a reference to this object.
Definition: Object.cpp:190
The base class for all OPAL objects.
Definition: Object.h:48
int increment()
Increment and return the occurrence counter.
Definition: Object.cpp:320
virtual Object * clone(const std::string &name)=0
Return a clone.
void clear()
Clear the occurrence counter.
Definition: Object.cpp:315
const std::string name
void setOpalName(const std::string &name)
Set object name.
Definition: Object.cpp:305
bool sharedFlag
Definition: Object.h:269
void registerReference(Invalidator *a)
Register a reference to this object.
Definition: Object.cpp:185
int occurrenceCount()
Return the occurrence counter.
Definition: Object.cpp:325
bool builtin
Built-in flag.
Definition: Object.h:231
virtual void parse(Statement &)
Parse the object.
Definition: Object.cpp:100
bool isTreeMember(const Object *subTree) const
Test for tree membership.
Definition: Object.cpp:294
void setFlag(bool)
Flag/unflag this object, e. g. to control output of objects for.
Definition: Object.cpp:268
std::string itsName
Definition: Object.h:253
virtual bool canReplaceBy(Object *object)
Test if replacement is allowed.
Definition: Object.cpp:51