Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qrangemodeladapter_impl.h
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QRANGEMODELADAPTER_IMPL_H
6#define QRANGEMODELADAPTER_IMPL_H
7
8#ifndef Q_QDOC
9
10#ifndef QRANGEMODELADAPTER_H
11#error Do not include qrangemodeladapter_impl.h directly
12#endif
13
14#if 0
15#pragma qt_sync_skip_header_check
16#pragma qt_sync_stop_processing
17#endif
18
19#include <QtCore/qrangemodel.h>
20#include <QtCore/qspan.h>
21#include <set>
22#include <memory>
23
24QT_BEGIN_NAMESPACE
25
26namespace QRangeModelDetails
27{
28template <typename Range, typename Protocol>
33 >,
35 >;
36
37template <typename ...Args>
38using construct_rangeModel_test = decltype(QRangeModel(std::declval<Args &&>()...));
39
40template <typename ...Args>
42 Args...>;
43
44template <typename ...Args>
46
47template <typename Output, typename Input>
48decltype(auto) forwardOrConvert(Input&& input)
49{
50 if constexpr (std::is_same_v<q20::remove_cvref<Output>, q20::remove_cvref<Input>>)
51 return std::forward<Input>(input);
52 else
53 return Output(std::forward<Input>(input));
54}
55
56// we can't use wrapped_t, we only want to unpack smart pointers, and maintain
57// the pointer nature of the type.
58template <typename T, typename = void>
59struct data_type { using type = T; };
60template <>
61struct data_type<void> { using type = QVariant; };
62
63// pointer types of iterators use QtPrivate::ArrowProxy if the type does not
64// provide operator->() (or is a pointer).
65template <typename T, typename = void> struct test_pointerAccess : std::false_type {};
66template <typename T> struct test_pointerAccess<T *> : std::true_type {};
67template <typename T>
68struct test_pointerAccess<T, std::void_t<decltype(std::declval<T>().operator->())>>
69 : std::true_type
70{};
71
72template <typename T>
75
76// Helpers to make a type const "in depth", taking into account raw pointers
77// and wrapping types, like smart pointers and std::reference_wrapper.
78
79// We need to return data by value, not by reference, as we might only have
80// temporary values (i.e. a QVariant returned by QAIM::data).
81template <typename T, typename = void> struct AsConstData { using type = T; };
82template <typename T> struct AsConstData<const T &> { using type = T; };
83template <typename T> struct AsConstData<T *> { using type = const T *; };
84template <template <typename> typename U, typename T>
86{ using type = U<const T>; };
87template <typename T> struct AsConstData<std::reference_wrapper<T>>
88{ using type = std::reference_wrapper<const T>; };
89
90template <typename T> using asConst_t = typename AsConstData<T>::type;
91
92// Rows get wrapped into a "view", as a begin/end iterator/sentinel pair.
93// The iterator dereferences to the const version of the value returned by
94// the underlying iterator.
95// Could be replaced with std::views::sub_range in C++ 20.
96template <typename const_row_type, typename Iterator, typename Sentinel>
97struct RowView
98{
99 // this is similar to C++23's std::basic_const_iterator, but we don't want
100 // to convert to the underlying const_iterator.
101 struct iterator
102 {
104 using difference_type = typename Iterator::difference_type;
108 using iterator_category = typename std::iterator_traits<Iterator>::iterator_category;
109
110 template <typename I, typename Category>
111 static constexpr bool is_atLeast = std::is_base_of_v<Category,
113 template <typename I, typename Category>
115
116 reference operator*() const { return *m_it; }
117 pointer operator->() const { return operator*(); }
118
119 // QRM requires at least forward_iterator, so we provide both post- and
120 // prefix increment unconditionally
121 friend constexpr iterator &operator++(iterator &it)
122 noexcept(noexcept(++std::declval<Iterator&>()))
123 {
124 ++it.m_it;
125 return it;
126 }
127 friend constexpr iterator operator++(iterator &it, int)
128 noexcept(noexcept(std::declval<Iterator&>()++))
129 {
130 iterator copy = it;
131 ++copy.m_it;
132 return copy;
133 }
134
135 template <typename I = Iterator, if_atLeast<I, std::bidirectional_iterator_tag> = true>
136 friend constexpr iterator &operator--(iterator &it)
137 noexcept(noexcept(--std::declval<I&>()))
138 {
139 --it.m_it;
140 return it;
141 }
142 template <typename I = Iterator, if_atLeast<I, std::bidirectional_iterator_tag> = true>
143 friend constexpr iterator operator--(iterator &it, int)
144 noexcept(noexcept(std::declval<I&>()--))
145 {
146 iterator copy = it;
147 --it.m_it;
148 return copy;
149 }
150
151 template <typename I = Iterator, if_atLeast<I, std::random_access_iterator_tag> = true>
152 friend constexpr iterator &operator+=(iterator &it, difference_type n)
153 noexcept(noexcept(std::declval<I&>() += 1))
154 {
155 it.m_it += n;
156 return it;
157 }
158 template <typename I = Iterator, if_atLeast<I, std::random_access_iterator_tag> = true>
159 friend constexpr iterator &operator-=(iterator &it, difference_type n)
160 noexcept(noexcept(std::declval<I&>() -= 1))
161 {
162 it.m_it -= n;
163 return it;
164 }
165
166 template <typename I = Iterator, if_atLeast<I, std::random_access_iterator_tag> = true>
167 friend constexpr iterator operator+(const iterator &it, difference_type n)
168 noexcept(noexcept(std::declval<I&>() + 1))
169 {
170 iterator copy = it;
171 copy.m_it += n;
172 return copy;
173 }
174 template <typename I = Iterator, if_atLeast<I, std::random_access_iterator_tag> = true>
175 friend constexpr iterator operator+(difference_type n, const iterator &it)
176 noexcept(noexcept(1 + std::declval<I&>()))
177 {
178 return it + n;
179 }
180 template <typename I = Iterator, if_atLeast<I, std::random_access_iterator_tag> = true>
181 friend constexpr iterator operator-(const iterator &it, difference_type n)
182 noexcept(noexcept(std::declval<I&>() - 1))
183 {
184 iterator copy = it;
185 copy.m_it = it.m_it - n;
186 return copy;
187 }
188
189 template <typename I = Iterator, if_atLeast<I, std::random_access_iterator_tag> = true>
190 constexpr reference operator[](difference_type n) const
191 noexcept(noexcept(I::operator[]()))
192 {
193 return m_it[n];
194 }
195
196 template <typename I = Iterator, if_atLeast<I, std::random_access_iterator_tag> = true>
197 friend constexpr difference_type operator-(const iterator &lhs, const iterator &rhs)
198 noexcept(noexcept(std::declval<I&>() - std::declval<I&>()))
199 {
200 return lhs.m_it - rhs.m_it;
201 }
202
203 template <typename I = Iterator, if_atLeast<I, std::random_access_iterator_tag> = true>
204 friend constexpr bool operator<(const iterator &lhs, const iterator &rhs)
205 noexcept(noexcept(std::declval<I&>() < std::declval<I&>()))
206 {
207 return lhs.m_it < rhs.m_it;
208 }
209 template <typename I = Iterator, if_atLeast<I, std::random_access_iterator_tag> = true>
210 friend constexpr bool operator<=(const iterator &lhs, const iterator &rhs)
211 noexcept(noexcept(std::declval<I&>() <= std::declval<I&>()))
212 {
213 return lhs.m_it <= rhs.m_it;
214 }
215
216 template <typename I = Iterator, if_atLeast<I, std::random_access_iterator_tag> = true>
217 friend constexpr bool operator>(const iterator &lhs, const iterator &rhs)
218 noexcept(noexcept(std::declval<I&>() > std::declval<I&>()))
219 {
220 return lhs.m_it > rhs.m_it;
221 }
222 template <typename I = Iterator, if_atLeast<I, std::random_access_iterator_tag> = true>
223 friend constexpr bool operator>=(const iterator &lhs, const iterator &rhs)
224 noexcept(noexcept(std::declval<I&>() >= std::declval<I&>()))
225 {
226 return lhs.m_it >= rhs.m_it;
227 }
228
229 // This would implement the P2836R1 fix from std::basic_const_iterator,
230 // but a const_iterator on a range<pointer> would again allow us to
231 // mutate the pointed-to object, which is exactly what we want to
232 // prevent.
233 /*
234 template <typename CI, std::enable_if_t<std::is_convertible_v<const Iterator &, CI>, bool> = true>
235 operator CI() const
236 {
237 return CI{m_it};
238 }
239
240 template <typename CI, std::enable_if_t<std::is_convertible_v<Iterator, CI>, bool> = true>
241 operator CI() &&
242 {
243 return CI{std::move(m_it)};
244 }
245 */
246
247 friend bool comparesEqual(const iterator &lhs, const iterator &rhs) noexcept
248 {
249 return lhs.m_it == rhs.m_it;
250 }
252
253 Iterator m_it;
254 };
255
256 using value_type = typename iterator::value_type;
258
259 RowView() = default;
260 RowView(Iterator begin, Sentinel end)
261 : m_begin(std::move(begin)), m_end(std::move(end))
262 {}
263 RowView(const RowView &) = default;
264 RowView(RowView &&) = default;
265 RowView &operator=(const RowView &) = default;
266 RowView &operator=(RowView &&) = default;
267 ~RowView() = default;
268
269 template <typename Row>
272 std::is_convertible<decltype(QRangeModelDetails::begin(std::declval<const Row &>())),
273 Iterator>,
274 std::is_convertible<decltype(QRangeModelDetails::end(std::declval<const Row &>())),
275 Sentinel>
276 >, bool>;
277
278 // A view of a row is implicitly constructible from the row; this makes the
279 // view the common reference type of the row type and the row reference
280 // type of QRangeModelAdapter's iterators.
281 template <typename Row, if_viewable_row<Row> = true>
285 // The view would dangle if constructed from a temporary row.
286 template <typename Row, if_viewable_row<Row> = true>
287 RowView(const Row &&row) = delete;
288
289 friend bool comparesEqual(const RowView &lhs, const RowView &rhs) noexcept
290 {
291 return lhs.m_begin == rhs.m_begin;
292 }
294
295 template <typename RHS>
296 bool operator==(const RHS &rhs) const noexcept
297 {
299 }
300 template <typename RHS>
301 bool operator!=(const RHS &rhs) const noexcept
302 {
303 return !operator==(rhs);
304 }
305
307
308 iterator begin() const { return iterator{m_begin}; }
309 iterator end() const { return iterator{m_end}; }
310
312 Sentinel m_end;
313};
314
315// Const-in-depth mapping for row types. We do store row types, and they might
316// be move-only, so we return them by const reference.
317template <typename T, typename = void> struct AsConstRow { using type = const T &; };
318// Otherwise the mapping for basic row types is the same as for data.
319template <typename T> struct AsConstRow<T *> : AsConstData<T *> {};
320template <template <typename> typename U, typename T>
322template <typename T> struct AsConstRow<std::reference_wrapper<T>>
324
325template <typename T> using if_range = std::enable_if_t<is_range_v<T>, bool>;
326// If the row type is a range, then we assume that the first type is the
327// element type.
328template <template <typename, typename ...> typename R, typename T, typename ...Args>
329struct AsConstRow<R<T, Args...>, if_range<R<T, Args...>>>
330{
331 using type = const R<T, Args...> &;
332};
333
334// specialize for range of pointers and smart pointers
335template <template <typename, typename ...> typename R, typename T, typename ...Args>
343
344template <template <typename, typename ...> typename R, typename T, typename ...Args>
354
355template <typename T>
356using asConstRow_t = typename AsConstRow<T>::type;
357
358Q_CORE_EXPORT QVariant qVariantAtIndex(const QModelIndex &index);
359
360template <typename Type>
361static inline Type dataAtIndex(const QModelIndex &index)
362{
363 Q_ASSERT_X(index.isValid(), "QRangeModelAdapter::dataAtIndex", "Index at position is invalid");
364 QVariant variant = qVariantAtIndex(index);
365
366 if constexpr (std::is_same_v<QVariant, Type>)
367 return variant;
368 else
369 return variant.value<Type>();
370}
371
372template <typename Type>
373static inline Type dataAtIndex(const QModelIndex &index, int role)
374{
375 Q_ASSERT_X(index.isValid(), "QRangeModelAdapter::dataAtIndex", "Index at position is invalid");
376 QVariant variant = index.data(role);
377
378 if constexpr (std::is_same_v<QVariant, Type>)
379 return variant;
380 else
381 return variant.value<Type>();
382}
383
384// Whether It is a forward iterator that dereferences to something convertible
385// to Reference. SFINAE-friendly for types that are not iterators, e.g. when
386// called with insert(pos, int, int).
387template <typename It, typename Reference, typename = void>
389
390template <typename It, typename Reference>
400
401// Whether the container C has assign(size_type, const value_type &)
402template <typename C, typename = void>
404
405template <typename C>
406struct has_assign_count<C, std::void_t<decltype(std::declval<C &>().assign(
407 std::declval<typename C::size_type>(),
408 std::declval<const typename C::value_type &>()))>>
409 : std::true_type
410{};
411
412// A minimal range over an iterator pair, so that we can reuse the helpers
413// that operate on ranges.
414template <typename It>
416{
419 It begin() const { return first; }
420 It end() const { return last; }
421 std::size_t size() const { return std::size_t(std::distance(first, last)); }
422};
423
424template <bool isTree = false>
426{
427 ParentIndex(const QModelIndex &dummy = {}) { Q_ASSERT(!dummy.isValid()); }
428 QModelIndex root() const { return {}; }
429};
430
431template <>
432struct ParentIndex<true>
433{
435 QModelIndex root() const { return m_rootIndex; }
436};
437
438template <typename Model, typename Impl>
439struct AdapterStorage : ParentIndex<Impl::protocol_traits::is_tree>
440{
441 // If it is, then we can shortcut the model and operate on the container.
442 // Otherwise we have to go through the model's vtable. For now, this is always
443 // the case.
444 static constexpr bool isRangeModel = std::is_same_v<Model, QRangeModel>;
445 static_assert(isRangeModel, "The model must be a QRangeModel (not a subclass).");
447
448 template <typename I = Impl, std::enable_if_t<I::protocol_traits::is_tree, bool> = true>
449 explicit AdapterStorage(const std::shared_ptr<QRangeModel> &model, const QModelIndex &root)
450 : ParentIndex<Impl::protocol_traits::is_tree>{root}, m_model(model)
451 {
452 }
453
454 explicit AdapterStorage(Model *model)
455 : m_model{model}
456 {}
457
458 const Impl *implementation() const
459 {
460 return static_cast<const Impl *>(QRangeModelImplBase::getImplementation(m_model.get()));
461 }
462
464 {
465 return static_cast<Impl *>(QRangeModelImplBase::getImplementation(m_model.get()));
466 }
467
468 auto *operator->()
469 {
470 if constexpr (isRangeModel)
471 return implementation();
472 else
473 return m_model.get();
474 }
475
476 const auto *operator->() const
477 {
478 if constexpr (isRangeModel)
479 return implementation();
480 else
481 return m_model.get();
482 }
483};
484
485} // QRangeModelDetails
486
487QT_END_NAMESPACE
488
489#endif // Q_QDOC
490
491#endif // QRANGEMODELADAPTER_IMPL_H
static Type dataAtIndex(const QModelIndex &index, int role)
static Type dataAtIndex(const QModelIndex &index)
decltype(auto) forwardOrConvert(Input &&input)
static constexpr bool can_construct_rangeModel
decltype(QRangeModel(std::declval< Args && >()...)) construct_rangeModel_test
std::shared_ptr< QRangeModel > m_model
AdapterStorage(const std::shared_ptr< QRangeModel > &model, const QModelIndex &root)
ParentIndex(const QModelIndex &dummy={})
friend constexpr iterator & operator-=(iterator &it, difference_type n) noexcept(noexcept(std::declval< I & >() -=1))
friend constexpr iterator operator+(const iterator &it, difference_type n) noexcept(noexcept(std::declval< I & >()+1))
friend constexpr iterator & operator+=(iterator &it, difference_type n) noexcept(noexcept(std::declval< I & >()+=1))
friend constexpr iterator operator-(const iterator &it, difference_type n) noexcept(noexcept(std::declval< I & >() - 1))
friend constexpr iterator operator+(difference_type n, const iterator &it) noexcept(noexcept(1+std::declval< I & >()))
friend constexpr difference_type operator-(const iterator &lhs, const iterator &rhs) noexcept(noexcept(std::declval< I & >() - std::declval< I & >()))
friend constexpr bool operator<=(const iterator &lhs, const iterator &rhs) noexcept(noexcept(std::declval< I & >()<=std::declval< I & >()))
friend constexpr bool operator>(const iterator &lhs, const iterator &rhs) noexcept(noexcept(std::declval< I & >() > std::declval< I & >()))
friend constexpr iterator & operator++(iterator &it) noexcept(noexcept(++std::declval< Iterator & >()))
friend constexpr bool operator<(const iterator &lhs, const iterator &rhs) noexcept(noexcept(std::declval< I & >()< std::declval< I & >()))
typename std::iterator_traits< Iterator >::iterator_category iterator_category
friend constexpr iterator operator++(iterator &it, int) noexcept(noexcept(std::declval< Iterator & >()++))
friend constexpr iterator & operator--(iterator &it) noexcept(noexcept(--std::declval< I & >()))
friend bool comparesEqual(const iterator &lhs, const iterator &rhs) noexcept
typename Iterator::difference_type difference_type
friend constexpr bool operator>=(const iterator &lhs, const iterator &rhs) noexcept(noexcept(std::declval< I & >() >=std::declval< I & >()))
friend constexpr iterator operator--(iterator &it, int) noexcept(noexcept(std::declval< I & >() --))
constexpr reference operator[](difference_type n) const noexcept(noexcept(I::operator[]()))
typename iterator::difference_type difference_type
RowView(const Row &&row)=delete
RowView(const RowView &)=default
RowView(RowView &&)=default
RowView(Iterator begin, Sentinel end)
typename iterator::value_type value_type
friend bool comparesEqual(const RowView &lhs, const RowView &rhs) noexcept
RowView & operator=(const RowView &)=default
RowView & operator=(RowView &&)=default