Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
modmedLog
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MODMED
modmedLog
Commits
99d363dc
Commit
99d363dc
authored
Nov 19, 2019
by
EXT Arnaud Clère
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
WIP move error handling to user's hands
parent
d523f5ea
Changes
7
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
358 additions
and
377 deletions
+358
-377
tests/QBind/QCbor_impl.h
tests/QBind/QCbor_impl.h
+42
-52
tests/QBind/QJson_impl.h
tests/QBind/QJson_impl.h
+40
-47
tests/QBind/QModel_impl.h
tests/QBind/QModel_impl.h
+25
-18
tests/QBind/QSettings_impl.h
tests/QBind/QSettings_impl.h
+5
-9
tests/QBind/QValue.h
tests/QBind/QValue.h
+129
-131
tests/QBind/QVariant_impl.h
tests/QBind/QVariant_impl.h
+20
-25
tests/QBind/main.cpp
tests/QBind/main.cpp
+97
-95
No files found.
tests/QBind/QCbor_impl.h
View file @
99d363dc
...
...
@@ -198,7 +198,7 @@ protected:
bool
tryBind
(
bool
&&
b
)
{
set
(
QCborValue
(
b
));
return
true
;
}
bool
tryBind
(
float
&&
d
)
{
set
(
QCborValue
(
double
(
d
)));
return
true
;
}
bool
tryBind
(
double
&&
d
)
{
set
(
QCborValue
(
d
));
return
true
;
}
bool
tryBind
(
quint64
&&
n
)
{
if
(
quint64
(
std
::
numeric_limits
<
qint64
>::
max
())
<
n
)
{
_
reportError
(
qBindExpectedSmallerNumber
);
return
false
;
}
bool
tryBind
(
quint64
&&
n
)
{
if
(
quint64
(
std
::
numeric_limits
<
qint64
>::
max
())
<
n
)
{
reportError
(
qBindExpectedSmallerNumber
);
return
false
;
}
set
(
QCborValue
(
qint64
(
n
)));
return
true
;
}
bool
tryBind
(
qint64
&&
n
)
{
set
(
QCborValue
(
n
));
return
true
;
}
bool
tryBind
(
QByteArray
&&
s
)
{
set
(
QCborValue
(
s
));
return
true
;
}
...
...
@@ -231,10 +231,7 @@ class QCborVisitor : public QAbstractValueReader
Q_DISABLE_COPY
(
QCborVisitor
)
public:
QCborVisitor
(
QCborValue
*
v
)
:
cbor
(
v
)
{
Q_ASSERT
(
v
);
}
void
reset
(
QCborValue
*
v
)
{
cbor
=
v
;
Q_ASSERT
(
v
);
steps
.
resize
(
0
);
errors
.
resize
(
0
);
}
struct
Error
{
QIdentifierLiteral
error
;
QAsciiData
path
;
QValueStatus
zap
(
QValue
&&
value
)
{
return
value
.
bind
(
QUtf8Data
(
error
.
utf8
()
+
' '
+
path
.
utf8
()));
}
};
QVector
<
Error
>
errors
;
void
reset
(
QCborValue
*
v
)
{
cbor
=
v
;
Q_ASSERT
(
v
);
steps
.
resize
(
0
);
}
QAsciiData
currentPath
()
{
QByteArray
path
;
...
...
@@ -250,17 +247,17 @@ public:
/**/
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
));
}
protected:
bool
trySequence
(
quint32
*
=
nullptr
)
{
if
(
current
().
isArray
())
{
steps
.
push
(
Step
());
return
true
;
}
_
reportError
(
qBindExpectedSequence
);
return
false
;
}
bool
tryRecord
(
quint32
*
=
nullptr
)
{
if
(
current
().
isMap
())
{
steps
.
push
(
Step
());
return
true
;
}
_
reportError
(
qBindExpectedRecord
);
return
false
;
}
bool
tryNull
(
)
{
if
(
current
().
isNull
())
{
return
true
;
}
_
reportError
(
qBindExpectedNull
);
return
false
;
}
bool
trySequence
(
quint32
*
=
nullptr
)
{
if
(
current
().
isArray
())
{
steps
.
push
(
Step
());
return
true
;
}
reportError
(
qBindExpectedSequence
);
return
false
;
}
bool
tryRecord
(
quint32
*
=
nullptr
)
{
if
(
current
().
isMap
())
{
steps
.
push
(
Step
());
return
true
;
}
reportError
(
qBindExpectedRecord
);
return
false
;
}
bool
tryNull
(
)
{
if
(
current
().
isNull
())
{
return
true
;
}
reportError
(
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
;
}
_
reportError
(
qBindExpectedText
);
return
false
;
}
bool
tryBind
(
bool
&
v
)
{
if
(
current
().
isBool
())
{
v
=
current
().
toBool
();
return
true
;
}
_
reportError
(
qBindExpectedBoolean
);
return
false
;
}
bool
tryBind
(
qint64
&
t
)
{
if
(
current
().
isInteger
())
{
t
=
current
().
toInteger
();
return
true
;
}
_
reportError
(
qBindExpectedInteger
);
return
false
;
}
bool
tryBind
(
QString
&
v
)
{
if
(
current
().
isString
())
{
v
=
current
().
toString
();
return
true
;
}
reportError
(
qBindExpectedText
);
return
false
;
}
bool
tryBind
(
bool
&
v
)
{
if
(
current
().
isBool
())
{
v
=
current
().
toBool
();
return
true
;
}
reportError
(
qBindExpectedBoolean
);
return
false
;
}
bool
tryBind
(
qint64
&
t
)
{
if
(
current
().
isInteger
())
{
t
=
current
().
toInteger
();
return
true
;
}
reportError
(
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
;
}
bool
tryBind
(
double
&
v
)
{
if
(
current
().
isDouble
())
{
v
=
current
().
toDouble
();
return
true
;
}
_
reportError
(
qBindExpectedDecimal
);
return
false
;
}
bool
tryBind
(
QByteArray
&
v
)
{
QString
s
;
if
(
current
().
isByteArray
())
{
v
=
current
().
toByteArray
();
return
true
;
}
_
reportError
(
qBindExpectedBytes
);
return
false
;
}
bool
tryBind
(
double
&
v
)
{
if
(
current
().
isDouble
())
{
v
=
current
().
toDouble
();
return
true
;
}
reportError
(
qBindExpectedDecimal
);
return
false
;
}
bool
tryBind
(
QByteArray
&
v
)
{
QString
s
;
if
(
current
().
isByteArray
())
{
v
=
current
().
toByteArray
();
return
true
;
}
reportError
(
qBindExpectedBytes
);
return
false
;
}
bool
tryItem
(
QIdentifierLiteral
u
)
{
steps
.
last
().
key
=
u
;
return
!
(
steps
.
last
().
item
=
current
(
1
).
toMap
().
value
(
steps
.
last
().
key
.
latin1
())).
isUndefined
();
}
bool
tryItem
(
QIdentifier
&
k
)
{
steps
.
last
().
key
=
k
;
return
!
(
steps
.
last
().
item
=
current
(
1
).
toMap
().
value
(
steps
.
last
().
key
.
latin1
())).
isUndefined
();
}
...
...
@@ -268,15 +265,13 @@ protected:
bool
tryOut
(
)
{
steps
.
pop
()
;
return
true
;
}
bool
_isOk
()
const
noexcept
{
return
cbor
;
}
void
_setChoice
(
bool
v
)
{
isChoice
=
v
;
}
void
_reportError
(
QIdentifierLiteral
e
)
{
if
(
!
isChoice
)
errors
.
append
(
Error
{
e
,
currentPath
()
});
}
void
reportError
(
QIdentifierLiteral
e
,
QString
context
=
QString
())
{
if
(
errorHandler
)
errorHandler
(
e
,
QString
(
currentPath
().
latin1
()).
append
(
context
));
}
private:
const
QCborValue
&
current
(
int
outer
=
0
)
const
{
Q_ASSERT
(
0
<=
outer
);
return
steps
.
size
()
-
outer
<=
0
?
*
cbor
:
steps
[
steps
.
size
()
-
outer
-
1
].
item
;
}
QCborValue
*
cbor
;
struct
Step
{
QIdentifier
key
;
int
idx
=-
1
;
QCborValue
item
;
Step
()
=
default
;
};
QStack
<
Step
>
steps
=
QStack
<
Step
>
();
bool
isChoice
=
false
;
};
// --------------------------------------------------------------------------
...
...
@@ -290,20 +285,17 @@ class QCborReader : public QAbstractValueReader, public QCborStreamReader
public:
QCborReader
(
QIODevice
*
io
)
:
QCborStreamReader
(
io
),
cacheVisitor
(
&
cachedValue
)
{
Q_ASSERT
(
io
);
}
struct
Error
{
QIdentifierLiteral
error
;
qint64
index
;
QCborError
cborError
;
QValueStatus
zap
(
QValue
&&
value
)
{
QByteArray
utf8
(
error
.
utf8
());
utf8
.
append
(
' '
).
append
(
QByteArray
::
number
(
index
));
return
value
.
bind
(
utf8
.
constData
());
}
};
QVector
<
Error
>
errors
;
// Shortcuts
/**/
QValue
value
(
)
{
return
QValueStatus
(
this
).
value
();
}
/**/
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
));
}
protected:
bool
trySequence
(
quint32
*
s
=
nullptr
)
{
if
(
caching
)
{
cacheLevel
++
;
return
caching
->
trySequence
(
s
);
}
skipTag
();
if
(
isArray
()
&&
enterContainer
())
{
levels
.
push
(
Level
());
return
true
;
}
_
reportError
(
qBindExpectedSequence
);
return
false
;
}
skipTag
();
if
(
isArray
()
&&
enterContainer
())
{
levels
.
push
(
Level
());
return
true
;
}
reportError
(
qBindExpectedSequence
);
return
false
;
}
bool
tryRecord
(
quint32
*
s
=
nullptr
)
{
if
(
caching
)
{
cacheLevel
++
;
return
caching
->
tryRecord
(
s
);
}
skipTag
();
if
(
isMap
()
&&
enterContainer
())
{
levels
.
push
(
Level
());
return
true
;
}
_
reportError
(
qBindExpectedRecord
);
return
false
;
}
skipTag
();
if
(
isMap
()
&&
enterContainer
())
{
levels
.
push
(
Level
());
return
true
;
}
reportError
(
qBindExpectedRecord
);
return
false
;
}
bool
tryNull
(
)
{
if
(
caching
)
{
cacheLevel
++
;
return
caching
->
tryNull
()
&&
cacheOut
();
}
skipTag
();
if
(
isNull
()
&&
next
())
{
return
true
;
}
_
reportError
(
qBindExpectedNull
);
return
false
;
}
skipTag
();
if
(
isNull
()
&&
next
())
{
return
true
;
}
reportError
(
qBindExpectedNull
);
return
false
;
}
bool
tryBind
(
QUtf8Data
&
u
)
{
QString
s
;
if
(
tryBind
(
s
))
{
u
=
s
.
toUtf8
();
return
true
;
}
return
false
;
}
bool
tryBind
(
QString
&
s
)
{
if
(
caching
)
{
cacheLevel
++
;
return
caching
->
tryBind
(
s
)
&&
cacheOut
();
}
skipTag
();
if
(
isString
())
{
...
...
@@ -315,13 +307,13 @@ protected:
}
if
(
r
.
status
==
QCborStreamReader
::
Error
)
{
if
(
!
s
.
isEmpty
())
{
_
reportError
(
qBindIgnoredBytes
);
reportError
(
qBindIgnoredBytes
);
s
.
resize
(
0
);
}
return
false
;
}
return
true
;
}
else
{
_
reportError
(
qBindExpectedBytes
);
return
false
;
}
}
}
else
{
reportError
(
qBindExpectedBytes
);
return
false
;
}
}
bool
tryBind
(
QByteArray
&
s
)
{
if
(
caching
)
{
cacheLevel
++
;
return
caching
->
tryBind
(
s
)
&&
cacheOut
();
}
skipTag
();
if
(
isByteArray
())
{
s
.
resize
(
0
);
...
...
@@ -332,36 +324,36 @@ protected:
}
if
(
r
.
status
==
QCborStreamReader
::
Error
)
{
if
(
!
s
.
isEmpty
())
{
_
reportError
(
qBindIgnoredBytes
);
reportError
(
qBindIgnoredBytes
);
s
.
resize
(
0
);
}
return
false
;
}
return
true
;
}
else
{
_
reportError
(
qBindExpectedBytes
);
return
false
;
}
}
}
else
{
reportError
(
qBindExpectedBytes
);
return
false
;
}
}
bool
tryBind
(
bool
&
b
)
{
if
(
caching
)
{
cacheLevel
++
;
return
caching
->
tryBind
(
b
)
&&
cacheOut
();
}
skipTag
();
if
(
isBool
())
{
b
=
toBool
();
return
next
();
}
else
{
_
reportError
(
qBindExpectedBoolean
);
return
false
;
}
}
}
else
{
reportError
(
qBindExpectedBoolean
);
return
false
;
}
}
bool
tryBind
(
double
&
n
)
{
if
(
caching
)
{
cacheLevel
++
;
return
caching
->
tryBind
(
n
)
&&
cacheOut
();
}
return
getNumber
(
n
);
}
bool
tryBind
(
float
&
n
)
{
double
d
;
if
(
!
tryBind
(
d
))
{
return
false
;
}
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
)
{
reportError
(
qBindExpectedSmallerNumber
);
return
false
;
}
n
=
float
(
d
);
return
true
;
}
bool
tryBind
(
quint64
&
t
)
{
if
(
caching
)
{
cacheLevel
++
;
return
caching
->
tryBind
(
t
)
&&
cacheOut
();
}
quint64
i
;
bool
neg
;
if
(
!
getInteger
(
i
,
neg
))
{
_
reportError
(
qBindExpectedInteger
);
return
false
;
}
if
(
neg
)
{
_
reportError
(
qBindExpectedPositiveInteger
);
return
false
;
}
if
(
!
getInteger
(
i
,
neg
))
{
reportError
(
qBindExpectedInteger
);
return
false
;
}
if
(
neg
)
{
reportError
(
qBindExpectedPositiveInteger
);
return
false
;
}
t
=
i
;
return
true
;
}
bool
tryBind
(
qint64
&
t
)
{
if
(
caching
)
{
cacheLevel
++
;
return
caching
->
tryBind
(
t
)
&&
cacheOut
();
}
quint64
i
;
bool
neg
;
if
(
!
getInteger
(
i
,
neg
))
{
_
reportError
(
qBindExpectedInteger
);
return
false
;
}
if
(
neg
)
{
if
(
quint64
(
-
std
::
numeric_limits
<
qint64
>::
min
())
<
i
)
{
_
reportError
(
qBindExpectedSmallerNumber
);
return
false
;
}
t
=-
qint64
(
i
);
return
true
;
}
else
{
if
(
quint64
(
std
::
numeric_limits
<
qint64
>::
max
())
<
i
)
{
_
reportError
(
qBindExpectedSmallerNumber
);
return
false
;
}
t
=
qint64
(
i
);
return
true
;
}
if
(
!
getInteger
(
i
,
neg
))
{
reportError
(
qBindExpectedInteger
);
return
false
;
}
if
(
neg
)
{
if
(
quint64
(
-
std
::
numeric_limits
<
qint64
>::
min
())
<
i
)
{
reportError
(
qBindExpectedSmallerNumber
);
return
false
;
}
t
=-
qint64
(
i
);
return
true
;
}
else
{
if
(
quint64
(
std
::
numeric_limits
<
qint64
>::
max
())
<
i
)
{
reportError
(
qBindExpectedSmallerNumber
);
return
false
;
}
t
=
qint64
(
i
);
return
true
;
}
}
bool
tryItem
(
QIdentifierLiteral
u
)
{
if
(
caching
)
{
return
caching
->
tryItem
(
u
);
}
...
...
@@ -428,20 +420,19 @@ protected:
bool
tryAny
()
{
return
next
();
}
bool
_isOk
()
const
noexcept
{
return
const_cast
<
QCborReader
*>
(
this
)
->
lastError
()
==
QCborError
::
NoError
;
}
void
_setChoice
(
bool
b
)
{
isChoice
=
b
;
}
void
_reportError
(
QIdentifierLiteral
e
)
{
if
(
!
isChoice
)
errors
.
append
(
Error
{
e
,
currentOffset
(),
lastError
()
});
}
void
reportError
(
QIdentifierLiteral
e
,
QString
context
=
QString
())
{
if
(
errorHandler
)
errorHandler
(
e
,
QString
(
"at index:%1 "
).
arg
(
currentOffset
()).
append
(
context
));
}
private:
void
skipTag
()
{
if
(
isTag
())
next
();
}
bool
getNumber
(
double
&
d
)
{
skipTag
();
if
(
isFloat16
())
{
d
=
double
(
toFloat16
());
return
next
();
}
if
(
isFloat
())
{
d
=
double
(
toFloat
());
return
next
();
}
if
(
isDouble
())
{
d
=
toDouble
()
;
return
next
();
}
_
reportError
(
qBindExpectedDecimal
);
return
false
;
reportError
(
qBindExpectedDecimal
);
return
false
;
}
bool
getInteger
(
quint64
&
i
,
bool
&
isNegative
)
{
skipTag
();
if
(
isNegativeInteger
())
{
i
=
quint64
(
toNegativeInteger
());
isNegative
=
true
;
return
next
();
}
if
(
isUnsignedInteger
())
{
i
=
toUnsignedInteger
()
;
isNegative
=
false
;
return
next
();
}
_
reportError
(
qBindExpectedInteger
);
return
false
;
reportError
(
qBindExpectedInteger
);
return
false
;
}
bool
isChoice
=
false
;
...
...
@@ -494,8 +485,7 @@ struct QTransmogrifier<QCborValue> {
}
else
if
(
v
->
mode
()
==
Read
)
{
QValueStatus
r
;
{
QScopedChoice
choice
(
v
);
auto
suspended
=
v
->
setErrorHandler
();
QCborArray
a
;
if
((
r
=
v
.
bind
(
a
)))
{
j
=
a
;
return
r
;
}
QCborMap
o
;
if
((
r
=
v
.
bind
(
o
)))
{
j
=
o
;
return
r
;
}
QString
t
;
if
((
r
=
v
.
bind
(
t
)))
{
j
=
QCborValue
(
t
);
return
r
;
}
...
...
@@ -505,7 +495,7 @@ struct QTransmogrifier<QCborValue> {
j
=
QCborValue
(
double
(
u
));
return
r
;
}
double
d
;
if
((
r
=
v
.
bind
(
d
)))
{
j
=
QCborValue
(
d
);
return
r
;
}
QByteArray
y
;
if
((
r
=
v
.
bind
(
y
)))
{
j
=
QCborValue
(
y
);
return
r
;
}
}
v
->
setErrorHandler
(
suspended
);
if
(
!
(
r
=
v
.
null
()))
r
.
reportError
(
qBindIgnoredItem
);
/**/
j
=
QCborValue
(
);
return
r
;
}
...
...
tests/QBind/QJson_impl.h
View file @
99d363dc
...
...
@@ -102,10 +102,7 @@ class QJsonVisitor : public QAbstractValueReader
Q_DISABLE_COPY
(
QJsonVisitor
)
public:
QJsonVisitor
(
const
QJsonValue
*
v
)
:
json
(
v
)
{
Q_ASSERT
(
v
);
}
void
reset
(
QJsonValue
*
v
)
{
json
=
v
;
Q_ASSERT
(
v
);
steps
.
resize
(
0
);
errors
.
resize
(
0
);
}
struct
Error
{
QIdentifierLiteral
error
;
QAsciiData
path
;
QValueStatus
zap
(
QValue
&&
value
)
{
return
value
.
bind
(
QUtf8Data
(
error
.
utf8
()
+
' '
+
path
.
utf8
()));
}
};
QVector
<
Error
>
errors
;
void
reset
(
QJsonValue
*
v
)
{
json
=
v
;
Q_ASSERT
(
v
);
steps
.
resize
(
0
);
}
QAsciiData
currentPath
()
{
QByteArray
path
;
...
...
@@ -122,17 +119,17 @@ public:
template
<
typename
T
>
QValueStatus
bind
(
T
&&
t
)
{
return
QValueStatus
(
this
).
value
().
bind
(
std
::
forward
<
T
>
(
t
));
}
protected:
bool
trySequence
(
quint32
*
=
nullptr
)
{
if
(
current
().
isArray
())
{
steps
.
push
(
Step
());
return
true
;
}
_
reportError
(
qBindExpectedSequence
);
return
false
;
}
return
true
;
}
reportError
(
qBindExpectedSequence
);
return
false
;
}
bool
tryRecord
(
quint32
*
=
nullptr
)
{
if
(
current
().
isObject
())
{
steps
.
push
(
Step
());
return
true
;
}
_
reportError
(
qBindExpectedRecord
);
return
false
;
}
bool
tryNull
(
)
{
if
(
current
().
isNull
())
{
return
true
;
}
_
reportError
(
qBindExpectedNull
);
return
false
;
}
return
true
;
}
reportError
(
qBindExpectedRecord
);
return
false
;
}
bool
tryNull
(
)
{
if
(
current
().
isNull
())
{
return
true
;
}
reportError
(
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
;
}
_
reportError
(
qBindExpectedText
);
return
false
;
}
bool
tryBind
(
bool
&
v
)
{
if
(
current
().
isBool
())
{
v
=
current
().
toBool
();
return
true
;
}
_
reportError
(
qBindExpectedBoolean
);
return
false
;
}
bool
tryBind
(
QString
&
v
)
{
if
(
current
().
isString
())
{
v
=
current
().
toString
();
return
true
;
}
reportError
(
qBindExpectedText
);
return
false
;
}
bool
tryBind
(
bool
&
v
)
{
if
(
current
().
isBool
())
{
v
=
current
().
toBool
();
return
true
;
}
reportError
(
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
tryBind
(
double
&
v
)
{
if
(
current
().
isDouble
())
{
v
=
current
().
toDouble
();
return
true
;
}
_
reportError
(
qBindExpectedDecimal
);
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
(
QIdentifier
&
k
)
{
steps
.
last
().
key
=
k
;
return
!
(
steps
.
last
().
item
=
current
(
1
).
toObject
().
value
(
steps
.
last
().
key
.
latin1
())).
isUndefined
();
}
...
...
@@ -140,15 +137,13 @@ protected:
bool
tryOut
(
)
{
steps
.
removeLast
();
return
true
;
}
bool
_isOk
()
const
noexcept
{
return
true
;
}
void
_setChoice
(
bool
v
)
{
isChoice
=
v
;
}
void
_reportError
(
QIdentifierLiteral
e
)
{
if
(
!
isChoice
)
errors
.
append
(
Error
{
e
,
currentPath
()
});
}
void
reportError
(
QIdentifierLiteral
e
,
QString
context
=
QString
())
{
if
(
errorHandler
)
errorHandler
(
e
,
QString
(
currentPath
().
latin1
()).
append
(
context
));
}
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
*
json
;
struct
Step
{
QIdentifier
key
;
int
idx
=-
1
;
QJsonValue
item
;
Step
()
=
default
;
};
QStack
<
Step
>
steps
=
QStack
<
Step
>
();
bool
isChoice
=
false
;
};
// --------------------------------------------------------------------------
...
...
@@ -256,23 +251,23 @@ 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
;
}
_
reportError
(
qBindExpectedSequence
);
return
false
;
}
if
(
get
(
'['
,
"[{
\"
ntf-0123456789."
))
{
levels
.
push
(
Step
(
-
1
,
"]"
));
return
true
;
}
reportError
(
qBindExpectedSequence
);
return
false
;
}
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
;
}
reportError
(
qBindExpectedRecord
);
return
false
;
}
bool
tryNull
(
)
{
if
(
caching
)
{
cacheLevel
++
;
return
caching
->
tryNull
()
&&
cacheOut
();
}
if
(
get
(
'n'
,
"[{
\"
ntf-0123456789."
)
&&
get
(
'u'
,
"[{
\"
"
)
&&
get
(
'l'
,
"[{
\"
"
)
&&
get
(
'l'
,
"[{
\"
"
))
{
return
true
;
}
else
{
_
reportError
(
qBindExpectedNull
);
return
false
;
}
}
}
else
{
reportError
(
qBindExpectedNull
);
return
false
;
}
}
bool
tryBind
(
QUtf8Data
&
s
)
{
if
(
caching
)
{
cacheLevel
++
;
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
{
_
reportError
(
qBindExpectedText
);
return
false
;
}
}
}
else
{
reportError
(
qBindExpectedText
);
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
();
}
if
(
get
(
't'
,
"[{
\"
ntf-0123456789."
)
&&
...
...
@@ -287,11 +282,11 @@ protected:
get
(
's'
,
"[{
\"
"
)
&&
get
(
'e'
,
"[{
\"
"
))
{
b
=
false
;
return
true
;
}
else
{
_
reportError
(
qBindExpectedBoolean
);
return
false
;
}
}
}
else
{
reportError
(
qBindExpectedBoolean
);
return
false
;
}
}
bool
tryBind
(
float
&
n
)
{
if
(
caching
)
{
cacheLevel
++
;
return
caching
->
tryBind
(
double
(
n
))
&&
cacheOut
();
}
if
(
getNumber
()
==
None
)
return
false
;
// already reported qBindExpectedDecimal
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
)
{
reportError
(
qBindExpectedSmallerNumber
);
return
false
;
}
n
=
float
(
d
);
cachedNumber
=
None
;
return
true
;
}
bool
tryBind
(
double
&
n
)
{
if
(
caching
)
{
cacheLevel
++
;
return
caching
->
tryBind
(
double
(
n
))
&&
cacheOut
();
}
if
(
getNumber
()
==
None
)
return
false
;
// already reported qBindExpectedDecimal
...
...
@@ -299,33 +294,33 @@ protected:
bool
tryBind
(
quint64
&
t
)
{
if
(
caching
)
{
cacheLevel
++
;
return
caching
->
tryBind
(
double
(
t
))
&&
cacheOut
();
}
auto
r
=
getNumber
();
if
(
r
==
None
)
return
false
;
// already reported qBindExpectedDecimal
if
(
r
==
FloatingPoint
)
{
_
reportError
(
qBindExpectedInteger
);
return
false
;
}
if
(
neg
)
{
_
reportError
(
qBindExpectedPositiveInteger
);
return
false
;
}
if
(
r
==
FloatingPoint
)
{
reportError
(
qBindExpectedInteger
);
return
false
;
}
if
(
neg
)
{
reportError
(
qBindExpectedPositiveInteger
);
return
false
;
}
t
=
i
;
cachedNumber
=
None
;
return
true
;
}
bool
tryBind
(
qint64
&
t
)
{
if
(
caching
)
{
cacheLevel
++
;
return
caching
->
tryBind
(
double
(
t
))
&&
cacheOut
();
}
auto
r
=
getNumber
();
if
(
r
==
None
)
return
false
;
// already reported qBindExpectedDecimal
if
(
r
==
FloatingPoint
)
{
_
reportError
(
qBindExpectedInteger
);
return
false
;
}
if
(
r
==
FloatingPoint
)
{
reportError
(
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
>::
min
()))
{
t
=
-
qint64
(
i
);
cachedNumber
=
None
;
return
true
;
}
_
reportError
(
qBindExpectedSmallerNumber
);
return
false
;
}
reportError
(
qBindExpectedSmallerNumber
);
return
false
;
}
bool
tryBind
(
QVariant
&
dst
)
{
_setChoice
(
true
);
auto
suspended
=
setErrorHandler
(
);
quint32
size
=
0
;
QIdentifier
key
;
QVariant
item
;
if
(
trySequence
(
&
size
))
{
_setChoice
(
false
);
QVariantList
l
;
while
(
tryItem
(
))
{
l
.
append
(
tryBind
(
item
)
?
item
:
QVariant
());
}
dst
=
l
;
return
tryOut
();
}
if
(
tryRecord
(
&
size
))
{
_setChoice
(
false
);
QVariantMap
l
;
while
(
tryItem
(
key
))
{
l
.
insert
(
key
.
latin1
(),
tryBind
(
item
)
?
item
:
QVariant
());
}
dst
=
l
;
return
tryOut
();
}
bool
b
;
if
(
tryBind
(
b
))
{
_setChoice
(
false
);
dst
=
QVariant
(
b
);
return
true
;
}
quint64
u
;
if
(
tryBind
(
u
))
{
_setChoice
(
false
);
dst
=
QVariant
(
u
);
return
true
;
}
// may fail after consuming integer part
qint64
l
;
if
(
tryBind
(
l
))
{
_setChoice
(
false
);
dst
=
QVariant
(
l
);
return
true
;
}
// may fail after consuming integer part
double
d
;
if
(
tryBind
(
d
))
{
_setChoice
(
false
);
dst
=
QVariant
(
d
+
/*integer part consumed by one of*/
u
+
l
);
return
true
;
}
QUtf8Data
s
;
if
(
tryBind
(
s
))
{
_setChoice
(
false
);
if
(
trySequence
(
&
size
))
{
setErrorHandler
(
suspended
);
QVariantList
l
;
while
(
tryItem
(
))
{
l
.
append
(
tryBind
(
item
)
?
item
:
QVariant
());
}
dst
=
l
;
return
tryOut
();
}
if
(
tryRecord
(
&
size
))
{
setErrorHandler
(
suspended
);
QVariantMap
l
;
while
(
tryItem
(
key
))
{
l
.
insert
(
key
.
latin1
(),
tryBind
(
item
)
?
item
:
QVariant
());
}
dst
=
l
;
return
tryOut
();
}
bool
b
;
if
(
tryBind
(
b
))
{
setErrorHandler
(
suspended
);
dst
=
QVariant
(
b
);
return
true
;
}
quint64
u
;
if
(
tryBind
(
u
))
{
setErrorHandler
(
suspended
);
dst
=
QVariant
(
u
);
return
true
;
}
// may fail after consuming integer part
qint64
l
;
if
(
tryBind
(
l
))
{
setErrorHandler
(
suspended
);
dst
=
QVariant
(
l
);
return
true
;
}
// may fail after consuming integer part
double
d
;
if
(
tryBind
(
d
))
{
setErrorHandler
(
suspended
);
dst
=
QVariant
(
d
+
/*integer part consumed by one of*/
u
+
l
);
return
true
;
}
QUtf8Data
s
;
if
(
tryBind
(
s
))
{
setErrorHandler
(
suspended
);
QByteArray
b
;
if
(
toByteArray
(
b
,
s
))
{
toVariant
(
dst
,
b
);
return
true
;
}
dst
=
QVariant
(
QString
::
fromUtf8
(
s
.
utf8
()))
;
return
true
;
}
_setChoice
(
false
);
if
(
!
tryNull
())
_
reportError
(
QIdentifierLiteral
(
"ExpectedOneOfBooleanDecimalBytesTextSequenceRecordNull"
));
setErrorHandler
(
suspended
);
if
(
!
tryNull
())
reportError
(
QIdentifierLiteral
(
"ExpectedOneOfBooleanDecimalBytesTextSequenceRecordNull"
));
dst
=
QVariant
();
return
true
;
}
...
...
@@ -380,8 +375,7 @@ protected:
return
true
;
}
bool
_isOk
()
const
noexcept
{
return
io
;
}
void
_setChoice
(
bool
v
)
{
isChoice
=
v
;
}
void
_reportError
(
QIdentifierLiteral
e
)
{
if
(
!
isChoice
)
errors
.
append
(
Error
{
e
,
line
,
column
,
index
});
}
void
reportError
(
QIdentifierLiteral
e
,
QString
context
=
QString
())
{
if
(
errorHandler
)
errorHandler
(
e
,
QString
(
"at (%1,%2) "
).
arg
(
line
).
arg
(
column
).
append
(
context
));
}
private:
CachedNumber
getNumber
()
{
if
(
cachedNumber
!=
None
)
return
cachedNumber
;
...
...
@@ -389,7 +383,7 @@ private:
neg
=
get
(
'-'
,
"[{
\"
ntf-0123456789."
);
qint8
digit
;
if
((
digit
=
getDigit
())
<
0
)
{
_
reportError
(
qBindExpectedDecimal
);
reportError
(
qBindExpectedDecimal
);
return
None
;
// do not accept no digit otherwise we may accept an empty mantissa or string!
}
cachedNumber
=
Integer
;
...
...
@@ -484,7 +478,7 @@ private:
while
(
!
(
nextChar
()
==
expected
||
strchr
(
validChars
,
nextChar
())
||
nextChar
()
==
'\0'
))
{
char
ignored
=
getChar
();
if
(
!
isspace
(
ignored
))
{
errors
.
append
(
Error
{
qBindIgnoredCharacter
,
line
,
column
,
index
}
);
reportError
(
qBindIgnoredCharacter
);
}
}
}
...
...
@@ -538,8 +532,7 @@ struct QTransmogrifier<QJsonValue> {
}
else
if
(
v
->
mode
()
==
Read
)
{
QValueStatus
r
;
{
QScopedChoice
choice
(
v
);
auto
suspended
=
v
->
setErrorHandler
();
QJsonArray
a
;
if
((
r
=
v
.
bind
(
a
)))
{
j
=
a
;
return
r
;
}
QJsonObject
o
;
if
((
r
=
v
.
bind
(
o
)))
{
j
=
o
;
return
r
;
}
QString
t
;
if
((
r
=
v
.
bind
(
t
)))
{
j
=
QJsonValue
(
t
)
;
return
r
;
}
...
...
@@ -547,7 +540,7 @@ struct QTransmogrifier<QJsonValue> {
qint64
i
;
if
((
r
=
v
.
bind
(
i
)))
{
j
=
QJsonValue
(
double
(
i
));
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
;
}
}
v
->
setErrorHandler
(
suspended
);
if
(
!
(
r
=
v
.
null
()))
r
.
reportError
(
"ExpectedBoolDoubleQStringQJsonArrayQJsonOnjectNull"
);
/**/
j
=
QJsonValue
(
);
return
r
;
}
...
...
tests/QBind/QModel_impl.h
View file @
99d363dc
...
...
@@ -59,9 +59,6 @@ class QModelBind : public QAbstractValue
public:
QModelBind
(
QAbstractItemModel
*
m
,
bool
rowFirst
=
true
)
:
m
(
m
),
rowFirst
(
rowFirst
)
{
Q_ASSERT
(
m
);
}
struct
Error
{
QIdentifierLiteral
error
;
QModelIndex
index
;
QValueStatus
zap
(
QValue
&&
value
)
{
QByteArray
u
(
error
.
utf8
());
for
(;
index
.
isValid
();
index
=
index
.
parent
())
u
.
append
(
'/'
).
append
(
QByteArray
::
number
(
index
.
row
())).
append
(
','
).
append
(
QByteArray
::
number
(
index
.
column
()));
return
value
.
bind
(
QUtf8Data
(
u
));
}
};
QVector
<
Error
>
errors
;
QValue
value
()
{
return
QValueStatus
(
this
).
value
();
}
protected:
enum
:
int
{
...
...
@@ -157,7 +154,15 @@ protected:
virtual
bool
tryBind
(
QByteArray
&
r
)
{
return
tryBind
(
QByteArray
(
r
));
}
virtual
bool
tryBind
(
QVariant
&
r
)
{
return
tryBind
(
QVariant
(
r
));
}
virtual
void
_reportError
(
QIdentifierLiteral
n
)
{
errors
.
append
(
Error
{
n
,
parent
});
}
void
reportError
(
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
));
}
}
virtual
QAbstractValue
*
itemBind
()
=
0
;
...
...
@@ -210,6 +215,8 @@ protected:
QList
<
QByteArray
>
columnNames
;
bool
metaColumnNames
=
false
;
QVector
<
int
>
sizes
;
QValueErrorHandler
errorHandler
=
QValueErrorHandler
();
};
// --------------------------------------------------------------------------
...
...
@@ -306,7 +313,7 @@ protected:
row
++
;
col
=
0
;
return
true
;
}
_
reportError
(
qBindIgnoredItem
);
reportError
(
qBindIgnoredItem
);
return
false
;
}
return
true
;
...
...
@@ -321,7 +328,7 @@ protected:
row
=
0
;
col
++
;
return
true
;
}
_
reportError
(
qBindIgnoredItem
);
reportError
(
qBindIgnoredItem
);
return
false
;
}
return
true
;
...
...
@@ -352,7 +359,7 @@ protected:
col
=
columnNames
.
indexOf
(
n
.
utf8
());
// TODO if (max(dimension())<=col) col=-1;
if
(
col
<
0
)
{
_
reportError
(
qBindIgnoredItem
);
reportError
(
qBindIgnoredItem
);
}
}
return
true
;
...
...
@@ -369,7 +376,7 @@ protected:
}
else
{
// TODO if (rowNames...
_
reportError
(
qBindIgnoredItemName
);
reportError
(
qBindIgnoredItemName
);
return
true
;
}
}
...
...
@@ -502,7 +509,7 @@ protected:
if
(
col
<
m
->
columnCount
())
{
return
true
;
}
_
reportError
(
qBindIgnoredItem
);
reportError
(
qBindIgnoredItem
);
return
false
;
}
return
true
;
...
...
@@ -520,7 +527,7 @@ protected:
row
=
0
;
col
++
;
return
true
;
}
_
reportError
(
qBindIgnoredItem
);
reportError
(
qBindIgnoredItem
);
return
false
;
}
return
true
;
...
...
@@ -553,7 +560,7 @@ protected:
}
// TODO if (max(dimension())<=col) col=-1;
if
(
col
<
0
)
{
_
reportError
(
qBindIgnoredItem
);
reportError
(
qBindIgnoredItem
);
}
return
true
;
}
...
...
@@ -566,7 +573,7 @@ protected:
}
else
if
(
n
!=
childrenName
)
{
// TODO if (rowNames...
_
reportError
(
qBindIgnoredItemName
);
reportError
(
qBindIgnoredItemName
);
return
true
;
}
else
{
return
true
;
}
...
...
@@ -598,12 +605,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
)
{
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
(
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
;
}
...
...
tests/QBind/QSettings_impl.h
View file @
99d363dc
...
...
@@ -96,9 +96,6 @@ public:
Q_ENABLE_MOVE
(
QSettingsReader
,
std
::
swap
(
isChoice
,
o
.
isChoice
);
)
QSettingsReader
(
QSettings
*
s
)
:
settings
(
s
)
{
Q_ASSERT
(
s
);
levels
.
push
(
Level
(
qBindExpectedItem
));
}
struct
Error
{
QIdentifierLiteral
error
;
QAsciiData
path
;
QValueStatus
zap
(
QValue
&&
value
)
{
return
value
.
bind
(
QUtf8Data
(
error
.
utf8
()
+
' '
+
path
.
utf8
()));
}
};
QVector
<
Error
>
errors
;
QAsciiData
currentPath
()
{
QByteArray
path
;
Q_FOREACH
(
Level
l
,
levels
)
{
...
...
@@ -130,20 +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
;
}
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
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
_isOk
()
const
noexcept
{
return
settings
;
}
void
_setChoice
(
bool
v
)
{
isChoice
=
v
;
}
void
_reportError
(
QIdentifierLiteral
error
)
{
if
(
!
isChoice
)
errors
.
append
(
Error
{
error
,
currentPath
()
});
}
void
reportError
(
QIdentifierLiteral
error
,
QString
context
=
QString
())
{
if
(
errorHandler
)
errorHandler
(
error
,
QString
(
currentPath
().
latin1
()).
append
(
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
;
}
_
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
;
}
reportError
(
error
);
return
false
;
}
QString
key
()
{
Q_ASSERT
(
!
levels
.
isEmpty
());
return
!
levels
.
last
().
key
.
isNull
()
...
...
tests/QBind/QValue.h
View file @
99d363dc
This diff is collapsed.
Click to expand it.
tests/QBind/QVariant_impl.h
View file @
99d363dc
...
...
@@ -120,10 +120,7 @@ class QVariantVisitor : public QAbstractValueReader
public:
Q_ENABLE_MOVE_DEFAULT
(
QVariantVisitor
)
QVariantVisitor
(
const
QVariant
*
v
)
:
value
(
v
)
{
Q_ASSERT
(
v
);
}
void
reset
(
const
QVariant
*
v
)
{
value
=
v
;
Q_ASSERT
(
v
);
levels
.
resize
(
0
);
errors
.
resize
(
0
);
}
struct
Error
{
QIdentifierLiteral
error
;
QAsciiData
path
;
template
<
class
T
>
T
bind
(
QVal
<
T
>&&
value
)
{
return
value
.
bind
(
QUtf8Data
(
error
.
utf8
()
+
' '
+
path
.
utf8
()));
}
};
QVector
<
Error
>
errors
;
void
reset
(
const
QVariant
*
v
)
{
value
=
v
;
Q_ASSERT
(
v
);
levels
.
resize
(
0
);
}
QAsciiData
currentPath
()
{
QByteArray
path
;
...
...
@@ -137,54 +134,52 @@ 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