Commit c4293e8e authored by EXT Arnaud Clère's avatar EXT Arnaud Clère

WIP refactor error handling into QCur

parent af19d606
......@@ -249,12 +249,12 @@ public:
/**/ QSequence sequence(quint32* s=nullptr) { return QCur(this).value().sequence(s); }
template<typename T> QCur bind ( T&& t) { return QCur(this).value().bind(std::forward<T>(t)); }
protected:
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 tryNull ( ) { if (current().isNull ()) { return true; } handleError(qBindExpectedNull ); return false; }
bool tryBind ( QUtf8Data& u) { QString s; if (tryBind(s) ) { u = s .toUtf8 (); return true; } return false; }
bool tryBind ( QString& v) { if (current().isString ()) { v = current().toString (); return true; } handleError(qBindExpectedText ); return false; }
bool tryBind ( bool& v) { if (current().isBool ()) { v = current().toBool (); return true; } handleError(qBindExpectedBoolean ); return false; }
bool trySequence(quint32* =nullptr) { if (current().isArray ()) { steps.push(Step()); return true; } return false; }
bool tryRecord (quint32* =nullptr) { if (current().isMap ()) { steps.push(Step()); return true; } return false; }
bool tryNull ( ) { if (current().isNull ()) { return true; } return false; }
bool tryBind ( QUtf8Data& u) { QString s; if (tryBind(s) ) { u = s .toUtf8 (); return true; } return false; }
bool tryBind ( QString& v) { if (current().isString ()) { v = current().toString (); return true; } return false; }
bool tryBind ( bool& v) { if (current().isBool ()) { v = current().toBool (); return true; } return false; }
bool tryBind ( qint64& t) { if (current().isInteger ()) { t = current().toInteger (); return true; } handleError(qBindExpectedInteger ); return false; }
bool tryBind ( quint64& t) { qint64 i; if (tryBind(i) ) { t = quint64(i) ; return true; } return false; }
bool tryBind ( float& v) { double d; if (tryBind(d) ) { v = float(d) ; return true; } return false; }
......@@ -294,13 +294,13 @@ public:
template<typename T> QValueEnd bind ( T&& t) { return QCur(this).value().bind(std::forward<T>(t)); }
protected:
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; } return false; }
bool tryRecord (quint32* s=nullptr) { if (caching) { cacheLevel++; return caching->tryRecord(s); }
skipTag(); if (isMap () && enterContainer()) { levels.push(Level()); return true; } handleError(qBindExpectedRecord ); return false; }
skipTag(); if (isMap () && enterContainer()) { levels.push(Level()); return true; } return false; }
bool tryAny ( ) { if (caching) { return caching->tryAny() && cacheOut(); }
skipTag(); return next(); }
bool tryNull ( ) { if (caching) { return caching->tryNull() && cacheOut(); }
skipTag(); if (isNull() && next()) { return true; } handleError(qBindExpectedNull); return false; }
skipTag(); return isNull() && next(); }
bool tryBind ( QUtf8Data& u) { QString s; if (tryBind(s)) { u = s.toUtf8(); return true; } return false; }
bool tryBind ( QString& s) { if (caching) { return caching->tryBind(s) && cacheOut(); }
skipTag(); if (isString()) {
......@@ -317,7 +317,7 @@ protected:
return handleError(qBindIgnoredBytes);
}
return true;
} else { handleError(qBindExpectedText); return false; } }
} else { return false; } }
bool tryBind ( QByteArray& s) { if (caching) { return caching->tryBind(s) && cacheOut(); }
skipTag(); if (isByteArray()) {
s.resize(0);
......@@ -338,7 +338,7 @@ protected:
skipTag(); if (isBool()) {
b=toBool();
return next();
} else { handleError(qBindExpectedBoolean); return false; } }
} else { return false; } }
bool tryBind ( double& n) { if (caching) { return caching->tryBind(n) && cacheOut(); }
return getNumber(n); }
bool tryBind ( float& n) { double d;
......
......@@ -119,15 +119,15 @@ public:
/**/ QSequence sequence(quint32* s=nullptr) { return QCur(this).value().sequence(s); }
template<typename T> QValueEnd bind ( T&& t) { return QCur(this).value().bind(std::forward<T>(t)); }
protected:
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 tryNull ( ) { if (current().isNull() ) { return true; } handleError(qBindExpectedNull ); return false; }
bool tryBind ( QUtf8Data& u) { QString s; if (tryBind(s) ) { u = s .toUtf8 (); return true; } return false; }
bool tryBind ( QString& v) { if (current().isString()) { v = current().toString(); return true; } handleError(qBindExpectedText ); return false; }
bool tryBind ( bool& v) { if (current().isBool ()) { v = current().toBool (); return true; } handleError(qBindExpectedBoolean ); return false; }
bool tryBind ( qint64& t) { double d; if (tryBind(d) ) { t = qint64(d) ; return true; } return false; }
bool tryBind ( quint64& t) { double d; if (tryBind(d) ) { t = quint64(d) ; return true; } return false; }
bool tryBind ( float& v) { double d; if (tryBind(d) ) { v = float(d) ; return true; } return false; }
bool trySequence(quint32* =nullptr) { if (current().isArray ()) { steps.push(Step()); return true; } return false; }
bool tryRecord (quint32* =nullptr) { if (current().isObject()) { steps.push(Step()); return true; } return false; }
bool tryNull ( ) { if (current().isNull() ) { return true; } return false; }
bool tryBind ( QUtf8Data& u) { QString s; if (tryBind(s) ) { u = s .toUtf8 (); return true; } return false; }
bool tryBind ( QString& v) { if (current().isString()) { v = current().toString(); return true; } return false; }
bool tryBind ( bool& v) { if (current().isBool ()) { v = current().toBool (); return true; } return false; }
bool tryBind ( qint64& t) { double d; if (tryBind(d) ) { t = qint64(d) ; return true; } return false; }
bool tryBind ( quint64& t) { double d; if (tryBind(d) ) { t = quint64(d) ; return true; } return false; }
bool tryBind ( float& v) { double d; if (tryBind(d) ) { v = float(d) ; return true; } return false; }
bool tryBind ( double& v) { if (current().isDouble()) { v = current().toDouble(); return true; } handleError(qBindExpectedDecimal ); return false; }
bool tryItem(QIdentifierLiteral u) { steps.last().key=u; return !(steps.last().item = current(1).toObject().value(steps.last().key.latin1())).isUndefined(); }
......@@ -254,9 +254,9 @@ protected:
enum CachedNumber : quint8 { None=0, Integer, FloatingPoint };
bool trySequence(quint32* s=nullptr) { if (caching) { cacheLevel++; return caching->trySequence(s); }
if (get('[', "[{\"ntf-0123456789.")) { levels.push(Step(-1,"]")); return true; } handleError(qBindExpectedSequence); return false; }
if (get('[', "[{\"ntf-0123456789.")) { levels.push(Step(-1,"]")); return true; } return false; }
bool tryRecord (quint32* s=nullptr) { if (caching) { cacheLevel++; return caching->tryRecord(s); }
if (get('{', "[{\"ntf-0123456789.")) { levels.push(Step(-1,"}")); return true; } handleError(qBindExpectedRecord ); return false; }
if (get('{', "[{\"ntf-0123456789.")) { levels.push(Step(-1,"}")); return true; } return false; }
bool tryAny ( ) { if (caching) { return caching->tryAny() && cacheOut(); }
return next(",}]"); }
bool tryNull ( ) { if (caching) { return caching->tryNull() && cacheOut(); }
......@@ -265,14 +265,14 @@ protected:
get('l') &&
get('l')) {
return true;
} else { handleError(qBindExpectedNull); return false; } }
} else { return false; } }
bool tryBind ( QUtf8Data& s) { if (caching) { return caching->tryBind(s) && cacheOut(); }
QByteArray u;
if ( get('"', "[{\"ntf-0123456789.")) { u.resize(0); char c;
while ((c=getCharInString()) != '\0' ) { u.append(c); }
s=QUtf8Data(u);
return get('"');
} else { handleError(qBindExpectedText); return false; } }
} else { return false; } }
bool tryBind ( QString& s) { QUtf8Data u; if (tryBind(u)) { s = QString(u.utf8()); return true; } return false; }
bool tryBind ( bool& b) { if (caching) { return caching->tryBind(b) && cacheOut(); }
if (get('t', "[{\"ntf-0123456789.") &&
......@@ -287,7 +287,7 @@ protected:
get('s') &&
get('e')) { b=false;
return true;
} else { handleError(qBindExpectedBoolean); return false; } }
} else { return false; } }
bool tryBind ( float& n) { if (caching) { return caching->tryBind(n) && cacheOut(); }
if (getNumber()==None) return false; // already reported qBindExpectedDecimal
if (d<double( std::numeric_limits<float >::min())||
......
......@@ -121,7 +121,7 @@ protected:
template<typename T>
bool tryBind(T& t) { return set(t, "Expected declared metatype T"); }
bool tryBind(QUtf8Data& t) { return set(t, qBindExpectedText ); }
bool tryBind( QUtf8Data& t) { return set(t, qBindExpectedText ); }
bool tryBind( QString& t) { return set(t, qBindExpectedText ); }
bool tryBind( bool& t) { return set(t, qBindExpectedBoolean); }
bool tryBind( QByteArray& t) { return set(t, qBindExpectedBytes ); }
......@@ -137,10 +137,10 @@ protected:
bool tryBind( float& t) { return set(t, qBindExpectedDecimal ); }
bool tryBind( double& t) { return set(t, qBindExpectedDecimal ); }
bool trySequence(quint32* =nullptr) { if (levels.last().isGroup) { levels.push(Level( )); return true; } handleError(qBindExpectedSequence); return false; }
bool tryRecord (quint32* =nullptr) { if (levels.last().isGroup) { levels.push(Level(qBindExpectedItem)); return true; } handleError(qBindExpectedRecord ); return false; }
bool trySequence(quint32* =nullptr) { if (levels.last().isGroup) { levels.push(Level( )); return true; } return false; }
bool tryRecord (quint32* =nullptr) { if (levels.last().isGroup) { levels.push(Level(qBindExpectedItem)); return true; } return false; }
bool tryNull() { if ( settings->value(key()).isNull ()) { return true; } handleError(qBindExpectedNull); return false; }
bool tryNull() { return settings->value(key()).isNull(); }
bool tryItem(QIdentifier& k) { levels.last().key=k ; return true; }
bool tryItem( ) { levels.last().idx++ ; return true; }
......
......@@ -233,7 +233,7 @@ struct QAbstractValue {
errorHandler = newHandler;
return previousHandler;
}
virtual bool handleError(QIdentifierLiteral e, QString context = QString()) const { return errorHandler ? errorHandler(e, context) : false; }
virtual bool handleError(QIdentifierLiteral e, QString context = QString()) const { return errorHandler && errorHandler(e, context); }
protected:
QValueErrorHandler errorHandler = nullptr;
};
......@@ -303,19 +303,30 @@ protected:
template<class T_> friend class QRec;
void _meta (QIdentifierLiteral& n,
QAsciiData& m) { if (Q_LIKELY(impl) ) impl->_meta (n,m); } //!< idempotent (can be called by more than one code)
bool trySequence( quint32* s=nullptr) { return Q_LIKELY(impl) && impl->trySequence(s); }
bool tryRecord ( quint32* s=nullptr) { return Q_LIKELY(impl) && impl->tryRecord (s); }
bool tryNull ( ) { return Q_LIKELY(impl) && impl->tryNull ( ); }
bool tryAny ( ) { return Q_LIKELY(impl) && impl->tryAny ( ); }
bool tryBind ( QUtf8DataView u) { return Q_LIKELY(impl) && impl->tryBind (u); }
bool tryBind ( QAsciiDataView a) { return Q_LIKELY(impl) && impl->tryBind (a); }
bool tryBind ( QLatin1String l) { return Q_LIKELY(impl) && impl->tryBind (l); }
bool tryBind ( QStringView u) { return Q_LIKELY(impl) && impl->tryBind (u); }
QAsciiData& m) { if (Q_LIKELY(impl) ) impl->_meta(n,m); } //!< idempotent (can be called by more than one code)
bool tryAny ( ) { return Q_LIKELY(impl) && impl->tryAny(); }
bool trySequence( quint32* s=nullptr) { return Q_LIKELY(impl) && (impl->trySequence(s) || (handleError(qBindExpectedSequence) && impl->tryAny())); }
bool tryRecord ( quint32* s=nullptr) { return Q_LIKELY(impl) && (impl->tryRecord (s) || (handleError(qBindExpectedRecord ) && impl->tryAny())); }
bool tryNull ( ) { return Q_LIKELY(impl) && (impl->tryNull ( ) || (handleError(qBindExpectedNull ) && impl->tryAny())); }
bool tryBind ( QUtf8DataView u) { return Q_LIKELY(impl) && (impl->tryBind (u) || (handleError(qBindExpectedText ) && impl->tryAny())); }
bool tryBind ( QAsciiDataView a) { return Q_LIKELY(impl) && (impl->tryBind (a) || (handleError(qBindExpectedText ) && impl->tryAny())); }
bool tryBind ( QLatin1String l) { return Q_LIKELY(impl) && (impl->tryBind (l) || (handleError(qBindExpectedText ) && impl->tryAny())); }
bool tryBind ( QStringView u) { return Q_LIKELY(impl) && (impl->tryBind (u) || (handleError(qBindExpectedText ) && impl->tryAny())); }
bool tryBind ( QString& b) { return Q_LIKELY(impl) && (impl->tryBind (b) || (handleError(qBindExpectedBoolean) && impl->tryAny())); }
bool tryBind ( const QString& b) { return Q_LIKELY(impl) && (impl->tryBind (b) || (handleError(qBindExpectedBoolean) && impl->tryAny())); }
bool tryBind ( QString&& b) { return Q_LIKELY(impl) && (impl->tryBind (b) || (handleError(qBindExpectedBoolean) && impl->tryAny())); }
bool tryBind ( QUtf8Data& b) { return Q_LIKELY(impl) && (impl->tryBind (b) || (handleError(qBindExpectedBoolean) && impl->tryAny())); }
bool tryBind ( const QUtf8Data& b) { return Q_LIKELY(impl) && (impl->tryBind (b) || (handleError(qBindExpectedBoolean) && impl->tryAny())); }
bool tryBind ( QUtf8Data&& b) { return Q_LIKELY(impl) && (impl->tryBind (b) || (handleError(qBindExpectedBoolean) && impl->tryAny())); }
bool tryBind ( bool& b) { return Q_LIKELY(impl) && (impl->tryBind (b) || (handleError(qBindExpectedBoolean) && impl->tryAny())); }
bool tryBind ( const bool& b) { return Q_LIKELY(impl) && (impl->tryBind (b) || (handleError(qBindExpectedBoolean) && impl->tryAny())); }
bool tryBind ( bool&& b) { return Q_LIKELY(impl) && (impl->tryBind (b) || (handleError(qBindExpectedBoolean) && impl->tryAny())); }
template<typename T> bool tryBind( T&& t) { return tryBind(BindSupport<RemoveCvRef<T>>(), std::forward<T>(t)); }
private:
template<typename T> bool tryBind(BindNative , T&& t) { return Q_LIKELY(impl) && impl->tryBind( std::forward<T>(t)); }
template<typename T> bool tryBind(BindNative , T&& t) { return Q_LIKELY(impl) && impl->tryBind(std::forward<T>(t)); }
template<typename T> bool tryBind(BindGeneric, T&& t) ;
bool tryItem(QIdentifierLiteral n) { return Q_LIKELY(impl) && impl->tryItem(n); }
......@@ -452,6 +463,8 @@ private:
QValueErrorHandler suspended = nullptr;
T_ outer = T_(); //!< moved context of current traversal up to QCur that will point to the value itself (be it a QIODevice or QCborValue, ...)
static bool ignoreError(QIdentifierLiteral,QString){ return true; }
};
template<class T_> class QSeq
......@@ -774,7 +787,7 @@ struct QAbstractValueReader : public QAbstractValue
virtual bool tryBind( bool& b) { QUtf8Data u; if (!tryBind(u)) return false;
if (u.utf8().compare("true" , Qt::CaseInsensitive)==0) { b=true ; }
else if (u.utf8().compare("false", Qt::CaseInsensitive)==0) { b=false; }
else { handleError(qBindExpectedBoolean); return false; } return true; }
else { return false; } return true; }
virtual bool tryBind( qint8& n) { qint64 l; if (!tryBind(l)) return false; if (l<std::numeric_limits< qint8>::min() || std::numeric_limits< qint8>::max()<l) { handleError(qBindExpectedSmallerNumber); return false; } n= qint8(l); return true; }
virtual bool tryBind( quint8& n) { quint64 l; if (!tryBind(l)) return false; if ( std::numeric_limits< quint8>::max()<l) { handleError(qBindExpectedSmallerNumber); return false; } n= quint8(l); return true; }
virtual bool tryBind( qint16& n) { qint64 l; if (!tryBind(l)) return false; if (l<std::numeric_limits< qint16>::min() || std::numeric_limits< qint16>::max()<l) { handleError(qBindExpectedSmallerNumber); return false; } n= qint16(l); return true; }
......
......@@ -137,9 +137,9 @@ protected:
bool tryBind(T& t) { if (current()->type()==qMetaTypeId<T>()) { t = current()->value<T>(); return true; } handleError(QIdentifierLiteral("ExpectedDeclaredMetatypeT")); return false; }
bool tryBind( QUtf8Data& t) { if (current()->userType() // TODO ->type() if QUtf8Data lands into Qt
==qMetaTypeId< QUtf8Data>()) { t = current()->value< QUtf8Data>(); return true; } handleError(qBindExpectedText ); return false; }
bool tryBind( QString& t) { if (current()->type()==qMetaTypeId< QString>()) { t = current()->value< QString>(); return true; } handleError(qBindExpectedText ); return false; }
bool tryBind( bool& t) { if (current()->type()==qMetaTypeId< bool>()) { t = current()->value< bool>(); return true; } handleError(qBindExpectedBoolean ); return false; }
==qMetaTypeId< QUtf8Data>()) { t = current()->value< QUtf8Data>(); return true; } return false; }
bool tryBind( QString& t) { if (current()->type()==qMetaTypeId< QString>()) { t = current()->value< QString>(); return true; } return false; }
bool tryBind( bool& t) { if (current()->type()==qMetaTypeId< bool>()) { t = current()->value< bool>(); return true; } return false; }
bool tryBind(QByteArray& t) { if (current()->type()==qMetaTypeId<QByteArray>()) { t = current()->value<QByteArray>(); return true; } handleError(qBindExpectedBytes ); return false; }
// Convert numerical types to strictly larger ones // TODO convert all compatible values
bool tryBind( qint8& t) { if (current()->type()==qMetaTypeId< qint8>()) { t = current()->value< qint8>(); return true; } handleError(qBindExpectedInteger ); return false; }
......@@ -166,10 +166,10 @@ protected:
bool tryBind( double& t) { if (current()->type()==qMetaTypeId< float>()||
current()->type()==qMetaTypeId< double>()) { t = current()->value< double>(); return true; } handleError(qBindExpectedDecimal ); return false; }
bool trySequence(quint32* =nullptr) { if (current()->type()==QVariant::List ) { levels.push(Level()); return true; } handleError(qBindExpectedSequence); return false; }
bool tryRecord (quint32* =nullptr) { if (current()->type()==QVariant::Map ) { levels.push(Level()); return true; } handleError(qBindExpectedRecord ); return false; }
bool trySequence(quint32* =nullptr) { if (current()->type()==QVariant::List ) { levels.push(Level()); return true; } return false; }
bool tryRecord (quint32* =nullptr) { if (current()->type()==QVariant::Map ) { levels.push(Level()); return true; } return false; }
bool tryNull() { if ( current()->isNull ()) { return true; } handleError(qBindExpectedNull ); return false; }
bool tryNull() { return current()->isNull(); }
bool tryItem(QIdentifier& k) { levels.last().key=k; return (levels.last().item = current(1)->toMap ().value(QString(levels.last().key.latin1()), QVariant())).isValid(); }
bool tryItem( ) { levels.last().idx++; return (levels.last().item = current(1)->toList().value( levels.last().idx , QVariant())).isValid(); }
......
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