00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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
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
00182 this->checkAccessTypeSet( id );
00183
00184 value = v;
00185
00186
00187 this->event.ptr = NULL;
00188
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
00199 this->checkAccessTypeGet( id );
00200
00201
00202 this->upToDate();
00203 return value;
00204 }
00205 }
00206 #endif