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 39a55e64 authored by EXT Arnaud Clère's avatar EXT Arnaud Clère
Browse files

Renamed QValueStatus now it is mostly hidden and taking into account it

may be templated on QValueMode...
parent e2e2a17d
...@@ -64,25 +64,25 @@ end ...@@ -64,25 +64,25 @@ end
The automaton is implemented as follows: The automaton is implemented as follows:
- `QVal<_>`, `QRec<_>` and `QSeq<_>` types implement possible states where _ denotes the type of outer state - `QVal<_>`, `QRec<_>` and `QSeq<_>` types implement possible states where _ denotes the type of outer state
- State types only expose public methods corresponding to possible transitions, that return the destination state type - State types only expose public methods corresponding to possible transitions, that return the destination state type
- The initial state type is `QValue` and the final state is `QValueStatus` (for instance: `QValue::null()` returns a `QValueStatus`) - The initial state type is `QValue` and the final state is `QCur` (for instance: `QValue::null()` returns a `QCur`)
- Returning a `QRec<_>` or `QSeq<_>` type automatically convert to the final `QValueStatus` type invoking as much `out()` transitions as required - Returning a `QRec<_>` or `QSeq<_>` type automatically convert to the final `QCur` type invoking as much `out()` transitions as required
- `QValueStatus` is a non-owning pointer to an `QAbstractValue` interface which implementations translate data traversal into specific - `QCur` is a non-owning pointer to an `QAbstractValue` interface which implementations translate data traversal into specific
data format read/write actions data format read/write actions
- `QValueStatus` instance is moved from the start state type to the end state type only for successful transitions, allowing to test - `QCur` instance is moved from the start state type to the end state type only for successful transitions, allowing to test
alternatives before proceeding with the traversal alternatives before proceeding with the traversal
- Transitions may fail for various reasons specific to `QAbstractValue` implementations: - Transitions may fail for various reasons specific to `QAbstractValue` implementations:
- builders may not be able to allocate new items - builders may not be able to allocate new items
- readers may read data not matching the expected transition - readers may read data not matching the expected transition
- ... - ...
- In case of unsuccessfull transition the returned state type receives a null `QValueStatus` that transparently bypasses calls to `QAbstractValue` - In case of unsuccessfull transition the returned state type receives a null `QCur` that transparently bypasses calls to `QAbstractValue`
- `bind<T>()` calls are forwarded to the actual `QAbstractValue` or generic `QTransmogrifier` depending on `BindSupport<T>`: - `bind<T>()` calls are forwarded to the actual `QAbstractValue` or generic `QTransmogrifier` depending on `BindSupport<T>`:
- BindNative : **QAbstractValue** interface method - BindNative : **QAbstractValue** interface method
- BindGeneric : **QTransmogrifier** template specialization for T - BindGeneric : **QTransmogrifier** template specialization for T
- Every `bind<T>()` starts from a QValue which is an un *unsafe* QValueStatus copy wrt well-formedness (these `unsafeItem()` copies are protected from incorrect use) - Every `bind<T>()` starts from a QValue which is an un *unsafe* QCur copy wrt well-formedness (these `unsafeItem()` copies are protected from incorrect use)
## C++ types extensibility ## C++ types extensibility
QTransmogrifier is a functor templated on T type receiving a Value and T reference (either lvalue or rvalue reference) and returning the QValueStatus. QTransmogrifier is a functor templated on T type receiving a Value and T reference (either lvalue or rvalue reference) and returning the QCur.
Template specializations can be defined for any T and optionally refined for specific Cur<TImpl> with different sets of BindNative types. Template specializations can be defined for any T and optionally refined for specific Cur<TImpl> with different sets of BindNative types.
A default QTransmogrifier specialization attempts to call `T::bind(...)` to conveniently bind `T* this` without having to understand template syntax, A default QTransmogrifier specialization attempts to call `T::bind(...)` to conveniently bind `T* this` without having to understand template syntax,
...@@ -96,28 +96,28 @@ editor will propose to either `bind(myData.item)`, or to construct a `sequence() ...@@ -96,28 +96,28 @@ editor will propose to either `bind(myData.item)`, or to construct a `sequence()
## Well-formedness guarantees ## Well-formedness guarantees
Thanks to this design, the compiler will make sure that the only possibility to return a QValueStatus from a `QValue` is to traverse Thanks to this design, the compiler will make sure that the only possibility to return a QCur from a `QValue` is to traverse
the data without backtracking, calling only and all necessary QAbstractValue virtual methods. the data without backtracking, calling only and all necessary QAbstractValue virtual methods.
The addition of default and optional values take into account most data schema evolutions in a purely declarative fluent interface without The addition of default and optional values take into account most data schema evolutions in a purely declarative fluent interface without
having to test schema versions and the like. The benefit is that it is not possible to introduce bugs using just the fluent interface. having to test schema versions and the like. The benefit is that it is not possible to introduce bugs using just the fluent interface.
The downside is that writing loops with the fluent interface is unnatural as one must never forget to follow the valid QValueStatus. The downside is that writing loops with the fluent interface is unnatural as one must never forget to follow the valid QCur.
For instance: For instance:
```cpp ```cpp
auto seq(v.sequence()); auto seq(v.sequence());
for (auto&& t : ts) { for (auto&& t : ts) {
seq = seq.bind(t); // do not forget to reassign seq, or subsequent items will be `bind` to the moved-from QValueStatus and automatically ignored seq = seq.bind(t); // do not forget to reassign seq, or subsequent items will be `bind` to the moved-from QCur and automatically ignored
} }
``` ```
## Write performance ## Write performance
Since `QValueStatus`, `QVal`, `QRec` and `QSeq` have no data member other than outer types and `QAbstractValue*`, calling their methods can be Since `QCur`, `QVal`, `QRec` and `QSeq` have no data member other than outer types and `QAbstractValue*`, calling their methods can be
optimized and replaced with just the following operations: optimized and replaced with just the following operations:
1. test the QAbstractValue pointer validity [^1] 1. test the QAbstractValue pointer validity [^1]
2. call the QAbstractValue virtual method corresponding to the possible transitions 2. call the QAbstractValue virtual method corresponding to the possible transitions
3. return the resulting QValueStatus, QVal, QRec or QSeq with a valid or invalid QValueStatus depending on QAbstractValue method success or failure 3. return the resulting QCur, QVal, QRec or QSeq with a valid or invalid QCur depending on QAbstractValue method success or failure
[^1]: Experiments to use constexpr to bypass this step for writers that always return true did not seem to improve performance. [^1]: Experiments to use constexpr to bypass this step for writers that always return true did not seem to improve performance.
......
...@@ -108,9 +108,9 @@ public: ...@@ -108,9 +108,9 @@ public:
QCborWriter(QByteArray* io) : io(io) { Q_ASSERT(io); } QCborWriter(QByteArray* io) : io(io) { Q_ASSERT(io); }
// Shortcuts // Shortcuts
/**/ QValue value ( ) { return QValueStatus(this).value(); } /**/ QValue value ( ) { return QCur(this).value(); }
/**/ QSequence sequence(quint32* s=nullptr) { return QValueStatus(this).value().sequence(s); } /**/ QSequence sequence(quint32* s=nullptr) { return QCur(this).value().sequence(s); }
template<typename T> QValueStatus bind ( T&& t) { return QValueStatus(this).value().bind(std::forward<T>(t)); } template<typename T> QCur bind ( T&& t) { return QCur(this).value().bind(std::forward<T>(t)); }
protected: protected:
bool isValid() const noexcept { return io; } bool isValid() const noexcept { return io; }
...@@ -169,7 +169,7 @@ private: ...@@ -169,7 +169,7 @@ private:
} }
QByteArray* io; QByteArray* io;
std::vector<bool> levels; //!< minimal dynamic context to ensure well-formedness in case QValueStatus is abandoned: true indicates Definite levels with prefixed size std::vector<bool> levels; //!< minimal dynamic context to ensure well-formedness in case QCur is abandoned: true indicates Definite levels with prefixed size
}; };
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
...@@ -186,9 +186,9 @@ public: ...@@ -186,9 +186,9 @@ public:
QCborBuilder(QCborValue* v) : cbor(v) { Q_ASSERT(v); } QCborBuilder(QCborValue* v) : cbor(v) { Q_ASSERT(v); }
// Shortcuts // Shortcuts
/**/ QValue value ( ) { return QValueStatus(this).value(); } /**/ QValue value ( ) { return QCur(this).value(); }
/**/ QSequence sequence(quint32* s=nullptr) { return QValueStatus(this).value().sequence(s); } /**/ QSequence sequence(quint32* s=nullptr) { return QCur(this).value().sequence(s); }
template<typename T> QValueStatus bind ( T&& t) { return QValueStatus(this).value().bind(std::forward<T>(t)); } template<typename T> QCur bind ( T&& t) { return QCur(this).value().bind(std::forward<T>(t)); }
protected: protected:
bool trySequence(quint32* =nullptr) { levels.push(Step( )); return true; } bool trySequence(quint32* =nullptr) { levels.push(Step( )); return true; }
bool tryRecord (quint32* =nullptr) { levels.push(Step(qBindExpectedItem )); return true; } bool tryRecord (quint32* =nullptr) { levels.push(Step(qBindExpectedItem )); return true; }
...@@ -245,9 +245,9 @@ public: ...@@ -245,9 +245,9 @@ public:
} }
// Shortcuts // Shortcuts
/**/ QValue value ( ) { return QValueStatus(this).value(); } /**/ QValue value ( ) { return QCur(this).value(); }
/**/ QSequence sequence(quint32* s=nullptr) { return QValueStatus(this).value().sequence(s); } /**/ QSequence sequence(quint32* s=nullptr) { return QCur(this).value().sequence(s); }
template<typename T> QValueStatus bind ( T&& t) { return QValueStatus(this).value().bind(std::forward<T>(t)); } template<typename T> QCur bind ( T&& t) { return QCur(this).value().bind(std::forward<T>(t)); }
protected: protected:
bool trySequence(quint32* =nullptr) { if (current().isArray ()) { steps.push(Step()); return true; } handleError(qBindExpectedSequence); return false; } bool trySequence(quint32* =nullptr) { if (current().isArray ()) { steps.push(Step()); return true; } handleError(qBindExpectedSequence); return false; }
bool tryRecord (quint32* =nullptr) { if (current().isMap ()) { steps.push(Step()); return true; } handleError(qBindExpectedRecord ); return false; } bool tryRecord (quint32* =nullptr) { if (current().isMap ()) { steps.push(Step()); return true; } handleError(qBindExpectedRecord ); return false; }
...@@ -289,9 +289,9 @@ public: ...@@ -289,9 +289,9 @@ public:
QCborReader(QIODevice* io) : QCborStreamReader(io), cacheVisitor(&cachedValue) { Q_ASSERT(io); } QCborReader(QIODevice* io) : QCborStreamReader(io), cacheVisitor(&cachedValue) { Q_ASSERT(io); }
// Shortcuts // Shortcuts
/**/ QValue value ( ) { return QValueStatus(this).value(); } /**/ QValue value ( ) { return QCur(this).value(); }
/**/ QSequence sequence(quint32* s=nullptr) { return QValueStatus(this).value().sequence(s); } /**/ QSequence sequence(quint32* s=nullptr) { return QCur(this).value().sequence(s); }
template<typename T> QValueStatus bind ( T&& t) { return QValueStatus(this).value().bind(std::forward<T>(t)); } template<typename T> QCur bind ( T&& t) { return QCur(this).value().bind(std::forward<T>(t)); }
protected: protected:
bool trySequence(quint32* s=nullptr) { if (caching) { cacheLevel++; return caching->trySequence(s); } bool trySequence(quint32* s=nullptr) { if (caching) { cacheLevel++; return caching->trySequence(s); }
skipTag(); if (isArray () && enterContainer()) { levels.push(Level()); return true; } handleError(qBindExpectedSequence); return false; } skipTag(); if (isArray () && enterContainer()) { levels.push(Level()); return true; } handleError(qBindExpectedSequence); return false; }
...@@ -374,11 +374,11 @@ protected: ...@@ -374,11 +374,11 @@ protected:
else { else {
cacheReader->setDevice(device()); cacheReader->setDevice(device());
} }
caching = cacheReader.get(); // let outer QValueStatus drive QCborReader depending on bound T caching = cacheReader.get(); // let outer QCur drive QCborReader depending on bound T
} }
else { else {
cacheVisitor.reset(&cachedValue); cacheVisitor.reset(&cachedValue);
caching = &cacheVisitor; // let outer QValueStatus drive QCborReader depending on bound T caching = &cacheVisitor; // let outer QCur drive QCborReader depending on bound T
} }
return true; return true;
} }
...@@ -476,7 +476,7 @@ struct QTransmogrifier<QCborValue> { ...@@ -476,7 +476,7 @@ struct QTransmogrifier<QCborValue> {
if (j.isByteArray()) return v.bind(j.toByteArray()); if (j.isByteArray()) return v.bind(j.toByteArray());
if (j.isNull ()) return v.null(); if (j.isNull ()) return v.null();
if (j.isUndefined() || j.isInvalid() || v->handleError(qBindUnexpectedValue)) return v.any(); if (j.isUndefined() || j.isInvalid() || v->handleError(qBindUnexpectedValue)) return v.any();
return QValueStatus(); return QCur();
} }
else { Q_ASSERT_X(false, Q_FUNC_INFO, "Unsupported v->mode()"); return v.any(); } else { Q_ASSERT_X(false, Q_FUNC_INFO, "Unsupported v->mode()"); return v.any(); }
} }
...@@ -485,7 +485,7 @@ struct QTransmogrifier<QCborValue> { ...@@ -485,7 +485,7 @@ struct QTransmogrifier<QCborValue> {
return zap(std::move(v),std::move(j)); return zap(std::move(v),std::move(j));
} }
else if (v->mode()==Read) { else if (v->mode()==Read) {
QValueStatus r; QCur r;
auto suspended = v->setErrorHandler(); auto suspended = v->setErrorHandler();
QCborArray a; if ((r = v.bind(a))) { j = a ; v->setErrorHandler(suspended); return r; } QCborArray a; if ((r = v.bind(a))) { j = a ; v->setErrorHandler(suspended); return r; }
QCborMap o; if ((r = v.bind(o))) { j = o ; v->setErrorHandler(suspended); return r; } QCborMap o; if ((r = v.bind(o))) { j = o ; v->setErrorHandler(suspended); return r; }
......
...@@ -62,12 +62,12 @@ public: ...@@ -62,12 +62,12 @@ public:
QValueMode mode() const noexcept { return QValueMode::Write; } QValueMode mode() const noexcept { return QValueMode::Write; }
// Shortcuts // Shortcuts
QValue value ( ) { return QValueStatus(this).value(); } QValue value ( ) { return QCur(this).value(); }
QSequence sequence(quint32* s=nullptr) { return QValueStatus(this).value().sequence(s); } QSequence sequence(quint32* s=nullptr) { return QCur(this).value().sequence(s); }
template<typename T> QValueEnd zap(T&& t) { return QValueStatus(this).value().bind(std::forward<T>(t)); } template<typename T> QValueEnd zap(T&& t) { return QCur(this).value().bind(std::forward<T>(t)); }
protected: protected:
friend class QValueStatus; friend class QCur;
bool trySequence(quint32* s=nullptr) { if (s) *io << *s; return true; } bool trySequence(quint32* s=nullptr) { if (s) *io << *s; return true; }
bool tryRecord (quint32* s=nullptr) { if (s) *io << *s; return true; } bool tryRecord (quint32* s=nullptr) { if (s) *io << *s; return true; }
bool tryAny ( ) { return handleError(qBindUnexpectedValue); } bool tryAny ( ) { return handleError(qBindUnexpectedValue); }
......
...@@ -60,9 +60,9 @@ public: ...@@ -60,9 +60,9 @@ public:
void reset(QJsonValue* v) { json=v; Q_ASSERT(v); steps.resize(0); } void reset(QJsonValue* v) { json=v; Q_ASSERT(v); steps.resize(0); }
// Shortcuts // Shortcuts
/**/ QValue value ( ) { return QValueStatus(this).value(); } /**/ QValue value ( ) { return QCur(this).value(); }
/**/ QSequence sequence(quint32* s=nullptr) { return QValueStatus(this).value().sequence(s); } /**/ QSequence sequence(quint32* s=nullptr) { return QCur(this).value().sequence(s); }
template<typename T> QValueStatus bind ( T&& t) { return QValueStatus(this).value().bind(std::forward<T>(t)); } template<typename T> QValueEnd bind ( T&& t) { return QCur(this).value().bind(std::forward<T>(t)); }
protected: protected:
friend class QJsonReader; //!< Calls methods below for out-of-order cachedItems friend class QJsonReader; //!< Calls methods below for out-of-order cachedItems
bool trySequence(quint32* s=nullptr) { Q_UNUSED(s); steps.push(Step( )); return true; } bool trySequence(quint32* s=nullptr) { Q_UNUSED(s); steps.push(Step( )); return true; }
...@@ -115,9 +115,9 @@ public: ...@@ -115,9 +115,9 @@ public:
} }
// Shortcuts // Shortcuts
/**/ QValue value ( ) { return QValueStatus(this).value(); } /**/ QValue value ( ) { return QCur(this).value(); }
/**/ QSequence sequence(quint32* s=nullptr) { return QValueStatus(this).value().sequence(s); } /**/ QSequence sequence(quint32* s=nullptr) { return QCur(this).value().sequence(s); }
template<typename T> QValueStatus bind ( T&& t) { return QValueStatus(this).value().bind(std::forward<T>(t)); } template<typename T> QValueEnd bind ( T&& t) { return QCur(this).value().bind(std::forward<T>(t)); }
protected: protected:
bool trySequence(quint32* =nullptr) { if (current().isArray ()) { steps.push(Step()); return true; } handleError(qBindExpectedSequence); return false; } bool trySequence(quint32* =nullptr) { if (current().isArray ()) { steps.push(Step()); return true; } handleError(qBindExpectedSequence); return false; }
bool tryRecord (quint32* =nullptr) { if (current().isObject()) { steps.push(Step()); return true; } handleError(qBindExpectedRecord ); return false; } bool tryRecord (quint32* =nullptr) { if (current().isObject()) { steps.push(Step()); return true; } handleError(qBindExpectedRecord ); return false; }
...@@ -158,9 +158,9 @@ public: ...@@ -158,9 +158,9 @@ public:
~QJsonWriter() { while (!levels.isEmpty()) write(levels.takeLast().end); } ~QJsonWriter() { while (!levels.isEmpty()) write(levels.takeLast().end); }
// Shortcuts // Shortcuts
/**/ QValue value ( ) { return QValueStatus(this).value(); } /**/ QValue value ( ) { return QCur(this).value(); }
/**/ QSequence sequence(quint32* s=nullptr) { return QValueStatus(this).value().sequence(s); } /**/ QSequence sequence(quint32* s=nullptr) { return QCur(this).value().sequence(s); }
template<typename T> QValueStatus bind ( T&& t) { return QValueStatus(this).value().bind(std::forward<T>(t)); } template<typename T> QValueEnd bind ( T&& t) { return QCur(this).value().bind(std::forward<T>(t)); }
protected: protected:
template<class T> friend class QModelWriter; template<class T> friend class QModelWriter;
...@@ -244,9 +244,9 @@ public: ...@@ -244,9 +244,9 @@ public:
QVector<Error> errors; QVector<Error> errors;
// Shortcuts // Shortcuts
/**/ QValue value ( ) { return QValueStatus(this).value(); } /**/ QValue value ( ) { return QCur(this).value(); }
/**/ QSequence sequence(quint32* s=nullptr) { return QValueStatus(this).value().sequence(s); } /**/ QSequence sequence(quint32* s=nullptr) { return QCur(this).value().sequence(s); }
template<typename T> QValueStatus bind ( T&& t) { return QValueStatus(this).value().bind(std::forward<T>(t)); } template<typename T> QValueEnd bind ( T&& t) { return QCur(this).value().bind(std::forward<T>(t)); }
protected: protected:
enum CachedNumber : quint8 { None=0, Integer, FloatingPoint }; enum CachedNumber : quint8 { None=0, Integer, FloatingPoint };
...@@ -343,7 +343,7 @@ protected: ...@@ -343,7 +343,7 @@ protected:
cachedValue = levels.last().cachedItems.take(QIdentifier(u)); cachedValue = levels.last().cachedItems.take(QIdentifier(u));
cacheReader.reset(&cachedValue); cacheReader.reset(&cachedValue);
Q_ASSERT(!cacheLevel); Q_ASSERT(!cacheLevel);
caching = &cacheReader; // let outer QValueStatus drive QJsonReader depending on bound T caching = &cacheReader; // let outer QCur drive QJsonReader depending on bound T
return true; return true;
} }
else if (!tryItem(s)) { // record() end reached else if (!tryItem(s)) { // record() end reached
...@@ -352,10 +352,10 @@ protected: ...@@ -352,10 +352,10 @@ protected:
else if (u == s ) { else if (u == s ) {
return true ; return true ;
} }
else { // read cachedValue using a dedicated QValueStatus and QTransmogrifier<QValue> accepting value of any 'shape' (outer QTransmogrifier is specialized on outer T which is not usually generic enough) else { // read cachedValue using a dedicated QCur and QTransmogrifier<QValue> accepting value of any 'shape' (outer QTransmogrifier is specialized on outer T which is not usually generic enough)
cachedValue = QJsonValue(); cachedValue = QJsonValue();
cacheWriter.reset(&cachedValue); cacheWriter.reset(&cachedValue);
if (!QValueStatus(this).value().bind(QValueStatus(&cacheWriter).value())) return false; if (!QCur(this).value().bind(QCur(&cacheWriter).value())) return false;
levels.last().cachedItems.insert(s, cachedValue); levels.last().cachedItems.insert(s, cachedValue);
continue; // until !tryItem(s) or u==s continue; // until !tryItem(s) or u==s
} }
...@@ -532,7 +532,7 @@ struct QTransmogrifier<QJsonValue> { ...@@ -532,7 +532,7 @@ struct QTransmogrifier<QJsonValue> {
if (j.isString()) return v.bind(j.toString()); if (j.isString()) return v.bind(j.toString());
if (j.isNull ()) return v.null(); if (j.isNull ()) return v.null();
if (j.isUndefined() || v->handleError(qBindUnexpectedValue)) return v.any(); if (j.isUndefined() || v->handleError(qBindUnexpectedValue)) return v.any();
return QValueStatus(); return QValueEnd();
} }
else { Q_ASSERT_X(false, Q_FUNC_INFO, "Unsupported v->mode()"); return v.any(); } else { Q_ASSERT_X(false, Q_FUNC_INFO, "Unsupported v->mode()"); return v.any(); }
} }
...@@ -541,7 +541,7 @@ struct QTransmogrifier<QJsonValue> { ...@@ -541,7 +541,7 @@ struct QTransmogrifier<QJsonValue> {
return zap(std::move(v),std::move(j)); return zap(std::move(v),std::move(j));
} }
else if (v->mode()==Read) { else if (v->mode()==Read) {
QValueStatus r; QValueEnd r;
auto suspended = v->setErrorHandler(); auto suspended = v->setErrorHandler();
QJsonArray a; if ((r = v.bind(a))) { j = a ; v->setErrorHandler(suspended); return r; } QJsonArray a; if ((r = v.bind(a))) { j = a ; v->setErrorHandler(suspended); return r; }
QJsonObject o; if ((r = v.bind(o))) { j = o ; v->setErrorHandler(suspended); return r; } QJsonObject o; if ((r = v.bind(o))) { j = o ; v->setErrorHandler(suspended); return r; }
......
...@@ -59,7 +59,7 @@ class QModelBind : public QAbstractValue ...@@ -59,7 +59,7 @@ class QModelBind : public QAbstractValue
public: public:
QModelBind(QAbstractItemModel* m, bool rowFirst=true) : m(m), rowFirst(rowFirst) { Q_ASSERT(m); } QModelBind(QAbstractItemModel* m, bool rowFirst=true) : m(m), rowFirst(rowFirst) { Q_ASSERT(m); }
QValue value() { return QValueStatus(this).value(); } QValue value() { return QCur(this).value(); }
protected: protected:
enum : int { enum : int {
T=0, //!< initial state T=0, //!< initial state
......
...@@ -55,9 +55,9 @@ public: ...@@ -55,9 +55,9 @@ public:
QSettingsWriter(QSettings* s) : settings(s) { Q_ASSERT(s); levels.push(Level(qBindExpectedItem)); } QSettingsWriter(QSettings* s) : settings(s) { Q_ASSERT(s); levels.push(Level(qBindExpectedItem)); }
// Shortcuts // Shortcuts
/**/ QValue value ( ) { return QValueStatus(this).value(); } /**/ QValue value ( ) { return QCur(this).value(); }
/**/ QSequence sequence(quint32* s=nullptr) { return QValueStatus(this).value().sequence(s); } /**/ QSequence sequence(quint32* s=nullptr) { return QCur(this).value().sequence(s); }
template<typename T> QValueStatus bind ( T&& t) { return QValueStatus(this).value().bind(std::forward<T>(t)); } template<typename T> QValueEnd bind ( T&& t) { return QCur(this).value().bind(std::forward<T>(t)); }
protected: protected:
bool tryBind ( quint8&& t) { settings->setValue(key(), int(t) ); return true; } bool tryBind ( quint8&& t) { settings->setValue(key(), int(t) ); return true; }
bool tryBind ( quint16&& t) { settings->setValue(key(), int(t) ); return true; } bool tryBind ( quint16&& t) { settings->setValue(key(), int(t) ); return true; }
...@@ -108,7 +108,7 @@ public: ...@@ -108,7 +108,7 @@ public:
} }
// Shortcuts // Shortcuts
template<typename T> QValueStatus bind(T&& t) { return QValueStatus(this).value().bind(std::forward<T>(t)); } template<typename T> QValueEnd bind(T&& t) { return QCur(this).value().bind(std::forward<T>(t)); }
protected: protected:
template<typename T> template<typename T>
bool tryBind(T& t) { return set(t, "Expected declared metatype T"); } bool tryBind(T& t) { return set(t, "Expected declared metatype T"); }
......
...@@ -27,4 +27,4 @@ QIdentifierLiteral qmChildren ("children" ); ...@@ -27,4 +27,4 @@ QIdentifierLiteral qmChildren ("children" );
QIdentifierLiteral qmName ("name" ); QIdentifierLiteral qmName ("name" );
QIdentifierLiteral qmColor ("color" ); QIdentifierLiteral qmColor ("color" );
QValue QValueStatus::value() noexcept { return QValue(std::move(*this)); } QValue QCur::value() noexcept { return QValue(std::move(*this)); }
...@@ -145,7 +145,7 @@ template<> struct BindSupport< QVariant> : BindNative {}; ...@@ -145,7 +145,7 @@ template<> struct BindSupport< QVariant> : BindNative {};
//#include <QtCore/qdatetime.h> //#include <QtCore/qdatetime.h>
//#include <QtCore/quuid.h> //#include <QtCore/quuid.h>
//! Interface for QValueStatus with a fixed subset of BindNative types and just a few optional methods //! Interface for QValue implementations with a fixed subset of BindNative types and just a few optional methods
struct QAbstractValue { struct QAbstractValue {
virtual ~QAbstractValue() = default; virtual ~QAbstractValue() = default;
...@@ -237,36 +237,48 @@ template<class T_> class QVal; ...@@ -237,36 +237,48 @@ template<class T_> class QVal;
#include <type_traits> #include <type_traits>
template<class T> using RemoveCvRef = typename std::remove_cv<typename std::remove_reference<T>::type>::type; template<class T> using RemoveCvRef = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
struct QValueEnd { bool isValid = false; }; //! A QValueEnd represents the end of a QValue traversal and processing
//!
//! \warning isValid does not take into account intermediate errors that are handled by QValueErrorHandler
//!
class QValueEnd {
public:
QValueEnd() = default;
QValueEnd(bool result) : status(result) {}
bool isValid() { return status; }
explicit operator bool() { return isValid(); }
private:
bool status = false;
};
//! A QValueStatus is a move-only, non-owning pointer to QAbstractValue //! A QCur is a cursor inside a structured value implemented as a move-only, non-owning pointer to QAbstractValue
//! //!
//! Its public interface only allows checking the status of resulting QAbstractValue operation(s) using operator bool() and to handleError(). //! Its public interface only allows to check the status of QAbstractValue calls using explicit operator bool(), and to handleError().
//! Its protected interface is used by QVal, QSeq, QRec to only allow new QAbstractValue operation from the previous valid QValueStatus. //! Its protected interface is used by QVal, QSeq, QRec to only allow QAbstractValue calls from the previous valid QCur.
//! //!
//! It allows to easily add QTransmogrifier support to existing Reader/Writers by adding at least one value() method returning a QValueStatus(this).value() //! It allows to easily add QTransmogrifier support to existing Reader/Writers by adding at least one value() method returning a QCur(this).value()
//! \see TextWriter in main.cpp //! \see TextWriter in main.cpp
//! //!
//! \remark QValueStatus allows using the fluent interface without checking intermediate status by setting impl=nullptr on error //! \remark QCur allows using a fluent interface without checking intermediate status by setting impl=nullptr on error
//! and subsequently bypassing impl calls. Errors can result from: //! and subsequently bypassing structured value impl calls. Errors can result from:
//! - using the same intermediate QVal/QSeq/QRec twice (a programmer error), or //! - using the same intermediate QVal/QSeq/QRec twice (a programmer error), or
//! - runtime impl errors like trying to bind a trySequence() whereas the data read matches a tryRecord() //! - runtime impl errors like trying to bind a trySequence() whereas the data read matches a tryRecord()
//! //!
class QValueStatus class QCur
{ {
Q_DISABLE_COPY(QValueStatus) Q_DISABLE_COPY(QCur)
public: public:
QValueStatus ( ) noexcept : impl(nullptr) {} QCur ( ) noexcept : impl(nullptr) {}
QValueStatus (QValueStatus&& o) noexcept : impl(o.impl) { o.impl = nullptr; } QCur (QCur&& o) noexcept : impl(o.impl) { o.impl = nullptr; }
QValueStatus& operator=(QValueStatus&& o) noexcept { auto prev = impl; impl = o.impl; o.impl = prev; return *this; } QCur& operator=(QCur&& o) noexcept { auto prev = impl; impl = o.impl; o.impl = prev; return *this; }
explicit QValueStatus(QAbstractValue* i) : impl(i) { Q_ASSERT(impl); } explicit QCur(QAbstractValue* i) : impl(i) { Q_ASSERT(impl); }
QValueMode mode() const noexcept { return impl ? impl->mode() : QValueMode::Invalid; } QValueMode mode() const noexcept { return impl ? impl->mode() : QValueMode::Invalid; }
explicit operator bool() const noexcept { return impl && impl->isValid(); } //!< Drives QTransmogrifier<T>::bind() traversal explicit operator bool() const noexcept { return impl && impl->isValid(); } //!< Drives QTransmogrifier<T>::bind() traversal
QValueStatus* operator ->() noexcept { return this; } QCur* operator ->() noexcept { return this; }
QVal<QValueStatus> value() noexcept ; QVal<QCur> value() noexcept ;
operator QValueEnd() { return QValueEnd{impl!=nullptr}; } operator QValueEnd() { return QValueEnd{impl!=nullptr}; }
QValueErrorHandler setErrorHandler(QValueErrorHandler newHandler = nullptr) { return Q_LIKELY(impl) ? impl->setErrorHandler(newHandler) : nullptr; } QValueErrorHandler setErrorHandler(QValueErrorHandler newHandler = nullptr) { return Q_LIKELY(impl) ? impl->setErrorHandler(newHandler) : nullptr; }
bool handleError(QIdentifierLiteral name, QString context = QString()) { return Q_LIKELY(impl) ? impl->handleError(name, context) : false; } bool handleError(QIdentifierLiteral name, QString context = QString()) { return Q_LIKELY(impl) ? impl->handleError(name, context) : false; }
...@@ -297,7 +309,7 @@ private: ...@@ -297,7 +309,7 @@ private:
bool tryItem( ) { return Q_LIKELY(impl) && impl->tryItem( ); } bool tryItem( ) { return Q_LIKELY(impl) && impl->tryItem( ); }
bool tryOut ( ) { return Q_LIKELY(impl) && impl->tryOut ( ); } bool tryOut ( ) { return Q_LIKELY(impl) && impl->tryOut ( ); }
private: private:
QValueStatus _unsafeCopy() noexcept { return impl ? QValueStatus(impl) : QValueStatus(); } QCur _unsafeCopy() noexcept { return impl ? QCur(impl) : QCur(); }
QAbstractValue* impl = nullptr; QAbstractValue* impl = nullptr;
}; };
...@@ -315,9 +327,9 @@ template<class T_> class QRec; //!< a Record data structure defined below ...@@ -315,9 +327,9 @@ template<class T_> class QRec; //!< a Record data structure defined below
template<class T_> class QSeq; //!< a Sequence data structure defined below template<class T_> class QSeq; //!< a Sequence data structure defined below
template<class T_> class QVal; //!< a choice of sequence(), record(), null(), or values with at least a textual representation and possibly binary ones template<class T_> class QVal; //!< a choice of sequence(), record(), null(), or values with at least a textual representation and possibly binary ones
using QValue = QVal<QValueStatus>; using QValue = QVal<QCur>;
using QSequence = QSeq<QValueStatus>; using QSequence = QSeq<QCur>;
using QRecord = QRec<QValueStatus>; using QRecord = QRec<QCur>;
// Custom bind support // Custom bind support
...@@ -327,8 +339,8 @@ using QRecord = QRec<QValueStatus>; ...@@ -327,8 +339,8 @@ using QRecord = QRec<QValueStatus>;
template<typename T> using QValFunction = QValueEnd(*)(T &,QValue &&) ; template<typename T> using QValFunction = QValueEnd(*)(T &,QValue &&) ;
template<class Ts> using QSeqFunction = QSequence(*)(Ts&,QSequence&&) ; template<class Ts> using QSeqFunction = QSequence(*)(Ts&,QSequence&&) ;
#endif #endif
/**/ using QValLambda = std::function<QValueEnd(QValue &&)>; using QValLambda = std::function<QValueEnd(QValue &&)>;
/**/ using QSeqLambda = std::function<QSequence(QSequence&&)>; using QSeqLambda = std::function<QSequence(QSequence&&)>;