Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   Related Pages  

nodeptr.h

00001 //
00002 // OpenVRML
00003 //
00004 // Copyright (C) 2000  Braden McDaniel
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License, or (at your option) any later version.
00010 //
00011 // This library 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 GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 //
00020 
00021 # ifndef OPENVRML_NODEPTR_H
00022 #   define OPENVRML_NODEPTR_H
00023 
00024 #   include <assert.h>
00025 #   include <map>
00026 #   include "common.h"
00027 
00028 namespace OpenVRML {
00029 
00030     class Node;
00031 
00032     class OPENVRML_SCOPE NodePtr {
00033 
00034         friend class ScriptNode;
00035 
00036         std::map<Node *, size_t>::value_type * countPtr; // MSVC6 doesn't like std::map<>::pointer
00037 
00038     public:
00039         explicit NodePtr(Node * node = 0);
00040         NodePtr(const NodePtr & nodePtr);
00041         ~NodePtr();
00042 
00043         operator bool() const;
00044 
00045         NodePtr & operator=(const NodePtr & nodePtr);
00046 
00047         bool operator==(const NodePtr & nodePtr) const;
00048 
00049         Node & operator*() const;
00050         Node * operator->() const;
00051         Node * get() const;
00052 
00053         void reset(Node * node = 0);
00054         void swap(NodePtr & nodePtr) throw ();
00055 
00056     private:
00057         void dispose() throw ();
00058         void share(std::map<Node *, size_t>::value_type * countPtr) throw ();
00059     };
00060 
00061 
00062     inline NodePtr::~NodePtr()
00063     {
00064         this->dispose();
00065     }
00066 
00067     inline NodePtr::operator bool() const
00068     {
00069         return this->countPtr;
00070     }
00071 
00072     inline NodePtr & NodePtr::operator=(const NodePtr & nodePtr)
00073     {
00074         this->share(nodePtr.countPtr);
00075         return *this;
00076     }
00077 
00078     inline Node & NodePtr::operator*() const
00079     {
00080         assert(this->countPtr);
00081         assert(this->countPtr->first);
00082         return *this->countPtr->first;
00083     }
00084 
00085     inline Node * NodePtr::operator->() const
00086     {
00087         assert(this->countPtr);
00088         assert(this->countPtr->first);
00089         return this->countPtr->first;
00090     }
00091 
00092     inline Node * NodePtr::get() const
00093     {
00094         return this->countPtr ? this->countPtr->first : 0;
00095     }
00096 
00097     inline void NodePtr::swap(NodePtr & nodePtr) throw ()
00098     {
00099         std::swap(this->countPtr, nodePtr.countPtr);
00100     }
00101 
00102     inline bool NodePtr::operator==(const NodePtr & nodePtr) const
00103     {
00104         return (this->countPtr == nodePtr.countPtr);
00105     }
00106 }
00107 
00108 # endif