observer_ptr
Go to the documentation of this file.
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef OSG_OBSERVER_PTR
#define OSG_OBSERVER_PTR
#include <osg/Notify>
#include <osg/ref_ptr>
#include <osg/Observer>
#include <OpenThreads/ScopedLock>
#include <OpenThreads/Mutex>
namespace osg {
template<class T>
class observer_ptr
{
public:
typedef T element_type;
observer_ptr() : _reference(0), _ptr(0) {}
observer_ptr(const ref_ptr<T>& rp)
{
_reference = rp.valid() ? rp->getOrCreateObserverSet() : 0;
_ptr = (_reference.valid() && _reference->getObserverdObject()!=0) ? rp.get() : 0;
}
observer_ptr(T* rp)
{
_reference = rp ? rp->getOrCreateObserverSet() : 0;
_ptr = (_reference.valid() && _reference->getObserverdObject()!=0) ? rp : 0;
}
observer_ptr(const observer_ptr& wp) :
_reference(wp._reference),
_ptr(wp._ptr)
{
}
~observer_ptr()
{
}
observer_ptr& operator = (const observer_ptr& wp)
{
if (&wp==this) return *this;
_reference = wp._reference;
_ptr = wp._ptr;
return *this;
}
observer_ptr& operator = (const ref_ptr<T>& rp)
{
_reference = rp.valid() ? rp->getOrCreateObserverSet() : 0;
_ptr = (_reference.valid() && _reference->getObserverdObject()!=0) ? rp.get() : 0;
return *this;
}
observer_ptr& operator = (T* rp)
{
_reference = rp ? rp->getOrCreateObserverSet() : 0;
_ptr = (_reference.valid() && _reference->getObserverdObject()!=0) ? rp : 0;
return *this;
}
bool lock(ref_ptr<T>& rptr) const
{
if (!_reference)
{
rptr = 0;
return false;
}
Referenced* obj = _reference->addRefLock();
if (!obj)
{
rptr = 0;
return false;
}
rptr = _ptr;
obj->unref_nodelete();
return rptr.valid();
}
bool operator == (const observer_ptr& wp) const { return _reference == wp._reference; }
bool operator != (const observer_ptr& wp) const { return _reference != wp._reference; }
bool operator < (const observer_ptr& wp) const { return _reference < wp._reference; }
bool operator > (const observer_ptr& wp) const { return wp._reference < _reference; }
// Non-strict interface, for compatibility
// comparison operator for const T*.
inline bool operator == (const T* ptr) const { return _ptr == ptr; }
inline bool operator != (const T* ptr) const { return _ptr != ptr; }
inline bool operator < (const T* ptr) const { return _ptr < ptr; }
inline bool operator > (const T* ptr) const { return _ptr > ptr; }
// Convenience methods for operating on object, however, access is not automatically threadsafe.
// To make thread safe, one should either ensure at a high level
// that the object will not be deleted while operating on it, or
// by using the observer_ptr<>::lock() to get a ref_ptr<> that
// ensures the objects stay alive throughout all access to it.
// Throw an error if _reference is null?
inline T& operator*() const { return *_ptr; }
inline T* operator->() const { return _ptr; }
// get the raw C pointer
inline T* get() const { return (_reference.valid() && _reference->getObserverdObject()!=0) ? _ptr : 0; }
inline bool operator!() const { return get() == 0; }
inline bool valid() const { return get() != 0; }
protected:
osg::ref_ptr<ObserverSet> _reference;
T* _ptr;
};
}
#endif
Mutex
Notify
Observer
ScopedLock
osg::Referenced
Definition Referenced:44
osg::Referenced::unref_nodelete
int unref_nodelete() const
osg::observer_ptr< object_type >::_ptr
object_type * _ptr
Definition observer_ptr:154
osg::observer_ptr::element_type
T element_type
Definition observer_ptr:41
osg::observer_ptr::operator=
observer_ptr & operator=(const observer_ptr &wp)
Definition observer_ptr:73
osg::observer_ptr< object_type >::_reference
osg::ref_ptr< ObserverSet > _reference
Definition observer_ptr:153
osg::observer_ptr::operator*
T & operator*() const
Definition observer_ptr:142
osg::observer_ptr::operator!
bool operator!() const
Definition observer_ptr:148
osg::observer_ptr::lock
bool lock(ref_ptr< T > &rptr) const
Definition observer_ptr:100
osg::observer_ptr::observer_ptr
observer_ptr(T *rp)
Definition observer_ptr:57
osg::observer_ptr::operator<
bool operator<(const observer_ptr &wp) const
Definition observer_ptr:125
osg::observer_ptr::observer_ptr
observer_ptr(const observer_ptr &wp)
Definition observer_ptr:63
osg::observer_ptr::valid
bool valid() const
Definition observer_ptr:149
osg::observer_ptr::observer_ptr
observer_ptr()
Definition observer_ptr:42
osg::observer_ptr::operator==
bool operator==(const observer_ptr &wp) const
Definition observer_ptr:123
osg::observer_ptr::~observer_ptr
~observer_ptr()
Definition observer_ptr:69
osg::observer_ptr::observer_ptr
observer_ptr(const ref_ptr< T > &rp)
Definition observer_ptr:47
osg::observer_ptr::operator!=
bool operator!=(const observer_ptr &wp) const
Definition observer_ptr:124
osg::observer_ptr::operator->
T * operator->() const
Definition observer_ptr:143
osg::observer_ptr::get
T * get() const
Definition observer_ptr:146
osg::observer_ptr::operator>
bool operator>(const observer_ptr &wp) const
Definition observer_ptr:126
osg::ref_ptr
Definition ref_ptr:32
osg::ref_ptr::get
T * get() const
Definition ref_ptr:117
osg::ref_ptr::valid
bool valid() const
Definition ref_ptr:120
osg
Definition AlphaFunc:19
ref_ptr