libyui  3.4.2
YProperty.h
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: YProperty.h
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 #ifndef YProperty_h
26 #define YProperty_h
27 
28 #include <string>
29 #include <vector>
30 
31 
32 
33 enum YPropertyType
34 {
35  YUnknownPropertyType = 0,
36  YOtherProperty, // requires futher checking
37  YStringProperty, // const std::string &
38  YBoolProperty, // bool
39  YIntegerProperty // YCP Integer == C++ long long
40 };
41 
42 class YWidget;
43 class YProperty;
44 
45 typedef long long YInteger;
46 
47 
48 /**
49  * Class for widget properties.
50  **/
51 class YProperty
52 {
53 public:
54  /**
55  * Constructor: Create a property with the specified name and type.
56  * 'isReadOnly' is for properties that cannot be set, only retrieved.
57  **/
58  YProperty( const std::string & name, YPropertyType type, bool isReadOnly = false )
59  : _name( name )
60  , _type( type )
61  , _isReadOnly( isReadOnly )
62  {}
63 
64  /**
65  * Returns the name of this property.
66  **/
67  std::string name() const { return _name; }
68 
69  /**
70  * Returns the type of this property.
71  **/
72  YPropertyType type() const { return _type; }
73 
74  /**
75  * Returns 'true' if this property cannot be changed, only retrieved.
76  **/
77  bool isReadOnly() const { return _isReadOnly; }
78 
79  /**
80  * Returns the type of this property as string.
81  **/
82  std::string typeAsStr() const { return YProperty::typeAsStr( _type ); }
83 
84  /**
85  * Returns a string description of a property type.
86  **/
87  static std::string typeAsStr( YPropertyType type );
88 
89 private:
90 
91  std::string _name;
92  YPropertyType _type;
93  bool _isReadOnly;
94 };
95 
96 
97 /**
98  * Transport class for the value of simple properties.
99  *
100  * More complex properties (lists of items, tree descriptions, ...) have to
101  * be handled specifically someplace else, but most properties are of
102  * simple types and can be treated in similar ways.
103  **/
105 {
106 public:
107 
108  /**
109  * Constructor for string properties.
110  **/
111  YPropertyValue( const std::string & str ):
112  _type( YStringProperty ), _stringVal( str ) {}
113 
114  /**
115  * Constructor for const char * (string) properties.
116  **/
117  YPropertyValue( const char * str ):
118  _type( YStringProperty ), _stringVal( str ) {}
119 
120  /**
121  * Constructor for bool properties.
122  **/
123  explicit YPropertyValue( bool b ):
124  _type( YBoolProperty ), _boolVal( b ) {}
125 
126  /**
127  * Constructor for numerical (YCP integer) properties.
128  **/
129  explicit YPropertyValue( YInteger num ):
130  _type( YIntegerProperty ), _integerVal( num ) {}
131 
132  /**
133  * Constructor for numerical (YCP integer) properties.
134  **/
135  explicit YPropertyValue( int num ):
136  _type( YIntegerProperty ), _integerVal( num ) {}
137 
138  explicit YPropertyValue( YPropertyType type ) :
139  _type( type ) {}
140 
141  /**
142  * Default constructor
143  **/
145  _type( YUnknownPropertyType ) {}
146 
147  /**
148  * Destructor.
149  **/
150  ~YPropertyValue();
151 
152  /**
153  * Equality operator, can compare with another YPropertyValue.
154  * @throw YUIException for incompatible property types
155  * @return true if the value is the same
156  */
157  bool operator==( const YPropertyValue &other ) const;
158 
159  /** Inequality operator
160  * @throw YUIException for incompatible property types
161  * @see operator==
162  */
163  bool operator!=( const YPropertyValue &other ) const;
164 
165  /**
166  * Returns the type of this property value.
167  * Use this to determine which xyVal() method to use.
168  **/
169  YPropertyType type() const { return _type; }
170 
171  /**
172  * Returns the type of this property value as string.
173  **/
174  std::string typeAsStr() const { return YProperty::typeAsStr( _type ); }
175 
176  /**
177  * Methods to get the value of this property.
178  * Check with type() which one to use.
179  **/
180  std::string stringVal() const { return _stringVal; }
181  bool boolVal() const { return _boolVal; }
182  YInteger integerVal() const { return _integerVal; }
183 
184 
185 private:
186 
187  YPropertyType _type;
188  std::string _stringVal;
189  bool _boolVal;
190  YInteger _integerVal;
191 };
192 
193 
194 /**
195  * A set of properties to check names and types against.
196  **/
198 {
199 public:
200  /**
201  * Constructor.
202  **/
203  YPropertySet();
204 
205  /**
206  * Check if a property 'propertyName' exists in this property set.
207  * Throw a YUIUnknownPropertyException if it does not exist.
208  * Use YPropertySet::contains() for a check that simply returns 'false'
209  * if it does not exist.
210  **/
211  void check( const std::string & propertyName ) const;
212 
213  /**
214  * Check if a property 'propertyName' exists in this property set.
215  * Throw a YUIUnknownPropertyException if it does not exist.
216  *
217  * If there is a property with that name, check also the expected type
218  * against 'type'. If the types don't match, throw a
219  * YUIPropertyTypeMismatchException.
220  * If the property is read-only, throw a YUISetReadOnlyPropertyException.
221  **/
222  void check( const std::string & propertyName, YPropertyType type ) const;
223 
224  /**
225  * Same as above, overloaded for convenience.
226  **/
227  void check( const YProperty & prop ) const
228  { check( prop.name(), prop.type() ); }
229 
230  /**
231  * Check if a property 'propertyName' exists in this property set.
232  * Returns 'true' if it exists, 'false' if not.
233  *
234  * Use YPropertySet::check() for a check that throws exceptions if
235  * there is no such property.
236  **/
237  bool contains( const std::string & propertyName ) const throw();
238 
239  /**
240  * Check if a property 'propertyName' exists in this property set.
241  * Returns 'true' if it exists, 'false' if not.
242  *
243  * If there is a property with that name, check also the expected type
244  * against 'type'. If the types don't match, throw a
245  * YUIPropertyTypeMismatchException.
246  *
247  * If the property is read-only, throw a YUISetReadOnlyPropertyException.
248  *
249  * Use YPropertySet::check() for a check that throws exceptions if
250  * there is no such property.
251  **/
252  bool contains( const std::string & propertyName, YPropertyType type ) const;
253 
254  /**
255  * Same as above, overloaded for convenience.
256  **/
257  bool contains( const YProperty & prop ) const
258  { return contains( prop.name(), prop.type() ); }
259 
260  /**
261  * Returns 'true' if this property set does not contain anything.
262  **/
263  bool isEmpty() const { return _properties.empty(); }
264 
265  /**
266  * Returns the number of properties in this set.
267  **/
268  int size() const { return (int) _properties.size(); }
269 
270  /**
271  * Add a property to this property set.
272  **/
273  void add( const YProperty & prop );
274 
275  /**
276  * Adds all properties of another property set.
277  *
278  * If that other set contains duplicates (properties that are already
279  * in this set), those others will never be found with lookup().
280  **/
281  void add( const YPropertySet & otherSet );
282 
283  typedef std::vector<YProperty>::const_iterator const_iterator;
284 
285  /**
286  * Returns an iterator that points to the first property in this set.
287  **/
288  const_iterator propertiesBegin() const;
289 
290  /**
291  * Returns an iterator that points after the last property in this set.
292  **/
293  const_iterator propertiesEnd() const;
294 
295 private:
296 
297  /**
298  * This class uses a simple std::vector as a container to hold the
299  * properties: Normally, the number of properties for each widget is so
300  * small (2..5) that using any more sophisticated container like
301  * std::set etc. would not pay off. More likely, it would add overhead.
302  **/
303  std::vector<YProperty> _properties;
304 };
305 
306 
307 #endif // YProperty_h
YProperty(const std::string &name, YPropertyType type, bool isReadOnly=false)
Constructor: Create a property with the specified name and type.
Definition: YProperty.h:58
bool isEmpty() const
Returns &#39;true&#39; if this property set does not contain anything.
Definition: YProperty.h:263
YPropertyValue()
Default constructor.
Definition: YProperty.h:144
YPropertyValue(int num)
Constructor for numerical (YCP integer) properties.
Definition: YProperty.h:135
Transport class for the value of simple properties.
Definition: YProperty.h:104
YPropertyType type() const
Returns the type of this property.
Definition: YProperty.h:72
A set of properties to check names and types against.
Definition: YProperty.h:197
std::string typeAsStr() const
Returns the type of this property as string.
Definition: YProperty.h:82
int size() const
Returns the number of properties in this set.
Definition: YProperty.h:268
std::string name() const
Returns the name of this property.
Definition: YProperty.h:67
YPropertyValue(const char *str)
Constructor for const char * (string) properties.
Definition: YProperty.h:117
std::string stringVal() const
Methods to get the value of this property.
Definition: YProperty.h:180
void check(const YProperty &prop) const
Same as above, overloaded for convenience.
Definition: YProperty.h:227
Class for widget properties.
Definition: YProperty.h:51
bool isReadOnly() const
Returns &#39;true&#39; if this property cannot be changed, only retrieved.
Definition: YProperty.h:77
std::string typeAsStr() const
Returns the type of this property value as string.
Definition: YProperty.h:174
bool contains(const YProperty &prop) const
Same as above, overloaded for convenience.
Definition: YProperty.h:257
YPropertyValue(YInteger num)
Constructor for numerical (YCP integer) properties.
Definition: YProperty.h:129
YPropertyValue(bool b)
Constructor for bool properties.
Definition: YProperty.h:123
Abstract base class of all UI widgets.
Definition: YWidget.h:54
YPropertyType type() const
Returns the type of this property value.
Definition: YProperty.h:169
YPropertyValue(const std::string &str)
Constructor for string properties.
Definition: YProperty.h:111