LibCore: Add CObject::for_each_child_of_type<T>()

Use this to iterate over all the GRadioButtons in a group.
This commit is contained in:
Andreas Kling 2019-05-27 04:18:24 +02:00
parent 0c85d3dba9
commit 3873c51781
Notes: sideshowbarker 2024-07-19 13:55:21 +09:00
3 changed files with 21 additions and 5 deletions

View file

@ -29,6 +29,8 @@ public:
}
}
template<typename T, typename Callback> 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<CObject*> m_children;
};
template<typename T> inline bool is(const CObject&) { return false; }
template<> inline bool is<CObject>(const CObject&) { return true; }
template<typename T> inline bool is(const CObject&) { return true; }
template<typename T>
inline T& to(CObject& object)
@ -75,3 +76,13 @@ inline const T& to(const CObject& object)
ASSERT(is<T>(object));
return static_cast<const T&>(object);
}
template<typename T, typename Callback>
inline void CObject::for_each_child_of_type(Callback callback)
{
for_each_child([&] (auto& child) {
if (is<T>(child))
return callback(to<T>(child));
return IterationDecision::Continue;
});
}

View file

@ -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<GRadioButton>([&] (auto& child) {
return callback(static_cast<GRadioButton&>(child));
});
}

View file

@ -20,3 +20,10 @@ private:
template<typename Callback> void for_each_in_group(Callback);
static Size circle_size();
};
template<> inline bool is<GRadioButton>(const CObject& object)
{
if (!is<GWidget>(object))
return false;
return to<GWidget>(object).is_radio_button();
}