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.
...@@ -118,18 +118,16 @@ public: ...@@ -118,18 +118,16 @@ public:
/**/ QSequence sequence(quint32* s=nullptr) { return QValueStatus(this).value().sequence(s); } /**/ QSequence sequence(quint32* s=nullptr) { return QValueStatus(this).value().sequence(s); }
template<typename T> QValueStatus bind ( T&& t) { return QValueStatus(this).value().bind(std::forward<T>(t)); } template<typename T> QValueStatus bind ( T&& t) { return QValueStatus(this).value().bind(std::forward<T>(t)); }
protected: protected:
bool trySequence(quint32* =nullptr) { if (current().isArray ()) { steps.push(Step()); bool trySequence(quint32* =nullptr) { if (current().isArray ()) { steps.push(Step()); return true; } handleError(qBindExpectedSequence); return false; }
return true; } reportError(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()); bool tryNull ( ) { if (current().isNull ()) { return true; } handleError(qBindExpectedNull ); return false; }
return true; } reportError(qBindExpectedRecord ); return false; } bool tryBind ( QUtf8Data& u) { QString s; if (tryBind(s) ) { u = s .toUtf8 (); return true; } return false; }
bool tryNull ( ) { if (current().isNull ()) { return true; } reportError(qBindExpectedNull ); return false; } bool tryBind ( QString& v) { if (current().isString()) { v = current().toString(); return true; } handleError(qBindExpectedText ); return false; }
bool tryBind ( QUtf8Data& u) { QString s; if (tryBind(s) ) { u = s .toUtf8 (); return true; } return false; } bool tryBind ( bool& v) { if (current().isBool ()) { v = current().toBool (); return true; } handleError(qBindExpectedBoolean ); return false; }
bool tryBind ( QString& v) { if (current().isString()) { v = current().toString(); return true; } reportError(qBindExpectedText ); return false; } bool tryBind ( qint64& t) { double d; if (tryBind(d) ) { t = qint64(d) ; return true; } return false; }
bool tryBind ( bool& v) { if (current().isBool ()) { v = current().toBool (); return true; } reportError(qBindExpectedBoolean ); return false; } bool tryBind ( quint64& t) { double d; if (tryBind(d) ) { t = quint64(d) ; return true; } return false; }
bool tryBind ( qint64& t) { double d; if (tryBind(d) ) { t = qint64(d) ; return true; } return false; } bool tryBind ( float& v) { double d; if (tryBind(d) ) { v = float(d) ; return true; } return false; }
bool tryBind ( quint64& t) { double d; if (tryBind(d) ) { t = quint64(d) ; return true; } return false; } bool tryBind ( double& v) { if (current().isDouble()) { v = current().toDouble(); return true; } handleError(qBindExpectedDecimal ); 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; } reportError(qBindExpectedDecimal ); return false; }
bool tryItem(QIdentifierLiteral u) { steps.last().key=u; return !(steps.last().item = current(1).toObject().value(steps.last().key.latin1())).isUndefined(); } bool tryItem(QIdentifierLiteral u) { steps.last().key=u; return !(steps.last().item = current(1).toObject().value(steps.last().key.latin1())).isUndefined(); }
bool tryItem(QIdentifier& k) { steps.last().key=k; return !(steps.last().item = current(1).toObject().value(steps.last().key.latin1())).isUndefined(); } bool tryItem(QIdentifier& k) { steps.last().key=k; return !(steps.last().item = current(1).toObject().value(steps.last().key.latin1())).isUndefined(); }
...@@ -137,7 +135,7 @@ protected: ...@@ -137,7 +135,7 @@ protected:
bool tryOut ( ) { steps.removeLast(); return true; } bool tryOut ( ) { steps.removeLast(); return true; }
bool isValid() const noexcept { return true; } bool isValid() const noexcept { return true; }
void reportError(QIdentifierLiteral e, QString context = QString()) { if (errorHandler) errorHandler(e, QString(currentPath().latin1()).append(context)); } bool handleError(QIdentifierLiteral e, QString context = QString()) { return errorHandler ? errorHandler(e, QString(currentPath().latin1()).append(context)) : false; }
private: private:
const QJsonValue& current(int outer=0) const { Q_ASSERT(0<=outer && json); return steps.size()-outer <= 0 ? *json : steps[steps.size()-outer-1].item; } const QJsonValue& current(int outer=0) const { Q_ASSERT(0<=outer && json); return steps.size()-outer <= 0 ? *json : steps[steps.size()-outer-1].item; }
...@@ -229,7 +227,6 @@ public: ...@@ -229,7 +227,6 @@ public:
void reset(QIODevice* other) { void reset(QIODevice* other) {
errors.clear(); levels.clear(); errors.clear(); levels.clear();
line=0; column=0; index=-1; line=0; column=0; index=-1;
isChoice=false;
cachedNumber = None; d=.0; i=0; neg=bool(); cachedNumber = None; d=.0; i=0; neg=bool();
cacheLevel = 0; cachedValue = QJsonValue(); cacheLevel = 0; cachedValue = QJsonValue();
cacheWriter.reset(&cachedValue); cacheWriter.reset(&cachedValue);
...@@ -251,23 +248,23 @@ protected: ...@@ -251,23 +248,23 @@ protected:
enum CachedNumber : quint8 { None=0, Integer, FloatingPoint }; enum CachedNumber : quint8 { None=0, Integer, FloatingPoint };
bool trySequence(quint32* s=nullptr) { if (caching) { cacheLevel++; return caching->trySequence(s); } bool trySequence(quint32* s=nullptr) { if (caching) { cacheLevel++; return caching->trySequence(s); }
if (get('[', "[{\"ntf-0123456789.")) { levels.push(Step(-1,"]")); return true; } reportError(qBindExpectedSequence); return false; } if (get('[', "[{\"ntf-0123456789.")) { levels.push(Step(-1,"]")); return true; } handleError(qBindExpectedSequence); return false; }
bool tryRecord (quint32* s=nullptr) { if (caching) { cacheLevel++; return caching->tryRecord(s); } bool tryRecord (quint32* s=nullptr) { if (caching) { cacheLevel++; return caching->tryRecord(s); }
if (get('{', "[{\"ntf-0123456789.")) { levels.push(Step(-1,"}")); return true; } reportError(qBindExpectedRecord ); return false; } if (get('{', "[{\"ntf-0123456789.")) { levels.push(Step(-1,"}")); return true; } handleError(qBindExpectedRecord ); return false; }
bool tryNull ( ) { if (caching) { cacheLevel++; return caching->tryNull() && cacheOut(); } bool tryNull ( ) { if (caching) { cacheLevel++; return caching->tryNull() && cacheOut(); }
if (get('n', "[{\"ntf-0123456789.") && if (get('n', "[{\"ntf-0123456789.") &&
get('u', "[{\"" ) && get('u', "[{\"" ) &&
get('l', "[{\"" ) && get('l', "[{\"" ) &&
get('l', "[{\"" )) { get('l', "[{\"" )) {
return true; return true;
} else { reportError(qBindExpectedNull); return false; } } } else { handleError(qBindExpectedNull); return false; } }
bool tryBind ( QUtf8Data& s) { if (caching) { cacheLevel++; return caching->tryBind(s) && cacheOut(); } bool tryBind ( QUtf8Data& s) { if (caching) { cacheLevel++; return caching->tryBind(s) && cacheOut(); }
QByteArray u; QByteArray u;
if ( get('"', "[{\"ntf-0123456789.")) { u.resize(0); char c; if ( get('"', "[{\"ntf-0123456789.")) { u.resize(0); char c;
while ((c=getCharInString()) != '\0' ) { u.append(c); } while ((c=getCharInString()) != '\0' ) { u.append(c); }
s=QUtf8Data(u); s=QUtf8Data(u);
return get('"'); return get('"');
} else { reportError(qBindExpectedText); return false; } } } else { handleError(qBindExpectedText); return false; } }
bool tryBind ( QString& s) { QUtf8Data u; if (tryBind(u)) { s = QString(u.utf8()); return true; } 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) { cacheLevel++; return caching->tryBind(bool(b)) && cacheOut(); } bool tryBind ( bool& b) { if (caching) { cacheLevel++; return caching->tryBind(bool(b)) && cacheOut(); }
if (get('t', "[{\"ntf-0123456789.") && if (get('t', "[{\"ntf-0123456789.") &&
...@@ -282,11 +279,11 @@ protected: ...@@ -282,11 +279,11 @@ protected:
get('s', "[{\"" ) && get('s', "[{\"" ) &&
get('e', "[{\"" )) { b=false; get('e', "[{\"" )) { b=false;
return true; return true;
} else { reportError(qBindExpectedBoolean); return false; } } } else { handleError(qBindExpectedBoolean); return false; } }
bool tryBind ( float& n) { if (caching) { cacheLevel++; return caching->tryBind(double(n)) && cacheOut(); } bool tryBind ( float& n) { if (caching) { cacheLevel++; return caching->tryBind(double(n)) && cacheOut(); }
if (getNumber()==None) return false; // already reported qBindExpectedDecimal if (getNumber()==None) return false; // already reported qBindExpectedDecimal
if (d<double( std::numeric_limits<float >::min())|| if (d<double( std::numeric_limits<float >::min())||
double( std::numeric_limits<float >::max())<d) { reportError(qBindExpectedSmallerNumber ); return false; } double( std::numeric_limits<float >::max())<d) { handleError(qBindExpectedSmallerNumber); return false; }
n = float(d); cachedNumber=None; return true; } n = float(d); cachedNumber=None; return true; }
bool tryBind ( double& n) { if (caching) { cacheLevel++; return caching->tryBind(double(n)) && cacheOut(); } bool tryBind ( double& n) { if (caching) { cacheLevel++; return caching->tryBind(double(n)) && cacheOut(); }
if (getNumber()==None) return false; // already reported qBindExpectedDecimal if (getNumber()==None) return false; // already reported qBindExpectedDecimal
...@@ -294,16 +291,16 @@ protected: ...@@ -294,16 +291,16 @@ protected:
bool tryBind ( quint64& t) { if (caching) { cacheLevel++; return caching->tryBind(double(t)) && cacheOut(); } bool tryBind ( quint64& t) { if (caching) { cacheLevel++; return caching->tryBind(double(t)) && cacheOut(); }
auto r=getNumber(); auto r=getNumber();
if (r==None) return false; // already reported qBindExpectedDecimal if (r==None) return false; // already reported qBindExpectedDecimal
if (r==FloatingPoint) { reportError(qBindExpectedInteger); return false; } if (r==FloatingPoint) { handleError(qBindExpectedInteger); return false; }
if (neg) { reportError(qBindExpectedPositiveInteger); return false; } if (neg) { handleError(qBindExpectedPositiveInteger); return false; }
t = i ; cachedNumber=None; return true; } t = i ; cachedNumber=None; return true; }
bool tryBind ( qint64& t) { if (caching) { cacheLevel++; return caching->tryBind(double(t)) && cacheOut(); } bool tryBind ( qint64& t) { if (caching) { cacheLevel++; return caching->tryBind(double(t)) && cacheOut(); }
auto r=getNumber(); auto r=getNumber();
if (r==None) return false; // already reported qBindExpectedDecimal if (r==None) return false; // already reported qBindExpectedDecimal
if (r==FloatingPoint) { reportError(qBindExpectedInteger); return false; } if (r==FloatingPoint) { handleError(qBindExpectedInteger); 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())) { t = qint64(i); cachedNumber=None; return true; }
if ( neg && i<quint64(-std::numeric_limits<qint64>::min())) { t = -qint64(i); cachedNumber=None; return true; } if ( neg && i<quint64(-std::numeric_limits<qint64>::min())) { t = -qint64(i); cachedNumber=None; return true; }
reportError(qBindExpectedSmallerNumber ); return false; } handleError(qBindExpectedSmallerNumber ); return false; }
bool tryBind ( QVariant& dst) { bool tryBind ( QVariant& dst) {
auto suspended = setErrorHandler(); auto suspended = setErrorHandler();
...@@ -320,7 +317,7 @@ protected: ...@@ -320,7 +317,7 @@ protected:
dst = QVariant(QString::fromUtf8(s.utf8())) ; return true; dst = QVariant(QString::fromUtf8(s.utf8())) ; return true;
} }
setErrorHandler(suspended); setErrorHandler(suspended);
if (!tryNull()) reportError(QIdentifierLiteral("ExpectedOneOfBooleanDecimalBytesTextSequenceRecordNull")); if (!tryNull()) handleError(QIdentifierLiteral("ExpectedOneOfBooleanDecimalBytesTextSequenceRecordNull"));
dst = QVariant(); return true; dst = QVariant(); return true;
} }
...@@ -375,7 +372,7 @@ protected: ...@@ -375,7 +372,7 @@ protected:
return true; } return true; }
bool isValid() const noexcept { return io; } bool isValid() const noexcept { return io; }
void reportError(QIdentifierLiteral e, QString context = QString()) { if (errorHandler) errorHandler(e, QString("at (%1,%2) ").arg(line).arg(column).append(context)); } bool handleError(QIdentifierLiteral e, QString context = QString()) { return errorHandler ? errorHandler(e, QString("at (%1,%2) ").arg(line).arg(column).append(context)) : false; }
private: private:
CachedNumber getNumber() { CachedNumber getNumber() {
if (cachedNumber!=None) return cachedNumber; if (cachedNumber!=None) return cachedNumber;
...@@ -383,7 +380,7 @@ private: ...@@ -383,7 +380,7 @@ private:
neg = get('-', "[{\"ntf-0123456789."); neg = get('-', "[{\"ntf-0123456789.");
qint8 digit; qint8 digit;
if ((digit = getDigit()) < 0) { if ((digit = getDigit()) < 0) {
reportError(qBindExpectedDecimal); handleError(qBindExpectedDecimal);
return None; // do not accept no digit otherwise we may accept an empty mantissa or string! return None; // do not accept no digit otherwise we may accept an empty mantissa or string!
} }
cachedNumber=Integer; cachedNumber=Integer;
...@@ -478,7 +475,9 @@ private: ...@@ -478,7 +475,9 @@ private:
while (!(nextChar() == expected || strchr(validChars, nextChar()) || nextChar() == '\0')) { while (!(nextChar() == expected || strchr(validChars, nextChar()) || nextChar() == '\0')) {
char ignored = getChar(); char ignored = getChar();
if (!isspace(ignored)) { if (!isspace(ignored)) {
reportError(qBindIgnoredCharacter); if (!handleError(qBindIgnoredCharacter)) {
return false;
}
} }
} }
} }
...@@ -488,7 +487,6 @@ private: ...@@ -488,7 +487,6 @@ private:
QIODevice* io; QIODevice* io;
int line = 0, column = 0, index = -1; int line = 0, column = 0, index = -1;
QStack<Step> levels = QStack<Step>(); //!< dynamic context required to implement item() and report meaningful errors QStack<Step> levels = QStack<Step>(); //!< dynamic context required to implement item() and report meaningful errors
bool isChoice = false;
// Read caching of numbers // Read caching of numbers
CachedNumber cachedNumber = None; CachedNumber cachedNumber = None;
...@@ -541,7 +539,7 @@ struct QTransmogrifier<QJsonValue> { ...@@ -541,7 +539,7 @@ struct QTransmogrifier<QJsonValue> {
quint64 u; if ((r = v.bind(u))) { j = QJsonValue(double(u)); return r; } quint64 u; if ((r = v.bind(u))) { j = QJsonValue(double(u)); return r; }
double d; if ((r = v.bind(d))) { j = QJsonValue( d ); return r; } double d; if ((r = v.bind(d))) { j = QJsonValue( d ); return r; }
v->setErrorHandler(suspended); v->setErrorHandler(suspended);
if (!(r = v.null())) r.reportError("ExpectedBoolDoubleQStringQJsonArrayQJsonOnjectNull"); if (!(r = v.null())) r.handleError("ExpectedBoolDoubleQStringQJsonArrayQJsonOnjectNull");
/**/ j = QJsonValue( ); return r; /**/ j = QJsonValue( ); return r;
} }
else { Q_ASSERT_X(!v, Q_FUNC_INFO, "Unsupported v->mode()"); return v.null(); } else { Q_ASSERT_X(!v, Q_FUNC_INFO, "Unsupported v->mode()"); return v.null(); }
......
...@@ -154,13 +154,16 @@ protected: ...@@ -154,13 +154,16 @@ protected:
virtual bool tryBind( QByteArray& r) { return tryBind(QByteArray(r)); } virtual bool tryBind( QByteArray& r) { return tryBind(QByteArray(r)); }
virtual bool tryBind( QVariant& r) { return tryBind( QVariant(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) { if (errorHandler) {
QString path; QString path;
for (auto current = parent; parent.isValid(); current = current.parent()) { for (auto current = parent; parent.isValid(); current = current.parent()) {
path.prepend(QString('/').append(current.row()).append(',').append(current.column())); 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: ...@@ -216,7 +219,7 @@ protected:
bool metaColumnNames=false; bool metaColumnNames=false;
QVector<int> sizes; QVector<int> sizes;
QValueErrorHandler errorHandler = QValueErrorHandler(); QValueErrorHandler errorHandler = nullptr;
}; };
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
...@@ -313,7 +316,7 @@ protected: ...@@ -313,7 +316,7 @@ protected:
row++; col=0; row++; col=0;
return true; return true;
} }
reportError(qBindIgnoredItem); handleError(qBindIgnoredItem);
return false; return false;
} }
return true; return true;
...@@ -328,7 +331,7 @@ protected: ...@@ -328,7 +331,7 @@ protected:
row=0; col++; row=0; col++;
return true; return true;
} }
reportError(qBindIgnoredItem); handleError(qBindIgnoredItem);
return false; return false;
} }
return true; return true;
...@@ -358,8 +361,8 @@ protected: ...@@ -358,8 +361,8 @@ protected:
else { else {
col=columnNames.indexOf(n.utf8()); col=columnNames.indexOf(n.utf8());
// TODO if (max(dimension())<=col) col=-1; // TODO if (max(dimension())<=col) col=-1;
if (col<0) { if (col<0 && !handleError(qBindIgnoredItem)) {
reportError(qBindIgnoredItem); return false;
} }
} }
return true; return true;
...@@ -376,8 +379,7 @@ protected: ...@@ -376,8 +379,7 @@ protected:
} }
else { else {
// TODO if (rowNames... // TODO if (rowNames...
reportError(qBindIgnoredItemName); return handleError(qBindIgnoredItemName);
return true;
} }
} }
else { Q_ASSERT(I<=d); else { Q_ASSERT(I<=d);
...@@ -509,7 +511,7 @@ protected: ...@@ -509,7 +511,7 @@ protected:
if (col < m->columnCount()) { if (col < m->columnCount()) {
return true; return true;
} }
reportError(qBindIgnoredItem); handleError(qBindIgnoredItem);
return false; return false;
} }
return true; return true;
...@@ -527,7 +529,7 @@ protected: ...@@ -527,7 +529,7 @@ protected:
row=0; col++; row=0; col++;
return true; return true;
} }
reportError(qBindIgnoredItem); handleError(qBindIgnoredItem);
return false; return false;
} }
return true; return true;
...@@ -559,8 +561,8 @@ protected: ...@@ -559,8 +561,8 @@ protected:
col=columnNames.indexOf(n.utf8()); col=columnNames.indexOf(n.utf8());
} }
// TODO if (max(dimension())<=col) col=-1; // TODO if (max(dimension())<=col) col=-1;
if (col<0) { if (col<0 && !handleError(qBindIgnoredItem)) {
reportError(qBindIgnoredItem); return false;
} }
return true; return true;
} }
...@@ -573,8 +575,7 @@ protected: ...@@ -573,8 +575,7 @@ protected:
} }
else if (n!=childrenName) { else if (n!=childrenName) {
// TODO if (rowNames... // TODO if (rowNames...
reportError(qBindIgnoredItemName); return handleError(qBindIgnoredItemName);
return true;
} }
else { return true; } else { return true; }
} }
...@@ -605,12 +606,12 @@ protected: ...@@ -605,12 +606,12 @@ protected:
// TODO QDate*, QTime // TODO QDate*, QTime
// TODO QPixmap if metadata suggests a QMimeData image ? // 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( 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) { reportError(qBindExpectedSmallerNumber); return false; } n= quint8(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) { reportError(qBindExpectedSmallerNumber); return false; } n= qint16(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) { reportError(qBindExpectedSmallerNumber); return false; } n=quint16(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) { reportError(qBindExpectedSmallerNumber); return false; } n= qint32(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) { reportError(qBindExpectedSmallerNumber); return false; } n=quint32(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( 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; } 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 ...@@ -93,7 +93,7 @@ class QSettingsReader : public QAbstractValueReader
{ {
Q_DISABLE_COPY(QSettingsReader) Q_DISABLE_COPY(QSettingsReader)
public: 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)); } QSettingsReader(QSettings* s) : settings(s) { Q_ASSERT(s); levels.push(Level(qBindExpectedItem)); }
QAsciiData currentPath() { QAsciiData currentPath() {
...@@ -127,19 +127,19 @@ protected: ...@@ -127,19 +127,19 @@ protected:
bool tryBind( float& t) { return set(t, qBindExpectedDecimal ); } bool tryBind( float& t) { return set(t, qBindExpectedDecimal ); }
bool tryBind( double& 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 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; } reportError(qBindExpectedRecord ); 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; } reportError(qBindExpectedNull ); 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(QIdentifier& k) { levels.last().key=k ; return true; }
bool tryItem( ) { levels.last().idx++ ; return true; } bool tryItem( ) { levels.last().idx++ ; return true; }
bool tryOut ( ) { levels.pop(); settings->endGroup(); return true; } bool tryOut ( ) { levels.pop(); settings->endGroup(); return true; }
bool isValid() const noexcept { return settings; } 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: private:
template<typename T> 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() { QString key() {
Q_ASSERT(!levels.isEmpty()); Q_ASSERT(!levels.isEmpty());
return !levels.last().key.isNull() return !levels.last().key.isNull()
...@@ -150,5 +150,4 @@ private: ...@@ -150,5 +150,4 @@ private:
QSettings* settings; QSettings* settings;
struct Level { QIdentifier key; int idx=-1; bool isGroup=true; Level(QIdentifier n=QIdentifier(), bool isGroup=true) : key(n), isGroup(isGroup) {} }; struct Level { QIdentifier key; int idx=-1; bool isGroup=true; Level(QIdentifier n=QIdentifier(), bool isGroup=true) : key(n), isGroup(isGroup) {} };
QStack<Level> levels; QStack<Level> levels;
bool isChoice = false;
}; };
This diff is collapsed.
...@@ -134,48 +134,48 @@ protected: ...@@ -134,48 +134,48 @@ protected:
// TODO Support _meta to be able to cache and restitute all metadata as well as data+datatype // TODO Support _meta to be able to cache and restitute all metadata as well as data+datatype
template<typename T> 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 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; } ==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; } reportError(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; } reportError(qBindExpectedBoolean ); 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; } reportError(qBindExpectedBytes ); 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 // 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>()|| 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>()|| bool tryBind( qint32& t) { if (current()->type()==qMetaTypeId< qint8>()||
current()->type()==qMetaTypeId< qint16>()|| 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>()|| bool tryBind( qint64& t) { if (current()->type()==qMetaTypeId< qint8>()||
current()->type()==qMetaTypeId< qint16>()|| current()->type()==qMetaTypeId< qint16>()||
current()->type()==qMetaTypeId< qint32>()|| current()->type()==qMetaTypeId< qint32>()||
current()->type()==qMetaTypeId< qint64>()) { t = current()->value< qint64>(); return true; } reportError(qBindExpectedInteger ); 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; } reportError(qBindExpectedPositiveInteger); 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>()|| 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>()|| bool tryBind( quint32& t) { if (current()->type()==qMetaTypeId< quint8>()||
current()->type()==qMetaTypeId< quint16>()|| 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>()|| bool tryBind( quint64& t) { if (current()->type()==qMetaTypeId< quint8>()||
current()->type()==qMetaTypeId< quint16>()|| current()->type()==qMetaTypeId< quint16>()||
current()->type()==qMetaTypeId< quint32>()|| current()->type()==qMetaTypeId< quint32>()||
current()->type()==qMetaTypeId< quint64>()) { t = current()->value< quint64>(); return true; } reportError(qBindExpectedPositiveInteger); 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; } reportError(qBindExpectedDecimal ); 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>()|| 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 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; } reportError(qBindExpectedRecord ); 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; } reportError(qBindExpectedNull ); return false; } bool tryNull ( ) { if (current()->isNull() ) { return true; } handleError(qBindExpectedNull ); return false; }