diff options
author | Kjell Ahlstedt <kjellahlstedt@gmail.com> | 2022-08-29 16:13:32 +0200 |
---|---|---|
committer | Kjell Ahlstedt <kjellahlstedt@gmail.com> | 2022-08-29 16:13:32 +0200 |
commit | d0d08f9d826517fd2e02ae6300732faefe072f9d (patch) | |
tree | 11db1249dec5afec6c362a8d7a1a967b0139754e /gio/src/liststore.ccg | |
parent | 818d7a22708ddc225b6363cd845e6cd72481028d (diff) | |
download | glibmm-d0d08f9d826517fd2e02ae6300732faefe072f9d.tar.gz |
Gio::ListStore: Add find()
* gio/src/liststore.[ccg|hg]: Add two ListStoreBase::find() and
two ListStore::find().
* tests/giomm_listmodel/main.cc: Test ListStore::find().
Diffstat (limited to 'gio/src/liststore.ccg')
-rw-r--r-- | gio/src/liststore.ccg | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/gio/src/liststore.ccg b/gio/src/liststore.ccg index 3b2191d9..9c95e7ef 100644 --- a/gio/src/liststore.ccg +++ b/gio/src/liststore.ccg @@ -32,6 +32,19 @@ int ListStoreBase_CompareDataFunc(gconstpointer a, gconstpointer b, gpointer use return (*slot)(item_a, item_b); } + +// gboolean is int +gboolean ListStoreBase_EqualFuncFull(gconstpointer a, gconstpointer b, gpointer user_data) +{ + auto slot = static_cast<Gio::ListStoreBase::SlotEqual*>(user_data); + + const Glib::RefPtr<const Glib::ObjectBase> item_a = + Glib::wrap(static_cast<Glib::Object::BaseObjectType*>(const_cast<gpointer>(a)), true); + const Glib::RefPtr<const Glib::ObjectBase> item_b = + Glib::wrap(static_cast<Glib::Object::BaseObjectType*>(const_cast<gpointer>(b)), true); + + return (*slot)(item_a, item_b); +} } } // anonymous namespace @@ -49,4 +62,25 @@ void ListStoreBase::splice(guint position, guint n_removals, g_list_store_splice(gobj(), position, n_removals, g_additions.get(), n_additions); } +std::pair<bool, unsigned int> ListStoreBase::find(const Glib::RefPtr<const Glib::ObjectBase>& item) const +{ + unsigned int position = std::numeric_limits<unsigned int>::max(); + bool result = g_list_store_find(const_cast<GListStore*>(gobj()), + const_cast<GObject*>(item->gobj()), &position); + return {result, position}; +} + +std::pair<bool, unsigned int> ListStoreBase::find( + const Glib::RefPtr<const Glib::ObjectBase>& item, const SlotEqual& slot) const +{ + // Use the original slot (not a copy). + auto slot_ptr = const_cast<SlotEqual*>(&slot); + + unsigned int position = std::numeric_limits<unsigned int>::max(); + bool result = g_list_store_find_with_equal_func_full( + const_cast<GListStore*>(gobj()), const_cast<GObject*>(item->gobj()), + &ListStoreBase_EqualFuncFull, slot_ptr, &position); + return {result, position}; +} + } // namespace Gio |