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

@ -394,7 +394,7 @@ MARK_AS_ADVANCED(OSG_DISABLE_MSVC_WARNINGS)
OPTION(OSG_PROVIDE_READFILE "Set to ON for include/osgDB/ReadFile to provide the osgDB::read*File(() methods. " ON)
OPTION(OSG_USE_REF_PTR_IMPLICIT_OUTPUT_CONVERSION "Set to ON to use the ref_ptr<> T* operator() output conversion. " ON)
OPTION(OSG_USE_REF_PTR_SAFE_DEREFERENCE "Set to ON to throw an exception whenever ref_ptr<> is dereferenced or called. " ON)
# Map the OPENGL_PROFILE to OSG_GL*_AVAILABLE settings
SET(OPENGL_PROFILE "GL2" CACHE STRING "OpenGL Profile to use, choose from GL1, GL2, GL3, GLES1, GLES2, GLES3")

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; }

View File

@ -29,6 +29,7 @@
#cmakedefine OSG_USE_FLOAT_BOUNDINGSPHERE
#cmakedefine OSG_USE_FLOAT_BOUNDINGBOX
#cmakedefine OSG_USE_REF_PTR_IMPLICIT_OUTPUT_CONVERSION
#cmakedefine OSG_USE_REF_PTR_SAFE_DEREFERENCE
#cmakedefine OSG_USE_UTF8_FILENAME
#cmakedefine OSG_DISABLE_MSVC_WARNINGS
#cmakedefine OSG_PROVIDE_READFILE