Commit ee64cd32 authored by Kjell Ahlstedt's avatar Kjell Ahlstedt

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.
parent 96a997b1
......@@ -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>
......
#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;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment