Author: sh-sh-sh
Date: Wed May 23 17:57:12 2007
New Revision: 38117
URL: http://svn.opensuse.org/viewcvs/yast?rev=38117&view=rev
Log:
base for widget properties
Added:
branches/tmp/sh/mod-ui/core/libyui/src/YProperty.h
Added: 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=38117&view=auto
==============================================================================
--- branches/tmp/sh/mod-ui/core/libyui/src/YProperty.h (added)
+++ branches/tmp/sh/mod-ui/core/libyui/src/YProperty.h Wed May 23 17:57:12 2007
@@ -0,0 +1,143 @@
+/*---------------------------------------------------------------------\
+| |
+| __ __ ____ _____ ____ |
+| \ \ / /_ _/ ___|_ _|___ \ |
+| \ V / _` \___ \ | | __) | |
+| | | (_| |___) || | / __/ |
+| |_|\__,_|____/ |_| |_____| |
+| |
+| core system |
+| |
+| (c) SuSE Linux Products GmbH |
+\----------------------------------------------------------------------/
+
+ File: YProperty.h
+
+ Author: Stefan Hundhammer
+
+/-*/
+
+#ifndef YProperty_h
+#define YProperty_h
+
+#include <string>
+
+namespace YUI
+{
+ /**
+ * Base class for all property classes. Necessary to store different
+ * template instances in the same container class.
+ **/
+ class YPropertyBase
+ {
+ protected:
+ /**
+ * Constructor: Create a property with the specified name.
+ **/
+ YPropertyBase( const std::string & name )
+ : _name( name )
+ {}
+
+ virtual ~YPropertyBase() {}
+
+ public:
+ /**
+ * Returns the name of this property.
+ **/
+ std::string name() const { return _name; }
+
+ private:
+
+ std::string _name;
+ };
+
+
+ /**
+ * Template class for properties of different types for classes of type
+ * Widget_T.
+ **/
+ template class YProperty:
+ public YPropertyBase
+ {
+ public:
+
+ /**
+ * Constructor: Create a property with the specified name.
+ * The value of this property will be retrieved with the specified
+ * 'getMethod' and set with the specified 'setMethod'.
+ *
+ * Example:
+ *
+ * class MyWidget: public YWidget
+ * {
+ * private:
+ * std::string _label;
+ *
+ * public:
+ * ...
+ * std::string label() const { return _label; }
+ * void setLabel( std::string newLabel ) { _label = newLabel; }
+ * };
+ *
+ * YProperty labelProp( "Label",
+ * &(MyWidget::label),
+ * &(MyWidget::setLabel) );
+ * MyWidget w = new MyWidget(...);
+ * std::string myLabel = labelProp.get( w, "Label" );
+ **/
+ YProperty( const std::string & name,
+ Value_T (Widget_T::*getMethod)(),
+ void (Widget_T::*setMethod)( Value_T ) )
+ : YPropertyBase( name )
+ , _getMethod( getMethod )
+ , _setMethodByValue( setMethod )
+ , _setMethodByConstRef( 0 )
+ {}
+
+ /**
+ * Constructor with a setMethod that accepts a const ref rather than
+ * the value directly.
+ **/
+ YProperty( const std::string & name,
+ Value_T (Widget_T::*getMethod)(),
+ void (Widget_T::*setMethod)( const Value_T & ) )
+ : YPropertyBase( name )
+ , _getMethod( getMethod )
+ , _setMethodByValue( 0 )
+ , _setMethodByConstRef( setMethod )
+ {}
+
+ /**
+ * Destructor.
+ **/
+ virtual ~YProperty() {}
+
+ /**
+ * Retrieve the current value of this property from 'widget'.
+ **/
+ Value_T get( Widget_T * widget ) const
+ {
+ return (widget->*_getMethod)();
+ }
+
+ /**
+ * Set this property in 'widget'.
+ **/
+ void set( Widget_T * widget, Value_T newValue )
+ {
+ if ( _setMethodByValue )
+ (widget->*_setMethodByValue)( newValue );
+ else
+ (widget->*_setMethodByConstRef)( newValue );
+ }
+
+ private:
+ Value_T (Widget_T::*_getMethod)();
+ void (Widget_T::*_setMethodByValue)( Value_T );
+ void (Widget_T::*_setMethodByConstRef)( const Value_T & );
+ };
+
+
+} // namespace YUI
+
+#endif // YProperty_h
--
To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org
For additional commands, e-mail: yast-commit+help@opensuse.org