SField.h

00001 
00002 //    Copyright 2004, SenseGraphics AB
00003 //
00004 //    This file is part of H3D API.
00005 //
00006 //    H3D API is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    H3D API is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with H3D API; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 //
00020 //    A commercial license is also available. Please contact us at 
00021 //    www.sensegraphics.com for more information.
00022 //
00023 //
00024 // \file SField.h Contains the SField template class.
00025 //
00027 #ifndef __SFIELD_H__
00028 #define __SFIELD_H__
00029 
00030 #include "X3DFieldConversion.h"
00031 #include "TypedField.h"
00032 #include "Node.h"
00033 
00034 namespace H3D {
00035 
00038  
00040   class H3DAPI_API SFieldClass {
00041   public:
00042     // Virtual destructor.
00043     virtual ~SFieldClass() {};
00044 
00050     virtual int setValueFromVoidPtr( void *data, unsigned int size, 
00051                                      int id = 0 ) = 0;
00052 
00059     virtual int getValueAsVoidPtr( void *data, unsigned int size, 
00060                                    int id = 0 ) = 0;
00061 
00063     virtual unsigned int valueTypeSize() = 0;
00064   };
00065 
00066 
00072   template< class Type > 
00073   class SField: public TypedField< ParsableField,
00074                                    void,
00075                                    AnyNumber< SField< Type> > >,
00076                 public SFieldClass {
00077   public:   
00079     typedef Type value_type;
00080 
00082     SField() {}
00083         
00085     SField( const Type &_value ) {
00086       value = _value;
00087     }
00088 
00094     inline virtual int setValueFromVoidPtr( void *data, unsigned int len, 
00095                                             int id = 0 ) {
00096       if( len != sizeof( value_type ) )
00097         return -1;
00098       setValue( *( static_cast< Type * >( data ) ), id );
00099       return 0;
00100     }
00101 
00108     inline virtual int getValueAsVoidPtr( void *data, unsigned int len, 
00109                                           int id = 0 ) {
00110       unsigned int size = sizeof( value_type );
00111       if( len < size ) {
00112         return -1;
00113       }
00114       *static_cast< Type * >( data ) = getValue( id );
00115       return size;
00116     } 
00117 
00119     inline virtual unsigned int valueTypeSize() {
00120       return sizeof( value_type );
00121     }
00122 
00124     inline virtual void setValue( const Type &v, int id = 0 );
00126     inline virtual const Type &getValue( int id = 0 );
00127         
00132     inline virtual void setValueFromString( const string &s ) {
00133       this->setValue( X3D::X3DStringToValue< Type >( s ) ); 
00134     }
00135 
00137     inline virtual string getValueAsString( const string& separator = " " ) {
00138       stringstream s;
00139       s << getValue();
00140       return s.str();
00141     }
00142   
00144     virtual string getTypeName() {
00145       return classTypeName();
00146     }
00147 
00149         static string classTypeName() { return typeid( SField< Type > ).name(); }
00150 
00151   protected:
00153     inline virtual void update();
00154 
00156     Type value;
00157 
00158   };
00159 
00160   template< class Type  >
00161   void SField< Type >::update() {
00162 #ifdef DEBUG
00163     Console(1) << "SField< " << typeid( Type ).name() 
00164                << " >(" << this->name << ")::update()" << endl;
00165 #endif
00166     if( this->owner )
00167       this->value = 
00168         static_cast< SField<Type>* >
00169         (this->event.ptr)->getValue( this->owner->id );
00170     else 
00171       this->value = 
00172         static_cast< SField<Type>* >(this->event.ptr)->getValue();
00173   }
00174 
00175   template< class Type >
00176   void SField<Type>::setValue( const Type &v, int id ) {
00177 #ifdef DEBUG
00178     Console(1) << "SField< " << typeid( Type ).name() 
00179                << " >(" << this->name << ")::setValue()" << endl;
00180 #endif
00181     // check that we have the correct access type
00182     this->checkAccessTypeSet( id );
00183     
00184     value = v;
00185     // reset the event pointer since we want to ignore any pending
00186     // events when the field is set to a new value.
00187     this->event.ptr = NULL;
00188     // generate an event.
00189     this->startEvent();
00190   }
00191 
00192   template< class Type >
00193   const Type &SField<Type>::getValue( int id ) {
00194 #ifdef DEBUG
00195     Console(1) << "SField< " << typeid( Type ).name() 
00196                << " >(" <<  this->name << ")::getValue()" << endl;
00197 #endif
00198     // check that we have the correct access type
00199     this->checkAccessTypeGet( id );
00200 
00201     // check that the field is up-to-date first
00202     this->upToDate();
00203     return value;
00204   }
00205 }
00206 #endif

Generated on Thu Aug 24 12:38:34 2006 for H3D API by  doxygen 1.4.5