Vous avez reçu un message "Your GitLab account has been locked ..." ? Pas d'inquiétude : lisez cet article https://docs.gricad-pages.univ-grenoble-alpes.fr/help/unlock/

Commit 5cc756d7 authored by EXT Arnaud Clère's avatar EXT Arnaud Clère
Browse files

refactored IWriter/Reader into IBind

parent a0f23cc2
......@@ -19,7 +19,7 @@ TEMPLATE = app
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# C++17 allows replacing 'if /*constexpr*/' with 'if constepxr' with little performance improvement
# C++17 allows replacing 'if /*constexpr*/' with 'if constexpr' with little performance improvement
#gcc:QMAKE_CXXFLAGS += -std=c++1z
#msvc:QMAKE_CXXFLAGS += /std=c++17
......
This diff is collapsed.
......@@ -59,6 +59,8 @@ class QDataWriter : public IWriter // allows runtime flexibility using a compile
public:
QDataWriter(QDataStream* io) : io(io) { Q_ASSERT(io); }
virtual BindMode mode() const { return BindMode::Write; }
// Shortcuts
Val<Cur<QDataWriter>> value ( ) { return Cur<QDataWriter>(this).value() ; }
Val<Cur<QDataWriter>> meta ( QMetaData&& m) { return Cur<QDataWriter>(this).value().meta (m); }
......@@ -88,10 +90,10 @@ template<typename T> struct BindSupport<QDataWriter,T> : BindSupport<IWriter,T>
// TODO BindNative smallest numeric types
template<> struct BindSupport<QDataWriter, qint32> : BindNative {};
template<> struct BindSupport<QDataWriter,quint32> : BindNative {};
// TODO BindNative all types for which T stream operators are defined : QMetaTypeId<typename std::remove_reference<typename std::remove_cv<T>>>::Defined ?
// TODO BindNative other types that cannot be QBind with types above
template<> struct BindSupport<QDataWriter, QColor> : BindNative {};
template<typename T> struct BindSupport<QDataWriter, T,typename std::enable_if<
QtPrivate::IsAssociativeContainer<typename std::remove_reference<typename std::remove_cv<T>::type>::type>::Value // Not handled correctly by _record()._item(k) which makes the keys implicit
QtPrivate::IsAssociativeContainer<RemoveCvRef<T>>::Value // Not handled correctly by _record()._item(k) which makes the keys implicit
>::type
> : BindNative {};
......
......@@ -420,7 +420,7 @@ private:
template<class TResult>
struct QBind<TResult, QJsonValue> {
static TResult bind(Val<TResult>&& v, QJsonValue&& j) {
if /*constexpr*/ (TResult::Mode==Write) {
if (v->mode()==Write) {
if (j.isObject()) return v.bind(j.toObject());
if (j.isArray ()) return v.bind(j.toArray ());
if (j.isBool ()) return v.bind(j.toBool ());
......@@ -428,7 +428,7 @@ struct QBind<TResult, QJsonValue> {
if (j.isString()) return v.bind(j.toString());
return v.null();
}
else if /*constexpr*/ (TResult::Mode==Read) {
else if (v->mode()==Read) {
TResult r;
{
QScopedChoice<Val<TResult>> choice(v);
......@@ -441,6 +441,7 @@ struct QBind<TResult, QJsonValue> {
if (!(r = v.null())) r.reportError("Expected bool|double|QString|QJsonArray|QJsonOnject|null");
/**/ j = QJsonValue( ); return r;
}
else { Q_ASSERT_X(false, Q_FUNC_INFO, "Unsupported v->mode()"); return v.null(); }
}
static TResult bind(Val<TResult>&& v, QJsonValue& j) {
return bind(std::move(v),std::move(j));
......@@ -450,7 +451,7 @@ struct QBind<TResult, QJsonValue> {
template<class TResult>
struct QBind<TResult, QJsonArray> {
static TResult bind(Val<TResult>&& v, QJsonArray&& j) {
if /*constexpr*/ (TResult::Mode==Write) {
if (v->mode()==Write) {
quint32 size=quint32(j.size());
auto s(v.sequence(&size));
for (QJsonValue&& item : j) {
......@@ -458,7 +459,7 @@ struct QBind<TResult, QJsonArray> {
}
return s;
}
else if /*constexpr*/ (TResult::Mode==Read) {
else if (v->mode()==Read) {
auto s(v.sequence());
Val<Seq<TResult>> i;
while ((i = s.item())) {
......@@ -468,6 +469,7 @@ struct QBind<TResult, QJsonArray> {
}
return s;
}
else { Q_ASSERT_X(false, Q_FUNC_INFO, "Unsupported v->mode()"); return v.null(); }
}
static TResult bind(Val<TResult>&& v, QJsonArray& j) {
return bind(std::move(v),std::move(j));
......@@ -477,7 +479,7 @@ struct QBind<TResult, QJsonArray> {
template<class TResult>
struct QBind<TResult, QJsonObject> {
static TResult bind(Val<TResult>&& v, QJsonObject&& j) {
if /*constexpr*/ (TResult::Mode==Write) {
if (v->mode()==Write) {
quint32 size=quint32(j.size());
auto s(v.record(&size));
for (QString key : j.keys()) {
......@@ -485,7 +487,7 @@ struct QBind<TResult, QJsonObject> {
}
return s;
}
else if /*constexpr*/ (TResult::Mode==Read) {
else if (v->mode()==Read) {
auto s(v.record());
QByteArray k; Val<Rec<TResult>> i;
while ((i = s.item(k))) {
......@@ -495,6 +497,7 @@ struct QBind<TResult, QJsonObject> {
}
return s;
}
else { Q_ASSERT_X(false, Q_FUNC_INFO, "Unsupported v->mode()"); return v.null(); }
}
static TResult bind(Val<TResult>&& v, QJsonObject& j) {
return bind(std::move(v),std::move(j));
......
......@@ -137,40 +137,40 @@ private:
template<class TResult>
struct QBind<TResult, QVariant> {
static TResult bind(Val<TResult>&& dst, QVariant&& src) {
if /*constexpr*/ (TResult::Mode==Write) {
if (src.type()==QVariant::List ) return dst.bind(src.value<QVariantList>()); // TODO QSequentialIterable ?
if (src.type()==QVariant::Map ) return dst.bind(src.value<QVariantMap >()); // TODO QAssociativeIterable ?
static TResult bind(Val<TResult>&& v, QVariant&& src) {
if (v->mode()==Write) {
if (src.type()==QVariant::List ) return v.bind(src.value<QVariantList>()); // TODO QSequentialIterable ?
if (src.type()==QVariant::Map ) return v.bind(src.value<QVariantMap >()); // TODO QAssociativeIterable ?
// TODO QVariant::Hash
if (src.type()==QVariant::Bool ) return dst.bind(src.value< bool>());
if (src.type()==QVariant::ByteArray) return dst.bind(src.value< QByteArray>());
if (src.type()==QVariant::String ) return dst.bind(src.value< QString>());
if (src.type()==QVariant::Char ) return dst.bind(src.toString());
if (src.type()==QVariant::Bool ) return v.bind(src.value< bool>());
if (src.type()==QVariant::ByteArray) return v.bind(src.value< QByteArray>());
if (src.type()==QVariant::String ) return v.bind(src.value< QString>());
if (src.type()==QVariant::Char ) return v.bind(src.toString());
if (src.type()==QVariant::ULongLong) return dst.bind(src.value< qulonglong>());
if (src.type()==QVariant::UInt ) return dst.bind(src.value<unsigned int>());
if (src.type()==QVariant::LongLong ) return dst.bind(src.value< qlonglong>());
if (src.type()==QVariant::Int ) return dst.bind(src.value< int>());
if (src.type()==QVariant::Double ) return dst.bind(src.value< double>());
if (src.type()==QVariant::ULongLong) return v.bind(src.value< qulonglong>());
if (src.type()==QVariant::UInt ) return v.bind(src.value<unsigned int>());
if (src.type()==QVariant::LongLong ) return v.bind(src.value< qlonglong>());
if (src.type()==QVariant::Int ) return v.bind(src.value< int>());
if (src.type()==QVariant::Double ) return v.bind(src.value< double>());
// TODO convert builtin types which canConvert<QString>
// \sa QBind<TSrcResult, Val<TDst>&&, IsReader<TSrcResult>>
return dst.null();
return v.null();
}
static_assert(TResult::Mode!=Write,"Unsupported TResult::Mode");
else { Q_ASSERT_X(false, Q_FUNC_INFO, "Unsupported v->mode()"); return v.null(); }
}
static TResult bind(Val<TResult>&& src, QVariant& dst) {
if /*constexpr*/ (TResult::Mode==Write) {
return bind(std::move(src),std::move(dst));
static TResult bind(Val<TResult>&& v, QVariant& dst) {
if (v->mode()==Write) {
return bind(std::move(v),std::move(dst));
}
static_assert(TResult::Mode!=Write,"Unsupported TResult::Mode");
else { Q_ASSERT_X(false, Q_FUNC_INFO, "Unsupported v->mode()"); return v.null(); }
}
};
//template<class TResult>
//struct QBind<TResult, QVariant&, IsReader<TResult>> { static TResult bind(Val<TResult>&& src, QVariant& dst) {
//struct QBind<TResult, QVariant&, IsReader<TResult>> { static TResult bind(Val<TResult>&& v, QVariant& dst) {
// TResult r;
// {
// QScopedChoice<Val<TResult>> choice(src);
......
......@@ -67,7 +67,7 @@ struct Person
QString firstName, lastName; double height; int age; QVector<QString> phones; QString comments;
template<class TResult>
TResult bind(Val<TResult>&& value) { // since we only bind T& (no const T&), it works with TResult::Mode==Read as well as Write
TResult bind(Val<TResult>&& value) { // since we only bind T& (no const T&), it works with v->mode()==Read as well as Write
return value
.record()
.sequence("names")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment