H3D API  2.4.1
DependentNodeFields.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 __DEPENDENTNODEFIELDS_H__
29 #define __DEPENDENTNODEFIELDS_H__
30 
31 #include <H3D/SFNode.h>
32 #include <H3D/MFNode.h>
33 
34 namespace H3D {
35 
45  template< class NodeType,
46  class FieldType,
47  H3DUniquePtr< FieldType > NodeType:: *member_ptr >
48  class FieldRef {
49  public:
50  typedef NodeType node_type;
51  typedef FieldType field_type;
52  typedef H3DUniquePtr< FieldType > NodeType:: * member_ptr_type;
53  static Field *getField( NodeType *n ) {
54  return (n->*member_ptr).get();
55  }
56  static member_ptr_type getMemberPtr() {
57  return member_ptr;
58  }
59  };
60 
61  namespace FieldInternals {
62 
67  template< class FieldRefType >
68  class DoRoutes {
69  public:
71  typedef typename FieldRefType::node_type node_type;
72 
77  static void route( node_type *n, Field *f ) {
78  FieldRefType::getField( n )->route( f );
79  }
80 
85  static void unroute( node_type *n, Field *f ) {
86  FieldRefType::getField( n )->unroute( f );
87  }
88  };
89 
93  template< class FieldRefType, class FieldRefs >
94  class DoRoutes< pair< FieldRefType, FieldRefs > > {
95  public:
97  typedef typename FieldRefType::node_type node_type;
98 
103  static void route( node_type *n, Field *f ) {
104  FieldRefType::getField( n )->route( f );
105  DoRoutes< FieldRefs >::route( n , f );
106  }
107 
112  static void unroute( node_type *n, Field *f ) {
113  DoRoutes< FieldRefs >::unroute( n , f );
114  FieldRefType::getField( n )->unroute( f );
115  }
116  };
117 
125  class H3DAPI_API EventField: public AutoUpdate< Field > {
126  public:
129  EventField( Field *f ):
130  field( f ) {
131  name = "EventField";
132  }
133 
135  virtual void update() {
136  field->touch();
137  }
139  Field *field;
140  };
141 
148  template< class BaseField, class FieldRefs >
149  class DependentNodeFieldBase: public BaseField {
150  public:
151  typedef typename FieldRefs::node_type fr_node_type;
152 
154  DependentNodeFieldBase() {
155  event_field.reset( new EventField( this ) );
156  }
157 
158  // Constructor.
159  // DependentNodeField( Node *_value ) :
160  // BaseFieldType( _value ) {
161  // event_field.reset( new EventField( this ) );
162  // }
163  protected:
166  H3DUniquePtr< EventField > event_field;
167  };
168 
175  template< class BaseField, class FieldRefs, bool DynamicCast = false >
176  class DependentNodeField: public DependentNodeFieldBase< BaseField, FieldRefs > {
177  protected:
178  typedef typename FieldRefs::node_type fr_node_type;
179 
183  virtual void onAdd( Node *n ) {
184  BaseField::onAdd( n );
185  fr_node_type *node = static_cast< fr_node_type * >( n );
186  if( node )
187  DoRoutes< FieldRefs >::route( node,
188  this->event_field.get() );
189  }
190 
191 
195  virtual void onRemove( Node *n ) {
196  fr_node_type *node = static_cast< fr_node_type * >( n );
197  if( node )
198  DoRoutes< FieldRefs >::unroute( node,
199  this->event_field.get() );
200  BaseField::onRemove( n );
201  }
202  };
203 
204  template< class BaseField, class FieldRefs >
205  class DependentNodeField< BaseField, FieldRefs, true >: public DependentNodeFieldBase< BaseField, FieldRefs > {
206  protected:
207  typedef typename FieldRefs::node_type fr_node_type;
208 
212  virtual void onAdd( Node *n ) {
213  BaseField::onAdd( n );
214  fr_node_type *node = dynamic_cast< fr_node_type * >( n );
215  if( node )
216  DoRoutes< FieldRefs >::route( node,
217  this->event_field.get() );
218  }
219 
220 
224  virtual void onRemove( Node *n ) {
225  fr_node_type *node = dynamic_cast< fr_node_type * >( n );
226  if( node )
227  DoRoutes< FieldRefs >::unroute( node,
228  this->event_field.get() );
229  BaseField::onRemove( n );
230  }
231  };
232  }
290  template< class NodeType, class FieldRefs = void, bool DynamicCast = false >
292  public FieldInternals::DependentNodeField<
293  TypedSFNode< NodeType >,
294  FieldRefs, DynamicCast > {
295  public:
296 
299 
301  virtual ~DependentSFNode(){
302  this->value = NULL;
303  }
304 
305  };
306 
310  template< class FieldRefs, bool DynamicCast >
311  class DependentSFNode< FieldRefs, void, DynamicCast >:
312  public FieldInternals::DependentNodeField<
313  TypedSFNode< typename FieldInternals::DoRoutes< FieldRefs >::node_type >,
314  FieldRefs, DynamicCast > {
315  public:
316 
318  DependentSFNode(){}
319  virtual ~DependentSFNode() {
320  this->value = NULL;
321  }
322 
323  };
324 
331  template< class NodeType, class FieldRefs = void, bool DynamicCast = false >
333  public FieldInternals::DependentNodeField<
334  TypedMFNode< NodeType >,
335  FieldRefs,
336  DynamicCast > {
337  public:
338 
341 
343  virtual ~DependentMFNode(){
344  this->clear( this->ownerId() );
345  }
346 
347  };
348 
352  template< class FieldRefs, bool DynamicCast >
353  class DependentMFNode< FieldRefs, void, DynamicCast >:
354  public FieldInternals::DependentNodeField<
355  TypedMFNode< typename FieldInternals::DoRoutes< FieldRefs >::node_type >,
356  FieldRefs,
357  DynamicCast > {
358  public:
359 
361  DependentMFNode(){}
362 
364  virtual ~DependentMFNode(){
365  this->clear( this->ownerId() );
366  }
367 
368  };
369 
370 
373  template< class NodeType, class FieldRefs = void, bool DynamicCast = false >
375  public FieldInternals::DependentNodeField<
376  TypedSFNodeObject< NodeType >,
377  FieldRefs, DynamicCast > {
378  public:
379 
382 
385  this->value = NULL;
386  }
387 
388  };
389 
392  template< class NodeType, class FieldRefs = void, bool DynamicCast = false >
394  public FieldInternals::DependentNodeField<
395  TypedMFNodeObject< NodeType >,
396  FieldRefs, DynamicCast > {
397  public:
398 
401 
404  this->clear( this->ownerId() );
405  }
406 
407  };
408 }
409 
410 #endif
Contains the MFNode class.
Contains the SFNode class.
DependentMFNodeObject is similar to DependentMFNode but uses TypedMFNodeObject instead of TypedMFNode...
Definition: DependentNodeFields.h:396
virtual ~DependentMFNodeObject()
Destructor.
Definition: DependentNodeFields.h:403
DependentMFNodeObject()
Constructor. Default value of value is NULL.
Definition: DependentNodeFields.h:400
DependentMFNode are modified TypedMFNode fields where the field dirty status is dependent on fields i...
Definition: DependentNodeFields.h:336
DependentMFNode()
Constructor. Default value of value is NULL.
Definition: DependentNodeFields.h:340
virtual ~DependentMFNode()
Destructor.
Definition: DependentNodeFields.h:343
DependentSFNodeObject is similar to DependentSFNode but uses TypedSFNodeObject instead of TypedSFNode...
Definition: DependentNodeFields.h:377
virtual ~DependentSFNodeObject()
Destructor.
Definition: DependentNodeFields.h:384
DependentSFNodeObject()
Constructor. Default value of value is NULL.
Definition: DependentNodeFields.h:381
DependentSFNode are modified TypedSFNode fields where the field dirty status is dependent on fields i...
Definition: DependentNodeFields.h:294
DependentSFNode()
Constructor. Default value of value is NULL.
Definition: DependentNodeFields.h:298
virtual ~DependentSFNode()
Destructor.
Definition: DependentNodeFields.h:301
Class used for specifying a field member of a node in a DependentSFNode or DependentMFNode specificat...
Definition: DependentNodeFields.h:48
The Field class.
Definition: Field.h:46
virtual void route(Field *, int id=0)
Set up a route from this field to the field given as argument.
Definition: Field.cpp:93
H3D API namespace.
Definition: Anchor.h:38