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
de11f501
Commit
de11f501
authored
Nov 29, 2019
by
EXT Arnaud Clère
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make it possible for errorhandler to stop operations on some condition
like errorName != qBindIgnoredXxxx
parent
616633d3
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
231 additions
and
236 deletions
+231
-236
tests/QBind/QCbor_impl.h
tests/QBind/QCbor_impl.h
+76
-79
tests/QBind/QJson_impl.h
tests/QBind/QJson_impl.h
+28
-30
tests/QBind/QModel_impl.h
tests/QBind/QModel_impl.h
+22
-21
tests/QBind/QSettings_impl.h
tests/QBind/QSettings_impl.h
+6
-7
tests/QBind/QValue.h
tests/QBind/QValue.h
+73
-74
tests/QBind/QVariant_impl.h
tests/QBind/QVariant_impl.h
+19
-19
tests/QBind/main.cpp
tests/QBind/main.cpp
+3
-2
tests/QBind/samples.txt
tests/QBind/samples.txt
+4
-4
No files found.
tests/QBind/QCbor_impl.h
View file @
de11f501
...
...
@@ -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
)
{
report
Error
(
qBindExpectedSmallerNumber
);
return
false
;
}
bool
tryBind
(
quint64
&&
n
)
{
if
(
quint64
(
std
::
numeric_limits
<
qint64
>::
max
())
<
n
)
{
handle
Error
(
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
;
}
...
...
@@ -247,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
;
}
report
Error
(
qBindExpectedSequence
);
return
false
;
}
bool
tryRecord
(
quint32
*
=
nullptr
)
{
if
(
current
().
isMap
())
{
steps
.
push
(
Step
());
return
true
;
}
report
Error
(
qBindExpectedRecord
);
return
false
;
}
bool
tryNull
(
)
{
if
(
current
().
isNull
())
{
return
true
;
}
report
Error
(
qBindExpectedNull
);
return
false
;
}
bool
trySequence
(
quint32
*
=
nullptr
)
{
if
(
current
().
isArray
())
{
steps
.
push
(
Step
());
return
true
;
}
handle
Error
(
qBindExpectedSequence
);
return
false
;
}
bool
tryRecord
(
quint32
*
=
nullptr
)
{
if
(
current
().
isMap
())
{
steps
.
push
(
Step
());
return
true
;
}
handle
Error
(
qBindExpectedRecord
);
return
false
;
}
bool
tryNull
(
)
{
if
(
current
().
isNull
())
{
return
true
;
}
handle
Error
(
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
;
}
report
Error
(
qBindExpectedText
);
return
false
;
}
bool
tryBind
(
bool
&
v
)
{
if
(
current
().
isBool
())
{
v
=
current
().
toBool
();
return
true
;
}
report
Error
(
qBindExpectedBoolean
);
return
false
;
}
bool
tryBind
(
qint64
&
t
)
{
if
(
current
().
isInteger
())
{
t
=
current
().
toInteger
();
return
true
;
}
report
Error
(
qBindExpectedInteger
);
return
false
;
}
bool
tryBind
(
QString
&
v
)
{
if
(
current
().
isString
())
{
v
=
current
().
toString
();
return
true
;
}
handle
Error
(
qBindExpectedText
);
return
false
;
}
bool
tryBind
(
bool
&
v
)
{
if
(
current
().
isBool
())
{
v
=
current
().
toBool
();
return
true
;
}
handle
Error
(
qBindExpectedBoolean
);
return
false
;
}
bool
tryBind
(
qint64
&
t
)
{
if
(
current
().
isInteger
())
{
t
=
current
().
toInteger
();
return
true
;
}
handle
Error
(
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
;
}
report
Error
(
qBindExpectedDecimal
);
return
false
;
}
bool
tryBind
(
QByteArray
&
v
)
{
QString
s
;
if
(
current
().
isByteArray
())
{
v
=
current
().
toByteArray
();
return
true
;
}
report
Error
(
qBindExpectedBytes
);
return
false
;
}
bool
tryBind
(
double
&
v
)
{
if
(
current
().
isDouble
())
{
v
=
current
().
toDouble
();
return
true
;
}
handle
Error
(
qBindExpectedDecimal
);
return
false
;
}
bool
tryBind
(
QByteArray
&
v
)
{
QString
s
;
if
(
current
().
isByteArray
())
{
v
=
current
().
toByteArray
();
return
true
;
}
handle
Error
(
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
();
}
...
...
@@ -265,7 +265,7 @@ protected:
bool
tryOut
(
)
{
steps
.
pop
()
;
return
true
;
}
bool
isValid
()
const
noexcept
{
return
cbor
;
}
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:
const
QCborValue
&
current
(
int
outer
=
0
)
const
{
Q_ASSERT
(
0
<=
outer
);
return
steps
.
size
()
-
outer
<=
0
?
*
cbor
:
steps
[
steps
.
size
()
-
outer
-
1
].
item
;
}
...
...
@@ -291,70 +291,68 @@ public:
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
;
}
report
Error
(
qBindExpectedSequence
);
return
false
;
}
skipTag
();
if
(
isArray
()
&&
enterContainer
())
{
levels
.
push
(
Level
());
return
true
;
}
handle
Error
(
qBindExpectedSequence
);
return
false
;
}
bool
tryRecord
(
quint32
*
s
=
nullptr
)
{
if
(
caching
)
{
cacheLevel
++
;
return
caching
->
tryRecord
(
s
);
}
skipTag
();
if
(
isMap
()
&&
enterContainer
())
{
levels
.
push
(
Level
());
return
true
;
}
report
Error
(
qBindExpectedRecord
);
return
false
;
}
bool
tryNull
(
)
{
if
(
caching
)
{
cacheLevel
++
;
return
caching
->
tryNull
()
&&
cacheOut
();
}
skipTag
();
if
(
isNull
()
&&
next
())
{
return
true
;
}
report
Error
(
qBindExpectedNull
);
return
false
;
}
skipTag
();
if
(
isMap
()
&&
enterContainer
())
{
levels
.
push
(
Level
());
return
true
;
}
handle
Error
(
qBindExpectedRecord
);
return
false
;
}
bool
tryNull
(
)
{
if
(
caching
)
{
cacheLevel
++
;
return
caching
->
tryNull
()
&&
cacheOut
();
}
skipTag
();
if
(
isNull
()
&&
next
())
{
return
true
;
}
handle
Error
(
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
())
{
s
.
resize
(
0
);
auto
r
=
readString
();
while
(
r
.
status
==
QCborStreamReader
::
Ok
)
{
s
.
append
(
r
.
data
);
r
=
readString
();
}
if
(
r
.
status
==
QCborStreamReader
::
Error
)
{
if
(
!
s
.
isEmpty
())
{
reportError
(
qBindIgnoredBytes
);
s
.
resize
(
0
);
}
return
false
;
}
return
true
;
}
else
{
reportError
(
qBindExpectedBytes
);
return
false
;
}
}
skipTag
();
if
(
isString
())
{
s
.
resize
(
0
);
auto
r
=
readString
();
while
(
r
.
status
==
QCborStreamReader
::
Ok
)
{
s
.
append
(
r
.
data
);
r
=
readString
();
}
if
(
r
.
status
==
QCborStreamReader
::
Error
)
{
if
(
!
s
.
isEmpty
())
{
s
.
resize
(
0
);
}
return
handleError
(
qBindIgnoredBytes
);
}
return
true
;
}
else
{
handleError
(
qBindExpectedText
);
return
false
;
}
}
bool
tryBind
(
QByteArray
&
s
)
{
if
(
caching
)
{
cacheLevel
++
;
return
caching
->
tryBind
(
s
)
&&
cacheOut
();
}
skipTag
();
if
(
isByteArray
())
{
s
.
resize
(
0
);
auto
r
=
readByteArray
();
while
(
r
.
status
==
QCborStreamReader
::
Ok
)
{
s
.
append
(
r
.
data
);
r
=
readByteArray
();
}
if
(
r
.
status
==
QCborStreamReader
::
Error
)
{
if
(
!
s
.
isEmpty
())
{
reportError
(
qBindIgnoredBytes
);
s
.
resize
(
0
);
}
return
false
;
}
return
true
;
}
else
{
reportError
(
qBindExpectedBytes
);
return
false
;
}
}
skipTag
();
if
(
isByteArray
())
{
s
.
resize
(
0
);
auto
r
=
readByteArray
();
while
(
r
.
status
==
QCborStreamReader
::
Ok
)
{
s
.
append
(
r
.
data
);
r
=
readByteArray
();
}
if
(
r
.
status
==
QCborStreamReader
::
Error
)
{
if
(
!
s
.
isEmpty
())
{
s
.
resize
(
0
);
}
return
handleError
(
qBindIgnoredBytes
);
}
return
true
;
}
else
{
handleError
(
qBindExpectedBytes
);
return
false
;
}
}
bool
tryBind
(
bool
&
b
)
{
if
(
caching
)
{
cacheLevel
++
;
return
caching
->
tryBind
(
b
)
&&
cacheOut
();
}
skipTag
();
if
(
isBool
())
{
b
=
toBool
();
return
next
();
}
else
{
report
Error
(
qBindExpectedBoolean
);
return
false
;
}
}
skipTag
();
if
(
isBool
())
{
b
=
toBool
();
return
next
();
}
else
{
handle
Error
(
qBindExpectedBoolean
);
return
false
;
}
}
bool
tryBind
(
double
&
n
)
{
if
(
caching
)
{
cacheLevel
++
;
return
caching
->
tryBind
(
n
)
&&
cacheOut
();
}
return
getNumber
(
n
);
}
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
)
{
report
Error
(
qBindExpectedSmallerNumber
);
return
false
;
}
n
=
float
(
d
);
return
true
;
}
if
(
!
tryBind
(
d
))
{
return
false
;
}
if
(
d
<
double
(
std
::
numeric_limits
<
float
>::
min
())
||
double
(
std
::
numeric_limits
<
float
>::
max
())
<
d
)
{
handle
Error
(
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
)
{
report
Error
(
qBindExpectedPositiveInteger
);
return
false
;
}
t
=
i
;
return
true
;
}
quint64
i
;
bool
neg
;
if
(
!
getInteger
(
i
,
neg
))
{
handleError
(
qBindExpectedInteger
);
return
false
;
}
if
(
neg
)
{
handle
Error
(
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
)
{
report
Error
(
qBindExpectedSmallerNumber
);
return
false
;
}
t
=-
qint64
(
i
);
return
true
;
}
else
{
if
(
quint64
(
std
::
numeric_limits
<
qint64
>::
max
())
<
i
)
{
report
Error
(
qBindExpectedSmallerNumber
);
return
false
;
}
t
=
qint64
(
i
);
return
true
;
}
}
quint64
i
;
bool
neg
;
if
(
!
getInteger
(
i
,
neg
))
{
handleError
(
qBindExpectedInteger
);
return
false
;
}
if
(
neg
)
{
if
(
quint64
(
-
std
::
numeric_limits
<
qint64
>::
min
())
<
i
)
{
handle
Error
(
qBindExpectedSmallerNumber
);
return
false
;
}
t
=-
qint64
(
i
);
return
true
;
}
else
{
if
(
quint64
(
std
::
numeric_limits
<
qint64
>::
max
())
<
i
)
{
handle
Error
(
qBindExpectedSmallerNumber
);
return
false
;
}
t
=
qint64
(
i
);
return
true
;
}
}
bool
tryItem
(
QIdentifierLiteral
u
)
{
if
(
caching
)
{
return
caching
->
tryItem
(
u
);
}
QIdentifier
s
;
...
...
@@ -420,23 +418,21 @@ protected:
bool
tryAny
()
{
return
next
();
}
bool
isValid
()
const
noexcept
{
return
const_cast
<
QCborReader
*>
(
this
)
->
lastError
()
==
QCborError
::
NoError
;
}
void
reportError
(
QIdentifierLiteral
e
,
QString
context
=
QString
())
{
if
(
errorHandler
)
errorHandler
(
e
,
QString
(
"at index:%1 "
).
arg
(
currentOffset
()).
append
(
context
))
;
}
bool
handleError
(
QIdentifierLiteral
e
,
QString
context
=
QString
())
{
return
errorHandler
?
errorHandler
(
e
,
QString
(
"at index:%1 "
).
arg
(
currentOffset
()).
append
(
context
))
:
false
;
}
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
();
}
report
Error
(
qBindExpectedDecimal
);
return
false
;
handle
Error
(
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
();
}
report
Error
(
qBindExpectedInteger
);
return
false
;
handle
Error
(
qBindExpectedInteger
);
return
false
;
}
bool
isChoice
=
false
;
bool
cacheOut
()
{
Q_ASSERT
(
cacheLevel
);
if
(
!--
cacheLevel
)
{
caching
=
nullptr
;
...
...
@@ -486,18 +482,19 @@ struct QTransmogrifier<QCborValue> {
else
if
(
v
->
mode
()
==
Read
)
{
QValueStatus
r
;
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
;
}
bool
b
;
if
((
r
=
v
.
bind
(
b
)))
{
j
=
QCborValue
(
b
);
return
r
;
}
qint64
i
;
if
((
r
=
v
.
bind
(
i
)))
{
j
=
QCborValue
(
i
);
return
r
;
}
QCborArray
a
;
if
((
r
=
v
.
bind
(
a
)))
{
j
=
a
;
v
->
setErrorHandler
(
suspended
);
return
r
;
}
QCborMap
o
;
if
((
r
=
v
.
bind
(
o
)))
{
j
=
o
;
v
->
setErrorHandler
(
suspended
);
return
r
;
}
QString
t
;
if
((
r
=
v
.
bind
(
t
)))
{
j
=
QCborValue
(
t
);
v
->
setErrorHandler
(
suspended
);
return
r
;
}
bool
b
;
if
((
r
=
v
.
bind
(
b
)))
{
j
=
QCborValue
(
b
);
v
->
setErrorHandler
(
suspended
);
return
r
;
}
qint64
i
;
if
((
r
=
v
.
bind
(
i
)))
{
j
=
QCborValue
(
i
);
v
->
setErrorHandler
(
suspended
);
return
r
;
}
quint64
u
;
if
((
r
=
v
.
bind
(
u
)))
{
Q_ASSERT
(
quint64
(
std
::
numeric_limits
<
qint64
>::
max
())
<
u
);
// or tryBind<qint64>() would have succeeded
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
;
}
j
=
QCborValue
(
double
(
u
));
v
->
setErrorHandler
(
suspended
);
return
r
;
}
double
d
;
if
((
r
=
v
.
bind
(
d
)))
{
j
=
QCborValue
(
d
);
v
->
setErrorHandler
(
suspended
);
return
r
;
}
QByteArray
y
;
if
((
r
=
v
.
bind
(
y
)))
{
j
=
QCborValue
(
y
);
v
->
setErrorHandler
(
suspended
);
return
r
;
}
/**/
if
((
r
=
v
.
null
(
)))
{
j
=
QCborValue
(
);
v
->
setErrorHandler
(
suspended
);
return
r
;
}
v
->
setErrorHandler
(
suspended
);
if
(
!
(
r
=
v
.
null
()))
r
.
report
Error
(
qBindIgnoredItem
);
/**/
j
=
QCborValue
(
);
return
r
;
v
->
handle
Error
(
qBindIgnoredItem
);
return
r
;
}
else
{
Q_ASSERT_X
(
!
v
,
Q_FUNC_INFO
,
"Unsupported v->mode()"
);
return
v
.
null
();
}
}
...
...
tests/QBind/QJson_impl.h
View file @
de11f501
...
...
@@ -118,18 +118,16 @@ 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
().
isObject
())
{
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
)
{
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
trySequence
(
quint32
*
=
nullptr
)
{
if
(
current
().
isArray
())
{
steps
.
push
(
Step
());
return
true
;
}
handleError
(
qBindExpectedSequence
);
return
false
;
}
bool
tryRecord
(
quint32
*
=
nullptr
)
{
if
(
current
().
isObject
())
{
steps
.
push
(
Step
());
return
true
;
}
handleError
(
qBindExpectedRecord
);
return
false
;
}
bool
tryNull
(
)
{
if
(
current
().
isNull
())
{
return
true
;
}
handleError
(
qBindExpectedNull
);
return
false
;
}
bool
tryBind
(
QUtf8Data
&
u
)
{
QString
s
;
if
(
tryBind
(
s
)
)
{
u
=
s
.
toUtf8
();
return
true
;
}
return
false
;
}
bool
tryBind
(
QString
&
v
)
{
if
(
current
().
isString
())
{
v
=
current
().
toString
();
return
true
;
}
handleError
(
qBindExpectedText
);
return
false
;
}
bool
tryBind
(
bool
&
v
)
{
if
(
current
().
isBool
())
{
v
=
current
().
toBool
();
return
true
;
}
handleError
(
qBindExpectedBoolean
);
return
false
;
}
bool
tryBind
(
qint64
&
t
)
{
double
d
;
if
(
tryBind
(
d
)
)
{
t
=
qint64
(
d
)
;
return
true
;
}
return
false
;
}
bool
tryBind
(
quint64
&
t
)
{
double
d
;
if
(
tryBind
(
d
)
)
{
t
=
quint64
(
d
)
;
return
true
;
}
return
false
;
}
bool
tryBind
(
float
&
v
)
{
double
d
;
if
(
tryBind
(
d
)
)
{
v
=
float
(
d
)
;
return
true
;
}
return
false
;
}
bool
tryBind
(
double
&
v
)
{
if
(
current
().
isDouble
())
{
v
=
current
().
toDouble
();
return
true
;
}
handleError
(
qBindExpectedDecimal
);
return
false
;
}
bool
tryItem
(
QIdentifierLiteral
u
)
{
steps
.
last
().
key
=
u
;
return
!
(
steps
.
last
().
item
=
current
(
1
).
toObject
().
value
(
steps
.
last
().
key
.
latin1
())).
isUndefined
();
}
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:
bool
tryOut
(
)
{
steps
.
removeLast
();
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:
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:
void
reset
(
QIODevice
*
other
)
{
errors
.
clear
();
levels
.
clear
();
line
=
0
;
column
=
0
;
index
=-
1
;
isChoice
=
false
;
cachedNumber
=
None
;
d
=
.0
;
i
=
0
;
neg
=
bool
();
cacheLevel
=
0
;
cachedValue
=
QJsonValue
();
cacheWriter
.
reset
(
&
cachedValue
);
...
...
@@ -251,23 +248,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
;
}
report
Error
(
qBindExpectedSequence
);
return
false
;
}
if
(
get
(
'['
,
"[{
\"
ntf-0123456789."
))
{
levels
.
push
(
Step
(
-
1
,
"]"
));
return
true
;
}
handle
Error
(
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
;
}
report
Error
(
qBindExpectedRecord
);
return
false
;
}
if
(
get
(
'{'
,
"[{
\"
ntf-0123456789."
))
{
levels
.
push
(
Step
(
-
1
,
"}"
));
return
true
;
}
handle
Error
(
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
{
report
Error
(
qBindExpectedNull
);
return
false
;
}
}
}
else
{
handle
Error
(
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
{
report
Error
(
qBindExpectedText
);
return
false
;
}
}
}
else
{
handle
Error
(
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."
)
&&
...
...
@@ -282,11 +279,11 @@ protected:
get
(
's'
,
"[{
\"
"
)
&&
get
(
'e'
,
"[{
\"
"
))
{
b
=
false
;
return
true
;
}
else
{
report
Error
(
qBindExpectedBoolean
);
return
false
;
}
}
}
else
{
handle
Error
(
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
)
{
handleError
(
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
...
...
@@ -294,16 +291,16 @@ 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
)
{
report
Error
(
qBindExpectedInteger
);
return
false
;
}
if
(
neg
)
{
report
Error
(
qBindExpectedPositiveInteger
);
return
false
;
}
if
(
r
==
FloatingPoint
)
{
handle
Error
(
qBindExpectedInteger
);
return
false
;
}
if
(
neg
)
{
handle
Error
(
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
)
{
report
Error
(
qBindExpectedInteger
);
return
false
;
}
if
(
r
==
FloatingPoint
)
{
handle
Error
(
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
;
}
report
Error
(
qBindExpectedSmallerNumber
);
return
false
;
}
handle
Error
(
qBindExpectedSmallerNumber
);
return
false
;
}
bool
tryBind
(
QVariant
&
dst
)
{
auto
suspended
=
setErrorHandler
();
...
...
@@ -320,7 +317,7 @@ protected:
dst
=
QVariant
(
QString
::
fromUtf8
(
s
.
utf8
()))
;
return
true
;
}
setErrorHandler
(
suspended
);
if
(
!
tryNull
())
report
Error
(
QIdentifierLiteral
(
"ExpectedOneOfBooleanDecimalBytesTextSequenceRecordNull"
));
if
(
!
tryNull
())
handle
Error
(
QIdentifierLiteral
(
"ExpectedOneOfBooleanDecimalBytesTextSequenceRecordNull"
));
dst
=
QVariant
();
return
true
;
}
...
...
@@ -375,7 +372,7 @@ protected:
return
true
;
}
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:
CachedNumber
getNumber
()
{
if
(
cachedNumber
!=
None
)
return
cachedNumber
;
...
...
@@ -383,7 +380,7 @@ private:
neg
=
get
(
'-'
,
"[{
\"
ntf-0123456789."
);
qint8
digit
;
if
((
digit
=
getDigit
())
<
0
)
{
report
Error
(
qBindExpectedDecimal
);
handle
Error
(
qBindExpectedDecimal
);
return
None
;
// do not accept no digit otherwise we may accept an empty mantissa or string!
}
cachedNumber
=
Integer
;
...
...
@@ -478,7 +475,9 @@ private:
while
(
!
(
nextChar
()
==
expected
||
strchr
(
validChars
,
nextChar
())
||
nextChar
()
==
'\0'
))
{
char
ignored
=
getChar
();
if
(
!
isspace
(
ignored
))
{
reportError
(
qBindIgnoredCharacter
);
if
(
!
handleError
(
qBindIgnoredCharacter
))
{
return
false
;
}
}
}
}
...
...
@@ -488,7 +487,6 @@ private:
QIODevice
*
io
;
int
line
=
0
,
column
=
0
,
index
=
-
1
;
QStack
<
Step
>
levels
=
QStack
<
Step
>
();
//!< dynamic context required to implement item() and report meaningful errors
bool
isChoice
=
false
;
// Read caching of numbers
CachedNumber
cachedNumber
=
None
;
...
...
@@ -541,7 +539,7 @@ struct QTransmogrifier<QJsonValue> {
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
.
report
Error
(
"ExpectedBoolDoubleQStringQJsonArrayQJsonOnjectNull"
);
if
(
!
(
r
=
v
.
null
()))
r
.
handle
Error
(
"ExpectedBoolDoubleQStringQJsonArrayQJsonOnjectNull"
);
/**/
j
=
QJsonValue
(
);
return
r
;
}
else
{
Q_ASSERT_X
(
!
v
,
Q_FUNC_INFO
,
"Unsupported v->mode()"
);
return
v
.
null
();
}
...
...
tests/QBind/QModel_impl.h
View file @
de11f501
...
...
@@ -154,13 +154,16 @@ protected:
virtual
bool
tryBind
(
QByteArray
&
r
)
{
return
tryBind
(
QByteArray
(
r
));
}
virtual
bool
tryBind
(
QVariant
&
r
)
{
return
tryBind
(
QVariant
(
r
));
}
void
report
Error
(
QIdentifierLiteral
e
,
QString
context
=
QString
())
{
bool
handle
Error
(
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
;
}
report
Error
(
qBindIgnoredItem
);
handle
Error
(
qBindIgnoredItem
);
return
false
;
}
return
true
;
...
...
@@ -328,7 +331,7 @@ protected:
row
=
0
;
col
++
;
return
true
;
}
report
Error
(
qBindIgnoredItem
);
handle
Error
(
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
)
{
re
portError
(
qBindIgnoredItem
)
;
if
(
col
<
0
&&
!
handleError
(
qBindIgnoredItem
)
)
{
re
turn
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
;
}
report
Error
(
qBindIgnoredItem
);
handle
Error
(
qBindIgnoredItem
);
return
false
;
}
return
true
;
...
...
@@ -527,7 +529,7 @@ protected:
row
=
0
;
col
++
;
return
true
;
}
report
Error
(
qBindIgnoredItem
);
handle
Error
(
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
)
{
re
portError
(
qBindIgnoredItem
)
;
if
(
col
<
0
&&
!
handleError
(
qBindIgnoredItem
)
)
{
re
turn
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
)
{
report
Error
(
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
)
{
report
Error
(
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
)
{
report
Error
(
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
)
{
report
Error
(
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
)
{
report
Error
(
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
)
{
report
Error
(
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
)
{
handle
Error
(
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
)
{
handle
Error
(
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
)
{
handle
Error
(
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
)
{
handle
Error
(
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
)
{
handle
Error
(
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
)
{
handle
Error
(
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 @
de11f501
...
...
@@ -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
;
}
report
Error
(
qBindExpectedSequence
);
return
false
;
}
bool
tryRecord
(
quint32
*
=
nullptr
)
{
if
(
levels
.
last
().
isGroup
)
{
levels
.
push
(
Level
(
qBindExpectedItem
));
return
true
;
}
report
Error
(
qBindExpectedRecord
);
return
false
;
}
bool
tryNull
(
)
{
if
(
settings
->
value
(
key
()).
isNull
())
{
return
true
;
}
report
Error
(
qBindExpectedNull
);
return
false
;
}
bool
trySequence
(
quint32
*
=
nullptr
)
{
if
(
levels
.
last
().
isGroup
)
{
levels
.
push
(
Level
(
));
return
true
;
}
handle
Error
(
qBindExpectedSequence
);
return
false
;
}
bool
tryRecord
(
quint32
*
=
nullptr
)
{
if
(
levels
.
last
().
isGroup
)
{
levels
.
push
(
Level
(
qBindExpectedItem
));
return
true
;
}
handle
Error
(
qBindExpectedRecord
);
return
false
;
}
bool
tryNull
(
)
{
if
(
settings
->
value
(
key
()).
isNull
())
{
return
true
;
}
handle
Error
(
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
;
}
report
Error
(
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
;
}
handle
Error
(
error
);
return
false
;
}
QString
key
()
{
Q_ASSERT
(
!
levels
.
isEmpty
());
return
!
levels
.
last
().
key
.
isNull
()
...
...
@@ -150,5 +150,4 @@ private:
QSettings
*
settings