H3D API  2.4.1
SFNode.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 __SFNODE_H__
29 #define __SFNODE_H__
30 
31 #include <H3D/FieldTemplates.h>
32 #include <H3D/RefCountSField.h>
33 #include <H3D/Node.h>
34 
35 namespace H3D {
36 
37 
41  class H3DAPI_API SFNode:
42  public RefCountSField< Node,
43  TypedField< Field,
44  void,
45  AnyNumber< SFNode > > > {
46  public:
48  SFNode() {
49  AUTOREF_DEBUG_NAME( value, "SFNode::value" )
50  }
51 
53  SFNode( Node *_value ):
56  void,
57  AnyNumber< SFNode > > > ( _value ) {
58  AUTOREF_DEBUG_NAME( value, "SFNode::value" )
59  }
60 
62  virtual string getTypeName() {
63  return classTypeName();
64  }
65 
67  static string classTypeName() {
68  return "SFNode";
69  }
70 
71 
73  virtual X3DTypes::X3DType getX3DType() { return X3DTypes::SFNODE; }
74 
75  protected:
76  virtual Node *preOnAdd( Node *n ) {
77  Node *pn = getPrototypeNode( n );
78  if( pn ) return pn;
79  else return n;
80  }
81 
82  virtual Node *preOnRemove( Node *n ) {
83  Node *pn = getPrototypeNode( n );
84  if( pn ) return pn;
85  else return n;
86  }
87 
88  Node *getPrototypeNode( Node *n );
89  };
90 
91 
96  template< class NodeType >
97  class TypedSFNode: public SFNode {
98  protected:
101  typedef NodeType typed_class_type;
104  typedef NodeType * typed_value_type;
105 
108  virtual void onAdd( Node *n) {
109  if( !dynamic_cast< NodeType * >( n ) ) {
110  Node *pi = getPrototypeNode( n );
111  if( !dynamic_cast< NodeType * >( pi ) ) {
112  stringstream s;
113  s << "Expecting " << typeid( NodeType ).name();
114  throw InvalidNodeType( n->getTypeName(),
115  s.str(),
117  }
118  }
119  SFNode::onAdd( n );
120  }
121  public:
123  virtual NodeType *getValue( int id = 0 ) {
124  return getCastedValue( id );
125  }
126 #ifdef DISABLE_PROTOTYPENODE
128  virtual NodeType *getCastedValue( int id = 0 ) {
129  return static_cast<NodeType *>( SFNode::getValue(id) );
130  }
131 #else
133  virtual NodeType *getCastedValue( int id = 0 ) {
134  Node *n = SFNode::getValue( id );
135  Node *pn = getPrototypeNode( n );
136  if( pn )
137  return static_cast< NodeType * >( pn );
138  else
139  return static_cast< NodeType * >( n );
140  }
141 #endif
142 
143  };
144 
157  template< class Type >
158  class TypedSFNodeObject: public SFNode {
159  protected:
161  virtual void onAdd( Node *n) {
162  if( !dynamic_cast< Type * >( n ) ) {
163  Node *pi = getPrototypeNode( n );
164  if( !dynamic_cast< Type * >( pi ) ) {
165  stringstream s;
166  s << "Expecting " << typeid( Type ).name();
167  throw InvalidNodeType( n->getTypeName(),
168  s.str(),
170  }
171  }
172  SFNode::onAdd( n );
173  }
174  public:
176  virtual Type *getCastedValue( int id = 0 ) {
177  Node *n = SFNode::getValue( id );
178  Node *pn = getPrototypeNode( n );
179  if( pn )
180  return reinterpret_cast< Type * >( pn );
181  else
182  return reinterpret_cast< Type * >( n );
183  }
184  };
185 }
186 #endif
#define H3D_FULL_LOCATION
Contains different templates to modify field behaviour.
Node base class.
Contains the RefCountSField class.
std::string getTypeName() const
The Field class.
Definition: Field.h:46
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
RefCountSField is almost like any SField but it encapsulates a pointer to a RefCountedClass.
Definition: RefCountSField.h:42
virtual void onAdd(Node *n)
This function will be called when the value of RefCountSField changes.
Definition: RefCountSField.h:164
virtual Node * getValue(int id=0)
Get the value of the field.
Definition: RefCountSField.h:217
SFNode is almost like any SField but it encapsulates a pointer to a Node.
Definition: SFNode.h:45
SFNode(Node *_value)
Constructor.
Definition: SFNode.h:53
virtual string getTypeName()
Returns a string name for this field type i.e. SFNode.
Definition: SFNode.h:62
virtual X3DTypes::X3DType getX3DType()
Returns the X3DType of the field.
Definition: SFNode.h:73
SFNode()
Constructor.
Definition: SFNode.h:48
static string classTypeName()
Returns a string name for this field type i.e. SFNode.
Definition: SFNode.h:67
A template modifier class for adding type checking on the routes to any Field class.
Definition: TypedField.h:84
Template to make sure that the Node that is set in a SFNode is of a specified type.
Definition: SFNode.h:158
virtual void onAdd(Node *n)
We check that the type of the Node is of the correct type.
Definition: SFNode.h:161
virtual Type * getCastedValue(int id=0)
Get the value casted to the Type.
Definition: SFNode.h:176
Template to make sure that the Node that is set in a SFNode is of a specified Node type.
Definition: SFNode.h:97
NodeType typed_class_type
The subclass of value_type of that the TypedSFNode has made sure that the node is of.
Definition: SFNode.h:101
virtual NodeType * getValue(int id=0)
Get the value casted to the NodeType.
Definition: SFNode.h:123
virtual NodeType * getCastedValue(int id=0)
Get the value casted to the NodeType.
Definition: SFNode.h:133
virtual void onAdd(Node *n)
We check that the type of the Node is of the correct type.
Definition: SFNode.h:108
NodeType * typed_value_type
A pointer type to the subclass value_type of that the TypedSFNode has made sure that the node is of.
Definition: SFNode.h:104
H3D API namespace.
Definition: Anchor.h:38
The AnyNumber template can be used in the OptionalArgTypes argument of a TypedField template instanti...
Definition: TypedFieldCheck.h:72