[yast-commit] r38222 - in /branches/tmp/sh/mod-ui/core/libyui/src: YProperty.cc YProperty.h YRadioButton.cc YUIException.cc YUIException.h YWidget.cc
Author: sh-sh-sh Date: Thu May 31 15:13:34 2007 New Revision: 38222 URL: http://svn.opensuse.org/viewcvs/yast?rev=38222&view=rev Log: property exception handling Modified: branches/tmp/sh/mod-ui/core/libyui/src/YProperty.cc branches/tmp/sh/mod-ui/core/libyui/src/YProperty.h branches/tmp/sh/mod-ui/core/libyui/src/YRadioButton.cc branches/tmp/sh/mod-ui/core/libyui/src/YUIException.cc branches/tmp/sh/mod-ui/core/libyui/src/YUIException.h branches/tmp/sh/mod-ui/core/libyui/src/YWidget.cc Modified: branches/tmp/sh/mod-ui/core/libyui/src/YProperty.cc URL: http://svn.opensuse.org/viewcvs/yast/branches/tmp/sh/mod-ui/core/libyui/src/YProperty.cc?rev=38222&r1=38221&r2=38222&view=diff ============================================================================== --- branches/tmp/sh/mod-ui/core/libyui/src/YProperty.cc (original) +++ branches/tmp/sh/mod-ui/core/libyui/src/YProperty.cc Thu May 31 15:13:34 2007 @@ -18,7 +18,7 @@ /-*/ #include "YProperty.h" - +#include "YUIException.h" #if 0 @@ -52,23 +52,42 @@ } -YPropertySet::SearchResult -YPropertySet::lookup( const std::string & propertyName ) const +void +YPropertySet::check( const std::string & propertyName ) const +{ + if ( ! contains( propertyName ) ) + YUI_THROW( YUIUnknownPropertyException( propertyName ) ); +} + + +void +YPropertySet::check( const std::string & propertyName, YPropertyType type ) const +{ + if ( ! contains( propertyName ) ) + YUI_THROW( YUIUnknownPropertyException( propertyName ) ); + + // YPropertySet::contains( const std::string &, YPropertyType ) will throw + // a YUIPropertyTypeMismatchException, if applicable +} + + +bool +YPropertySet::contains( const std::string & propertyName ) const throw() { for ( std::vector<YProperty>::const_iterator it = _properties.begin(); it != _properties.end(); ++it ) { if ( it->name() == propertyName ) - return Found; + return true; } - return NotFound; + return false; } -YPropertySet::SearchResult -YPropertySet::lookup( const std::string & propertyName, YPropertyType type ) const +bool +YPropertySet::contains( const std::string & propertyName, YPropertyType type ) const { for ( std::vector<YProperty>::const_iterator it = _properties.begin(); it != _properties.end(); @@ -76,14 +95,17 @@ { if ( it->name() == propertyName ) { + if ( it->isReadOnly() ) + YUI_THROW( YUISetReadOnlyPropertyException( *it ) ); + if ( it->type() == type ) - return Found; + return true; else - return TypeMismatch; + YUI_THROW( YUIPropertyTypeMismatchException( *it, type ) ); } } - return NotFound; + return false; } Modified: branches/tmp/sh/mod-ui/core/libyui/src/YProperty.h URL: http://svn.opensuse.org/viewcvs/yast/branches/tmp/sh/mod-ui/core/libyui/src/YProperty.h?rev=38222&r1=38221&r2=38222&view=diff ============================================================================== --- branches/tmp/sh/mod-ui/core/libyui/src/YProperty.h (original) +++ branches/tmp/sh/mod-ui/core/libyui/src/YProperty.h Thu May 31 15:13:34 2007 @@ -47,11 +47,13 @@ { public: /** - * Constructor: Create a property with the specified name. + * Constructor: Create a property with the specified name and type. + * 'isReadOnly' is for properties that cannot be set, only retrieved. **/ - YProperty( const std::string & name, YPropertyType type ) + YProperty( const std::string & name, YPropertyType type, bool isReadOnly = false ) : _name( name ) , _type( type ) + , _isReadOnly( isReadOnly ) {} /** @@ -65,6 +67,11 @@ YPropertyType type() const { return _type; } /** + * Returns 'true' if this property cannot be changed, only retrieved. + **/ + bool isReadOnly() const { return _isReadOnly; } + + /** * Returns the type of this property as string. **/ std::string typeAsStr() const { return YProperty::typeAsStr( _type ); } @@ -73,11 +80,12 @@ * Returns a string description of a property type. **/ static std::string typeAsStr( YPropertyType type ); - + private: std::string _name; YPropertyType _type; + bool _isReadOnly; }; @@ -149,30 +157,60 @@ **/ YPropertySet(); - enum SearchResult - { - NotFound = 0, // = 0 for use like: if ( ! propSet.lookup( "Label" ) ) ... - Found, - TypeMismatch // Found, but wrong type - }; + /** + * Check if a property 'propertyName' exists in this property set. + * Throw a YUIUnknownPropertyException if it does not exist. + * Use YPropertySet::contains() for a check that simply returns 'false' + * if it does not exist. + **/ + void check( const std::string & propertyName ) const; + + /** + * Check if a property 'propertyName' exists in this property set. + * Throw a YUIUnknownPropertyException if it does not exist. + * + * If there is a property with that name, check also the expected type + * against 'type'. If the types don't match, throw a + * YUIPropertyTypeMismatchException. + * If the property is read-only, throw a YUISetReadOnlyPropertyException. + **/ + void check( const std::string & propertyName, YPropertyType type ) const; + + /** + * Same as above, overloaded for convenience. + **/ + void check( const YProperty & prop ) const + { check( prop.name(), prop.type() ); } /** - * Look up a property with 'propertyName' in this property set. + * Check if a property 'propertyName' exists in this property set. + * Returns 'true' if it exists, 'false' if not. + * + * Use YPropertySet::check() for a check that throws exceptions if + * there is no such property. **/ - SearchResult lookup( const std::string & propertyName ) const; + bool contains( const std::string & propertyName ) const throw(); /** - * Look up a property with 'propertyName' in this property set. - * If it contains a property with that name, check also the expected - * type against 'type'. + * Check if a property 'propertyName' exists in this property set. + * Returns 'true' if it exists, 'false' if not. + * + * If there is a property with that name, check also the expected type + * against 'type'. If the types don't match, throw a + * YUIPropertyTypeMismatchException. + * + * If the property is read-only, throw a YUISetReadOnlyPropertyException. + * + * Use YPropertySet::check() for a check that throws exceptions if + * there is no such property. **/ - SearchResult lookup( const std::string & propertyName, YPropertyType type ) const; + bool contains( const std::string & propertyName, YPropertyType type ) const; /** * Same as above, overloaded for convenience. **/ - SearchResult lookup( const YProperty & prop ) const - { return lookup( prop.name(), prop.type() ); } + bool contains( const YProperty & prop ) const + { return contains( prop.name(), prop.type() ); } /** * Returns 'true' if this property set does not contain anything. Modified: branches/tmp/sh/mod-ui/core/libyui/src/YRadioButton.cc URL: http://svn.opensuse.org/viewcvs/yast/branches/tmp/sh/mod-ui/core/libyui/src/YRadioButton.cc?rev=38222&r1=38221&r2=38222&view=diff ============================================================================== --- branches/tmp/sh/mod-ui/core/libyui/src/YRadioButton.cc (original) +++ branches/tmp/sh/mod-ui/core/libyui/src/YRadioButton.cc Thu May 31 15:13:34 2007 @@ -95,14 +95,13 @@ void YRadioButton::setProperty( const std::string & propertyName, const YPropertyValue & val ) { - YPropertySet::SearchResult result = propertySet().lookup( propertyName, val.type() ); + propertySet().check( propertyName, val.type() ); // throws exceptions if not found or type mismatch - if ( result == YPropertySet::Found ) + if ( propertyName == YUIProperty_Value ) setValue( val.boolVal() ); + else if ( propertyName == YUIProperty_Label ) setLabel( val.stringVal() ); + else { - if ( propertyName == YUIProperty_Value ) setValue( val.boolVal() ); - else if ( propertyName == YUIProperty_Label ) setLabel( val.stringVal() ); - else - YWidget::setProperty( propertyName, val ); + YWidget::setProperty( propertyName, val ); } } @@ -110,14 +109,13 @@ YPropertyValue YRadioButton::getProperty( const std::string & propertyName ) { - YPropertySet::SearchResult result = propertySet().lookup( propertyName ); + propertySet().check( propertyName ); // throws exceptions if not found - if ( result == YPropertySet::Found ) + if ( propertyName == YUIProperty_Value ) return YPropertyValue( value() ); + if ( propertyName == YUIProperty_Label ) return YPropertyValue( label() ); + else { - if ( propertyName == YUIProperty_Value ) return YPropertyValue( value() ); - if ( propertyName == YUIProperty_Label ) return YPropertyValue( label() ); - else - return YWidget::getProperty( propertyName ); + return YWidget::getProperty( propertyName ); } } Modified: branches/tmp/sh/mod-ui/core/libyui/src/YUIException.cc URL: http://svn.opensuse.org/viewcvs/yast/branches/tmp/sh/mod-ui/core/libyui/src/YUIException.cc?rev=38222&r1=38221&r2=38222&view=diff ============================================================================== --- branches/tmp/sh/mod-ui/core/libyui/src/YUIException.cc (original) +++ branches/tmp/sh/mod-ui/core/libyui/src/YUIException.cc Thu May 31 15:13:34 2007 @@ -156,16 +156,33 @@ ostream & YUIPropertyTypeMismatchException::dumpOn( ostream & str ) const { - const char * widgetClass = widget() ? - widget()->widgetClass() : - "<Unknown Widget Type>"; + std::string widgetClass; + + if ( widget() ) + widgetClass = std::string( widget()->widgetClass() ) + "::"; return str << "Property type mismatch: " << widgetClass - << "::" << property().name() + << property().name() << "is type " << property().typeAsStr() << ", not " - << value().typeAsStr() + << YProperty::typeAsStr( type() ) + << endl; +} + + +ostream & +YUISetReadOnlyPropertyException::dumpOn( ostream & str ) const +{ + std::string widgetClass; + + if ( widget() ) + widgetClass = std::string( widget()->widgetClass() ) + "::"; + + return str << "Property " + << widgetClass + << property().name() + << "is read-only!" << endl; } Modified: branches/tmp/sh/mod-ui/core/libyui/src/YUIException.h URL: http://svn.opensuse.org/viewcvs/yast/branches/tmp/sh/mod-ui/core/libyui/src/YUIException.h?rev=38222&r1=38221&r2=38222&view=diff ============================================================================== --- branches/tmp/sh/mod-ui/core/libyui/src/YUIException.h (original) +++ branches/tmp/sh/mod-ui/core/libyui/src/YUIException.h Thu May 31 15:13:34 2007 @@ -362,10 +362,15 @@ YProperty property() const { return _property; } /** - * Returns the associated widget or 0 if there was none. + * Returns the corresponding widget or 0 if there was none. **/ YWidget * widget() const { return _widget; } + /** + * Set the corresponding widget. + **/ + void setWidget( YWidget * w ) { _widget = w; } + protected: /** @@ -387,7 +392,8 @@ class YUIUnknownPropertyException: public YUIPropertyException { public: - YUIUnknownPropertyException( const std::string & propertyName, YWidget * widget ) + YUIUnknownPropertyException( const std::string & propertyName, + YWidget * widget = 0 ) : YUIPropertyException( YProperty( propertyName, YUnknownPropertyType ), widget ) {} @@ -412,20 +418,20 @@ { public: - YUIPropertyTypeMismatchException( const YProperty & property, - const YPropertyValue & value, - YWidget * widget ) + YUIPropertyTypeMismatchException( const YProperty & property, + YPropertyType type, + YWidget * widget = 0 ) : YUIPropertyException( property, widget ) - , _val( value ) + , _type( type ) {} virtual ~YUIPropertyTypeMismatchException() throw() {} /** - * Return the property value (and type) the application tried to set. + * Return the property type the application tried to set. **/ - YPropertyValue value() const { return _val; } + YPropertyType type() const { return _type; } protected: @@ -436,7 +442,32 @@ virtual ostream & dumpOn( ostream & str ) const; private: - YPropertyValue _val; + YPropertyType _type; +}; + + +/** + * Exception class for attempt to set a read-only property. + **/ +class YUISetReadOnlyPropertyException: public YUIPropertyException +{ +public: + + YUISetReadOnlyPropertyException( const YProperty & property, + YWidget * widget = 0 ) + : YUIPropertyException( property, widget ) + {} + + virtual ~YUISetReadOnlyPropertyException() throw() + {} + +protected: + + /** + * Write proper error message with all relevant data. + * Reimplemented from YUIException. + **/ + virtual ostream & dumpOn( ostream & str ) const; }; Modified: branches/tmp/sh/mod-ui/core/libyui/src/YWidget.cc URL: http://svn.opensuse.org/viewcvs/yast/branches/tmp/sh/mod-ui/core/libyui/src/YWidget.cc?rev=38222&r1=38221&r2=38222&view=diff ============================================================================== --- branches/tmp/sh/mod-ui/core/libyui/src/YWidget.cc (original) +++ branches/tmp/sh/mod-ui/core/libyui/src/YWidget.cc Thu May 31 15:13:34 2007 @@ -33,6 +33,7 @@ #include "YUIComponent.h" #include "YUI.h" #include "YDialog.h" +#include "YUIException.h" using std::string; @@ -156,15 +157,15 @@ YWidget::propertySet() { static YPropertySet propSet; - + if ( propSet.isEmpty() ) { propSet.add( YProperty( YUIProperty_Enabled, YBoolProperty ) ); propSet.add( YProperty( YUIProperty_Notify, YBoolProperty ) ); propSet.add( YProperty( YUIProperty_WindowID, YIntegerProperty) ); - propSet.add( YProperty( YUIProperty_WidgetClass, YStringProperty ) ); - propSet.add( YProperty( YUIProperty_DebugLabel, YStringProperty ) ); - propSet.add( YProperty( YUIProperty_DialogDebugLabel, YStringProperty ) ); + propSet.add( YProperty( YUIProperty_WidgetClass, YStringProperty, true ) ); // read-only + propSet.add( YProperty( YUIProperty_DebugLabel, YStringProperty, true ) ); // read-only + propSet.add( YProperty( YUIProperty_DialogDebugLabel, YStringProperty, true ) ); // read-only } return propSet; @@ -174,38 +175,41 @@ void YWidget::setProperty( const std::string & propertyName, const YPropertyValue & val ) { - YPropertySet::SearchResult result = propertySet().lookup( propertyName, val.type() ); - - if ( result == YPropertySet::Found ) + try { - if ( propertyName == YUIProperty_Enabled ) setEnabling( val.boolVal() ); - else if ( propertyName == YUIProperty_Notify ) setNotify ( val.boolVal() ); - else - { - if ( propertyName == YUIProperty_WidgetClass || - propertyName == YUIProperty_DebugLabel || - propertyName == YUIProperty_DialogDebugLabel ) - { - y2error( "Property \"%s\" is read-only", propertyName.c_str() ); - } - } + propertySet().check( propertyName, val.type() ); // throws exceptions if not found or type mismatch } + catch( YUIPropertyException & exception ) + { + exception.setWidget( this ); + YUI_RETHROW( exception ); + } + + if ( propertyName == YUIProperty_Enabled ) setEnabling( val.boolVal() ); + else if ( propertyName == YUIProperty_Notify ) setNotify ( val.boolVal() ); } YPropertyValue YWidget::getProperty( const std::string & propertyName ) { - YPropertySet::SearchResult result = propertySet().lookup( propertyName ); - - if ( result == YPropertySet::Found ) + try { - if ( propertyName == YUIProperty_Enabled ) return YPropertyValue( getEnabling() ); - if ( propertyName == YUIProperty_Notify ) return YPropertyValue( getNotify() ); - if ( propertyName == YUIProperty_WidgetClass ) return YPropertyValue( widgetClass() ); - if ( propertyName == YUIProperty_DebugLabel ) return YPropertyValue( debugLabel() ); - if ( propertyName == YUIProperty_DialogDebugLabel ) return YPropertyValue( yDialog()->debugLabel() ); + propertySet().check( propertyName ); // throws exceptions if not found } + catch( YUIPropertyException & exception ) + { + exception.setWidget( this ); + YUI_RETHROW( exception ); + } + + if ( propertyName == YUIProperty_Enabled ) return YPropertyValue( getEnabling() ); + if ( propertyName == YUIProperty_Notify ) return YPropertyValue( getNotify() ); + if ( propertyName == YUIProperty_WidgetClass ) return YPropertyValue( widgetClass() ); + if ( propertyName == YUIProperty_DebugLabel ) return YPropertyValue( debugLabel() ); + if ( propertyName == YUIProperty_DialogDebugLabel ) return YPropertyValue( yDialog()->debugLabel() ); + + return YPropertyValue( false ); // NOTREACHED } @@ -271,16 +275,16 @@ if ( symbol == YUIProperty_WindowID ) return YCPInteger( windowID ); /** * @property string WidgetClass the widget class of this widget (YLabel, YPushButton, ...) - */ + */ if ( symbol == YUIProperty_WidgetClass ) return YCPString( widgetClass() ); /** * @property string DebugLabel a (possibly translated) text describing this widget for debugging - */ + */ if ( symbol == YUIProperty_DebugLabel ) return YCPString( debugLabel() ); /** * @property string DialogDebugLabel a (possibly translated) text describing this dialog for debugging - */ - if ( symbol == YUIProperty_DialogDebugLabel ) + */ + if ( symbol == YUIProperty_DialogDebugLabel ) { return YUIComponent::ui()->currentDialog()->queryWidget( property ); } -- To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org For additional commands, e-mail: yast-commit+help@opensuse.org
participants (1)
-
sh-sh-sh@svn.opensuse.org