ref_ptr.h

Go to the documentation of this file.
00001 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield 
00002  *
00003  * This library is open source and may be redistributed and/or modified under  
00004  * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 
00005  * (at your option) any later version.  The full license is in LICENSE file
00006  * included with this distribution, and on the openscenegraph.org website.
00007  * 
00008  * This library is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
00011  * OpenSceneGraph Public License for more details.
00012 */
00013 
00014 #ifndef OPENALPP_REF_PTR
00015 #define OPENALPP_REF_PTR 1
00016 
00017 namespace openalpp {
00018 
00020 template<class T>
00021 class ref_ptr
00022 {
00023 
00024     public:
00025         typedef T element_type;
00026 
00027         ref_ptr() :_ptr(0L) {}
00028         ref_ptr(T* t):_ptr(t)              { if (_ptr) _ptr->ref(); }
00029         ref_ptr(const ref_ptr& rp):_ptr(rp._ptr)  { if (_ptr) _ptr->ref(); }
00030         ~ref_ptr()                           { if (_ptr) _ptr->unref(); _ptr=0; }
00031 
00032         inline ref_ptr& operator = (const ref_ptr& rp)
00033         {
00034             if (_ptr==rp._ptr) return *this;
00035             T* tmp_ptr = _ptr;
00036             _ptr = rp._ptr;
00037             if (_ptr) _ptr->ref();
00038             // unref second to prevent any deletion of any object which might
00039             // be referenced by the other object. i.e rp is child of the
00040             // original _ptr.
00041             if (tmp_ptr) tmp_ptr->unref();
00042             return *this;
00043         }
00044 
00045         inline ref_ptr& operator = (T* ptr)
00046         {
00047             if (_ptr==ptr) return *this;
00048             T* tmp_ptr = _ptr;
00049             _ptr = ptr;
00050             if (_ptr) _ptr->ref();
00051             // unref second to prevent any deletion of any object which might
00052             // be referenced by the other object. i.e rp is child of the
00053             // original _ptr.
00054             if (tmp_ptr) tmp_ptr->unref();
00055             return *this;
00056         }
00057 
00058         // comparison operators for ref_ptr.
00059         inline bool operator == (const ref_ptr& rp) const { return (_ptr==rp._ptr); }
00060         inline bool operator != (const ref_ptr& rp) const { return (_ptr!=rp._ptr); }
00061         inline bool operator < (const ref_ptr& rp) const { return (_ptr<rp._ptr); }
00062         inline bool operator > (const ref_ptr& rp) const { return (_ptr>rp._ptr); }
00063 
00064         // comparison operator for const T*.
00065         inline bool operator == (const T* ptr) const { return (_ptr==ptr); }
00066         inline bool operator != (const T* ptr) const { return (_ptr!=ptr); }
00067         inline bool operator < (const T* ptr) const { return (_ptr<ptr); }
00068         inline bool operator > (const T* ptr) const { return (_ptr>ptr); }
00069 
00070 
00071         inline T& operator*()  { return *_ptr; }
00072 
00073         inline const T& operator*() const { return *_ptr; }
00074 
00075         inline T* operator->() { return _ptr; }
00076 
00077         inline const T* operator->() const   { return _ptr; }
00078 
00079         inline bool operator!() const   { return _ptr==0L; }
00080 
00081         inline bool valid() const       { return _ptr!=0L; }
00082         
00083         inline T* get() { return _ptr; }
00084 
00085         inline const T* get() const { return _ptr; }
00086 
00091         inline T* take() { return release();}
00092 
00093         inline T* release() { T* tmp=_ptr; if (_ptr) _ptr->unref_nodelete(); _ptr=0; return tmp;}
00094 
00095     private:
00096         T* _ptr;
00097 };
00098 
00099 }
00100 
00101 #endif

Generated on Thu May 18 00:49:37 2006 for openalpp by  doxygen 1.4.6