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

Make it possible for errorhandler to stop operations on some condition

like errorName != qBindIgnoredXxxx
parent 616633d3
This diff is collapsed.
This diff is collapsed.
......@@ -154,13 +154,16 @@ protected:
virtual bool tryBind( QByteArray& r) { return tryBind(QByteArray(r)); }
virtual bool tryBind( QVariant& r) { return tryBind( QVariant(r)); }
void reportError(QIdentifierLiteral e, QString context = QString()) {
bool handleError(QIdentifierLiteral e, QString context = QString()) {
if (errorHandler) {
QString path;
for (auto current = parent; parent.isValid(); current = current.parent()) {
path.prepend(QString('/').append(current.row()).append(',').append(current.column()));
}
errorHandler(e, path.append(context));
return errorHandler(e, path.append(context));
}
else {
return false;
}
}
......@@ -216,7 +219,7 @@ protected:
bool metaColumnNames=false;
QVector<int> sizes;
QValueErrorHandler errorHandler = QValueErrorHandler();
QValueErrorHandler errorHandler = nullptr;
};
// --------------------------------------------------------------------------
......@@ -313,7 +316,7 @@ protected:
row++; col=0;
return true;
}
reportError(qBindIgnoredItem);
handleError(qBindIgnoredItem);
return false;
}
return true;
......@@ -328,7 +331,7 @@ protected:
row=0; col++;
return true;
}
reportError(qBindIgnoredItem);
handleError(qBindIgnoredItem);
return false;
}
return true;
......@@ -358,8 +361,8 @@ protected:
else {
col=columnNames.indexOf(n.utf8());
// TODO if (max(dimension())<=col) col=-1;
if (col<0) {
reportError(qBindIgnoredItem);
if (col<0 && !handleError(qBindIgnoredItem)) {
return false;
}
}
return true;
......@@ -376,8 +379,7 @@ protected:
}
else {
// TODO if (rowNames...
reportError(qBindIgnoredItemName);
return true;
return handleError(qBindIgnoredItemName);
}
}
else { Q_ASSERT(I<=d);
......@@ -509,7 +511,7 @@ protected:
if (col < m->columnCount()) {
return true;
}
reportError(qBindIgnoredItem);
handleError(qBindIgnoredItem);
return false;
}
return true;
......@@ -527,7 +529,7 @@ protected:
row=0; col++;
return true;
}
reportError(qBindIgnoredItem);
handleError(qBindIgnoredItem);
return false;
}
return true;
......@@ -559,8 +561,8 @@ protected:
col=columnNames.indexOf(n.utf8());
}
// TODO if (max(dimension())<=col) col=-1;
if (col<0) {
reportError(qBindIgnoredItem);
if (col<0 && !handleError(qBindIgnoredItem)) {
return false;
}
return true;
}
......@@ -573,8 +575,7 @@ protected:
}
else if (n!=childrenName) {
// TODO if (rowNames...
reportError(qBindIgnoredItemName);
return true;
return handleError(qBindIgnoredItemName);
}
else { return true; }
}
......@@ -605,12 +606,12 @@ protected:
// TODO QDate*, QTime
// TODO QPixmap if metadata suggests a QMimeData image ?
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) { reportError(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) { reportError(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) { reportError(qBindExpectedSmallerNumber); return false; } n= qint16(l); return true; }
virtual bool tryBind( quint16& n) { quint64 l; if (!tryBind(l)) return false; if ( std::numeric_limits<quint16>::max()<l) { reportError(qBindExpectedSmallerNumber); return false; } n=quint16(l); return true; }
virtual bool tryBind( qint32& n) { qint64 l; if (!tryBind(l)) return false; if (l<std::numeric_limits< qint32>::min() || std::numeric_limits< qint32>::max()<l) { reportError(qBindExpectedSmallerNumber); return false; } n= qint32(l); return true; }
virtual bool tryBind( quint32& n) { quint64 l; if (!tryBind(l)) return false; if ( std::numeric_limits<quint32>::max()<l) { reportError(qBindExpectedSmallerNumber); return false; } n=quint32(l); 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; }
virtual bool tryBind( quint16& n) { quint64 l; if (!tryBind(l)) return false; if ( std::numeric_limits<quint16>::max()<l) { handleError(qBindExpectedSmallerNumber); return false; } n=quint16(l); return true; }
virtual bool tryBind( qint32& n) { qint64 l; if (!tryBind(l)) return false; if (l<std::numeric_limits< qint32>::min() || std::numeric_limits< qint32>::max()<l) { handleError(qBindExpectedSmallerNumber); return false; } n= qint32(l); return true; }
virtual bool tryBind( quint32& n) { quint64 l; if (!tryBind(l)) return false; if ( std::numeric_limits<quint32>::max()<l) { handleError(qBindExpectedSmallerNumber); return false; } n=quint32(l); return true; }
virtual bool tryBind( QUtf8Data& r) { if (hidden()) return true; if (I<=d) return itemBind()->tryBind(r); QVariant v=read(); if (v.type()!=QVariant ::String ) return false; r=QUtf8Data(v.toString().toUtf8()); return true; }
virtual bool tryBind( QString& r) { if (hidden()) return true; if (I<=d) return itemBind()->tryBind(r); QVariant v=read(); if (v.type()!=QVariant ::String ) return false; r= v.toString () ; return true; }
......
......@@ -93,7 +93,7 @@ class QSettingsReader : public QAbstractValueReader
{
Q_DISABLE_COPY(QSettingsReader)
public:
Q_ENABLE_MOVE(QSettingsReader, std::swap(isChoice, o.isChoice); )
Q_ENABLE_MOVE_DEFAULT(QSettingsReader)
QSettingsReader(QSettings* s) : settings(s) { Q_ASSERT(s); levels.push(Level(qBindExpectedItem)); }
QAsciiData currentPath() {
......@@ -127,19 +127,19 @@ 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; } reportError(qBindExpectedSequence); return false; }
bool tryRecord (quint32* =nullptr) { if (levels.last().isGroup) { levels.push(Level(qBindExpectedItem)); return true; } reportError(qBindExpectedRecord ); return false; }
bool tryNull ( ) { if (settings->value(key()).isNull()) { return true; } reportError(qBindExpectedNull ); return false; }
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 tryNull ( ) { if (settings->value(key()).isNull()) { return true; } handleError(qBindExpectedNull ); return false; }
bool tryItem(QIdentifier& k) { levels.last().key=k ; return true; }
bool tryItem( ) { levels.last().idx++ ; return true; }
bool tryOut ( ) { levels.pop(); settings->endGroup(); return true; }
bool isValid() const noexcept { return settings; }
void reportError(QIdentifierLiteral error, QString context = QString()) { if (errorHandler) errorHandler(error, QString(currentPath().latin1()).append(context)); }
bool handleError(QIdentifierLiteral error, QString context = QString()) { return errorHandler ? errorHandler(error, QString(currentPath().latin1()).append(context)) : false; }
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; } reportError(error); return false; }
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; }
QString key() {
Q_ASSERT(!levels.isEmpty());
return !levels.last().key.isNull()
......@@ -150,5 +150,4 @@ private:
QSettings* settings;
struct Level { QIdentifier key; int idx=-1; bool isGroup=true; Level(QIdentifier n=QIdentifier(), bool isGroup=true) : key(n), isGroup(isGroup) {} };
QStack<Level> levels;
bool isChoice = false;
};
This diff is collapsed.
......@@ -134,48 +134,48 @@ protected:
// TODO Support _meta to be able to cache and restitute all metadata as well as data+datatype
template<typename T>
bool tryBind(T& t) { if (current()->type()==qMetaTypeId<T>()) { t = current()->value<T>(); return true; } reportError(QIdentifierLiteral("ExpectedDeclaredMetatypeT")); return false; }
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; } reportError(qBindExpectedText ); return false; }
bool tryBind( QString& t) { if (current()->type()==qMetaTypeId< QString>()) { t = current()->value< QString>(); return true; } reportError(qBindExpectedText ); return false; }
bool tryBind( bool& t) { if (current()->type()==qMetaTypeId< bool>()) { t = current()->value< bool>(); return true; } reportError(qBindExpectedBoolean ); return false; }
bool tryBind(QByteArray& t) { if (current()->type()==qMetaTypeId<QByteArray>()) { t = current()->value<QByteArray>(); return true; } reportError(qBindExpectedBytes ); return false; }
==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; }
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; } reportError(qBindExpectedInteger ); return false; }
bool tryBind( qint8& t) { if (current()->type()==qMetaTypeId< qint8>()) { t = current()->value< qint8>(); return true; } handleError(qBindExpectedInteger ); return false; }
bool tryBind( qint16& t) { if (current()->type()==qMetaTypeId< qint8>()||
current()->type()==qMetaTypeId< qint16>()) { t = current()->value< qint16>(); return true; } reportError(qBindExpectedInteger ); return false; }
current()->type()==qMetaTypeId< qint16>()) { t = current()->value< qint16>(); return true; } handleError(qBindExpectedInteger ); 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; } reportError(qBindExpectedInteger ); return false; }
current()->type()==qMetaTypeId< qint32>()) { t = current()->value< qint32>(); return true; } handleError(qBindExpectedInteger ); 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; } reportError(qBindExpectedInteger ); return false; }
bool tryBind( quint8& t) { if (current()->type()==qMetaTypeId< quint8>()) { t = current()->value< quint8>(); return true; } reportError(qBindExpectedPositiveInteger); return false; }
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; }
bool tryBind( quint16& t) { if (current()->type()==qMetaTypeId< quint8>()||
current()->type()==qMetaTypeId< quint16>()) { t = current()->value< quint16>(); return true; } reportError(qBindExpectedPositiveInteger); return false; }
current()->type()==qMetaTypeId< quint16>()) { t = current()->value< quint16>(); return true; } handleError(qBindExpectedPositiveInteger); 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; } reportError(qBindExpectedPositiveInteger); return false; }
current()->type()==qMetaTypeId< quint32>()) { t = current()->value< quint32>(); return true; } handleError(qBindExpectedPositiveInteger); 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; } reportError(qBindExpectedPositiveInteger); return false; }
bool tryBind( float& t) { if (current()->type()==qMetaTypeId< float>()) { t = current()->value< float>(); return true; } reportError(qBindExpectedDecimal ); return false; }
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; }
bool tryBind( double& t) { if (current()->type()==qMetaTypeId< float>()||
current()->type()==qMetaTypeId< double>()) { t = current()->value< double>(); return true; } reportError(qBindExpectedDecimal ); return false; }
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; } reportError(qBindExpectedSequence); return false; }
bool tryRecord (quint32* =nullptr) { if (current()->type()==QVariant::Map ) { levels.push(Level()); return true; } reportError(qBindExpectedRecord ); return false; }
bool tryNull ( ) { if (current()->isNull() ) { return true; } reportError(qBindExpectedNull ); 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 tryNull ( ) { if (current()->isNull() ) { return true; } handleError(qBindExpectedNull ); return false; }
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(); }
bool tryOut ( ) { levels.pop() ; return true; }
bool isValid() const noexcept { return value; }
void reportError(QIdentifierLiteral error, QString context = QString()) { if (errorHandler) errorHandler(error, QString(currentPath().latin1()).append(context)); }
bool handleError(QIdentifierLiteral error, QString context = QString()) { return errorHandler ? errorHandler(error, QString(currentPath().latin1()).append(context)) : false; }
private:
const QVariant* current(unsigned outer=0) const { return unsigned(levels.size())-outer <= 0 ? value : &(levels[unsigned(levels.size())-outer-1].item); }
......
......@@ -267,7 +267,7 @@ struct QTransmogrifier<QColor> {
do {
v=v.meta(n, m);
if (n==qmDataStreamVersion) {
if (m.utf8().toInt()<7) { v->reportError("UnsupportedQDataStreamVersion");
if (m.utf8().toInt()<7) { v->handleError("UnsupportedQDataStreamVersion");
return v.null();
}
QRgba64 c64 = c.rgba64();
......@@ -862,6 +862,7 @@ int main(int argc, char *argv[])
QStringList errors;
auto handler = [&errors](QIdentifierLiteral error, QString context) {
errors.append(context.append(error.latin1()));
return true;
};
GROUP("Person<>Json")//====================================================
{
......@@ -1144,7 +1145,7 @@ void doGuiExample() {
persons[1].phones.append(Phone{Phone::Office,"112"});
QStringList errors;
auto handler = [&errors](QIdentifierLiteral name, QString context){ errors.append(context.append(name.latin1())); };
auto handler = [&errors](QIdentifierLiteral name, QString context){ errors.append(context.append(name.latin1())); return true; };
// Various possibilities to customize bind
......
......@@ -57,12 +57,12 @@ Bindable |
Bindable>Cbor |bf647479706502666e756d6265726b2b34342031323334353637ff
Bindable>Json |{"type":2,"number":"+44 1234567"}
Person<>Json |================================================================================
Json>P |(Person) names:[ John Doe] height:1.75 age:-1 phones:[] comments: children:[] | IgnoredCharacter 0:1 IgnoredCharacter 0:15 IgnoredCharacter 0:23 IgnoredCharacter 0:40 IgnoredCharacter 0:42 IgnoredCharacter 0:58 IgnoredCharacter 0:102
P>Json |{"names":["John","Doe"],"height":1.75,"age":-1,"phones":[],"comments":"","children":[]}
Json>P |(Person) names:[ John Doe] height:1.75 age:-1 phones:[] comments: children:[] | at (0,1) IgnoredCharacter, at (0,15) IgnoredCharacter, at (0,23) IgnoredCharacter, at (0,40) IgnoredCharacter
P>Json |{"names":["John","Doe"],"height":1.7500000000000002,"age":-1,"phones":[],"comments":"","children":[]}
Json>P |(Person) names:[ John Doe] height:1.75 age:-1 phones:[] comments: children:[] |
P>JsonValue |{"age":-1,"children":[],"comments":"","height":1.75,"names":["John","Doe"],"phones":[]}
P>JsonValue |{"age":-1,"children":[],"comments":"","height":1.7500000000000004,"names":["John","Doe"],"phones":[]}
JsonValue>P |(Person) names:[ John Doe] height:1.75 age:-1 phones:[] comments: children:[] |
Json>JsonValue|{"age":-1,"children":[],"comments":"","height":1.75,"names":["John","Doe"],"phones":[]} |
Json>JsonValue|{"age":-1,"children":[],"comments":"","height":1.7500000000000004,"names":["John","Doe"],"phones":[]} |
Json>Cbor |bf656e616d65739f644a6f686e63446f65ff66686569676874fa3fe0000063616765206670686f6e65739fff68636f6d6d656e747360686368696c6472656e9fffff
Person<>Cbor |================================================================================
Cbor>P |(Person) names:[ John Doe] height:1.75 age:-1 phones:[] comments: children:[] |
......
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