libyui
3.0.10
|
00001 /* 00002 Copyright (C) 2000-2012 Novell, Inc 00003 This library is free software; you can redistribute it and/or modify 00004 it under the terms of the GNU Lesser General Public License as 00005 published by the Free Software Foundation; either version 2.1 of the 00006 License, or (at your option) version 3.0 of the License. This library 00007 is distributed in the hope that it will be useful, but WITHOUT ANY 00008 WARRANTY; without even the implied warranty of MERCHANTABILITY or 00009 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 00010 License for more details. You should have received a copy of the GNU 00011 Lesser General Public License along with this library; if not, write 00012 to the Free Software Foundation, Inc., 51 Franklin Street, Fifth 00013 Floor, Boston, MA 02110-1301 USA 00014 */ 00015 00016 00017 /*-/ 00018 00019 File: YProperty.h 00020 00021 Author: Stefan Hundhammer <sh@suse.de> 00022 00023 /-*/ 00024 00025 #ifndef YProperty_h 00026 #define YProperty_h 00027 00028 #include <string> 00029 #include <vector> 00030 00031 00032 00033 enum YPropertyType 00034 { 00035 YUnknownPropertyType = 0, 00036 YOtherProperty, // requires futher checking 00037 YStringProperty, // const std::string & 00038 YBoolProperty, // bool 00039 YIntegerProperty // YCP Integer == C++ long long 00040 }; 00041 00042 class YWidget; 00043 class YProperty; 00044 00045 typedef long long YInteger; 00046 00047 00048 /** 00049 * Class for widget properties. 00050 **/ 00051 class YProperty 00052 { 00053 public: 00054 /** 00055 * Constructor: Create a property with the specified name and type. 00056 * 'isReadOnly' is for properties that cannot be set, only retrieved. 00057 **/ 00058 YProperty( const std::string & name, YPropertyType type, bool isReadOnly = false ) 00059 : _name( name ) 00060 , _type( type ) 00061 , _isReadOnly( isReadOnly ) 00062 {} 00063 00064 /** 00065 * Returns the name of this property. 00066 **/ 00067 std::string name() const { return _name; } 00068 00069 /** 00070 * Returns the type of this property. 00071 **/ 00072 YPropertyType type() const { return _type; } 00073 00074 /** 00075 * Returns 'true' if this property cannot be changed, only retrieved. 00076 **/ 00077 bool isReadOnly() const { return _isReadOnly; } 00078 00079 /** 00080 * Returns the type of this property as string. 00081 **/ 00082 std::string typeAsStr() const { return YProperty::typeAsStr( _type ); } 00083 00084 /** 00085 * Returns a string description of a property type. 00086 **/ 00087 static std::string typeAsStr( YPropertyType type ); 00088 00089 private: 00090 00091 std::string _name; 00092 YPropertyType _type; 00093 bool _isReadOnly; 00094 }; 00095 00096 00097 /** 00098 * Transport class for the value of simple properties. 00099 * 00100 * More complex properties (lists of items, tree descriptions, ...) have to 00101 * be handled specifically someplace else, but most properties are of 00102 * simple types and can be treated in similar ways. 00103 **/ 00104 class YPropertyValue 00105 { 00106 public: 00107 00108 /** 00109 * Constructor for string properties. 00110 **/ 00111 YPropertyValue( const std::string & str ): 00112 _type( YStringProperty ), _stringVal( str ) {} 00113 00114 /** 00115 * Constructor for const char * (string) properties. 00116 **/ 00117 YPropertyValue( const char * str ): 00118 _type( YStringProperty ), _stringVal( str ) {} 00119 00120 /** 00121 * Constructor for bool properties. 00122 **/ 00123 explicit YPropertyValue( bool b ): 00124 _type( YBoolProperty ), _boolVal( b ) {} 00125 00126 /** 00127 * Constructor for numerical (YCP integer) properties. 00128 **/ 00129 explicit YPropertyValue( YInteger num ): 00130 _type( YIntegerProperty ), _integerVal( num ) {} 00131 00132 /** 00133 * Constructor for numerical (YCP integer) properties. 00134 **/ 00135 explicit YPropertyValue( int num ): 00136 _type( YIntegerProperty ), _integerVal( num ) {} 00137 00138 explicit YPropertyValue( YPropertyType type ) : 00139 _type( type ) {} 00140 00141 /** 00142 * Default constructor 00143 **/ 00144 YPropertyValue(): 00145 _type( YUnknownPropertyType ) {} 00146 00147 /** 00148 * Destructor. 00149 **/ 00150 ~YPropertyValue(); 00151 00152 /** 00153 * Returns the type of this property value. 00154 * Use this to determine which xyVal() method to use. 00155 **/ 00156 YPropertyType type() const { return _type; } 00157 00158 /** 00159 * Returns the type of this property value as string. 00160 **/ 00161 std::string typeAsStr() const { return YProperty::typeAsStr( _type ); } 00162 00163 /** 00164 * Methods to get the value of this property. 00165 * Check with type() which one to use. 00166 **/ 00167 std::string stringVal() const { return _stringVal; } 00168 bool boolVal() const { return _boolVal; } 00169 YInteger integerVal() const { return _integerVal; } 00170 00171 00172 private: 00173 00174 YPropertyType _type; 00175 std::string _stringVal; 00176 bool _boolVal; 00177 YInteger _integerVal; 00178 }; 00179 00180 00181 /** 00182 * A set of properties to check names and types against. 00183 **/ 00184 class YPropertySet 00185 { 00186 public: 00187 /** 00188 * Constructor. 00189 **/ 00190 YPropertySet(); 00191 00192 /** 00193 * Check if a property 'propertyName' exists in this property set. 00194 * Throw a YUIUnknownPropertyException if it does not exist. 00195 * Use YPropertySet::contains() for a check that simply returns 'false' 00196 * if it does not exist. 00197 **/ 00198 void check( const std::string & propertyName ) const; 00199 00200 /** 00201 * Check if a property 'propertyName' exists in this property set. 00202 * Throw a YUIUnknownPropertyException if it does not exist. 00203 * 00204 * If there is a property with that name, check also the expected type 00205 * against 'type'. If the types don't match, throw a 00206 * YUIPropertyTypeMismatchException. 00207 * If the property is read-only, throw a YUISetReadOnlyPropertyException. 00208 **/ 00209 void check( const std::string & propertyName, YPropertyType type ) const; 00210 00211 /** 00212 * Same as above, overloaded for convenience. 00213 **/ 00214 void check( const YProperty & prop ) const 00215 { check( prop.name(), prop.type() ); } 00216 00217 /** 00218 * Check if a property 'propertyName' exists in this property set. 00219 * Returns 'true' if it exists, 'false' if not. 00220 * 00221 * Use YPropertySet::check() for a check that throws exceptions if 00222 * there is no such property. 00223 **/ 00224 bool contains( const std::string & propertyName ) const throw(); 00225 00226 /** 00227 * Check if a property 'propertyName' exists in this property set. 00228 * Returns 'true' if it exists, 'false' if not. 00229 * 00230 * If there is a property with that name, check also the expected type 00231 * against 'type'. If the types don't match, throw a 00232 * YUIPropertyTypeMismatchException. 00233 * 00234 * If the property is read-only, throw a YUISetReadOnlyPropertyException. 00235 * 00236 * Use YPropertySet::check() for a check that throws exceptions if 00237 * there is no such property. 00238 **/ 00239 bool contains( const std::string & propertyName, YPropertyType type ) const; 00240 00241 /** 00242 * Same as above, overloaded for convenience. 00243 **/ 00244 bool contains( const YProperty & prop ) const 00245 { return contains( prop.name(), prop.type() ); } 00246 00247 /** 00248 * Returns 'true' if this property set does not contain anything. 00249 **/ 00250 bool isEmpty() const { return _properties.empty(); } 00251 00252 /** 00253 * Returns the number of properties in this set. 00254 **/ 00255 int size() const { return (int) _properties.size(); } 00256 00257 /** 00258 * Add a property to this property set. 00259 **/ 00260 void add( const YProperty & prop ); 00261 00262 /** 00263 * Adds all properties of another property set. 00264 * 00265 * If that other set contains duplicates (properties that are already 00266 * in this set), those others will never be found with lookup(). 00267 **/ 00268 void add( const YPropertySet & otherSet ); 00269 00270 typedef std::vector<YProperty>::const_iterator const_iterator; 00271 00272 /** 00273 * Returns an iterator that points to the first property in this set. 00274 **/ 00275 const_iterator propertiesBegin() const; 00276 00277 /** 00278 * Returns an iterator that points after the last property in this set. 00279 **/ 00280 const_iterator propertiesEnd() const; 00281 00282 private: 00283 00284 /** 00285 * This class uses a simple std::vector as a container to hold the 00286 * properties: Normally, the number of properties for each widget is so 00287 * small (2..5) that using any more sophisticated container like 00288 * std::set etc. would not pay off. More likely, it would add overhead. 00289 **/ 00290 std::vector<YProperty> _properties; 00291 }; 00292 00293 00294 #endif // YProperty_h