add safety checking when dereferencing ref_ptr
This commit is contained in:
parent
ae3ba28fee
commit
da34da18ca
@ -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")
|
||||
|
@ -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; }
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user