C++: fix crash when accessing empty model from Slint

Fixes #3704
This commit is contained in:
Olivier Goffart 2023-10-19 16:44:42 +02:00 committed by Olivier Goffart
parent 403bb6c716
commit 172dcfa775
4 changed files with 17 additions and 3 deletions

View file

@ -341,13 +341,25 @@ using ModelPeer = std::weak_ptr<ModelChangeListener>;
template<typename M>
auto access_array_index(const M &model, size_t index)
{
if (const auto v = model->row_data_tracked(index)) {
if (!model) {
return decltype(*model->row_data_tracked(index)) {};
} else if (const auto v = model->row_data_tracked(index)) {
return *v;
} else {
return decltype(*v) {};
}
}
template<typename M>
long int model_length(const M &model) {
if (!model) {
return 0;
} else {
model->track_row_count_changes();
return model->row_count();
}
}
} // namespace private_api
/// \rst