H3D API  2.4.1
SField.h
Go to the documentation of this file.
1 // Copyright 2004-2019, SenseGraphics AB
3 //
4 // This file is part of H3D API.
5 //
6 // H3D API is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // H3D API is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with H3D API; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 //
20 // A commercial license is also available. Please contact us at
21 // www.sensegraphics.com for more information.
22 //
23 //
26 //
28 #ifndef __SFIELD_H__
29 #define __SFIELD_H__
30 
31 #include <H3D/X3DFieldConversion.h>
32 #include <H3D/TypedField.h>
33 #include <H3D/Node.h>
34 
35 namespace H3D {
36 
39 
41  class H3DAPI_API SFieldClass {
42  public:
44  virtual ~SFieldClass() {};
45 
48  DEPRECATED( "int setValueFromVoidPtr( const void *data, unsigned int size, int id = 0 )", )
50  virtual int setValueFromVoidPtr( void *data, unsigned int size,
51  int id = 0 ) {
52  return setValueFromVoidPtr( (const void*)data, size, id );
53  }
54 
63  virtual int getValueAsVoidPtr( void *data, unsigned int size,
64  int id = 0 ) = 0;
65 
67  virtual unsigned int valueTypeSize() = 0;
68 
76  virtual int setValueFromVoidPtr( const void *data, unsigned int size,
77  int id = 0 ) = 0;
78  };
79 
80 
86  template< class Type >
87  class SField: public TypedField< ParsableField,
88  void,
89  AnyNumber< SField< Type> > >,
90  public SFieldClass {
91  public:
93  typedef Type value_type;
94 
96  SField() {}
97 
99  SField( const Type &_value ) {
100  value = _value;
101  }
102 
110  inline virtual int setValueFromVoidPtr( const void *data, unsigned int len,
111  int id = 0 ) {
112  if( len != sizeof( value_type ) )
113  return -1;
114  setValue( *( static_cast< const Type * >( data ) ), id );
115  return 0;
116  }
117 
126  inline virtual int getValueAsVoidPtr( void *data, unsigned int len,
127  int id = 0 ) {
128  unsigned int size = sizeof( value_type );
129  if( len < size ) {
130  return -1;
131  }
132  *static_cast< Type * >( data ) = getValue( id );
133  return size;
134  }
135 
137  inline virtual unsigned int valueTypeSize() {
138  return sizeof( value_type );
139  }
140 
142  inline virtual void setValue( const Type &v, int id = 0 );
143 
144  inline void setValueIfChanged( const Type &v, int id = 0 );
146  inline virtual const Type &getValue( int id = 0 );
147 
152  inline virtual void setValueFromString( const string &s ) {
153  this->setValue( X3D::X3DStringToValue< Type >( s ) );
154  }
155 
157  inline virtual string getValueAsString( const string& /*separator*/ = " " ) {
158  stringstream s;
159  s << getValue();
160  return s.str();
161  }
162 
164  virtual string getTypeName() {
165  return classTypeName();
166  }
167 
169  static string classTypeName() { return typeid( SField< Type > ).name(); }
170 
171  protected:
173  inline virtual void update();
174 
176  Type value;
177 
178  };
179 
180  template< class Type >
182 #ifdef DEBUG
183  Console(LogLevel::Debug) << "SField< " << typeid( Type ).name()
184  << " >(" << this->name << ")::update()" << endl;
185 #endif
186  if( this->owner )
187  this->value =
188  static_cast< SField<Type>* >
189  (this->event.ptr)->getValue( this->owner->id );
190  else
191  this->value =
192  static_cast< SField<Type>* >(this->event.ptr)->getValue();
193  }
194 
195  template< class Type >
196  void SField<Type>::setValueIfChanged( const Type &v, int id ) {
197 #ifdef DEBUG
198  Console(1) << "SField< " << typeid( Type ).name()
199  << " >(" << this->name << ")::setValue()" << endl;
200 #endif
201  // check that we have the correct access type
202  this->checkAccessTypeSet( id );
203 
204  // reset the event pointer since we want to ignore any pending
205  // events when the field is set to a new value.
206  this->event.ptr = NULL;
207  if( value != v ) {
208  value = v;
209  // generate an event.
210  this->startEvent();
211  }
212  }
213 
214  template< class Type >
215  void SField<Type>::setValue( const Type &v, int id ) {
216 #ifdef DEBUG
217  Console(LogLevel::Debug) << "SField< " << typeid( Type ).name()
218  << " >(" << this->name << ")::setValue()" << endl;
219 #endif
220  // check that we have the correct access type
221  this->checkAccessTypeSet( id );
222 
223  value = v;
224 
225 
226  // reset the event pointer since we want to ignore any pending
227  // events when the field is set to a new value.
228  this->event.ptr = NULL;
229 
230  // generate an event.
231  this->startEvent();
232  }
233 
234  template< class Type >
235  const Type &SField<Type>::getValue( int id ) {
236 #ifdef DEBUG
237  Console(LogLevel::Debug) << "SField< " << typeid( Type ).name()
238  << " >(" << this->name << ")::getValue()" << endl;
239 #endif
240  // check that we have the correct access type
241  this->checkAccessTypeGet( id );
242 
243  // check that the field is up-to-date first
244  this->upToDate();
245  return value;
246  }
247 }
248 #endif
Node base class.
Contains the TypedField template class.
This file contains functions for convertion from a string to a value of an X3D field type.
string name
The name of the field.
Definition: Field.h:312
Base class for all fields created with the SField template.
Definition: SField.h:41
virtual unsigned int valueTypeSize()=0
Returns the size in bytes of the value type the sfield encapsulates.
virtual ~SFieldClass()
Virtual destructor.
Definition: SField.h:44
virtual int getValueAsVoidPtr(void *data, unsigned int size, int id=0)=0
Get the value of the data copied into a memory buffer.
virtual int setValueFromVoidPtr(const void *data, unsigned int size, int id=0)=0
Set the value of the field given a pointer to where the value of the field is.
Template class that adds the Field mechanisms to an encapsulated value of specified type.
Definition: SField.h:90
virtual void setValue(const Type &v, int id=0)
Set the value of the field.
Definition: SField.h:215
virtual void update()
Make the field up to date given that an event has occured.
Definition: SField.h:181
Type value_type
The type of the value member.
Definition: SField.h:93
virtual const Type & getValue(int id=0)
Get the value of the field.
Definition: SField.h:235
virtual unsigned int valueTypeSize()
Returns the size in bytes of the value type the sfield encapsulates.
Definition: SField.h:137
virtual string getTypeName()
Returns a string name for this field type e.g. SFInt32.
Definition: SField.h:164
virtual void setValueFromString(const string &s)
Set the value of the field given a string.
Definition: SField.h:152
virtual int setValueFromVoidPtr(const void *data, unsigned int len, int id=0)
Set the value of the field given a pointer to where the value of the field is.
Definition: SField.h:110
SField()
Constructor.
Definition: SField.h:96
SField(const Type &_value)
Constructor.
Definition: SField.h:99
virtual string getValueAsString(const string &=" ")
Get the value of the field as a string.
Definition: SField.h:157
Type value
The encapsulated value.
Definition: SField.h:176
virtual int getValueAsVoidPtr(void *data, unsigned int len, int id=0)
Get the value of the data copied into a memory buffer.
Definition: SField.h:126
static string classTypeName()
Returns a string name for this field type e.g. SFInt32.
Definition: SField.h:169
A template modifier class for adding type checking on the routes to any Field class.
Definition: TypedField.h:84
Type getValue(const char *s, const char *&rest)
Function that reads characters from a char * and converts them to a given type.
Definition: X3DFieldConversion.h:134
H3D API namespace.
Definition: Anchor.h:38