include/propertized.h

Go to the documentation of this file.
00001 /********************************************************************************
00002  *  Neural Network Framework.                                                   *
00003  *  Copyright (C) 2005-2008 Gianluca Massera <emmegian@yahoo.it>                *
00004  *                                                                              *
00005  *  This program is free software; you can redistribute it and/or modify        *
00006  *  it under the terms of the GNU General Public License as published by        *
00007  *  the Free Software Foundation; either version 2 of the License, or           *
00008  *  (at your option) any later version.                                         *
00009  *                                                                              *
00010  *  This program is distributed in the hope that it will be useful,             *
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
00013  *  GNU General Public License for more details.                                *
00014  *                                                                              *
00015  *  You should have received a copy of the GNU General Public License           *
00016  *  along with this program; if not, write to the Free Software                 *
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
00018  ********************************************************************************/
00019 
00020 #ifndef PROPERTIZED_H
00021 #define PROPERTIZED_H
00022 
00023 #include "types.h"
00024 #include "clonable.h"
00025 #include <map>
00026 #include <string>
00027 
00035 namespace nnfw {
00036 
00037 class OutputFunction;
00038 class Propertized;
00039 class AbstractPropertyAccess;
00040 
00042 typedef VectorData<AbstractPropertyAccess*> PropertyAccessVec;
00043 
00046 class NNFW_API Variant {
00047 public:
00049     typedef enum { t_null=0, t_real, t_int, t_uint, t_char, t_uchar, t_bool, t_string,
00050                 t_realvec, t_realmat, t_outfunction, t_cluster, t_linker, t_propertized,
00051                 t_dataptr } types;
00052 
00056     Variant();
00058     Variant( const Variant& src );
00060     Variant( Real d );
00062     Variant( int d );
00064     Variant( u_int d );
00066     Variant( char d );
00068     Variant( unsigned char d );
00070     Variant( bool d );
00072     Variant( const char* d );
00074     Variant( char* d );
00076     Variant( RealVec* d );
00078     Variant( RealMat* d );
00080     Variant( OutputFunction* d );
00082     Variant( Cluster* d );
00084     Variant( Linker* d );
00086     Variant( Propertized* d );
00088     template<typename T>
00089     Variant( T* d ) {
00090         dtype = t_dataptr;
00091         ddataptr = d;
00092     };
00094 
00097     Variant& operator=( const Variant& src );
00099 
00103     types type() const;
00106     const char* typeName() const;
00109     bool isNull();
00111 
00114     Real getReal() const;
00116     int getInt() const;
00118     u_int getUInt() const;
00120     char getChar() const;
00122     unsigned char getUChar() const;
00124     bool getBool() const;
00126     const char* getString() const;
00128     RealVec* getRealVec() const;
00130     RealMat* getRealMat() const;
00132     OutputFunction* getOutputFunction() const;
00134     Cluster* getCluster() const;
00136     Linker* getLinker() const;
00138     Propertized* getPropertized() const;
00140     template<typename T>
00141     T* getDataPtr() const {
00142         checkType( t_dataptr );
00143         T* r = static_cast<T*>( ddataptr );
00144         if ( r == 0 ) {
00145             nError() << "Called getDataPtr with wrong type; returning NULL pointer ";
00146         }
00147         return r;
00148     };
00150 
00153     static const char* typeName( types t );
00154 
00155 private:
00157     types dtype;
00159     static const char* typen[ t_dataptr+1 ];
00160     Real    dreal;
00161     int     dint;
00162     u_int   duint;
00163     char    dchar;
00164     unsigned char   duchar;
00165     bool            dbool;
00166     char*           dstring;
00167     RealVec*         drealvec;
00168     RealMat*         drealmat;
00169     OutputFunction*  doutfun;
00170     Cluster*         dcluster;
00171     Linker*          dlinker;
00172     Propertized*     dprop;
00173     void*            ddataptr;
00174 
00176     void checkType( types t ) const;
00177 };
00178 
00183 class NNFW_API AbstractPropertyAccess : public Clonable {
00184 public:
00188     AbstractPropertyAccess( const char* name ) {
00189         u_int size = strlen(name);
00190         this->namep = new char[size+1];
00191         strcpy( this->namep, name );
00192     };
00194     virtual ~AbstractPropertyAccess() {
00195         delete []namep;
00196     };
00198 
00201     virtual bool set( const Variant& data ) = 0;
00203     virtual Variant get() const = 0;
00204 
00206     virtual bool set( u_int i, const Variant& data ) = 0;
00208     virtual Variant get( u_int i ) const = 0;
00209 
00211     const char* name() const {
00212         return namep;
00213     };
00215     bool isWritable() const {
00216         return writable;
00217     };
00219     bool isVector() const {
00220         return vectorv;
00221     };
00223     Variant::types type() const {
00224         return typep;
00225     };
00227     virtual Propertized* object() = 0;
00229     virtual AbstractPropertyAccess* clone() const = 0;
00231 
00232 protected:
00234     char* namep;
00236     Variant::types typep;
00238     bool writable;
00240     bool vectorv;
00241 };
00242 
00247 template<class T>
00248 class NNFW_TEMPLATE PropertyAccess : public AbstractPropertyAccess {
00249 public:
00253     PropertyAccess( const char* name, Variant::types t, T* o, Variant (T::*g)(), bool (T::*s)( const Variant& ) = 0 )
00254         : AbstractPropertyAccess( name ) {
00255         typep = t;
00256         obj = o;
00257         vectorv = false;
00258         if ( s == 0 ) {
00259             writable = false;
00260             setPtm = 0;
00261         } else {
00262             writable = true;
00263             setPtm = s;
00264         }
00265         getPtm = g;
00266     };
00268 
00271     virtual Variant get() const {
00272         return (obj->*getPtm)();
00273     };
00278     virtual bool set( const Variant& data ) {
00279         return (obj->*setPtm)( data );
00280     };
00282     virtual bool set( u_int, const Variant& ) {
00283         return false;
00284     };
00286     virtual Variant get( u_int ) const {
00287         return Variant();
00288     };
00289 
00291     virtual Propertized* object() {
00292         return obj;
00293     };
00295     virtual PropertyAccess* clone() const {
00296         return new PropertyAccess( name(), type(), obj, getPtm, setPtm );
00297     };
00299 private:
00300     T* obj;
00301     bool (T::*setPtm)( const Variant& );
00302     Variant (T::*getPtm)();
00303 };
00304 
00309 template<class T>
00310 class NNFW_TEMPLATE VectorPropertyAccess : public AbstractPropertyAccess {
00311 public:
00315     VectorPropertyAccess( const char* name, Variant::types t, T* o, Variant (T::*g)(u_int), bool (T::*s)(u_int, const Variant&) = 0 )
00316         : AbstractPropertyAccess( name ) {
00317         typep = t;
00318         obj = o;
00319         vectorv = true;
00320         if ( s == 0 ) {
00321             writable = false;
00322             setPtm = 0;
00323         } else {
00324             writable = true;
00325             setPtm = s;
00326         }
00327         getPtm = g;
00328     };
00330 
00333     virtual bool set( const Variant& ) {
00334         return false;
00335     };
00337     virtual Variant get() const {
00338         return Variant();
00339     };
00341     virtual Variant get( u_int i ) const {
00342         return (obj->*getPtm)(i);
00343     };
00348     virtual bool set( u_int i, const Variant& data ) {
00349         return (obj->*setPtm)( i, data );
00350     };
00352     virtual Propertized* object() {
00353         return obj;
00354     };
00356     virtual VectorPropertyAccess* clone() const {
00357         return new VectorPropertyAccess( name(), type(), obj, getPtm, setPtm );
00358     };
00360 private:
00361     T* obj;
00362     bool (T::*setPtm)( u_int i, const Variant& );
00363     Variant (T::*getPtm)( u_int i );
00364 };
00365 
00370 class NNFW_API PropertyData {
00371 public:
00373     PropertyData( const Variant& v ) {
00374         data = v; isVector = false; index = -1;
00375     };
00377     PropertyData( const Variant& v, int id ) {
00378         data = v; isVector = true; index = id;
00379     };
00381     Variant data;
00383     bool isVector;
00385     int index;
00386 };
00388 typedef std::map< std::string, Variant > PropertySettings;
00389 
00405 class NNFW_API Propertized : public Clonable {
00406 public:
00410     Propertized();
00412     ~Propertized();
00413 
00415 
00417 
00422     template<class T>
00423     void addProperty( const char* name, Variant::types t, T* obj, Variant (T::*read)(), bool (T::*write)( const Variant& ) = 0 ) {
00424         PropertyAccess<T>* access = new PropertyAccess<T>( name, t, obj, read, write );
00425         props[name] = access;
00426         vecProps.append( access );
00427     };
00428 
00433     template<class T>
00434     void addVectorProperty( const char* name, Variant::types t, T* obj, Variant (T::*read)(u_int i), bool (T::*write)(u_int i, const Variant&) = 0 ) {
00435         VectorPropertyAccess<T>* access = new VectorPropertyAccess<T>( name, t, obj, read, write );
00436         props[name] = access;
00437         vecProps.append( access );
00438     };
00439 
00442     Variant property( const char* name ) {
00443         return props[name]->get();
00444     };
00445 
00448     bool setProperty( const char* name, const Variant& data ) {
00449         if ( props.count( name ) == 0 ) return false;
00450         AbstractPropertyAccess& p = *(props[name]);
00451         return ( p.isWritable() ? p.set(data) : false );
00452     };
00453 
00456     bool setVectorProperty( const char* name, u_int i, const Variant& data ) {
00457         if ( props.count( name ) == 0 ) return false;
00458         AbstractPropertyAccess& p = *(props[name]);
00459         return ( p.isWritable() ? p.set(i, data) : false );
00460     };
00461 
00465     void setProperties( PropertySettings& prop );
00466 
00468     PropertyAccessVec& properties() const {
00469         return (PropertyAccessVec&)vecProps;
00470     };
00471 
00473     void propertySettings( PropertySettings& prop ) const {
00474         for( int i=0; i<(int)vecProps.size(); i++ ) {
00475             if ( vecProps[i]->isVector() ) {
00476                 nWarning() << "propertySettings doesn't handle Vector-Property yet";
00477                 continue;
00478             }
00479             prop[ vecProps[i]->name() ] = vecProps[i]->get();
00480         }
00481     };
00482 
00485     AbstractPropertyAccess* propertySearch( const char* name ) const {
00486         if ( props.count( name ) > 0 ) {
00487             return ((std::map< std::string, AbstractPropertyAccess* >) props)[name];
00488         }
00489         return 0;
00490     };
00491 
00494     Variant getTypename() const {
00495         return Variant( vtypename );
00496     };
00497 
00500     Variant getTypename() {
00501         return Variant( vtypename );
00502     };
00503 
00507     Variant convertStringTo( const Variant& str, Variant::types t );
00508 
00510     virtual Propertized* clone() const;
00511 
00513 
00514 protected:
00518     void setTypename( const char* type );
00519 
00520 private:
00522     std::map< std::string, AbstractPropertyAccess* > props;
00524     PropertyAccessVec vecProps;
00526     char* vtypename;
00527 };
00528 
00529 }
00530 
00531 #endif
BerliOS Developer Logo Valid XHTML 1.0 Transitional Valid CSS!