GFC Logo GFC Title Logo
Reference Manual
Main Page  |  Namespace List  |  Alphabetical List  |  Class List  |  File List

property.hh

Go to the documentation of this file.
00001 /* GFC: Gtk+ Foundation Classes (Core Library) 00002 * Copyright (C) 2002-2004 The GFC Development Team. 00003 * 00004 * This program is free software; you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License as published by 00006 * the Free Software Foundation; either version 2 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU Library General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU Library General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 */ 00018 00033 00034 #ifndef GFC_G_PROPERTY_HH 00035 #define GFC_G_PROPERTY_HH 00036 00037 #ifndef GFC_G_VALUE_HH 00038 #include <gfc/glib/value.hh> 00039 #endif 00040 00041 namespace GFC { 00042 00043 namespace Gtk { 00044 class Widget; 00045 } 00046 00047 namespace G { 00048 00049 class Object; 00050 00051 /* G::PropertyBase 00052 */ 00053 00054 class PropertyBase 00055 { 00056 const char *const name_; 00057 00058 protected: 00059 PropertyBase(const char* name); 00060 00061 ~PropertyBase(); 00062 00063 GParamSpec* find_property(const Object *object) const; 00064 00065 void get_property(const Object *object, Value& value) const; 00066 00067 void set_property(const Object *object, Value& value) const; 00068 }; 00069 00070 /* G::Property (readable and writable) 00071 */ 00072 00073 template<typename DataType, typename ValueType = DataType> 00074 class Property : public PropertyBase 00075 { 00076 public: 00077 typedef DataType Data; 00078 00079 Property(const char* name) : PropertyBase(name) 00080 { 00081 } 00082 00083 void get(const Object *object, DataType& data) const 00084 { 00085 GParamSpec *pspec = find_property(object); 00086 ValueType tmp_data = 0; 00087 if (pspec) 00088 { 00089 Value value(pspec); 00090 get_property(object, value); 00091 value.get(tmp_data); 00092 } 00093 data = static_cast<DataType>(tmp_data); 00094 } 00095 00096 void set(const Object *object, const DataType& data) const 00097 { 00098 GParamSpec *pspec = find_property(object); 00099 if (pspec) 00100 { 00101 Value value(pspec); 00102 value.set((ValueType)data); 00103 set_property(object, value); 00104 } 00105 } 00106 }; 00107 00108 /* G::ReadableProperty (read only) 00109 */ 00110 00111 template<typename DataType, typename ValueType = DataType> 00112 class ReadableProperty : private Property<DataType, ValueType> 00113 { 00114 typedef Property<DataType, ValueType> Base; 00115 00116 public: 00117 typedef DataType Data; 00118 00119 using Base::get; 00120 00121 ReadableProperty(const char *name) : Base(name) 00122 { 00123 } 00124 }; 00125 00126 /* G::WritableProperty (write only) 00127 */ 00128 00129 template<typename DataType, typename ValueType = DataType> 00130 class WritableProperty : private Property<DataType, ValueType> 00131 { 00132 typedef Property<DataType, ValueType> Base; 00133 00134 public: 00135 typedef DataType Data; 00136 00137 using Base::set; 00138 00139 WritableProperty(const char *name) : Base(name) 00140 { 00141 } 00142 }; 00143 00144 /* G::PropertyProxyBase 00145 */ 00146 00147 template<typename ObjectType, typename PropertyType> 00148 class PropertyProxyBase 00149 { 00150 ObjectType *const object_; 00151 const PropertyType *const property_; 00152 00153 protected: 00154 PropertyProxyBase(ObjectType *object, const PropertyType *property) : object_(object), property_(property) 00155 { 00156 } 00157 00158 ~PropertyProxyBase() 00159 { 00160 } 00161 00162 const ObjectType* object() const 00163 { 00164 return object_; 00165 } 00166 00167 const PropertyType* property() const 00168 { 00169 return property_; 00170 } 00171 }; 00172 00173 /* G::PropertyProxy 00174 */ 00175 00176 template<typename ObjectType, typename PropertyType> 00177 class PropertyProxy : public PropertyProxyBase<ObjectType, PropertyType> 00178 { 00179 typedef PropertyProxyBase<ObjectType, PropertyType> Base; 00180 00181 public: 00182 typedef typename PropertyType::Data DataType; 00183 00184 PropertyProxy(ObjectType *object, const PropertyType *property) : Base(object, property) 00185 { 00186 } 00187 00188 void get(DataType& data) const 00189 { 00190 Base::property()->get(Base::object(), data); 00191 } 00192 00193 void set(const DataType& data) const 00194 { 00195 Base::property()->set(Base::object(), data); 00196 } 00197 }; 00198 00199 } // namespace G 00200 00201 } // namespace GFC 00202 00203 #endif // GFC_G_PROPERTY_HH

Generated on Tue Aug 24 00:04:56 2004 for GFC-Core by doxygen 1.3.8