Commit 88ea9759 authored by EXT Arnaud Clère's avatar EXT Arnaud Clère
Browse files

Added Read+Write (dropping MSVC support until c++17 constexpr if is used)

parent 1d58227b
......@@ -93,8 +93,8 @@ public:
operator bool() { return m_seq; } //!< to drive QBind<TResult,T>() traversal
Val<Seq<T_>> item() { auto val = Q_LIKELY(m_seq) ? m_seq->item() : nullptr; return Val<Seq<T_>>(val, std::move(*this)); }
T_ out() { if (Q_LIKELY(m_seq) ) m_seq->out () ; return std::move(m_out) ; }
Val<Seq<T_>> item() { auto v = Q_LIKELY(m_seq) ? m_seq->item() : nullptr; return Val<Seq<T_>>(v, v ? std::move(*this) : Seq<T_>()); }
T_ out() { if (Q_LIKELY(m_seq) ) m_seq->out () ; return std::move(m_out) ; }
operator TResult() { return out(); /* recursively calls operator TResult() as mandated by T_ */ }
TResult result() { return operator TResult(); }
......@@ -227,6 +227,43 @@ struct QBind<TResult, QJsonValue> {
}
};
template<class TResult>
struct QBind<TResult, const QJsonValueRef> { static TResult bind(Val<TResult> dst, const QJsonValueRef& src) {
static_assert(TResult::Mode==Write,"Cannot Read from TResult into const QJsonValueRef&");
if (src.isArray ()) return dst.bind(src.toArray ());
if (src.isDouble()) return dst.bind(src.toDouble());
if (src.isString()) return dst.bind(src.toString());
return dst.null();
}};
template<class TResult>
struct QBind<TResult, QJsonValueRef> {
static TResult bind(Val<TResult> dst, QJsonValueRef& src, std::enable_if_t<TResult::Mode==Write>* = nullptr) {
if (src.isArray ()) return dst.bind(src.toArray ());
if (src.isDouble()) return dst.bind(src.toDouble());
if (src.isString()) return dst.bind(src.toString());
return dst.null();
}
static TResult bind(Val<TResult> src, QJsonValueRef& dst, std::enable_if_t<TResult::Mode==Read >* = nullptr) {
auto s(src.sequence());
if (s) {
QJsonArray array;
for (auto i = s.item(); i; i = s.item()) {
QJsonValueRef v;
s = i.bind(v);
if (s)
array.push_back(v);
}
dst = array;
return s;
}
double d; QString t;
auto
result = src.bind(d); if (result) { dst = QJsonValueRef(d); return result; }
result = src.bind(t); if (result) { dst = QJsonValueRef(t); return result; }
dst = QJsonValueRef(); return src.null();
}
};
#include <QtCore/qjsonarray.h>
template<class TResult>
......@@ -242,7 +279,7 @@ template<class TResult>
struct QBind<TResult, QJsonArray> {
static TResult bind(Val<TResult> dst, QJsonArray& src, std::enable_if_t<TResult::Mode==Write>* = nullptr) {
auto s(dst.sequence());
for (auto&& item : src) {
for (auto item : src) {
s = s.bind(item);
}
return s;
......@@ -616,13 +653,11 @@ int main()
QJsonReader(&b).bind(v);
}
STOP("Json>JsonValue",QString::fromUtf8(QJsonDocument(v.toArray()).toJson()));
#if 0
START {
b.seek(0); b.buffer().clear();
QCborWriter(&b).bind(v);
}
STOP("JsonValue>Cbor",QString::fromUtf8(b.buffer().toHex()));
#endif
}
GROUP_STOP
return fclose(samples);
......
Supports Markdown
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