Gio::ListModel get_object fails with interface classes
When using Gio::ListStore<T>
to store interfaces such as Gio::File
, get_item()
returns a NULL pointer unexpectedly. The underlying g_list_model_get_item
function works as expected and from debugging the problem seems to be a failed dynamic_cast in Glib::RefPtr<Object> wrap(GObject* object, bool take_copy)
whereby it tries to cast to a Glib::Object
which interfaces do not inherit from. They do however inherit from Glib::ObjectBase
which allows interfaces to pass the static_assert on T in Gio::ListStore<T>
.
From the glibmm docs it states for Gio::ListModel
that
All items in a ListModel are of (or derived from) the same type. get_item_type() returns that type. The type may be an interface, in which case all objects in the list must implement it.
so in theory an interfaces like Gio::File
should be supported.
I've included a minimal sample below and for some reason and I seem to encounter the same problem with 3.24 and 4.0.2.
#include <gtkmm.h>
int main()
{
Gio::init();
//This works
//auto val = Gio::FileInfo().dup();
//auto store = Gio::ListStore<Gio::FileInfo>::create();
// This doesn't
auto val = Gio::File::create_for_path("foo");
auto store = Gio::ListStore<Gio::File>::create();
store->append(val);
g_assert(store->get_n_items() == 1);
gpointer c_obj = g_list_model_get_item(G_LIST_MODEL(store->gobj()), 0);
g_assert(c_obj);
auto obj = store->get_object(0);
g_assert(obj);
}