H3D API  2.4.1
H3DNodeDatabase.h
Go to the documentation of this file.
1 
3 // Copyright 2004-2019, SenseGraphics AB
4 //
5 // This file is part of H3D API.
6 //
7 // H3D API is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // H3D API is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with H3D API; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 //
21 // A commercial license is also available. Please contact us at
22 // www.sensegraphics.com for more information.
23 //
24 //
28 //
30 
31 #ifndef __H3DNODEDATABSE_H__
32 #define __H3DNODEDATABSE_H__
33 
34 #include <H3D/H3DApi.h>
35 #include <H3D/Field.h>
36 #include <iostream>
37 #include <string>
38 #include <map>
39 #include <list>
40 #include <typeinfo>
41 
43 #define FIELDDB_ELEMENT( base, field, access ) \
44 FieldDBInsert field( access( &base::database, #field, &base::field ) );
45 
47 #define FIELDDB_ELEMENT_EX( base, field, access, fieldst ) \
48 FieldDBInsert fieldst( access( &base::database, #fieldst, &base::field ) );
49 
50 namespace H3D {
51 
52  class Node;
53  typedef Node*( *H3DCreateNodeFunc)();
54 
55  struct H3DNodeDatabase;
56 
58  struct H3DAPI_API FieldDBElement {
64  FieldDBElement( H3DNodeDatabase *_container,
65  const string & _name,
66  const Field::AccessType _access );
67 
69  virtual ~FieldDBElement() {};
70 
73  return container;
74  }
75 
77  const string& getName() {
78  return name;
79  }
80 
83  return access;
84  }
85 
88  virtual Field *getField( const Node * ) const { return NULL; };
89 
90  protected:
91  H3DNodeDatabase *container;
92  string name;
93  const Field::AccessType access;
94  };
95 
100  struct H3DAPI_API DynamicFieldDBElement : FieldDBElement {
102  const string&_name,
103  const Field::AccessType _access,
104  Field *_ptr ) :
105  FieldDBElement( _container, _name, _access ), ptr( _ptr ) {}
106 
109  virtual Field *getField( const Node *n ) const {
110  if( ptr && n == ptr->getOwner() )
111  return ptr;
112  else
113  return NULL;
114  }
115  protected:
118  };
119 
120 
122  template< class N, class F >
124  FieldDBTemplate( H3DNodeDatabase *_container,
125  const string& _name,
126  const Field::AccessType _access,
127  H3DUniquePtr<F> N:: *_ptr ) :
128  FieldDBElement( _container, _name, _access ), ptr( _ptr ) {}
129 
132  virtual Field *getField( const Node *n ) const {
133  const N *node = dynamic_cast<const N*>(n);
134  if ( node )
135  return (node->*ptr).get();
136  else
137  return NULL;
138  }
139  protected:
141  const H3DUniquePtr<F> N:: *ptr;
142  };
143 
145  template< class N, class F >
147  const std::string& _name,
148  H3DUniquePtr<F> N:: *_ptr ) {
149  return new FieldDBTemplate< N, F >( _container,
150  _name,
152  _ptr );
153  }
154 
156  template< class N, class F >
158  const std::string &_name,
159  H3DUniquePtr<F> N:: *_ptr ) {
160  return new FieldDBTemplate< N, F >( _container,
161  _name,
162  Field::OUTPUT_ONLY, _ptr );
163  }
164 
166  template< class N, class F >
168  const std::string &_name,
169  H3DUniquePtr<F> N:: *_ptr ) {
170  return new FieldDBTemplate< N, F >( _container,
171  _name,
173  _ptr );
174  }
175 
177  template< class N, class F >
179  const std::string &_name,
180  H3DUniquePtr<F> N:: *_ptr ) {
181  return new FieldDBTemplate< N, F >( _container,
182  _name,
184  _ptr );
185  }
186 
187 
194  struct H3DAPI_API H3DNodeDatabase {
195 
198  const std::type_info& t;
199 
201  TypeInfoWrapper (const std::type_info& t_)
202  : t (t_) {}
203 
205  bool operator< (const TypeInfoWrapper & o) const {
206  return t.before (o.t) != 0;
207  }
208  };
209 
210  typedef map< string, FieldDBElement* > FieldDBType;
211  typedef map< TypeInfoWrapper, H3DNodeDatabase* > H3DNodeDatabaseType;
212  typedef H3DNodeDatabaseType::const_iterator NodeDatabaseConstIterator;
213 
217  class H3DAPI_API FieldDBConstIterator {
218  public:
220  FieldDBConstIterator( H3DNodeDatabase * _ndb , bool is_end );
221 
224 
226  FieldDBConstIterator & operator=(const FieldDBConstIterator &rhs);
227 
230  return operator++();
231  }
232 
234  FieldDBConstIterator &operator++();
235 
237  bool operator==( FieldDBConstIterator iter );
238 
240  inline bool operator!=( FieldDBConstIterator iter ) {
241  return !operator==( iter );
242  }
243 
245  inline string operator*() {
246  if( status == INHERITED )
247  return *(*inherited_iterator);
248  else
250  return (*local_iterator).first;
251  }
252 
255  if( status == INHERITED )
256  return (*inherited_iterator).getFieldDBElement();
257  else
259  return (*local_iterator).second;
260  }
261 
263  inline Field * getField( Node *n ) {
264  if( status == INHERITED )
265  return (*inherited_iterator).getField( n );
266  else
268  return (*local_iterator).second->getField( n ) ;
269  }
270 
271  protected:
273  status( END ),
274  ndb( NULL ) {}
275 
277  typedef enum {
285  END
286  } Status;
287 
293  FieldDBType::const_iterator local_iterator;
296  H3DUniquePtr< FieldDBConstIterator > inherited_iterator;
299  };
300 #ifdef __BORLANDC__
301  friend class FieldDBConstIterator;
302 #endif
303 
304 
306  H3DNodeDatabase( const string&_name,
307  H3DCreateNodeFunc _createf,
308  const type_info &_ti,
309  H3DNodeDatabase *_parent = 0 );
310 
314  H3DNodeDatabase( const string&_name,
315  const string &_alias,
316  H3DCreateNodeFunc _createf,
317  const type_info &_ti,
318  H3DNodeDatabase *_parent = 0 );
319 
322  H3DNodeDatabase( const type_info &_ti,
323  H3DNodeDatabase *_parent = 0 );
324  ~H3DNodeDatabase(void);
325 
327  static H3DNodeDatabase *lookupTypeId( const type_info &t );
328 
330  static H3DNodeDatabase *lookupName( const string &name );
331 
333  inline void addAlias( const string &alias ) {
334  aliases.push_back( alias );
335  }
336 
341  static Node *createNode( const string &name );
342 
344  inline const string &getName() {
345  return name;
346  }
347 
349  inline const type_info &getTypeInfo() {
350  return ti;
351  }
352 
355  inline Node * createNode() {
356  if( createf ) return createf();
357  else return NULL;
358  }
359 
362  void addField( FieldDBElement *f );
363 
369  bool removeField ( const string& _name, Node* _node= NULL );
370 
373  Field *getField( const Node * n, const string& f ) const;
374 
377  void initFields( Node* ) const;
378 
380  void clearDynamicFields();
381 
384  void clearDynamicFields( Node *n );
385 
386  private:
388  Field *getFieldHelp( const Node * n, const string& f ) const;
389 
391  string name;
392 
394  const H3DCreateNodeFunc createf;
395 
397  const type_info &ti;
398 
400  H3DNodeDatabase *parent;
401 
403  FieldDBType fields;
404 
406  static bool initialized;
407 
410  list< string > aliases;
411 
412  public:
414  static H3DNodeDatabaseType *database;
415 
419  static NodeDatabaseConstIterator begin() {
420  return database->begin();
421  }
422 
424  static NodeDatabaseConstIterator end() {
425  return database->end();
426  }
427 
431  return FieldDBConstIterator( this, 0 );
432  }
433 
436  return FieldDBConstIterator( this,1 );
437  }
438 
440  inline size_t fieldDBSize() {
441  if( !parent ) {
442  return fields.size();
443  } else {
444  return fields.size() + parent->fieldDBSize();
445  }
446  }
447 
452  H3DNodeDatabase( const Node * n,
453  H3DNodeDatabase *_parent );
454 
455  typedef map< const Node *, H3DNodeDatabase* > H3DNodeInstanceDatabase;
457  static H3DNodeDatabase *lookupNodeInstance( const Node * n );
458  protected:
459  H3DNodeInstanceDatabase node_instance_database;
460  bool database_allocated_dynamic;
461 
462  };
463 
464  struct FieldDBInsert {
465  FieldDBInsert( FieldDBElement *f ) {
466  f->getContainer()->addField( f );
467  }
468  };
469 
470 
471 }
472 
473 #endif
Contains the Field class.
Base header file that handles all configuration related settings.
The Field class.
Definition: Field.h:46
AccessType
The different access types that a field can have.
Definition: Field.h:53
@ INPUT_OUTPUT
No restrictions on access to the field values or routes.
Definition: Field.h:69
@ 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
The FieldDBConstIterator is an iterator class used for iterating over the field instances in an H3DNo...
Definition: H3DNodeDatabase.h:217
H3DUniquePtr< FieldDBConstIterator > inherited_iterator
Iterator used for iterating through the fields inherited from the parent database if it exists.
Definition: H3DNodeDatabase.h:296
Status
Defines the status of the iterator.
Definition: H3DNodeDatabase.h:277
@ INHERITED
The iterator is currently iterating through the field definitions of the inherited database fields.
Definition: H3DNodeDatabase.h:283
@ LOCAL
The iterator is currently iterating through the local field definitions.
Definition: H3DNodeDatabase.h:280
Status status
The status of the iterator.
Definition: H3DNodeDatabase.h:290
string operator*()
Dereference operator.
Definition: H3DNodeDatabase.h:245
FieldDBElement * getFieldDBElement()
Get the FieldDBElement associated with the iterator.
Definition: H3DNodeDatabase.h:254
bool operator!=(FieldDBConstIterator iter)
Not equals operator.
Definition: H3DNodeDatabase.h:240
H3DNodeDatabase * ndb
The database which fields this iterator iterates through.
Definition: H3DNodeDatabase.h:298
Field * getField(Node *n)
Get the Field * associated with the iterator.
Definition: H3DNodeDatabase.h:263
FieldDBConstIterator & operator++(int)
Pre-increment operator.
Definition: H3DNodeDatabase.h:229
FieldDBType::const_iterator local_iterator
Iterator used for iterating through the local fields in the H3DNodeDatabase.Only valid if status == L...
Definition: H3DNodeDatabase.h:293
Node is the base class for all classes that can be part of the H3D scene-graph.
Definition: Node.h:46
bool operator==(const DualQuaternion &q1, const DualQuaternion &q2)
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
FieldDBTemplate< N, F > * OUTPUT_ONLY(H3DNodeDatabase *_container, const std::string &_name, H3DUniquePtr< F > N::*_ptr)
Specialization to handle case where access type is OUTPUT_ONLY.
Definition: H3DNodeDatabase.h:157
FieldDBTemplate< N, F > * INITIALIZE_ONLY(H3DNodeDatabase *_container, const std::string &_name, H3DUniquePtr< F > N::*_ptr)
Specialization to handle case where access type is INITIALIZE_ONLY.
Definition: H3DNodeDatabase.h:146
FieldDBTemplate< N, F > * INPUT_ONLY(H3DNodeDatabase *_container, const std::string &_name, H3DUniquePtr< F > N::*_ptr)
Specialization to handle case where access type is INPUT_ONLY.
Definition: H3DNodeDatabase.h:167
The DynamicFieldDBElement is a FieldDBElement for fields that are not actual members of a Node class,...
Definition: H3DNodeDatabase.h:100
Field * ptr
The pointer to the field.
Definition: H3DNodeDatabase.h:117
virtual Field * getField(const Node *n) const
Given a Node get a pointer to the field in that node that is represented by this FieldDBElement.
Definition: H3DNodeDatabase.h:109
A FieldDBElement is an encapsulation of a field in a H3DNodeDatabase.
Definition: H3DNodeDatabase.h:58
FieldDBElement(H3DNodeDatabase *_container, const string &_name, const Field::AccessType _access)
Constructor.
Definition: H3DNodeDatabase.cpp:255
Field::AccessType getAccessType()
Get the access type of the field.
Definition: H3DNodeDatabase.h:82
H3DNodeDatabase * getContainer()
Get the H3DNodeDatabase this instance is part of.
Definition: H3DNodeDatabase.h:72
const string & getName()
Get the name of the field.
Definition: H3DNodeDatabase.h:77
virtual Field * getField(const Node *) const
Given a Node get a pointer to the field in that node that is represented by this FieldDBElement.
Definition: H3DNodeDatabase.h:88
virtual ~FieldDBElement()
Destructor.
Definition: H3DNodeDatabase.h:69
Template class for specifying specialized FieldDBElement instances.
Definition: H3DNodeDatabase.h:123
const H3DUniquePtr< F > N::* ptr
The member pointer in the node for the field.
Definition: H3DNodeDatabase.h:141
virtual Field * getField(const Node *n) const
Given a Node get a pointer to the field in that node that is represented by this FieldDBElement.
Definition: H3DNodeDatabase.h:132
Wrapper class to be able to put type_info as a key in a map.
Definition: H3DNodeDatabase.h:197
TypeInfoWrapper(const std::type_info &t_)
Constructor.
Definition: H3DNodeDatabase.h:201
The H3DNodeDatabase contains a mapping between a name of a Node and the constructor for the Node with...
Definition: H3DNodeDatabase.h:194
void addAlias(const string &alias)
Add an alias for the node in the database.
Definition: H3DNodeDatabase.h:333
FieldDBConstIterator fieldDBEnd()
Returns an iterator pointing at the end of fields in the H3DNodeDatabase.
Definition: H3DNodeDatabase.h:435
const type_info & getTypeInfo()
Get the type_info of the node in the database object.
Definition: H3DNodeDatabase.h:349
size_t fieldDBSize()
Returns the number of fields in the H3DNodeDatabase.
Definition: H3DNodeDatabase.h:440
const string & getName()
Get the name of the node in the database object.
Definition: H3DNodeDatabase.h:344
static H3DNodeDatabaseType * database
The database with all H3DNodeDatabase instances.
Definition: H3DNodeDatabase.h:414
FieldDBConstIterator fieldDBBegin()
Gets an iterator to the beginning first field in the H3DNodeDatabase.
Definition: H3DNodeDatabase.h:430
Node * createNode()
Create a new instance of the node type the database object refers to.
Definition: H3DNodeDatabase.h:355
static NodeDatabaseConstIterator begin()
Iterator to the first element in the database.
Definition: H3DNodeDatabase.h:419
static NodeDatabaseConstIterator end()
Returns an iterator pointing at the end of the H3DNodeDatabase created.
Definition: H3DNodeDatabase.h:424