From 3873c51781e453b86e9b97495172713f24adfabb Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 27 May 2019 04:18:24 +0200 Subject: [PATCH] LibCore: Add CObject::for_each_child_of_type() Use this to iterate over all the GRadioButtons in a group. --- LibCore/CObject.h | 15 +++++++++++++-- LibGUI/GRadioButton.cpp | 4 +--- LibGUI/GRadioButton.h | 7 +++++++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/LibCore/CObject.h b/LibCore/CObject.h index 92fe544edd9..a4eac52eec2 100644 --- a/LibCore/CObject.h +++ b/LibCore/CObject.h @@ -29,6 +29,8 @@ public: } } + template void for_each_child_of_type(Callback callback); + CObject* parent() { return m_parent; } const CObject* parent() const { return m_parent; } @@ -59,8 +61,7 @@ private: Vector m_children; }; -template inline bool is(const CObject&) { return false; } -template<> inline bool is(const CObject&) { return true; } +template inline bool is(const CObject&) { return true; } template inline T& to(CObject& object) @@ -75,3 +76,13 @@ inline const T& to(const CObject& object) ASSERT(is(object)); return static_cast(object); } + +template +inline void CObject::for_each_child_of_type(Callback callback) +{ + for_each_child([&] (auto& child) { + if (is(child)) + return callback(to(child)); + return IterationDecision::Continue; + }); +} diff --git a/LibGUI/GRadioButton.cpp b/LibGUI/GRadioButton.cpp index 773bfd2369f..afcb2d9c32c 100644 --- a/LibGUI/GRadioButton.cpp +++ b/LibGUI/GRadioButton.cpp @@ -55,9 +55,7 @@ void GRadioButton::for_each_in_group(Callback callback) { if (!parent()) return; - for_each_child_widget([&] (auto& child) { - if (!child.is_radio_button()) - return IterationDecision::Continue; + parent()->for_each_child_of_type([&] (auto& child) { return callback(static_cast(child)); }); } diff --git a/LibGUI/GRadioButton.h b/LibGUI/GRadioButton.h index 21496b8c954..95897f5b47e 100644 --- a/LibGUI/GRadioButton.h +++ b/LibGUI/GRadioButton.h @@ -20,3 +20,10 @@ private: template void for_each_in_group(Callback); static Size circle_size(); }; + +template<> inline bool is(const CObject& object) +{ + if (!is(object)) + return false; + return to(object).is_radio_button(); +}