H3D API  2.4.1
MFNode.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 __MFNODE_H__
29 #define __MFNODE_H__
30 
31 #include <H3D/RefCountMField.h>
33 #include <H3D/FieldTemplates.h>
34 #include <H3D/Node.h>
36 
37 namespace H3D {
38 
44  class H3DAPI_API MFNode: public RefCountMField< Node > {
45  // Field > {
46  public:
47 
48  class const_iterator: public RefCountMField< Node >::const_iterator {
49  public:
50  typedef RefCountMField< Node >::const_iterator base_const_iterator;
51  const_iterator( base_const_iterator i ):
53  }
54 
55  Node * operator*() {
57  X3DPrototypeInstance *pi = dynamic_cast< X3DPrototypeInstance * >( n );
58  if( pi ) {
59  return pi->getPrototypedNode();
60  } else {
61  return n;
62  }
63  }
64  };
65 
66  class const_reverse_iterator:
67  public RefCountMField< Node >::const_reverse_iterator {
68  public:
69  typedef RefCountMField< Node >::const_reverse_iterator base_const_reverse_iterator;
71  const_reverse_iterator( base_const_reverse_iterator i ):
73 
74  }
75 
76  Node * operator*() {
78  X3DPrototypeInstance *pi = dynamic_cast< X3DPrototypeInstance * >( n );
79  if( pi ) {
80  return pi->getPrototypedNode();
81  } else {
82  return n;
83  }
84  }
85  };
86 
88  inline const_iterator begin( int id = 0 ) {
89  return const_iterator( RefCountMField<Node>::begin( id ) );
90  }
91 
93  inline const_iterator end( int id = 0 ) {
94  return const_iterator( RefCountMField<Node>::end( id ) );
95  }
96 
98  inline const_reverse_iterator rbegin( int id = 0 ) {
99  return const_reverse_iterator( RefCountMField<Node>::rbegin( id ) );
100  }
101 
103  inline const_reverse_iterator rend( int id = 0 ) {
104  return const_reverse_iterator( RefCountMField<Node>::rend( id ) );
105  }
106 
108  inline virtual Node * operator[](size_type i ) {
110  X3DPrototypeInstance *pi = dynamic_cast< X3DPrototypeInstance * >( n );
111  if( pi ) {
112  return pi->getPrototypedNode();
113  } else {
114  return n;
115  }
116  }
117 
119  inline virtual Node * front( int id = 0 ) {
121  X3DPrototypeInstance *pi = dynamic_cast< X3DPrototypeInstance * >( n );
122  if( pi ) {
123  return pi->getPrototypedNode();
124  } else {
125  return n;
126  }
127  }
128 
130  inline virtual Node * back( int id = 0 ) {
132  X3DPrototypeInstance *pi = dynamic_cast< X3DPrototypeInstance * >( n );
133  if( pi ) {
134  return pi->getPrototypedNode();
135  } else {
136  return n;
137  }
138  }
139 
141  inline virtual Node * getValueByIndex(
142  size_type i,
143  int id = 0 ) {
145  X3DPrototypeInstance *pi = dynamic_cast< X3DPrototypeInstance * >( n );
146  if( pi ) {
147  return pi->getPrototypedNode();
148  } else {
149  return n;
150  }
151  }
152 
153 
154 
155  virtual Node *preOnAdd( Node *n ) {
156  Node *pn = getPrototypeNode( n );
157  if( pn ) {
158  return pn;
159  }
160  else
161  return n;
162  }
163 
164  virtual Node *preOnRemove( Node *n ) {
165  Node *pn = getPrototypeNode( n );
166  if( pn )
167  return pn;
168  else
169  return n;
170  }
171 
172 
174  MFNode() {
175  AUTOREF_DEBUG_NAME( value, "MFNode::value" )
176  }
177 
179  MFNode( size_type sz ) :
180  RefCountMField< Node >( sz ) {
181  AUTOREF_DEBUG_NAME( value, "MFNode::value" )
182  }
183 
185  inline virtual const NodeVector &getValue( int id = 0 ) {
186  // check that we have the correct access type
187  checkAccessTypeGet( id );
188  // check that the field is up-to-date first
189  this->upToDate();
190  return value;
191  }
192 
194  virtual string getTypeName() {
195  return classTypeName();
196  }
197 
199  static string classTypeName() {
200  return "MFNode";
201  }
202 
204  virtual X3DTypes::X3DType getX3DType() { return X3DTypes::MFNODE; }
205 
206  protected:
207  Node *getPrototypeNode( Node *n );
208  };
209 
210  template<>
211  inline string MFieldBase<Node *>::classTypeName() {
212  return "MFNode";
213  }
214 
215 
220  template< class NodeType >
221  class TypedMFNode: public MFNode {
222  protected:
224  virtual void onAdd( Node *n) {
225  if( !dynamic_cast< NodeType * >( n ) ) {
226  Node *pi = getPrototypeNode( n );
227  if( !dynamic_cast< NodeType * >( pi ) ) {
228  stringstream s;
229  s << "Expecting " << typeid( NodeType ).name();
230  throw InvalidNodeType( n->getTypeName(),
231  s.str(),
233  }
234  }
235  MFNode::onAdd( n );
236  }
237 
238  public:
240  virtual NodeType *getValueByIndex( typename MFNode::size_type i,
241  int id = 0 ) {
242  return static_cast< NodeType * >( MFNode::getValueByIndex( i, id ) );
243  }
244 
246  virtual NodeType *getCastedValueByIndex( typename MFNode::size_type i,
247  int id = 0 ) {
248  return this->getValueByIndex( i, id );
249  }
250  };
251 
264  template< class Type >
265  class TypedMFNodeObject: public MFNode {
266  protected:
268  void onAdd( Node *n) {
269  if( !dynamic_cast< Type * >( n ) ) {
270  Node *pi = getPrototypeNode( n );
271  if( !dynamic_cast< Type * >( pi ) ) {
272  stringstream s;
273  s << "Expecting " << typeid( Type ).name();
274  throw InvalidNodeType( n->getTypeName(),
275  s.str(),
277  }
278  }
279  MFNode::onAdd( n );
280  }
281 
282  public:
284  virtual Type *getCastedValueByIndex( typename MFNode::size_type i,
285  int id = 0 ) {
286  return reinterpret_cast< Type * >( this->getValueByIndex( i, id ) );
287  }
288  };
289 
290 
291 }
292 
293 #endif
#define H3D_FULL_LOCATION
Contains different templates to modify field behaviour.
Contains the MFNodeAutoRefVector class.
Node base class.
Contains the RefCountMField class.
Header file for X3DPrototypeInstance, X3D scene-graph node.
std::string getTypeName() const
MFNode is almost like any MField but it encapsulates a vector of Node pointers.
Definition: MFNode.h:44
const_iterator end(int id=0)
Returns a const_iterator pointing to the end of the vector.
Definition: MFNode.h:93
virtual Node * front(int id=0)
Returns the first element.
Definition: MFNode.h:119
MFNode(size_type sz)
Creates an MFNode with space reserved for size nodes.
Definition: MFNode.h:179
const_reverse_iterator rbegin(int id=0)
Returns a const_reverse_iterator pointing to the beginning of the vector.
Definition: MFNode.h:98
static string classTypeName()
Returns a string name for this field type i.e. MFNode.
Definition: MFNode.h:199
virtual Node * getValueByIndex(size_type i, int id=0)
Get the value of the MField.
Definition: MFNode.h:141
virtual X3DTypes::X3DType getX3DType()
Returns the X3DType of the field.
Definition: MFNode.h:204
const_iterator begin(int id=0)
Returns a const_iterator pointing to the beginning of the vector.
Definition: MFNode.h:88
virtual Node * back(int id=0)
Returns the last element.
Definition: MFNode.h:130
MFNode()
Default constructor. Creates an empty MFNode.
Definition: MFNode.h:174
virtual const NodeVector & getValue(int id=0)
Get the value of the MField.
Definition: MFNode.h:185
virtual string getTypeName()
Returns a string name for this field type i.e. MFNode.
Definition: MFNode.h:194
const_reverse_iterator rend(int id=0)
Returns a const_reverse_iterator pointing to the end of the vector.
Definition: MFNode.h:103
virtual Node * operator[](size_type i)
Returns the n'th element.
Definition: MFNode.h:108
const_reference operator[](size_type n)
Returns the n'th element.
Definition: MField.h:211
static string classTypeName()
Returns a string name for this field type e.g. MFInt32.
Definition: MField.h:340
const_reference front(int id=0)
Returns the first element.
Definition: MField.h:219
VectorClass::const_iterator const_iterator
Const iterator used to iterate through a vector.
Definition: MField.h:122
VectorClass::const_reverse_iterator const_reverse_iterator
Const iterator used to iterate backwards through a vector.
Definition: MField.h:125
const_reference back(int id=0)
Returns the last element.
Definition: MField.h:227
VectorClass::size_type size_type
An unsigned integral type.
Definition: MField.h:118
Node is the base class for all classes that can be part of the H3D scene-graph.
Definition: Node.h:46
RefCountMField is almost like any MField but it encapsulates a vector of RefCoundtedClass pointers.
Definition: RefCountMField.h:50
virtual void onAdd(Node *n)
This function will be called when values of RefCountMField changes.
Definition: RefCountMField.h:222
virtual RefClass * getValueByIndex(typename BaseFieldType::size_type i, int id=0)
Get the value of the MField.
Definition: RefCountMField.h:91
Template to make sure that the Nodes that are set in a MFNode are of a specified type.
Definition: MFNode.h:265
virtual Type * getCastedValueByIndex(typename MFNode::size_type i, int id=0)
Get the value casted to the Type.
Definition: MFNode.h:284
void onAdd(Node *n)
We check that the type of the Node is of the correct type.
Definition: MFNode.h:268
Template to make sure that the Nodes that are added to a MFNode are of a specified Node type.
Definition: MFNode.h:221
virtual NodeType * getCastedValueByIndex(typename MFNode::size_type i, int id=0)
Get the value casted to the NodeType.
Definition: MFNode.h:246
virtual NodeType * getValueByIndex(typename MFNode::size_type i, int id=0)
Get the value casted to the NodeType.
Definition: MFNode.h:240
virtual void onAdd(Node *n)
We check that the type of the Node is of the correct type.
Definition: MFNode.h:224
This abstract node type is the base type for all prototype instances in the X3D system.
Definition: X3DPrototypeInstance.h:65
virtual Node * getPrototypedNode()
Get the node that is the internal scenegraph of the prototype.
Definition: X3DPrototypeInstance.h:81
DualQuaternion operator*(const DualQuaternion &q1, const DualQuaternion &q2)
H3D API namespace.
Definition: Anchor.h:38