diff --git a/CMakeLists.txt b/CMakeLists.txt index a9e912518..9cfa9d2ba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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") diff --git a/include/osg/ref_ptr b/include/osg/ref_ptr index f80b3bbd9..aff5f8311 100644 --- a/include/osg/ref_ptr +++ b/include/osg/ref_ptr @@ -15,6 +15,10 @@ #define OSG_REF_PTR 1 #include +#ifdef OSG_USE_REF_PTR_SAFE_DEREFERENCE +#include +#include +#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; } diff --git a/src/osg/Config.in b/src/osg/Config.in index 245af70bb..5411cf7c8 100644 --- a/src/osg/Config.in +++ b/src/osg/Config.in @@ -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