Commit 0cf044d6 authored by EXT Arnaud Clère's avatar EXT Arnaud Clère

WIP refactor error handling to the fluent interface

Simplify design by provising QCur with all QValue overloads instead of
relying on a trait (this allows to simplifying calls stacks for debug
and provide type sensitive error handling context too)
parent c4293e8e
......@@ -349,7 +349,7 @@ protected:
bool tryBind ( quint64& t) { if (caching) { return caching->tryBind(t) && cacheOut(); }
skipTag();
if (!isUnsignedInteger()) { handleError(qBindExpectedPositiveInteger); return false; }
if (!isUnsignedInteger()) { return false; }
t=toUnsignedInteger(); return true; }
bool tryBind ( qint64& t) { if (caching) { return caching->tryBind(t) && cacheOut(); }
quint64 i; bool neg;
......
......@@ -300,12 +300,12 @@ protected:
auto r=getNumber();
if (r==None) return false; // already reported qBindExpectedDecimal
if (r==FloatingPoint) { handleError(qBindExpectedInteger); return false; }
if (neg) { handleError(qBindExpectedPositiveInteger); return false; }
if (neg) { return false; }
t = i ; cachedNumber=None; return true; }
bool tryBind ( qint64& t) { if (caching) { return caching->tryBind(t) && cacheOut(); }
auto r=getNumber();
if (r==None) return false; // already reported qBindExpectedDecimal
if (r==FloatingPoint) { handleError(qBindExpectedInteger); return false; }
if (r==FloatingPoint) { return false; }
if (!neg && i<quint64( std::numeric_limits<qint64>::max()) ) { t = qint64(i); cachedNumber=None; return true; }
if ( neg && i<quint64(-std::numeric_limits<qint64>::max())-1) { t = -qint64(i); cachedNumber=None; return true; }
handleError(qBindExpectedSmallerNumber); return false; }
......
......@@ -121,21 +121,21 @@ 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( QString& t) { return set(t, qBindExpectedText ); }
bool tryBind( bool& t) { return set(t, qBindExpectedBoolean); }
bool tryBind( QByteArray& t) { return set(t, qBindExpectedBytes ); }
bool tryBind( QUtf8Data& t) { return set(t); }
bool tryBind( QString& t) { return set(t); }
bool tryBind( bool& t) { return set(t); }
bool tryBind( QByteArray& t) { return set(t); }
// Convert numerical types to strictly larger ones // TODO convert all compatible values
bool tryBind( qint8& t) { return set(t, qBindExpectedInteger ); }
bool tryBind( qint16& t) { return set(t, qBindExpectedInteger ); }
bool tryBind( qint32& t) { return set(t, qBindExpectedInteger ); }
bool tryBind( qint64& t) { return set(t, qBindExpectedInteger ); }
bool tryBind( quint8& t) { return set(t, qBindExpectedPositiveInteger); }
bool tryBind( quint16& t) { return set(t, qBindExpectedPositiveInteger); }
bool tryBind( quint32& t) { return set(t, qBindExpectedPositiveInteger); }
bool tryBind( quint64& t) { return set(t, qBindExpectedPositiveInteger); }
bool tryBind( float& t) { return set(t, qBindExpectedDecimal ); }
bool tryBind( double& t) { return set(t, qBindExpectedDecimal ); }
bool tryBind( qint8& t) { return set(t); }
bool tryBind( qint16& t) { return set(t); }
bool tryBind( qint32& t) { return set(t); }
bool tryBind( qint64& t) { return set(t); }
bool tryBind( quint8& t) { return set(t); }
bool tryBind( quint16& t) { return set(t); }
bool tryBind( quint32& t) { return set(t); }
bool tryBind( quint64& t) { return set(t); }
bool tryBind( float& t) { return set(t); }
bool tryBind( double& t) { return set(t); }
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; }
......@@ -148,10 +148,10 @@ protected:
bool tryAny() { return true; }
bool isValid() const noexcept { return settings; }
bool handleError(QIdentifierLiteral e, QString context = QString()) const { return errorHandler ? errorHandler(e, QString("at %1 %2").arg(currentPath(), context)) : false; }
bool handleError(QIdentifierLiteral e, QString context = QString()) const { return errorHandler && errorHandler(e, QString("at %1 %2").arg(currentPath(), context)); }
private:
template<typename T>
bool set(T& t, QIdentifierLiteral error) { QVariant v = settings->value(key()); if (v.convert(qMetaTypeId<T>())) { t = v.value<T>(); return true; } handleError(error); return false; }
bool set(T& t) { QVariant v = settings->value(key()); if (v.convert(qMetaTypeId<T>())) { t = v.value<T>(); return true; } return false; }
QString key() {
Q_ASSERT(!levels.isEmpty());
return !levels.last().key.isNull()
......
This diff is collapsed.
......@@ -140,31 +140,31 @@ protected:
==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; }
bool tryBind(QByteArray& t) { if (current()->type()==qMetaTypeId<QByteArray>()) { t = current()->value<QByteArray>(); return true; } 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; }
bool tryBind( qint8& t) { if (current()->type()==qMetaTypeId< qint8>()) { t = current()->value< qint8>(); return true; } return false; }
bool tryBind( qint16& t) { if (current()->type()==qMetaTypeId< qint8>()||
current()->type()==qMetaTypeId< qint16>()) { t = current()->value< qint16>(); return true; } handleError(qBindExpectedInteger ); return false; }
current()->type()==qMetaTypeId< qint16>()) { t = current()->value< qint16>(); return true; } return false; }
bool tryBind( qint32& t) { if (current()->type()==qMetaTypeId< qint8>()||
current()->type()==qMetaTypeId< qint16>()||
current()->type()==qMetaTypeId< qint32>()) { t = current()->value< qint32>(); return true; } handleError(qBindExpectedInteger ); return false; }
current()->type()==qMetaTypeId< qint32>()) { t = current()->value< qint32>(); return true; } return false; }
bool tryBind( qint64& t) { if (current()->type()==qMetaTypeId< qint8>()||
current()->type()==qMetaTypeId< qint16>()||
current()->type()==qMetaTypeId< qint32>()||
current()->type()==qMetaTypeId< qint64>()) { t = current()->value< qint64>(); return true; } handleError(qBindExpectedInteger ); return false; }
bool tryBind( quint8& t) { if (current()->type()==qMetaTypeId< quint8>()) { t = current()->value< quint8>(); return true; } handleError(qBindExpectedPositiveInteger); return false; }
current()->type()==qMetaTypeId< qint64>()) { t = current()->value< qint64>(); return true; } return false; }
bool tryBind( quint8& t) { if (current()->type()==qMetaTypeId< quint8>()) { t = current()->value< quint8>(); return true; } return false; }
bool tryBind( quint16& t) { if (current()->type()==qMetaTypeId< quint8>()||
current()->type()==qMetaTypeId< quint16>()) { t = current()->value< quint16>(); return true; } handleError(qBindExpectedPositiveInteger); return false; }
current()->type()==qMetaTypeId< quint16>()) { t = current()->value< quint16>(); return true; } return false; }
bool tryBind( quint32& t) { if (current()->type()==qMetaTypeId< quint8>()||
current()->type()==qMetaTypeId< quint16>()||
current()->type()==qMetaTypeId< quint32>()) { t = current()->value< quint32>(); return true; } handleError(qBindExpectedPositiveInteger); return false; }
current()->type()==qMetaTypeId< quint32>()) { t = current()->value< quint32>(); return true; } return false; }
bool tryBind( quint64& t) { if (current()->type()==qMetaTypeId< quint8>()||
current()->type()==qMetaTypeId< quint16>()||
current()->type()==qMetaTypeId< quint32>()||
current()->type()==qMetaTypeId< quint64>()) { t = current()->value< quint64>(); return true; } handleError(qBindExpectedPositiveInteger); return false; }
bool tryBind( float& t) { if (current()->type()==qMetaTypeId< float>()) { t = current()->value< float>(); return true; } handleError(qBindExpectedDecimal ); return false; }
current()->type()==qMetaTypeId< quint64>()) { t = current()->value< quint64>(); return true; } return false; }
bool tryBind( float& t) { if (current()->type()==qMetaTypeId< float>()) { t = current()->value< float>(); return true; } return false; }
bool tryBind( double& t) { if (current()->type()==qMetaTypeId< float>()||
current()->type()==qMetaTypeId< double>()) { t = current()->value< double>(); return true; } handleError(qBindExpectedDecimal ); return false; }
current()->type()==qMetaTypeId< double>()) { t = current()->value< double>(); return true; } 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; }
......
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