From e5424da39f95898f1bcb628c87502fd3e462486f Mon Sep 17 00:00:00 2001 From: Kjell Ahlstedt Date: Wed, 21 Mar 2018 10:13:20 +0100 Subject: Glib::RefPtr: Add get() Analogous to std::shared_ptr::get(). Bug 495762 --- glib/glibmm/refptr.h | 16 ++++++++++++++-- tests/glibmm_refptr/main.cc | 5 +++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/glib/glibmm/refptr.h b/glib/glibmm/refptr.h index 981dd796..74ed0413 100644 --- a/glib/glibmm/refptr.h +++ b/glib/glibmm/refptr.h @@ -42,7 +42,7 @@ namespace Glib * RefPtr<> can store any class that has reference() and unreference() methods, * and whose destructor is noexcept (the default for destructors). * In gtkmm, that is anything derived from Glib::ObjectBase, such as - * Gdk::Pixmap. + * Gdk::Pixbuf. * * See the "Memory Management" section in the "Programming with gtkmm" * book for further information. @@ -136,7 +136,7 @@ public: template inline RefPtr& operator=(RefPtr&& src) noexcept; - /** Copy from different, but castable type). + /** Copy from different, but castable type. * * Increments the reference count. */ @@ -156,6 +156,12 @@ public: */ inline T_CppObject* operator->() const noexcept; + /** Returns the stored pointer. + * + * @newin{2,56} + */ + inline T_CppObject* get() const noexcept; + /** Test whether the RefPtr<> points to any underlying instance. * * Mimics usage of ordinary pointers: @@ -395,6 +401,12 @@ RefPtr::operator!=(const RefPtr& src) const noexcept return (pCppObject_ != src.pCppObject_); } +template +inline T_CppObject* RefPtr::get() const noexcept +{ + return pCppObject_; +} + template inline RefPtr::operator bool() const noexcept { diff --git a/tests/glibmm_refptr/main.cc b/tests/glibmm_refptr/main.cc index 53a74492..4caea0c5 100644 --- a/tests/glibmm_refptr/main.cc +++ b/tests/glibmm_refptr/main.cc @@ -91,6 +91,11 @@ test_initial_refcount() Glib::RefPtr refSomething(new Something()); g_assert_cmpint(refSomething->ref_count(), ==, 1); g_assert_cmpint(refSomething->max_ref_count(), ==, 1); + + // Test the get() method: + g_assert_cmpint(refSomething.get()->ref_count(), ==, 1); + refSomething.reset(); + g_assert(refSomething.get() == nullptr); } static void -- cgit v1.2.1