libyui  3.1.4
 All Classes Files Functions Variables Typedefs Enumerations Friends Pages
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  * Returns the type of this property value.
154  * Use this to determine which xyVal() method to use.
155  **/
156  YPropertyType type() const { return _type; }
157 
158  /**
159  * Returns the type of this property value as string.
160  **/
161  std::string typeAsStr() const { return YProperty::typeAsStr( _type ); }
162 
163  /**
164  * Methods to get the value of this property.
165  * Check with type() which one to use.
166  **/
167  std::string stringVal() const { return _stringVal; }
168  bool boolVal() const { return _boolVal; }
169  YInteger integerVal() const { return _integerVal; }
170 
171 
172 private:
173 
174  YPropertyType _type;
175  std::string _stringVal;
176  bool _boolVal;
177  YInteger _integerVal;
178 };
179 
180 
181 /**
182  * A set of properties to check names and types against.
183  **/
185 {
186 public:
187  /**
188  * Constructor.
189  **/
190  YPropertySet();
191 
192  /**
193  * Check if a property 'propertyName' exists in this property set.
194  * Throw a YUIUnknownPropertyException if it does not exist.
195  * Use YPropertySet::contains() for a check that simply returns 'false'
196  * if it does not exist.
197  **/
198  void check( const std::string & propertyName ) const;
199 
200  /**
201  * Check if a property 'propertyName' exists in this property set.
202  * Throw a YUIUnknownPropertyException if it does not exist.
203  *
204  * If there is a property with that name, check also the expected type
205  * against 'type'. If the types don't match, throw a
206  * YUIPropertyTypeMismatchException.
207  * If the property is read-only, throw a YUISetReadOnlyPropertyException.
208  **/
209  void check( const std::string & propertyName, YPropertyType type ) const;
210 
211  /**
212  * Same as above, overloaded for convenience.
213  **/
214  void check( const YProperty & prop ) const
215  { check( prop.name(), prop.type() ); }
216 
217  /**
218  * Check if a property 'propertyName' exists in this property set.
219  * Returns 'true' if it exists, 'false' if not.
220  *
221  * Use YPropertySet::check() for a check that throws exceptions if
222  * there is no such property.
223  **/
224  bool contains( const std::string & propertyName ) const throw();
225 
226  /**
227  * Check if a property 'propertyName' exists in this property set.
228  * Returns 'true' if it exists, 'false' if not.
229  *
230  * If there is a property with that name, check also the expected type
231  * against 'type'. If the types don't match, throw a
232  * YUIPropertyTypeMismatchException.
233  *
234  * If the property is read-only, throw a YUISetReadOnlyPropertyException.
235  *
236  * Use YPropertySet::check() for a check that throws exceptions if
237  * there is no such property.
238  **/
239  bool contains( const std::string & propertyName, YPropertyType type ) const;
240 
241  /**
242  * Same as above, overloaded for convenience.
243  **/
244  bool contains( const YProperty & prop ) const
245  { return contains( prop.name(), prop.type() ); }
246 
247  /**
248  * Returns 'true' if this property set does not contain anything.
249  **/
250  bool isEmpty() const { return _properties.empty(); }
251 
252  /**
253  * Returns the number of properties in this set.
254  **/
255  int size() const { return (int) _properties.size(); }
256 
257  /**
258  * Add a property to this property set.
259  **/
260  void add( const YProperty & prop );
261 
262  /**
263  * Adds all properties of another property set.
264  *
265  * If that other set contains duplicates (properties that are already
266  * in this set), those others will never be found with lookup().
267  **/
268  void add( const YPropertySet & otherSet );
269 
270  typedef std::vector<YProperty>::const_iterator const_iterator;
271 
272  /**
273  * Returns an iterator that points to the first property in this set.
274  **/
275  const_iterator propertiesBegin() const;
276 
277  /**
278  * Returns an iterator that points after the last property in this set.
279  **/
280  const_iterator propertiesEnd() const;
281 
282 private:
283 
284  /**
285  * This class uses a simple std::vector as a container to hold the
286  * properties: Normally, the number of properties for each widget is so
287  * small (2..5) that using any more sophisticated container like
288  * std::set etc. would not pay off. More likely, it would add overhead.
289  **/
290  std::vector<YProperty> _properties;
291 };
292 
293 
294 #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
std::string typeAsStr() const
Returns the type of this property value as string.
Definition: YProperty.h:161
YPropertyValue()
Default constructor.
Definition: YProperty.h:144
YPropertyValue(int num)
Constructor for numerical (YCP integer) properties.
Definition: YProperty.h:135
YPropertyType type() const
Returns the type of this property.
Definition: YProperty.h:72
Transport class for the value of simple properties.
Definition: YProperty.h:104
void check(const std::string &propertyName) const
Check if a property 'propertyName' exists in this property set.
Definition: YProperty.cc:62
~YPropertyValue()
Destructor.
Definition: YProperty.cc:49
void add(const YProperty &prop)
Add a property to this property set.
Definition: YProperty.cc:120
A set of properties to check names and types against.
Definition: YProperty.h:184
const_iterator propertiesBegin() const
Returns an iterator that points to the first property in this set.
Definition: YProperty.cc:139
std::string stringVal() const
Methods to get the value of this property.
Definition: YProperty.h:167
std::string name() const
Returns the name of this property.
Definition: YProperty.h:67
int size() const
Returns the number of properties in this set.
Definition: YProperty.h:255
bool contains(const YProperty &prop) const
Same as above, overloaded for convenience.
Definition: YProperty.h:244
YPropertyValue(const char *str)
Constructor for const char * (string) properties.
Definition: YProperty.h:117
bool contains(const std::string &propertyName) const
Check if a property 'propertyName' exists in this property set.
Definition: YProperty.cc:81
YPropertySet()
Constructor.
Definition: YProperty.cc:55
bool isEmpty() const
Returns 'true' if this property set does not contain anything.
Definition: YProperty.h:250
Class for widget properties.
Definition: YProperty.h:51
const_iterator propertiesEnd() const
Returns an iterator that points after the last property in this set.
Definition: YProperty.cc:145
bool isReadOnly() const
Returns 'true' if this property cannot be changed, only retrieved.
Definition: YProperty.h:77
YPropertyValue(YInteger num)
Constructor for numerical (YCP integer) properties.
Definition: YProperty.h:129
YPropertyValue(bool b)
Constructor for bool properties.
Definition: YProperty.h:123
YPropertyType type() const
Returns the type of this property value.
Definition: YProperty.h:156
void check(const YProperty &prop) const
Same as above, overloaded for convenience.
Definition: YProperty.h:214
Abstract base class of all UI widgets.
Definition: YWidget.h:54
std::string typeAsStr() const
Returns the type of this property as string.
Definition: YProperty.h:82
YPropertyValue(const std::string &str)
Constructor for string properties.
Definition: YProperty.h:111