H3D API  2.4.1
Field.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 __FIELD_H__
29 #define __FIELD_H__
30 
31 #include <iostream>
32 #include <string>
33 #include <set>
34 #include <vector>
35 #include <H3D/H3DApi.h>
36 #include <H3D/H3DTypes.h>
37 #include <H3DUtil/TimeStamp.h>
38 #include <H3D/X3DTypes.h>
39 #include <memory>
40 
41 namespace H3D {
42  // forward declaration
43  class Node;
44 
46  class H3DAPI_API Field {
47  public:
51 
53  typedef enum {
70  } AccessType;
71 
75  struct H3DAPI_API Event {
76 
78  Event( Field *_ptr, const TimeStamp &_time_stamp ):
79  ptr( _ptr ),
80  time_stamp( _time_stamp ) {
81  }
82 
85 
88 
92  H3DTime event_time = TimeStamp::now ();
93  if( event_time <= last_event_time ) {
94 #ifdef DEBUG
95  Console(LogLevel::Debug) << "Field: New event has same timestamp as last generated event, incrementing timestamp" << std::endl;
96 #endif
97  // Casting a pointer to last_event_time to a long long (which is at least 64bit) so we can increment it, thus incrementing the mantissa of last_event_time and getting us the smallest possible higher value
98  unsigned long long* temp = reinterpret_cast<unsigned long long*>(&last_event_time);
99  ++(*temp);
100  event_time = last_event_time;
101  }
102  last_event_time = event_time;
103  return event_time;
104  }
105 
106  protected:
107 
110  };
111 
113  Field();
114 
116  virtual ~Field();
117 
122  virtual void checkFieldType( Field *f, int index );
123 
125  void setName( string _name ) { name = _name; }
126 
128  string getName();
129 
132  string getFullName();
133 
135  virtual string getTypeName() { return classTypeName(); }
136 
138  virtual X3DTypes::X3DType getX3DType() { return X3DTypes::UNKNOWN_X3D_TYPE; }
139 
142  static string classTypeName() { return "Field"; }
143 
146  virtual void route( Field*, int id = 0 );
147 
150  virtual void routeNoEvent( Field*, int id = 0 );
151 
153  virtual void unroute( Field* );
154 
157  virtual Field *replaceRoute( Field* f, unsigned int i, int id = 0 );
158 
161  virtual Field *replaceRouteNoEvent( Field* f, unsigned int i, int id = 0 );
162 
165  template< class F >
166  inline void route( H3DUniquePtr< F > &f, int id = 0 ) {
167  route( f.get(), id ); }
168 
171  template< class F >
172  inline void routeNoEvent( H3DUniquePtr< F > &f, int id = 0 ) {
173  routeNoEvent( f.get(), id );
174  }
175 
178  template< class F >
179  void unroute( H3DUniquePtr< F > &f ) { unroute( f.get() ); }
180 
182  inline void unrouteAll() {
183  while( routes_out.begin() != routes_out.end() ) {
184  unroute( *routes_out.begin() );
185  }
186  }
187 
190  virtual void upToDate();
191 
194  inline bool isUpToDate() {
195  return event.ptr == NULL;
196  }
197 
199  virtual void touch();
200 
202  inline Node *getOwner() {
203  return owner;
204  }
205 
207  void setOwner( Node *n );
208 
210  inline void setAccessType( AccessType _access_type ) {
211  access_type = _access_type;
212  }
213 
216  return access_type;
217  }
218 
219  typedef vector< Field * > FieldSet;
220  typedef vector< Field * > FieldVector;
221 
224  inline bool routesTo( Field *f ) {
225  for( FieldSet::const_iterator i = routes_out.begin();
226  i != routes_out.end(); ++i ) {
227  if( (*i) == f ) return true;
228  }
229  return false;
230  }
231 
234  inline bool hasRouteFrom( Field *f ) {
235  for( FieldVector::const_iterator i = routes_in.begin();
236  i != routes_in.end(); ++i ) {
237  if( (*i) == f ) return true;
238  }
239  return false;
240  }
241 
243  inline const FieldVector &getRoutesIn() {
244  return routes_in;
245  }
246 
248  inline const FieldSet &getRoutesOut() {
249  return routes_out;
250  }
251 
253  inline const Event &getLatestEvent() {
254  return event;
255  }
256 
259  inline void setAccessCheck( bool on ) {
260  access_check_on = on;
261  }
262 
264  inline bool isAccessCheckOn() {
265  return access_check_on;
266  }
267 
268  protected:
272  virtual void startEvent();
273 
275  virtual void propagateEvent( Event e );
276 
278  virtual void routeFrom( Field*, int id );
279 
281  virtual void unrouteFrom( Field* );
282 
284  virtual Field *replaceRouteFrom( Field* f, unsigned int i, int id );
285 
288  virtual void update() {}
289 
293  void checkAccessTypeRoute( Field *f, int id );
294 
298  void checkAccessTypeRouteFrom( Field *f, int id );
299 
302  void checkAccessTypeGet( int id );
303 
306  void checkAccessTypeSet( int id );
307 
309  int ownerId();
310 
312  string name;
313 
317 
321 
323  FieldSet routes_out;
325  FieldVector routes_in;
336 
337  friend class Scene;
338 
342  return is_program_setting;
343  }
344 
347  void markProgramSetting( bool used_as_setting ) {
348  is_program_setting = used_as_setting;
349  }
350 
351  bool is_program_setting;
352  };
353 
356  class H3DAPI_API ParsableField: public Field {
357  public:
361  virtual void setValueFromString( const string &s ) = 0;
364  inline virtual string getValueAsString( const string& /*separator*/ = " " ) {
365  return "";
366  }
367  };
368 
371  class H3DAPI_API ParsableMField: public ParsableField {
372  public:
376  virtual size_t getSize( ) = 0;
378  inline virtual string getElementAsString( size_t /*element*/ ) {
379  return "";
380  }
382  inline virtual void addElementFromString( const string &s ) = 0;
383  };
384 
385 
386 }
387 
388 #endif
389 
Base header file that handles all configuration related settings.
Include this file to include the H3D specific types.
Header file containing all X3D types enumerated.
Exception thrown when performing actions that conflict with the field's access type.
Definition: Field.h:50
The Field class.
Definition: Field.h:46
void markProgramSetting(bool used_as_setting)
Set it this field is used by program settings fields in scene or not.
Definition: Field.h:347
bool isProgramSetting()
Returns true if this field is part of program settings fields.
Definition: Field.h:341
void unrouteAll()
Remove all the routes from this field.
Definition: Field.h:182
const FieldSet & getRoutesOut()
Get the Fields this Field is routed to.
Definition: Field.h:248
virtual X3DTypes::X3DType getX3DType()
Returns the X3DType of the field.
Definition: Field.h:138
Node * owner
The node that contain this field.
Definition: Field.h:330
void setName(string _name)
Sets local the name of the field. Will be used in error reporting.
Definition: Field.h:125
const Event & getLatestEvent()
Get the latest event.
Definition: Field.h:253
bool isUpToDate()
Returns true if the field is up to date, i.e.
Definition: Field.h:194
bool access_check_on
If true, access type checks will be done when getting/setting values and setting up routes.
Definition: Field.h:335
AccessType
The different access types that a field can have.
Definition: Field.h:53
@ INPUT_ONLY
The value of the field can only set, not read.
Definition: Field.h:67
@ INITIALIZE_ONLY
The value of the field can only be changed before the Node that is the owner of the field has been in...
Definition: Field.h:57
@ OUTPUT_ONLY
The value of the field can only read, not set.
Definition: Field.h:62
void setAccessCheck(bool on)
Set access type checking on or off.
Definition: Field.h:259
Event event
The last event that happened.
Definition: Field.h:327
void setAccessType(AccessType _access_type)
Set the access type of the field.
Definition: Field.h:210
bool hasRouteFrom(Field *f)
Returns true if the field given as argument is routed to this field.
Definition: Field.h:234
bool event_lock
If true, stops events from being propagated.
Definition: Field.h:316
static string classTypeName()
Returns a string name for this field type.
Definition: Field.h:142
FieldVector routes_in
The Field that are routed to this field.
Definition: Field.h:325
bool isAccessCheckOn()
Returns true if checking of access types is on.
Definition: Field.h:264
void routeNoEvent(H3DUniquePtr< F > &f, int id=0)
Set up a route from this field to the field residing in the H3DUniquePtr given as argument.
Definition: Field.h:172
bool routesTo(Field *f)
Returns true if this field is routed to the field given as argument.
Definition: Field.h:224
bool update_lock
If true, stops the update() function from being called in the upToDate() function.
Definition: Field.h:320
void unroute(H3DUniquePtr< F > &f)
Remove the route from this field to the field residing in the H3DUniquePtr given as argument.
Definition: Field.h:179
void route(H3DUniquePtr< F > &f, int id=0)
Set up a route from this field to the field residing in the H3DUniquePtr given as argument.
Definition: Field.h:166
AccessType access_type
The type of access to the field.
Definition: Field.h:332
AccessType getAccessType()
Get the access type of the field.
Definition: Field.h:215
const FieldVector & getRoutesIn()
Get the Fields that are routed to this Field.
Definition: Field.h:243
virtual void update()
This function will be called to update the field if it has a pending event when the upToDate() functi...
Definition: Field.h:288
virtual string getTypeName()
Returns a string name for this field type.
Definition: Field.h:135
FieldSet routes_out
The Fields that this field is routed to.
Definition: Field.h:323
Node * getOwner()
Get the Node that contains this field.
Definition: Field.h:202
string name
The name of the field.
Definition: Field.h:312
Node is the base class for all classes that can be part of the H3D scene-graph.
Definition: Node.h:46
This is a field which value can be set by a string from the X3D parser.
Definition: Field.h:356
virtual void setValueFromString(const string &s)=0
Set the value of the field given a string.
virtual string getValueAsString(const string &=" ")
Get the value of the field as a string.
Definition: Field.h:364
This is a field which value can be set by a string from the X3D parser.
Definition: Field.h:371
virtual void addElementFromString(const string &s)=0
Add a new element to an MField from a string value.
virtual string getElementAsString(size_t)
Get the value of an element of the field as a string.
Definition: Field.h:378
virtual size_t getSize()=0
Set the value of the field given a string.
The Scene node is topmost node that takes care of the rendering of the scene graph both haptically an...
Definition: Scene.h:56
H3D_API_EXCEPTION(Vec2dNormalizeError)
double H3DTime
H3D API namespace.
Definition: Anchor.h:38
FieldDBTemplate< N, F > * INPUT_OUTPUT(H3DNodeDatabase *_container, const std::string &_name, H3DUniquePtr< F > N::*_ptr)
Specialization to handle case where access type is INPUT_OUTPUT.
Definition: H3DNodeDatabase.h:178
static TimeStamp now()
The Event struct encapsulates an event in the field network.
Definition: Field.h:75
static H3DTime generateEventTime()
Provides a value which is guaranteed to be unique (unlike TimeStamp()) and always increasing.
Definition: Field.h:91
Field * ptr
The Field that caused the event.
Definition: Field.h:84
TimeStamp time_stamp
The time of the event.
Definition: Field.h:87
static H3DTime last_event_time
Last used globally unique time.
Definition: Field.h:109
Event(Field *_ptr, const TimeStamp &_time_stamp)
Constructor.
Definition: Field.h:78