Object.h 9.54 KB
Newer Older
gsell's avatar
gsell committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
#ifndef OPAL_Object_HH
#define OPAL_Object_HH

// ------------------------------------------------------------------------
// $RCSfile: Object.h,v $
// ------------------------------------------------------------------------
// $Revision: 1.2 $
// ------------------------------------------------------------------------
// Copyright: see Copyright.readme
// ------------------------------------------------------------------------
//
// Class: Object
//
// ------------------------------------------------------------------------
//
// $Date: 2000/03/29 10:40:37 $
// $Author: opal $
//
// ------------------------------------------------------------------------

#include "AbstractObjects/Attribute.h"
#include "MemoryManagement/RCObject.h"
#include <iosfwd>
#include <set>
#include <string>
#include <vector>

class Invalidator;
class Parser;
class Statement;
class TokenStream;

// Class Object
// ------------------------------------------------------------------------
/// The base class for all OPAL objects.
//  Any OPAL command object which may be generated by parsing a command or
//  definition.
//  All known Objects are referred to via [b]Pointer<Object>[/b] to
//  make memory management easy.  For this purpose, the class Object is
//  derived from the abstract class RCObject.
//
//  Objects are linked by name to the OPAL directory, which resides in
//  the global object [b]OpalData[/b].  Each Object has a pointer pointing
//  to its parent object.  This may be an exemplar object, or a previously
//  generated command or definition object.  This mechanism allows to find
//  easily whether an Object belongs to a given class (see isTreeMember()).

class Object: public RCObject {

public:

    virtual ~Object();

    /// Test if replacement is allowed.
    //  By default redefinition of an existing Object is not allowed.
    virtual bool canReplaceBy(Object *object);

    /// Return a clone.
    //  Call the [b]clone[/b] constructor to generate a copy of [b]this[/b]
    //  with a new name [b]name[/b].
    virtual Object *clone(const std::string &name) = 0;

    /// Copy attributes from another object.
    void copyAttributes(const Object &);

    /// Execute the command.
    //  For definitions check validity, for actions execute the action.
    virtual void execute();

    /// Find an attribute by name.
    //  Find the Object's attribute identified by [b]name[/b]
    //  (version for non-constant object).
    virtual Attribute *findAttribute(const std::string &name);

    /// Find an attribute by name.
    //  Find the Object's attribute identified by [b]name[/b]
    //  (version for constant object).
    virtual const Attribute *findAttribute(const std::string &name) const;

    /// Return the object category as a string.
    //  Is overridden by the (still abstract) sub-classes of Object.
    virtual const std::string getCategory() const = 0;

    /// Trace flag.
    //  If true, the object's execute() function should be traced.
    //  For all commands for which OPAL should give tracing output this
    //  method must return true.
    virtual bool shouldTrace() const = 0;

    /// Update flag.
    //  If true, the data structure must be updated before calling execute().
    //  For all commands which require the OPAL data structure to be up to date
    //  for their execution this method must return true.
    virtual bool shouldUpdate() const = 0;

    /// Macro handler function.
    //  Construct an ``archetype'' object for a MACRO commands.
    //  The default version throws OpalException.
    virtual Object *makeTemplate(const std::string &, TokenStream &, Statement &);

    /// Macro handler function.
    //  Construct an instance object from an ``archetype'' object.
    //  The default version throws OpalException.
    virtual Object *makeInstance
    (const std::string &name, Statement &, const Parser *);

    /// Parse the object.
    //  Parse the statement and fill in the Object's attributes.
    virtual void parse(Statement &);

    /// Parser for single-attribute commands.
    //  This parser allows to use unnamed attributes for command which
    //  have only one attribute.
114 115 116
    //  If [b]eval[/b] is false, then the attribute is not evaluated immediately
    //  even if it the delimiter is ``=''.
    virtual void parseShortcut(Statement &, bool eval = true);
gsell's avatar
gsell committed
117 118 119 120 121

    /// Print the object.
    //  Print a OPAL-readable image of [b]this[/b] on the given output stream.
    virtual void print(std::ostream &) const;

122 123
    virtual void printValue(std::ostream &) const;

gsell's avatar
gsell committed
124
    /// Print help.
snuverink_j's avatar
snuverink_j committed
125
    //  Print help information for [b]this[/b] on the default output stream (argument not used).
gsell's avatar
gsell committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
    virtual void printHelp(std::ostream &) const;

