SFNode.h

Go to the documentation of this file.
00001 
00002 //    Copyright 2004, SenseGraphics AB
00003 //
00004 //    This file is part of H3D API.
00005 //
00006 //    H3D API is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    H3D API is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with H3D API; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 //
00020 //    A commercial license is also available. Please contact us at 
00021 //    www.sensegraphics.com for more information.
00022 //
00023 //
00026 //
00028 #ifndef __SFNODE_H__
00029 #define __SFNODE_H__
00030 
00031 #include "FieldTemplates.h"
00032 #include "RefCountSField.h"
00033 #include "Node.h"
00034 
00035 namespace H3D {
00036   
00037 
00041   class H3DAPI_API SFNode: 
00042     public RefCountSField< Node,
00043                            TypedField< Field,
00044                                        void, 
00045                                        AnyNumber< SFNode > > > {
00046   public:   
00048     SFNode() {}
00049     
00051     SFNode( Node *_value ):
00052       RefCountSField< Node,
00053                       TypedField< Field,
00054                                   void, 
00055                                   AnyNumber< SFNode > > > ( _value ) {}
00056     
00058     virtual string getTypeName() {
00059       return classTypeName();
00060     }
00061     
00063     static string classTypeName() {
00064       return "SFNode";
00065     }
00066 
00067 
00069     virtual X3DTypes::X3DType getX3DType() { return X3DTypes::SFNODE; }
00070  
00071   protected:
00072     virtual Node *preOnAdd( Node *n ) {
00073       Node *pn = getPrototypeNode( n );
00074       if( pn ) return pn;
00075       else return n;
00076     }
00077     
00078     virtual Node *preOnRemove( Node *n ) {
00079       Node *pn = getPrototypeNode( n );
00080       if( pn ) return pn;
00081       else return n;
00082     }
00083 
00084     Node *getPrototypeNode( Node *n );
00085   };
00086 
00087 
00092   template< class NodeType >
00093   class TypedSFNode: public SFNode {
00094   protected:
00097     typedef NodeType typed_class_type;
00100     typedef NodeType * typed_value_type;
00101 
00103     void onAdd( Node *n) {
00104       if( !dynamic_cast< NodeType * >( n ) ) {
00105         Node *pi = getPrototypeNode( n );
00106         if( !dynamic_cast< NodeType * >( pi ) ) {
00107           stringstream s;
00108           s << "Expecting " << typeid( NodeType ).name();
00109           throw InvalidNodeType( n->getTypeName(),
00110                                  s.str(),
00111                                  H3D_FULL_LOCATION );
00112         }
00113       }
00114       SFNode::onAdd( n );
00115     }
00116   public:
00118     virtual NodeType *getValue( int id = 0 ) {
00119       return getCastedValue( id );
00120     }
00121 
00123     virtual NodeType *getCastedValue( int id = 0 ) {
00124       Node *n = SFNode::getValue( id );
00125       Node *pn = getPrototypeNode( n );
00126       if( pn ) 
00127         return static_cast< NodeType * >( pn );
00128       else
00129         return static_cast< NodeType * >( n );
00130     }
00131 
00132   };
00133 
00146   template< class Type >
00147   class TypedSFNodeObject: public SFNode {
00148   protected:
00150     void onAdd( Node *n) {
00151       if( !dynamic_cast< Type * >( n ) ) {
00152         Node *pi = getPrototypeNode( n );
00153         if( !dynamic_cast< Type * >( pi ) ) {
00154           stringstream s;
00155           s << "Expecting " << typeid( Type ).name();
00156           throw InvalidNodeType( n->getTypeName(),
00157                                  s.str(),
00158                                  H3D_FULL_LOCATION );
00159         }
00160       } 
00161       SFNode::onAdd( n );
00162     }
00163   public:
00165     virtual Type *getCastedValue( int id = 0 ) {
00166       Node *n = SFNode::getValue( id );
00167       Node *pn = getPrototypeNode( n );
00168       if( pn ) 
00169         return reinterpret_cast< Type * >( pn );
00170       else
00171         return reinterpret_cast< Type * >( n );
00172     }
00173   };
00174 }
00175 #endif

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