OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
Attribute.h
Go to the documentation of this file.
1 #ifndef OPAL_Attribute_HH
2 #define OPAL_Attribute_HH
3 
4 // ------------------------------------------------------------------------
5 // $RCSfile: Attribute.h,v $
6 // ------------------------------------------------------------------------
7 // $Revision: 1.1.1.1 $
8 // ------------------------------------------------------------------------
9 // Copyright: see Copyright.readme
10 // ------------------------------------------------------------------------
11 //
12 // Class Attribute:
13 //
14 // ------------------------------------------------------------------------
15 //
16 // $Date: 2000/03/27 09:33:34 $
17 // $Author: Andreas Adelmann $
18 //
19 // ------------------------------------------------------------------------
20 
24 #include <iosfwd>
25 #include <string>
26 
27 class Statement;
28 
29 // Class Attribute
30 // ------------------------------------------------------------------------
32 // Contains a pointer [b]base[/b] to the polymorphic attribute value,
33 // derived from [b]AttributeBase[/b], and a pointer [b]handler[/b] to
34 // the polymorphic attribute parser, derived from [b]AttributeHandler[/b].
35 //
36 // The type of the attribute is determined by the class of AttributeHandler
37 // attached. The attribute handler must be set for all attributes by the
38 // exemplar constructor for a command object. The clone constructor does
39 // not copy the handlers, but it creates new references to the same handlers.
40 //
41 // The value is stored in the object pointed at by base. An object can be
42 // created for the default value of an attribute by the exemplar constructor,
43 // or it can be null, meaning an as yet undefined value. The validity of
44 // the pointer can be tested by the method Attribute::isValid().
45 //
46 // Each OPAL command object (derived from class Object) has a
47 // vector<Attribute> containing the command attributes read by the parser.
48 // This attribute vector is first filled by the clone constructor of the
49 // command class by copying the pointers in the model, and possibly by the
50 // OPAL parser with the values given by the user.
51 
52 class Attribute {
53 
54 public:
55 
57  // Leaves both pointers [b]NULL[/b].
58  // An object constructed by the default constructor must be initialized
59  // using assignment before it can be used.
60  // Failing to do so may cause a program crash.
61  Attribute();
62 
64  // Both value and parser are shared with [b]rhs[/b].
65  Attribute(const Attribute &rhs);
66 
68  // The default value is ``undefined''.
69  Attribute(AttributeHandler *h, AttributeBase *b = nullptr);
70 
71  ~Attribute();
72  const Attribute &operator=(const Attribute &);
73 
75  // Returns true, when the value is defined (non-null pointer).
76  operator bool() const;
77 
79  AttributeBase &getBase() const;
80 
81  bool isBaseAllocated() const;
82 
84  // This string duplicates the input expression defining the attribute.
85  std::string getImage() const;
86 
89 
91  // This string is stored in the parser object.
92  const std::string &getHelp() const;
93 
95  // This string is stored in the parser object.
96  const std::string &getName() const;
97 
99  // This string ("real", "logical", etc.) is stored in the parser object.
100  const std::string &getType() const;
101 
103  // If this flag is set, any expression is re-evaluated each time the
104  // value is fetched. Normally attribute evaluation is not deferred,
105  // i. e. any expression is evaluated only the first time the value is
106  // fetched after a new definition has been read.
107  // See [b]Expressions::ADeferred[/b] and [b]Expressions::SDeferred[/b]
108  // for information.
109  bool isDeferred() const;
110 
112  // If this flag is set, the attribute cannot be redefined by the user.
113  // See [b]Expressions::ADeferred[/b] and [b]Expressions::SDeferred[/b]
114  // for information.
115  void setDeferred(bool);
116 
118  // Return true, if the attribute is defined as an expression.
119  bool isExpression() const;
120 
122  // Return true, if the attribute cannot be redefined by the user.
123  bool isReadOnly() const;
124 
126  // Set or reset the attribute's read-only flag.
127  void setReadOnly(bool);
128 
130  // Use the contained parser to parse a new value for the attribute.
131  void parse(Statement &stat, bool eval);
132 
134  // Use the contained parser to parse a new value for an existing
135  // vector component. If the attribute is a scalar value, or if the
136  // component does not exist, this method throws [b]OpalException[/b].
137  void parseComponent(Statement &stat, bool eval, int index);
138 
140  // Assign a new value. The value must be compatible with the parser
141  // type, otherwise a [b]OpalException[/b] is thrown.
142  void set(AttributeBase *newBase);
143 
145  // Set the attribute value to the default value stored in the parser.
146  // If no default value exists, set it to ``undefined''.
147  void setDefault();
148 
150  // Print the attribute name, followed by an equals sign and its value.
151  void print(int &len) const;
152 
153  bool defaultUsed() const;
154 private:
155 
156  // Pointer to the value. The value can be shared for several objects.
158 
159  // Pointer to the shared attribute parser.
161 
162  bool isDefault;
163 };
164 
165 
166 // Class Attribute
167 // ------------------------------------------------------------------------
168 
169 inline Attribute::operator bool() const {
170  return base != nullptr;
171 }
172 
173 inline bool Attribute::defaultUsed() const {
174  return isDefault;
175 }
176 
177 extern std::ostream &operator<<(std::ostream &os, const Attribute &attr);
178 
179 #endif // OPAL_Attribute_HH
std::ostream & operator<<(std::ostream &os, const Attribute &attr)
Definition: Attribute.cpp:169
A representation of an Object attribute.
Definition: Attribute.h:52
void setDeferred(bool)
Set read-only flag.
Definition: Attribute.cpp:107
void print(int &len) const
Print attribute.
Definition: Attribute.cpp:151
AttributeBase & getBase() const
Return reference to polymorphic value.
Definition: Attribute.cpp:69
const std::string & getName() const
Return the attribute name.
Definition: Attribute.cpp:92
bool defaultUsed() const
Definition: Attribute.h:173
void setDefault()
Assign default value.
Definition: Attribute.cpp:145
void parse(Statement &stat, bool eval)
Parse attribute.
Definition: Attribute.cpp:127
bool isExpression() const
Test for expression.
Definition: Attribute.cpp:112
void set(AttributeBase *newBase)
Define new value.
Definition: Attribute.cpp:139
void parseComponent(Statement &stat, bool eval, int index)
Parse array component.
Definition: Attribute.cpp:133
const std::string & getHelp() const
Return the help string.
Definition: Attribute.cpp:82
bool isDeferred() const
Return [b]deferred[/b] flag.
Definition: Attribute.cpp:102
Attribute()
Default constructor.
Definition: Attribute.cpp:34
Pointer< AttributeHandler > handler
Definition: Attribute.h:160
const std::string & getType() const
Return the attribute type.
Definition: Attribute.cpp:97
bool isBaseAllocated() const
Definition: Attribute.cpp:73
std::string getImage() const
Return printable representation.
Definition: Attribute.cpp:87
bool isDefault
Definition: Attribute.h:162
AttributeHandler & getHandler() const
Return a reference to the parser.
Definition: Attribute.cpp:77
bool isReadOnly() const
Test for read only.
Definition: Attribute.cpp:117
void setReadOnly(bool)
Set read-only flag.
Definition: Attribute.cpp:122
Pointer< AttributeBase > base
Definition: Attribute.h:157
const Attribute & operator=(const Attribute &)
Definition: Attribute.cpp:59
Abstract base class for attribute values of different types.
Definition: AttributeBase.h:32
Abstract base class for attribute parsers.
Interface for statements.
Definition: Statement.h:38