Make XmlStringRef convertible to QAnyStringView/QStringView

Both QStringView and QAnyStringView implicitly convert from any
container with a fitting value_type, and working std::data, std::size,
std::begin and std::end.

Add these missing operations (and complementary ones) to XmlStringRef,
so it implicitly converts to QStringView and QAnyStringView, too.

Add a check to that effect and remove the now-superfluous operator
QStringView().

Task-number: QTBUG-103302
Change-Id: I89d586cf64447a82022e06d546d7ee8339fc6dc7
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Marc Mutz 2022-11-29 12:45:16 +01:00
parent 391e21d23d
commit 39473f9210
5 changed files with 37 additions and 1 deletions

View File

@ -54,7 +54,6 @@ public:
d.size = m_size;
return QXmlString(std::move(d));
}
operator QStringView() const { return view(); }
void clear() { m_string = nullptr; m_pos = 0; m_size= 0; }
QStringView view() const { return m_string ? QStringView(m_string->data() + m_pos, m_size) : QStringView(); }
@ -62,6 +61,33 @@ public:
bool isNull() const { return !m_string; }
QString toString() const { return view().toString(); }
using value_type = QStringView::value_type;
using size_type = QStringView::size_type;
using difference_type = QStringView::difference_type;
using pointer = QStringView::pointer;
using const_pointer = QStringView::const_pointer;
using reference = QStringView::reference;
using const_reference = QStringView::const_reference;
using iterator = QStringView::iterator;
using const_iterator = QStringView::const_iterator;
using reverse_iterator = QStringView::reverse_iterator;
using const_reverse_iterator = QStringView::const_reverse_iterator;
#define MAKE_MEMBER(name) \
auto name () const noexcept { return view(). name (); }
MAKE_MEMBER(data)
MAKE_MEMBER(size)
MAKE_MEMBER(empty)
MAKE_MEMBER(begin)
MAKE_MEMBER(end)
MAKE_MEMBER(cbegin)
MAKE_MEMBER(cend)
MAKE_MEMBER(rbegin)
MAKE_MEMBER(rend)
MAKE_MEMBER(crbegin)
MAKE_MEMBER(crend)
#undef MAKE_MEMBER
#define MAKE_OP(op) \
friend auto operator op(const XmlStringRef &lhs, const XmlStringRef &rhs) noexcept { return lhs.view() op rhs.view(); } \
/*end*/

View File

@ -8,6 +8,8 @@
qt_internal_add_test(tst_qanystringview
SOURCES
tst_qanystringview.cpp
LIBRARIES
Qt::CorePrivate
)
## Scopes:

View File

@ -7,6 +7,7 @@
#include <QString>
#include <QStringBuilder>
#include <QVarLengthArray>
#include <private/qxmlstream_p.h>
#include <QTest>
@ -165,6 +166,8 @@ static_assert(CanConvert<std::array<char16_t, 123>>);
static_assert(!CanConvert<std::deque<char16_t>>);
static_assert(!CanConvert<std::list<char16_t>>);
static_assert(CanConvert<QtPrivate::XmlStringRef>);
//
// char32_t
//

View File

@ -10,6 +10,8 @@
qt_internal_add_test(tst_qstringview
SOURCES
tst_qstringview.cpp
LIBRARIES
Qt::CorePrivate
)
## Scopes:

View File

@ -7,6 +7,7 @@
#include <QChar>
#include <QVarLengthArray>
#include <QList>
#include <private/qxmlstream_p.h>
#include <QTest>
@ -90,6 +91,8 @@ static_assert(CanConvert<std::array<char16_t, 123>>::value);
static_assert(!CanConvert<std::deque<char16_t>>::value);
static_assert(!CanConvert<std::list<char16_t>>::value);
static_assert(CanConvert<QtPrivate::XmlStringRef>::value);
//
// wchar_t
//