writeonlypropertybase.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 IDIOSKOPOSWRITEONLYPROPERTYBASE_H
00021 #define IDIOSKOPOSWRITEONLYPROPERTYBASE_H
00022 
00023 #include <idioskopos/object.h>
00024 #include <idioskopos/utility_stream.h>
00025 
00026 namespace Idioskopos
00027 {
00028 
00035   template <typename T>
00036   class WriteOnlyPropertyBase: public virtual Object
00037   {
00038     public:
00039       typedef IdioskoposPointer<WriteOnlyPropertyBase> pointer;
00040       typedef T value_type;
00041 
00042       IDIOSKOPOS_OBJECT( WriteOnlyPropertyBase );
00043 
00045       WriteOnlyPropertyBase ( const Glib::ustring& name,
00046                               sigc::slot<void> slot=sigc::slot<void>() ):
00047           Object( name, slot )
00048       { }
00049 
00054       WriteOnlyPropertyBase( const Glib::ustring& name,
00055                              Introspectable& container,
00056                              sigc::slot<void> slot=sigc::slot<void>() ):
00057           Object( name, container, slot )
00058       { }
00059 
00061       virtual ~WriteOnlyPropertyBase()
00062       { }
00063 
00064       virtual void set_value( const Glib::ustring& v )
00065       {
00066         T t;
00067         Idioskopos::set_value( v, t );
00068         this->set
00069         ( t );
00070       }
00071 
00073       WriteOnlyPropertyBase<T>& operator= ( const T& val )
00074       {
00075         this->set( val );
00076         return *this;
00077       }
00078 
00080       virtual void set( const T& val ) = 0;
00081 
00090       virtual Glib::ustring xml( int indent = -1 )
00091       {
00092         Glib::ustring s;
00093         for ( int i = 0; i < indent; i++ )
00094           s += "  ";
00095         s += "<property type=\"" + this->class_name() + "\" name=\"" + this->name() + "\">";
00096         if ( indent >= 0 )
00097           s += "\n";
00098         s += get_xml( this->get_value(), ( ( indent >= 0 ) ? indent + 1 : indent ) );
00099         if ( indent >= 0 )
00100           s += "\n";
00101         for ( int i = 0; i < indent; i++ )
00102           s += "  ";
00103         s += "</property>";
00104         if ( indent >= 0 )
00105           s += "\n";
00106         return s;
00107       }
00108 
00109       virtual void set_xml( const Glib::ustring& s, bool change_names = false, bool create_missing = true )
00110       {
00111         Object::set_xml( s, change_names, create_missing );
00112       }
00113 
00114       virtual void set_xml( const xmlpp::Node* n, bool change_names = false, bool create_missing = true )
00115       {
00116         xmlpp::Attribute * attribute;
00117         xmlpp::NodeSet values;
00118         const xmlpp::Element *elem, *elem2;
00119         const xmlpp::TextNode* text;
00120 
00121         elem = dynamic_cast<const xmlpp::Element*>( n );
00122         if ( !elem )
00123           return ;
00124 
00125         if ( change_names ) {
00126           attribute = elem->get_attribute( "name" );
00127           if ( attribute )
00128             this->set_name( attribute->get_value() );
00129         }
00130 
00131         values = elem->find( "value" );
00132 
00133         elem2 = dynamic_cast<const xmlpp::Element*>( values.front() );
00134         if ( !elem2 )
00135           return ;
00136         text = dynamic_cast<const xmlpp::TextNode*>( elem2->get_child_text() );
00137         if ( !text )
00138           return ;
00139         this->set_value( text->get_content() );
00140 
00141         return ;
00142       }
00143 
00147       const std::type_info& type() const
00148       {
00149         return typeid( T );
00150       }
00151 
00152     protected:
00153 
00154       virtual T& get_value() = 0;
00155 
00156 
00157   };
00158 
00162   template <typename X, typename A>
00163   class WriteOnlyPropertyBase<std::vector<X,A> >: public virtual Object
00164   {
00165     public:
00166       typedef IdioskoposPointer<WriteOnlyPropertyBase> pointer;
00167       typedef std::vector<X,A> value_type;
00168 
00169       IDIOSKOPOS_OBJECT( WriteOnlyPropertyBase );
00170 
00172       WriteOnlyPropertyBase ( const Glib::ustring& name,
00173                               sigc::slot<void> slot=sigc::slot<void>() ):
00174           Object( name, slot )
00175       { }
00176 
00181       WriteOnlyPropertyBase( const Glib::ustring& name,
00182                              Introspectable& container,
00183                              sigc::slot<void> slot=sigc::slot<void>() ):
00184           Object( name, container, slot )
00185       { }
00186 
00188       virtual ~WriteOnlyPropertyBase()
00189       { }
00190 
00191       virtual void set_value( const Glib::ustring& v )
00192       {
00193         std::vector<X,A> t;
00194         Idioskopos::set_value( v, t );
00195         this->set
00196         ( t );
00197       }
00198 
00200       WriteOnlyPropertyBase<std::vector<X,A> >& operator= ( const std::vector<X,A> & val )
00201       {
00202         this->set( val );
00203         return *this;
00204       }
00205 
00207       virtual void set( const std::vector<X,A> & val ) = 0;
00208 
00209       virtual void set_element( int n, const X& x ) {
00210         if ( n < 0 ) throw;
00211         this->get_value()[n] = x;
00212         m_signal_value_changed.emit();
00213       }
00214 
00215       virtual void set_element( unsigned int n, const X& x ) {
00216         this->get_value()[n] = x;
00217         m_signal_value_changed.emit();
00218       }
00219 
00220       virtual void set_element( long int n, const X& x ) {
00221         if ( n < 0 ) throw;
00222         this->get_value()[n] = x;
00223         m_signal_value_changed.emit();
00224       }
00225 
00226       virtual void set_element( long unsigned int n, const X& x ) {
00227         this->get_value()[n] = x;
00228         m_signal_value_changed.emit();
00229       }
00230 
00239       virtual Glib::ustring xml( int indent = -1 )
00240       {
00241         Glib::ustring s;
00242         for ( int i = 0; i < indent; i++ )
00243           s += "  ";
00244         s += "<property type=\"" + this->class_name() + "\" name=\"" + this->name() + "\">";
00245         if ( indent >= 0 )
00246           s += "\n";
00247         s += get_xml( this->get_value(), ( ( indent >= 0 ) ? indent + 1 : indent ) );
00248         if ( indent >= 0 )
00249           s += "\n";
00250         for ( int i = 0; i < indent; i++ )
00251           s += "  ";
00252         s += "</property>";
00253         if ( indent >= 0 )
00254           s += "\n";
00255         return s;
00256       }
00257 
00258       virtual void set_xml( const Glib::ustring& s, bool change_names = false, bool create_missing = true )
00259       {
00260         Object::set_xml( s, change_names, create_missing );
00261       }
00262 
00263       virtual void set_xml( const xmlpp::Node* n, bool change_names = false, bool create_missing = true )
00264       {
00265         xmlpp::Attribute * attribute;
00266         xmlpp::NodeSet values;
00267         const xmlpp::Element *elem, *elem2;
00268         const xmlpp::TextNode* text;
00269         X x;
00270 
00271         elem = dynamic_cast<const xmlpp::Element*>( n );
00272         if ( !elem )
00273           return ;
00274 
00275         if ( change_names ) {
00276           attribute = elem->get_attribute( "name" );
00277           if ( attribute )
00278             this->set_name( attribute->get_value() );
00279         }
00280 
00281         values = elem->find( "vector" );
00282         if ( values.size() == 0 )
00283           values = elem->find( "array" );
00284         if ( values.size() == 0 )
00285           return ;
00286 
00287         elem = dynamic_cast<const xmlpp::Element*>( values.front() );
00288         if ( !elem )
00289           return ;
00290 
00291         values = elem->find( "value" );
00292 
00293         xmlpp::NodeSet::iterator iter;
00294         size_t i;
00295 
00296         for ( i = 0, iter = values.begin(); iter != values.end(); i++, iter++ ) {
00297           elem2 = dynamic_cast<const xmlpp::Element*>( *iter );
00298           if ( !elem2 )
00299             return ;
00300           text = dynamic_cast<const xmlpp::TextNode*>( elem2->get_child_text() );
00301           if ( !text )
00302             return ;
00303           Idioskopos::set_value( text->get_content(), x );
00304           if ( i < get_value().size() )
00305             get_value() [ i ] = x;
00306           else
00307             get_value().push_back( x );
00308         }
00309 
00310         return ;
00311       }
00312 
00316       const std::type_info& type() const
00317       {
00318         return typeid( std::vector<X,A> );
00319       }
00320 
00321     protected:
00322 
00323       virtual std::vector<X,A> & get_value() = 0;
00324 
00325 
00326   };
00327 
00331   template <typename X, size_t N>
00332   class WriteOnlyPropertyBase<IdioskoposArray<X,N> >: public virtual Object
00333   {
00334     public:
00335       typedef IdioskoposPointer<WriteOnlyPropertyBase> pointer;
00336       typedef IdioskoposArray<X,N> value_type;
00337 
00338       IDIOSKOPOS_OBJECT( WriteOnlyPropertyBase );
00339 
00341       WriteOnlyPropertyBase ( const Glib::ustring& name,
00342                               sigc::slot<void> slot=sigc::slot<void>() ):
00343           Object( name, slot )
00344       { }
00345 
00350       WriteOnlyPropertyBase( const Glib::ustring& name,
00351                              Introspectable& container,
00352                              sigc::slot<void> slot=sigc::slot<void>() ):
00353           Object( name, container, slot )
00354       { }
00355 
00357       virtual ~WriteOnlyPropertyBase()
00358       { }
00359 
00360       virtual void set_value( const Glib::ustring& v )
00361       {
00362         IdioskoposArray<X,N> t;
00363         Idioskopos::set_value( v, t );
00364         this->set
00365         ( t );
00366       }
00367 
00369       WriteOnlyPropertyBase<IdioskoposArray<X,N> >& operator= ( const IdioskoposArray<X,N> & val )
00370       {
00371         this->set( val );
00372         return *this;
00373       }
00374 
00376       virtual void set( const IdioskoposArray<X,N> & val ) = 0;
00377 
00378       virtual void set_element( int n, const X& x ) {
00379         if ( n < 0 ) throw;
00380         this->get_value()[n] = x;
00381         m_signal_value_changed.emit();
00382       }
00383 
00384       virtual void set_element( unsigned int n, const X& x ) {
00385         this->get_value()[n] = x;
00386         m_signal_value_changed.emit();
00387       }
00388 
00389       virtual void set_element( long int n, const X& x ) {
00390         if ( n < 0 ) throw;
00391         this->get_value()[n] = x;
00392         m_signal_value_changed.emit();
00393       }
00394 
00395       virtual void set_element( long unsigned int n, const X& x ) {
00396         this->get_value()[n] = x;
00397         m_signal_value_changed.emit();
00398       }
00399 
00408       virtual Glib::ustring xml( int indent = -1 )
00409       {
00410         Glib::ustring s;
00411         for ( int i = 0; i < indent; i++ )
00412           s += "  ";
00413         s += "<property type=\"" + this->class_name() + "\" name=\"" + this->name() + "\">";
00414         if ( indent >= 0 )
00415           s += "\n";
00416         s += get_xml( this->get_value(), ( ( indent >= 0 ) ? indent + 1 : indent ) );
00417         if ( indent >= 0 )
00418           s += "\n";
00419         for ( int i = 0; i < indent; i++ )
00420           s += "  ";
00421         s += "</property>";
00422         if ( indent >= 0 )
00423           s += "\n";
00424         return s;
00425       }
00426 
00427       virtual void set_xml( const Glib::ustring& s, bool change_names = false, bool create_missing = true )
00428       {
00429         Object::set_xml( s, change_names, create_missing );
00430       }
00431 
00432       virtual void set_xml( const xmlpp::Node* n, bool change_names = false, bool create_missing = true )
00433       {
00434         xmlpp::Attribute * attribute;
00435         xmlpp::NodeSet values;
00436         const xmlpp::Element *elem, *elem2;
00437         const xmlpp::TextNode* text;
00438 
00439         elem = dynamic_cast<const xmlpp::Element*>( n );
00440         if ( !elem )
00441           return ;
00442 
00443         if ( change_names ) {
00444           attribute = elem->get_attribute( "name" );
00445           if ( attribute )
00446             this->set_name( attribute->get_value() );
00447         }
00448 
00449         values = elem->find( "array" );
00450         if ( values.size() == 0 )
00451           return ;
00452         elem = dynamic_cast<const xmlpp::Element*>( values.front() );
00453         if ( !elem )
00454           return ;
00455 
00456         values = elem->find( "value" );
00457 
00458         xmlpp::NodeSet::iterator iter;
00459         size_t i;
00460 
00461         for ( i = 0, iter = values.begin(); i < N && iter != values.end(); i++, iter++ ) {
00462           elem2 = dynamic_cast<const xmlpp::Element*>( *iter );
00463           if ( !elem2 )
00464             return ;
00465           text = dynamic_cast<const xmlpp::TextNode*>( elem2->get_child_text() );
00466           if ( !text )
00467             return ;
00468           Idioskopos::set_value( text->get_content(), get_value()[i] );
00469         }
00470 
00471         return ;
00472       }
00473 
00477       const std::type_info& type() const
00478       {
00479         return typeid( IdioskoposArray<X,N> );
00480       }
00481 
00482     protected:
00483 
00484       virtual IdioskoposArray<X,N> & get_value() = 0;
00485 
00486 
00487   };
00488 
00489 }
00490 
00491 #endif
00492 

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