AutoRef.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 //
00027 //
00029 #ifndef __AUTOREF_H__
00030 #define __AUTOREF_H__
00031 
00032 namespace H3D {
00040     
00041   template < class NodeType > 
00042   class AutoRef {
00043   public: 
00045     typedef NodeType element_type; 
00046         
00048     explicit AutoRef (NodeType* n = 0) throw() :
00049       node_ptr( n ) {
00050       ref( node_ptr );
00051     }
00052 
00054     AutoRef(const AutoRef<NodeType>&ar) throw () :
00055       node_ptr( NULL ){
00056       reset( ar.get() );
00057     }
00058             
00060     template <class Y> 
00061     AutoRef(const AutoRef<Y>&ar) throw() :
00062       node_ptr( NULL ){
00063       reset( ar.get() );
00064     }
00065         
00067     AutoRef<NodeType>& operator=(const AutoRef<NodeType>&ar) throw() {
00068       reset( ar.node_ptr );
00069       return *this;
00070     }
00071 
00073     template <class Y> 
00074     AutoRef<NodeType>& operator= (const AutoRef<Y>& ar) throw() {
00075       reset( ar.node_ptr );
00076       return *this; 
00077     } 
00078     virtual ~AutoRef() throw() {
00079       unref( node_ptr );
00080     } 
00081         
00083     NodeType& operator* () const throw() {
00084       return *node_ptr;
00085     } 
00086 
00088     NodeType* operator-> () const throw() {
00089       return node_ptr;
00090     } 
00091 
00093     NodeType* get () const throw() {
00094       return node_ptr;
00095     }
00096 
00100     void reset(NodeType* p = 0) throw() {
00101       if( p != node_ptr ) {
00102         if ( node_ptr ) unref( node_ptr );
00103         node_ptr = p;
00104         if ( node_ptr ) ref( node_ptr );
00105       }
00106     }
00107 
00108   protected:
00115     inline virtual void ref( NodeType *n ) {
00116       if( n )
00117         n->ref();
00118     }
00119 
00126     inline virtual void unref( NodeType *n ) {
00127       if( n )
00128         n->unref();
00129     }
00130         
00131     NodeType *node_ptr;
00132 
00133   };
00134 }
00135 
00136 #endif

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