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