[glibmm] Gio::AsyncResult: Explain why wrap() is not used in get_source_object_base()



commit ee64cd329725218231e5b1bade01c998482f5da7
Author: Kjell Ahlstedt <kjellahlstedt gmail com>
Date:   Fri Jan 25 16:57:19 2019 +0100

    Gio::AsyncResult: Explain why wrap() is not used in get_source_object_base()
    
    * gio/src/asyncresult.ccg: Replace a TODO comment with an explanation.
    * tests/giomm_asyncresult_sourceobject/main.cc: Add code that demonstrates
    why Glib::wrap(GObject* object, bool take_copy) returns an empty RefPtr.

 gio/src/asyncresult.ccg                      |  6 +++++-
 tests/giomm_asyncresult_sourceobject/main.cc | 17 ++++++++++++++++-
 2 files changed, 21 insertions(+), 2 deletions(-)
---
diff --git a/gio/src/asyncresult.ccg b/gio/src/asyncresult.ccg
index 8f1a0128..5570bbaf 100644
--- a/gio/src/asyncresult.ccg
+++ b/gio/src/asyncresult.ccg
@@ -30,11 +30,15 @@ unwrap_objectbase_custom(const Glib::RefPtr<Glib::ObjectBase>& cpp_instance)
 Glib::RefPtr<Glib::ObjectBase>
 AsyncResult::get_source_object_base()
 {
+  // Glib::wrap(cobj) can't be used here. See tests/giomm_asyncresult_sourceobject
+  // for a case where it would fail, and an explanation of why.
+  // In short, the source object is not necessarily a Glib::Object. It may be
+  // a Glib::Interface.
+
   auto cobj = g_async_result_get_source_object(gobj());
   auto cppobj = Glib::wrap_auto(cobj); // ObjectBase::_get_current_wrapper(cobj);
   return Glib::make_refptr_for_instance<Glib::ObjectBase>(
     cppobj); // g_async_result_get_source_object() gives us a ref, unusually.
-  // TODO: For some reason this fails: Glib::wrap(cobj);
 }
 
 Glib::RefPtr<const Glib::ObjectBase>
diff --git a/tests/giomm_asyncresult_sourceobject/main.cc b/tests/giomm_asyncresult_sourceobject/main.cc
index cf2b01be..6e71a755 100644
--- a/tests/giomm_asyncresult_sourceobject/main.cc
+++ b/tests/giomm_asyncresult_sourceobject/main.cc
@@ -1,5 +1,6 @@
 #include <giomm.h>
 #include <iostream>
+#include <typeinfo>
 
 void
 on_read_async(const Glib::RefPtr<Gio::AsyncResult>& result)
@@ -10,12 +11,26 @@ on_read_async(const Glib::RefPtr<Gio::AsyncResult>& result)
     exit(EXIT_FAILURE);
   }
 
-  if (!g_async_result_get_source_object(result->gobj()))
+  auto cobj = g_async_result_get_source_object(result->gobj());
+  if (!cobj)
   {
     std::cerr << G_STRFUNC << ": g_async_result_get_source_object() failed." << std::endl;
     exit(EXIT_FAILURE);
   }
 
+  // Show why Glib::wrap(cobj) can't be used in Gio::AsyncResult::get_source_object_base().
+  // cppobjbase is not a Glib::Object*, it's a Gio::File* which is a Glib::Interface*.
+  std::cout << "GType name: " << G_OBJECT_TYPE_NAME(cobj) << std::endl;
+  auto cppobjbase = Glib::wrap_auto(cobj); // Glib::ObjectBase::_get_current_wrapper(cobj);
+  if (cppobjbase)
+  {
+    std::cout << "C++ type name: " << typeid(*cppobjbase).name() << std::endl;
+    auto cppobj = dynamic_cast<Glib::Object*>(cppobjbase); // Part of Glib::wrap(GObject*, bool)
+    auto cppiface = dynamic_cast<Glib::Interface*>(cppobjbase);
+    std::cout << "dynamic_cast<Glib::Object*>: " << cppobj << std::endl;
+    std::cout << "dynamic_cast<Glib::Interface*>: " << cppiface << std::endl;
+  }
+
   if (!result->get_source_object_base())
   {
     std::cerr << G_STRFUNC << ": result->get_source_object_base() failed." << std::endl;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]