H3D API  2.4.1
FieldTemplates.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 __FIELDTEMPLATES_H__
29 #define __FIELDTEMPLATES_H__
30 
31 #include <string>
32 #include <set>
33 #include <vector>
34 
35 #include <H3D/SField.h>
36 #include <H3D/MField.h>
37 #include <H3D/TypedField.h>
38 
39 namespace H3D {
40 
41  H3D_VALUE_EXCEPTION( string, InvalidNodeType );
42 
48  template < class SF >
49  class OnValueChangeSField: public SF {
50  public:
51  virtual void setValue( const typename SF::value_type &v, int id = 0 ) {
52  typename SF::value_type old_value = this->value;
53  SF::setValue( v, id );
54  if( this->value != old_value ) {
55  onValueChange( this->value );
56  }
57  }
58 
59  protected:
61  virtual void onValueChange( const typename SF::value_type &new_value ) = 0;
62 
63  virtual void update() {
64  typename SF::value_type old_value = this->value;
65  SF::update();
66  if( this->value != old_value ) {
67  onValueChange( this->value );
68  }
69  }
70  };
71 
78  template < class SF >
79  class OnNewValueSField: public SF {
80  public:
81  virtual void setValue( const typename SF::value_type &v, int id = 0 ) {
82  SF::setValue( v, id );
83  onNewValue( this->value );
84  }
85 
86  protected:
88  virtual void onNewValue( const typename SF::value_type &new_value ) = 0;
89 
90  virtual void update() {
91  SF::update();
92  onNewValue( this->value );
93  }
94  };
95 
101  template< class RefCountedType, class BaseRefCountType >
102  class TypedRefCountField: public BaseRefCountType {
103  protected:
105  void onAdd( typename BaseRefCountType::value_type n) {
106  if( !dynamic_cast< RefCountedType * >( n ) ) {
107  stringstream s;
108  s << "Expecting " << typeid( RefCountedType ).name();
109  throw InvalidNodeType( typeid( n ).name(),
110  s.str(),
112 
113  } else {
114  BaseRefCountType::onAdd( n );
115  }
116  }
117  public:
119  virtual RefCountedType *getValue() {
120  return static_cast< RefCountedType * >( BaseRefCountType::getValue() );
121  }
122 
123  };
124 
129  template< class BaseFieldType >
130  struct AutoUpdate: public BaseFieldType {
134  virtual void propagateEvent( Field::Event e ) {
135  BaseFieldType::propagateEvent( e );
136  this->upToDate();
137  }
138  };
139 
144  template< class FieldType >
145  class EventCollectingField : public FieldType {
146  protected:
147 
150  virtual void propagateEvent( Field::Event e ) {
151  FieldType::propagateEvent( e );
152  event_fields.insert( e.ptr );
153  }
154 
155  public:
159  virtual void upToDate() {
160  FieldType::upToDate();
161  event_fields.clear();
162  }
163 
166  inline bool hasCausedEvent( Field *f ) {
167  return event_fields.find( f ) != event_fields.end();
168  }
169 
172  inline size_t nrPendingEvents() {
173  return event_fields.size();
174  }
175 
178  template< class FieldType2 >
179  inline bool hasCausedEvent( H3DUniquePtr< FieldType2 > &f ) {
180  return hasCausedEvent( f.get() );
181  }
182 
183  private:
186  set< Field * > event_fields;
187  };
188 
189 }
190 
191 #endif
192 
#define H3D_FULL_LOCATION
Contains base classes used to create multivalued fields.
Contains the SField template class.
Contains the TypedField template class.
The EventCollection field collects all fields routed to it that generates event between calls to the ...
Definition: FieldTemplates.h:145
size_t nrPendingEvents()
Returns the number of input fields that has generated an event to this field since the last call to t...
Definition: FieldTemplates.h:172
bool hasCausedEvent(Field *f)
Returns true if the Field given has generated an event to this field since the last call to the updat...
Definition: FieldTemplates.h:166
virtual void upToDate()
Check that the field is up-to-date, if not update() is called to make it up-to-date.
Definition: FieldTemplates.h:159
virtual void propagateEvent(Field::Event e)
When the event is propagated the field that created the event is saved.
Definition: FieldTemplates.h:150
bool hasCausedEvent(H3DUniquePtr< FieldType2 > &f)
Returns true if the Field given has generated an event to this field since the last call to the updat...
Definition: FieldTemplates.h:179
The Field class.
Definition: Field.h:46
Template for adding the virtual function onNewValue that can be overridden in subclasses in order to ...
Definition: FieldTemplates.h:79
virtual void onNewValue(const typename SF::value_type &new_value)=0
This function is called when the field is updated to a value.
Template for adding the virtual function onValueChange that can be overridden in subclasses in order ...
Definition: FieldTemplates.h:49
virtual void onValueChange(const typename SF::value_type &new_value)=0
This function is called when the value in the field has changed.
Template to make sure that the Node that is set in a SFNode is of a specified Node type.
Definition: FieldTemplates.h:102
void onAdd(typename BaseRefCountType::value_type n)
We check that the type of the Node is of the correct type.
Definition: FieldTemplates.h:105
virtual RefCountedType * getValue()
Get the value casted to the RefCountType.
Definition: FieldTemplates.h:119
H3D_VALUE_EXCEPTION(string, InvalidType)
An exception thrown when a field is of the wrong type when it is checked.
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
The AutoUpdate field is a template to force the BaseField to update itself as soon as an event is rec...
Definition: FieldTemplates.h:130
virtual void propagateEvent(Field::Event e)
Receive an event from another field.
Definition: FieldTemplates.h:134
The Event struct encapsulates an event in the field network.
Definition: Field.h:75
Field * ptr
The Field that caused the event.
Definition: Field.h:84