DEFNodes.h

Go to the documentation of this file.
00001 
00002 //    H3D API.   Copyright 2004, Danel Evestedt, Mark Dixon
00003 //    All Rights Reserved
00004 //
00006 //
00008 
00009 #ifndef __DEFNODES_H__
00010 #define __DEFNODES_H__
00011 
00012 #include "Exception.h"
00013 #include "Node.h"
00014 #include <map>
00015 #include <string>
00016 
00017 using namespace std;
00018 
00019 namespace H3D {
00020   namespace X3D {
00024     H3D_VALUE_EXCEPTION( const string, InvalidNodeType );
00025 
00029     H3D_VALUE_EXCEPTION( const string, NoSuchDEFName );
00030 
00034     class H3DAPI_API DEFNodes : 
00035       private map< const string, Node * > { //, Util::LtStr > {
00036     public: 
00037       
00039       ~DEFNodes() {
00040                   for( iterator i = map< const string, Node * >::begin(); 
00041                            i != map< const string, Node * >::end(); 
00042                            i++ ) {
00043           (*i).second->unref();
00044         }
00045       }
00046       
00047       typedef  map< const string, Node * >::const_iterator const_iterator;
00048 
00051       const_iterator begin() {
00052         return map< const string, Node * >::begin();
00053       }
00054 
00057       const_iterator end() {
00058         return map< const string, Node * >::end();
00059       }
00060 
00065       void addNode( const string &def_name, Node *def_node ) {
00066         iterator i = find( def_name );
00067         // increase the reference counter for the new node
00068         def_node->ref();
00069         if( i == end() ) {
00070           insert( make_pair( def_name, def_node ) );
00071         } else {
00072           // the def name already exists in the map. Since the node
00073           // that is there will be overwritten we unref it.
00074           (*i).second->unref();
00075           (*i).second = def_node;
00076         }
00077       }
00078 
00082       void removeNode( const string& def_name ) {
00083         iterator i = find( def_name );
00084         if( i == end() ) {
00085           throw X3D::NoSuchDEFName( def_name );
00086         } else {
00087           // unref the node that is removed
00088           (*i).second->unref();
00089           erase( i );
00090         }
00091       }
00092 
00095       void merge( DEFNodes *dn ) {
00096         if( dn ) {
00097           for( const_iterator i = dn->begin(); i != dn->end(); i++ )
00098             addNode( (*i).first, (*i).second );
00099         }
00100       }
00101 
00103       void clear() {
00104         for( const_iterator i = begin(); i != end(); i++ )
00105           // unref the node that is removed
00106           (*i).second->unref();
00107         map< const string, Node * >::clear();
00108       }
00109 
00116       Node *getNode( const string &def_name ) {
00117         iterator i = find( def_name );
00118         if( i == end() ) {
00119           return NULL; //throw Exception::NoSuchDEFNameInX3D( def_name, "getNode()" );
00120         } else {
00121           return (*i).second;
00122         }
00123       }
00124 
00133       template< class NodeType >
00134       void getNode( const string &def_name, 
00135                     NodeType *&return_node ) {
00136         Node *node = getNode( def_name ) ;
00137         NodeType *n = dynamic_cast< NodeType * >( node );
00138         if( node && !n ) {
00139           stringstream msg;
00140           msg << "DEFNodes::getNode (expecting " 
00141               << typeid( NodeType ).name() 
00142               << ")";
00143           throw InvalidNodeType( typeid( *node ).name(), 
00144                                  msg.str() );
00145         }
00146         return_node = n;
00147       }; 
00148       
00149     };
00150   }
00151 }
00152 
00153 #endif

Generated on Thu Aug 24 12:38:32 2006 for H3D API by  doxygen 1.4.5