H3D API  2.4.1
DEFNodes.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 
29 #ifndef __DEFNODES_H__
30 #define __DEFNODES_H__
31 
32 #include <H3DUtil/Exception.h>
33 #include <H3D/Node.h>
34 #include <map>
35 #include <string>
36 
37 #ifdef H3D_REFERENCE_COUNT_DEBUG
38 #include <set>
39 #include <string>
41 #define DEFNODES_DEBUG_NAME(ref_ptr, debug_name) (ref_ptr).name = debug_name;
42 #else
43 #define DEFNODES_DEBUG_NAME(ref_ptr, name)
44 #endif
45 
46 
47 
48 namespace H3D {
49  namespace X3D {
50 #ifdef H3D_REFERENCE_COUNT_DEBUG
53  class H3DAPI_API DEFNodesBase {
54  public:
55  DEFNodesBase();
56 
57  virtual ~DEFNodesBase();
58 
59  // lock for use for access to the DEF_nodes member.
60  static MutexLock DEF_nodes_lock;
61 
62  // set of all DEFNodes instances created. All access to this
63  // member must be using the DEF_nodes_lock member to gain access.
64  static std::set< DEFNodesBase * > DEF_nodes;
65 
66  // Name of this DEFNodes instance used in debugging messages.
67  std::string name;
68  };
69 #endif
70 
75 
80 
81 #if _MSC_VER >= 1500
82 #pragma warning( disable : 4275 )
83 #endif
87  class H3DAPI_API DEFNodes :
88  private map< const string, Node * >
89 #ifdef H3D_REFERENCE_COUNT_DEBUG
90  , public DEFNodesBase
91 #endif
92  { //, Util::LtStr > {
93 #if _MSC_VER >= 1500
94 #pragma warning( default : 4275 )
95 #endif
96  public:
97 
99  ~DEFNodes();
100 
101  typedef map< const string, Node * >::const_iterator const_iterator;
102 
105  const_iterator begin() {
106  return map< const string, Node * >::begin();
107  }
108 
111  const_iterator end() {
112  return map< const string, Node * >::end();
113  }
114 
119  void addNode( const string &def_name, Node *def_node ) {
120  iterator i = find( def_name );
121  // increase the reference counter for the new node
122  def_node->ref();
123  if( i == end() ) {
124  insert( make_pair( def_name, def_node ) );
125  } else {
126  // the def name already exists in the map. Since the node
127  // that is there will be overwritten we unref it.
128  (*i).second->unref();
129  (*i).second = def_node;
130  }
131  }
132 
136  void removeNode( const string& def_name ) {
137  iterator i = find( def_name );
138  if( i == end() ) {
139  throw X3D::NoSuchDEFName( def_name );
140  } else {
141  // unref the node that is removed
142  (*i).second->unref();
143  erase( i );
144  }
145  }
146 
149  void merge( DEFNodes *dn ) {
150  if( dn ) {
151  for( const_iterator i = dn->begin(); i != dn->end(); ++i )
152  addNode( (*i).first, (*i).second );
153  }
154  }
155 
157  void clear() {
158  for( const_iterator i = begin(); i != end(); ++i )
159  // unref the node that is removed
160  (*i).second->unref();
161  map< const string, Node * >::clear();
162  }
163 
164  bool empty() {
165  return map< const string, Node * >::empty();
166  }
167 
174  Node *getNode( const string &def_name ) {
175  iterator i = find( def_name );
176  if( i == end() ) {
177  return NULL; //throw Exception::NoSuchDEFNameInX3D( def_name, "getNode()" );
178  } else {
179  return (*i).second;
180  }
181  }
182 
191  template< class NodeType >
192  void getNode( const string &def_name,
193  NodeType *&return_node ) {
194  Node *node = getNode( def_name ) ;
195  NodeType *n = dynamic_cast< NodeType * >( node );
196  if( node && !n ) {
197  stringstream msg;
198  msg << "DEFNodes::getNode (expecting "
199  << typeid( NodeType ).name()
200  << ")";
201  throw InvalidNodeType( typeid( *node ).name(),
202  msg.str() );
203  }
204  return_node = n;
205  }
206 
207  };
208  }
209 }
210 
211 #endif
Node base class.
Node is the base class for all classes that can be part of the H3D scene-graph.
Definition: Node.h:46
Base class that adds functionallity needed for reference count debugging.
Definition: DEFNodes.h:53
Provides a mapping between defined DEF names in X3D and the nodes they refer to.
Definition: DEFNodes.h:92
const_iterator end()
Returns a const_iterator to the end element in the DEFNodes struct.
Definition: DEFNodes.h:111
Node * getNode(const string &def_name)
Get the node by its DEF name.
Definition: DEFNodes.h:174
void merge(DEFNodes *dn)
Add the entries from another DEFNodes structure do this one.
Definition: DEFNodes.h:149
void clear()
Removes all the entries in the map.
Definition: DEFNodes.h:157
void removeNode(const string &def_name)
Removes a DEF name/Node * pair from the structure.
Definition: DEFNodes.h:136
const_iterator begin()
Returns a const_iterator to the first element in the DEFNodes struct.
Definition: DEFNodes.h:105
void getNode(const string &def_name, NodeType *&return_node)
Get the node by its DEF name.
Definition: DEFNodes.h:192
void addNode(const string &def_name, Node *def_node)
Adds a DEF name/Node * pair to the structure.
Definition: DEFNodes.h:119
An exception for errors when the Node type is not what was expected.
Definition: DEFNodes.h:74
An exception for errors when the DEF name does not exist in the DEF nodes structure.
Definition: DEFNodes.h:79
H3D_VALUE_EXCEPTION(string, InvalidType)
An exception thrown when a field is of the wrong type when it is checked.
H3D API namespace.
Definition: Anchor.h:38