virtualproperty.h

00001 /***************************************************************************
00002  *   Copyright (C) 2004 by Rick L. Vinyard, Jr.                            *
00003  *   rvinyard@cs.nmsu.edu                                                  *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation version 2.1.                *
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 General Public License for more details.                          *
00013  *                                                                         *
00014  *   You should have received a copy of the GNU Lesser General Public      *
00015  *   License along with this library; if not, write to the                 *
00016  *   Free Software Foundation, Inc.,                                       *
00017  *   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA              *
00018  ***************************************************************************/
00019 
00020 #ifndef IDIOSKOPOSVIRTUALPROPERTY_H
00021 #define IDIOSKOPOSVIRTUALPROPERTY_H
00022 
00023 #include <idioskopos/propertybase.h>
00024 
00025 namespace Idioskopos {
00026 
00030   template <typename T>
00031   class ReadOnlyVirtualProperty: public ReadOnlyPropertyBase<T> {
00032     public:
00033       typedef IdioskoposPointer<ReadOnlyVirtualProperty> pointer;
00034       typedef T value_type;
00035       typedef sigc::slot<T> GetSlotType;
00036 
00037       IDIOSKOPOS_OBJECT(ReadOnlyVirtualProperty);
00038 
00042       ReadOnlyVirtualProperty ( const Glib::ustring& name,
00043                                 sigc::slot<T> get_slot = GetSlotType() ) :
00044           Object(name),
00045           ReadOnlyPropertyBase<T>(name),
00046           m_get_slot( get_slot ) { }
00047 
00053       ReadOnlyVirtualProperty ( const Glib::ustring& name, Introspectable& container, sigc::slot<T> get_slot = GetSlotType() ) :
00054           Object( name, container ),
00055           ReadOnlyPropertyBase<T>( name, container ),
00056           m_get_slot( get_slot ) { }
00057 
00058       static pointer create( const Glib::ustring& name, sigc::slot<T> get_slot = GetSlotType() )
00059       {
00060         return pointer( new ReadOnlyVirtualProperty(name, get_slot) );
00061       }
00062 
00063       static pointer create( const Glib::ustring& name, Introspectable& container, sigc::slot<T> get_slot = GetSlotType() )
00064       {
00065         return pointer( new ReadOnlyVirtualProperty(name, container, get_slot) );
00066       }
00067 
00068       virtual ~ReadOnlyVirtualProperty() { }
00069 
00071       virtual const T& get() const {
00072         static T t;
00073         if ( m_get_slot ) {
00074             t = m_get_slot();
00075             return t;
00076           }
00077           else
00078             // TODO throw a proper exception class instead of a general throw
00079             throw;
00080         }
00081 
00083       virtual T* get_pointer() {
00084         return 0x00;
00085       }
00086 
00087       sigc::slot<T> get_slot() { return m_get_slot; }
00088 
00089       void get_slot( sigc::slot<T> slot ) { m_get_slot = slot; }
00090 
00091       void get_slot_clear() { m_get_slot = GetSlotType(); }
00092 
00093     protected:
00094       sigc::slot<T> m_get_slot;
00095 
00096   };
00097 
00101   template <typename T>
00102   class WriteOnlyVirtualProperty: public WriteOnlyPropertyBase<T> {
00103     public:
00104       typedef IdioskoposPointer<WriteOnlyVirtualProperty> pointer;
00105       typedef T value_type;
00106       typedef sigc::slot<void, T> SetSlotType;
00107 
00108       IDIOSKOPOS_OBJECT(WriteOnlyVirtualProperty);
00109 
00113       WriteOnlyVirtualProperty ( const Glib::ustring& name, sigc::slot<void, T> set_slot = SetSlotType() ) :
00114           Object(name),
00115           WriteOnlyPropertyBase<T>(name),
00116           m_set_slot( set_slot ) { }
00117 
00123       WriteOnlyVirtualProperty ( const Glib::ustring& name, Introspectable& container, sigc::slot<void, T> set_slot = SetSlotType() ) :
00124           Object(name, container),
00125           WriteOnlyPropertyBase<T>(name, container),
00126           m_set_slot( set_slot ) { }
00127 
00128       static pointer create( const Glib::ustring& name, sigc::slot<void, T> set_slot = SetSlotType() )
00129       {
00130         return pointer( new WriteOnlyVirtualProperty(name, set_slot) );
00131       }
00132 
00133       static pointer create( const Glib::ustring& name, Introspectable& container, sigc::slot<void, T> set_slot = SetSlotType() )
00134       {
00135         return pointer( new WriteOnlyVirtualProperty(name, container, set_slot) );
00136       }
00137 
00138       virtual ~WriteOnlyVirtualProperty() { }
00139 
00141       virtual void set
00142         ( const T& val ) {
00143         if ( m_set_slot ) {
00144           m_set_slot( val );
00145           this->signal_value_changed().emit();
00146         }
00147       }
00148 
00149       sigc::slot<void, T> set_slot() { return m_set_slot; }
00150 
00151       void set_slot( sigc::slot<void, T> slot ) {
00152         m_set_slot = slot;
00153         this->signal_value_changed().emit();
00154       }
00155 
00156       void set_slot_clear() { m_set_slot = SetSlotType(); }
00157 
00158     protected:
00159       sigc::slot<void, T> m_set_slot;
00160 
00161       virtual T& get_value()
00162       {
00163         return T();
00164       }
00165 
00166   };
00167 
00171   template <typename T>
00172   class VirtualProperty: public PropertyBase<T> {
00173     public:
00174       typedef IdioskoposPointer<VirtualProperty> pointer;
00175       typedef T value_type;
00176       typedef sigc::slot<T> GetSlotType;
00177       typedef sigc::slot<void, T> SetSlotType;
00178 
00179       IDIOSKOPOS_OBJECT(VirtualProperty);
00180 
00184       VirtualProperty (const Glib::ustring& name):
00185         Object(name),
00186         PropertyBase<T>(name)
00187       { }
00188 
00194       VirtualProperty ( const Glib::ustring& name, Introspectable& container ) :
00195         Object(name, container),
00196         PropertyBase<T>(name, container)
00197         { }
00198 
00199       VirtualProperty ( const Glib::ustring& name, sigc::slot<T> get_slot, sigc::slot<void, T> set_slot ) :
00200         Object(name),
00201         PropertyBase<T>(name),
00202         m_get_slot( get_slot ),
00203         m_set_slot( set_slot )
00204       { }
00205 
00211       VirtualProperty ( const Glib::ustring& name,
00212                         Introspectable& container,
00213                         sigc::slot<T> get_slot,
00214                         sigc::slot<void, T> set_slot ) :
00215         Object(name, container),
00216         PropertyBase<T>(name, container),
00217         m_get_slot( get_slot ),
00218         m_set_slot( set_slot )
00219       { }
00220 
00221       static pointer create(const Glib::ustring& name)
00222       {
00223         return pointer( new VirtualProperty<T>(name) );
00224       }
00225 
00226       static pointer create( const Glib::ustring& name, Introspectable& container )
00227       {
00228         return pointer( new VirtualProperty<T>(name, container) );
00229       }
00230 
00231       static pointer create( const Glib::ustring& name, sigc::slot<T> get_slot, sigc::slot<void, T> set_slot )
00232       {
00233         return pointer( new VirtualProperty<T>(name, get_slot, set_slot) );
00234       }
00235 
00236       static pointer create( const Glib::ustring& name, Introspectable& container, sigc::slot<T> get_slot, sigc::slot<void, T> set_slot )
00237       {
00238         return pointer( new VirtualProperty<T>(name, container, get_slot, set_slot) );
00239       }
00240 
00241       virtual ~VirtualProperty() { }
00242 
00244       virtual const T& get() const {
00245         static T t;
00246         if ( m_get_slot ) {
00247           t = m_get_slot();
00248           return t;
00249         }
00250         else
00251             // TODO throw a proper exception class instead of a general throw
00252           throw;
00253       }
00254 
00256       virtual T* get_pointer() {
00257         return 0x00;
00258       }
00259 
00261       virtual void set( const T& val ) {
00262         if ( m_set_slot ) {
00263           m_set_slot( val );
00264           this->signal_value_changed().emit();
00265         }
00266       }
00267 
00268       sigc::slot<T> get_slot() { return m_get_slot; }
00269 
00270       void get_slot( sigc::slot<T> slot ) { m_get_slot = slot; }
00271 
00272       void get_slot_clear() { m_get_slot = GetSlotType(); }
00273 
00274       sigc::slot<void, T> set_slot() { return m_set_slot; }
00275 
00276       void set_slot( sigc::slot<void, T> slot ) {
00277         m_set_slot = slot;
00278         this->signal_value_changed().emit();
00279       }
00280 
00281       void set_slot_clear() { m_set_slot = SetSlotType(); }
00282 
00283     protected:
00284       sigc::slot<T> m_get_slot;
00285       sigc::slot<void, T> m_set_slot;
00286 
00287       virtual T& get_value()
00288       {
00289         static T t;
00290         if ( m_get_slot ) {
00291           t = m_get_slot();
00292           return t;
00293         }
00294         else
00295             // TODO throw a proper exception class instead of a general throw
00296           throw;
00297       }
00298   };
00299 
00300 
00301 // Utility macros
00310 #define INIT_VIRTUAL_PROPERTY( property, id, getter, setter )                          \
00311   property( id, *this, sigc::mem_fun(*this, &getter), sigc::mem_fun(*this, &setter) )
00312 
00322 #define INIT_VIRTUAL_PROPERTY_OVERLOADED( class, type, property, id, getter, setter )   \
00323   property( id, *this, sigc::mem_fun(*this, &getter),                                   \
00324                        sigc::mem_fun(*this, static_cast<void(class::*)(type)>(&setter)) \
00325           )
00326 
00327 }
00328 
00329 
00330 #endif
00331 

Generated on Thu Jan 11 00:26:41 2007 by  doxygen 1.5.1