Commit 7e92d744 authored by EXT Arnaud Clère's avatar EXT Arnaud Clère
Browse files

Added bind(T& t, T&& defaultT) to fluent interface

parent 2d4ed500
......@@ -168,9 +168,7 @@ public:
/**/ T_ null ( ) { return outer->_null ( ) ? std::move(outer) : T_ (); }
/**/ T_ any ( ) { return outer->_any ( ) ? std::move(outer) : T_ (); }
template<typename T> T_ bind ( T&& t) { return outer->_bind(std::forward<T>(t)) ? std::move(outer) : T_ (); }
template<typename T> T_ bind (T& t, T&& defaultT) { return outer->_bind(QBindDefault<T>{t,
defaultT}) ? std::move(outer) : T_ (); }
// TODO ? Add bind with default T specialised for Reader/Writer
template<typename T> T_ bind (T& t, T&& defaultT) { return outer->_bind(QBindDefault<T>{t,defaultT})? std::move(outer) : T_ (); }
// Literal metadata support
/**/ Val<T_> meta (QMetaData&& m) { auto v=m; return meta(v); }
......@@ -206,6 +204,7 @@ public:
/**/ Seq<T_> null ( ) { return item().null ( ); }
/**/ Seq<T_> any ( ) { return item().any ( ); }
template<typename T> Seq<T_> bind ( T&& t) { return item().bind(std::forward<T>(t)); }
template<typename T> Rec<T_> bind (T& t, T&& defaultT) { return item().bind(t,std::forward<T>(defaultT)); }
private:
template<typename T, class TResult, typename TEnabledIf> friend struct QBind;
Val<TResult> unsafeItem() noexcept { return outer->_item() ? Val<TResult>(outer._unsafeCopy()) : Val<TResult>(); }
......@@ -239,6 +238,7 @@ public:
/**/ Rec<Rec<T_>> record (QName n, quint32* s=nullptr) { return item(n).record ( s); }
/**/ Rec<T_> null (QName n ) { return item(n).null ( ); }
template<typename T> Rec<T_> bind (QName n, T&& t) { return item(n).bind(std::forward<T>(t)); }
template<typename T> Rec<T_> bind (QName n, T& t, T&& defaultT) { return item(n).bind(t,std::forward<T>(defaultT)); }
private:
template<typename T, class TResult, typename TEnabledIf> friend struct QBind;
Val<TResult> unsafeItem(QUtf8String& n) noexcept { return outer->_item(n) ? Val<TResult>(outer._unsafeCopy()) : Val<TResult>(); }
......
......@@ -75,7 +75,7 @@ struct Person
.bind( lastName)
.out()
.bind("height" ,height )
.bind("age" ,age ) // TODO item("age").bind(age,-1) to read null() as -1
.bind("age" ,age ,-1) // reads null() as -1
.bind("phones" ,phones ) // recursive calls to QBind will take care of that part
.bind("comments",comments)
; // automagically closes all opened structures
......@@ -490,7 +490,7 @@ int main(int argc, char *argv[])
{
QBuffer json;
json.open(QIODevice::ReadOnly);
json.buffer() = "_{ \"names\": [ _\"John\" _, \"Doe\"] , \"height\":_1.75 , null, \"phones\":[ \"+44 1234567\" , \"+44 2345678\" ], \"\":\"superfluous item\" _} ";
json.buffer() = "_{ \"names\": [ _\"John\" _, \"Doe\"] , \"height\":_1.75 , \"age\":null, \"phones\":[ \"+44 1234567\" , \"+44 2345678\" ], \"\":\"superfluous item\" _} ";
QJsonValue v;
#if QT_VERSION>=QT_VERSION_CHECK(5,12,0)
QCborValue c;
......
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