    /// Replace references.
    //  Called for all defined objects, when an object [b]oldObject[/b] is
    //  replaced by a new definition [b]newObject[/b].
    //  This default version does nothing.
    //  Some derived classes may react to the redefinition by invalidating
    //  themselves or by replacing reference inside their data.
    virtual void replace(Object *oldObject, Object *newObject);

    /// Update this object.
    //  Called for all defined objects, before an action command is executed.
    //  This default version does nothing.
    //  Derived classes may use this call to update their internal state.
    //  The beam line elements can update their CLASSIC counterpart.
    virtual void update();

    /// True, if [b]this[/b] is a built-in object.
    bool isBuiltin() const;

    /// Shared flag.
    //  If true, this object cannot be cloned to create new elements.
    //  If it is an element or a line, all references are to the same object.
    virtual bool isShared() const;

    /// Set/reset shared flag.
    //  If true, this object cannot be cloned to create new elements.
    //  If it is an element or a line, all references are to the same object.
    virtual void setShared(bool);

    /// Set/reset the [b]modified[/b] flag.
    //  Set the flag when an object is created and modified,
    //  reset it when the object is saved in the DOOM data base.
    void setDirty(bool);

    /// True, if the [b]modified[/b] flag is set.
    bool isDirty() const;

    /// Flag/unflag this object, e. g. to control output of objects for
    //  flagged objects only.
    void setFlag(bool);

    /// True, if [b]this[/b] is flagged by setFlag(true).
    bool isFlagged() const;

    /// Return the object's base type object.
    //  Return the top-most ancestor of this object, i. e. the built-in
    //  object from which [b]this[/b] is ultimately derived.
    const Object *getBaseObject() const;

    /// Return object name.
    const std::string &getOpalName() const;

    /// Return parent pointer.
    Object *getParent() const;

    /// Test for tree membership.
    //  Return true, if [b]this[/b] has been directly or indirectly derived
    //  from [b]subTree[/b].
    bool isTreeMember(const Object *subTree) const;

    /// Set object name.
    void setOpalName(const std::string &name);

    /// Set parent object.
    //  This method should normally be called only from Directory.
    void setParent(Object *);

    /// Clear the occurrence counter.
    void clear();

    /// Increment and return the occurrence counter.
    int increment();

    /// Return the occurrence counter.
    int occurrenceCount();

    /// Register a reference to this object.
    //  Place [b]a[/b] in the list of references.  Whenever [b]this[/b]
    //  is erased or modified, [b]a[/b] will be notified of the change.
    void registerReference(Invalidator *a);

    /// Unegister a reference to this object.
    //  Remove [b]a[/b] from the list of references.  The object [b]a[/b]
    //  will no longer be notified of any change to [b]this[/b].
    void unregisterReference(Invalidator *a);

213 214
    void registerOwnership(const AttributeHandler::OwnerType &itsClass) const;

215
    /// The object attributes.
gsell's avatar
gsell committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
    std::vector<Attribute> itsAttr;

protected:

    /// Constructor for exemplars.
    //  The exemplar object will have the name [b]name[/b], and the help
    //  text [b]help[/b]; it initially reserves [b]size[/b] attributes.
    Object(int size, const char *name, const char *help);

    /// Constructor for clones.
    //  The clone object will have the name [b]name[/b]; it inherits its
    //  attributes (shared) from the object [b]*parent[/b].
    Object(const std::string &name, Object *parent);

    /// Built-in flag.
    //  True, if the object is built-in to OPAL-9, i. e. if it is an exemplar
    //  or a predefined definition.
    bool builtin;

    /// Dirty flag.
    //  True when [b]this[/b] is new or modified and should be saved in the
    //  DOOM data base.
    bool modified;

    /// Object flag.
    //  This flag can be freely set and reset during the execution of a command.
    bool flagged;

private:

    // Not implemented.
    Object();
    Object(const Object &object);
    void operator=(const Object &);

    // Pointer to the parent object.
    Object *itsParent;

    // The object name.
    std::string itsName;

    // The help text.
    std::string itsHelp;

    // The occurrence counter.
    int occurrence;

    // The DOOM data base time stamp.
    double timeStamp;

    // The set of all references to this object.
    // This allows to invalidate the references when [b]this[/b] is deleted.
    std::set <Invalidator *> references;

    // If true, the object is shared.
    bool sharedFlag;
};


// Implementation.
// ------------------------------------------------------------------------
extern std::ostream &operator<<(std::ostream &os, const Object &object);

279 280 281 282 283 284
inline
void Object::printValue(std::ostream &os) const {
    print(os);
}

#endif // OPAL_Object_HH