add safety checking when dereferencing ref_ptr

This commit is contained in:
rdiankov
2016-05-21 21:29:14 +02:00
committed by Ferdinand Thiessen
parent ae3ba28fee
commit da34da18ca
3 changed files with 27 additions and 1 deletions

View File

@@ -15,6 +15,10 @@
#define OSG_REF_PTR 1
#include <osg/Config>
#ifdef OSG_USE_REF_PTR_SAFE_DEREFERENCE
#include <typeinfo>
#include <stdexcept>
#endif
namespace osg {
@@ -87,6 +91,27 @@ class ref_ptr
operator unspecified_bool_type() const { return valid()? &ref_ptr::_ptr : 0; }
#endif
T& operator*() const
{
#ifdef OSG_USE_REF_PTR_SAFE_DEREFERENCE
if( !_ptr ) {
// pointer is invalid, so throw an exception
throw std::runtime_error(std::string("could not dereference invalid osg pointer ") + std::string(typeid(T).name()));
}
#endif
return *_ptr;
}
T* operator->() const
{
#ifdef OSG_USE_REF_PTR_SAFE_DEREFERENCE
if( !_ptr ) {
// pointer is invalid, so throw an exception.
throw std::runtime_error(std::string("could not call invalid osg pointer ") + std::string(typeid(T).name()));
}
#endif
return _ptr;
}
T& operator*() const { return *_ptr; }
T* operator->() const { return _ptr; }
T* get() const { return _ptr; }