Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
MODMED
modmedLog
Commits
54f34275
Commit
54f34275
authored
Jul 04, 2018
by
EXT Arnaud Clère
Browse files
Fixed strange compile error when using QMovedResult::value() instead of
explicitely constructing it...
parent
02da707b
Changes
2
Hide whitespace changes
Inline
Side-by-side
tests/QBind/QJson_impl.h
View file @
54f34275
...
...
@@ -110,44 +110,44 @@ private:
};
QJsonBuilder
::
QJsonBuilder
(
QJsonValue
*
v
)
:
QScopedResult
(
new
QJsonBuilderImpl
(
v
),
true
)
{}
//
class JsonVisitor : public QMovedResult<JsonVisitor, BindMode::Read>
//
{
//
protected:
//
JsonVisitor(const JsonVisitor &o) : QMovedResult(), value(o.current()), steps(o.steps) {}
//
JsonVisitor &operator=(const JsonVisitor &) = delete;
//
public:
//
Q_ENABLE_MOVE(JsonVisitor, std::swap(value, o.value); std::swap(steps, o.steps); )
//
JsonVisitor(const QJsonValue* v) : value(v) { Q_ASSERT(v); }
//
//
operator bool() { return value; } // for QMovedResult
//
protected:
//
friend class QMovedResult<JsonVisitor, BindMode::Read>; // for slight optimization of fluent interface shortcuts
//
template<class T_> friend class Val; // calls methods below
//
//
bool _null() { if (current()->isNull ()) { return true; } return false; }
//
bool _record() { if (current()->isObject()) { steps.push(Step()); return true; } return false; }
//
bool _sequence() { if (current()->isArray ()) { steps.push(Step()); return true; } return false; }
//
bool _bind(QString& v) { if (current()->isString()) { v = current()->toString(); return true; } return false; }
//
bool _bind( double& v) { if (current()->isDouble()) { v = current()->toDouble(); return true; } return false; }
//
bool _bind( bool& v) { if (current()->isBool ()) { v = current()->toBool (); return true; } return false; }
//
//
//template<typename T> bool _bind(T& t, std::enable_if_t<std::is_arithmetic<T>::value>* = nullptr) { double d; if (_bind(d)) { t = d; return true; } return false; }
//
//
// Val<TResult> prevents QBind from getting out() of an outer Seq for instance
//
template<typename T> bool _bind(T& t) { return QBind<TResult,T>::bind(TResult(*this)
.value(
), t); }
//
//
template<class T_> friend class Seq; // calls methods below
//
//
bool _item(const char* key) { steps.last().key=key; steps.last().item=current(1)->toObject()[QString::fromUtf8(steps.last().key)]; return true; }
//
bool _item( ) { steps.last().idx++ ; steps.last().item=current(1)->toArray ()[ steps.last().idx ]; return true; }
//
bool _out() { steps.pop(); return true; }
//
private:
//
const QJsonValue* current(unsigned outer=0) const { return steps.size()-outer <= 0 ? value : &(steps[steps.size()-outer-1].item); }
//
//
const QJsonValue* value;
//
struct Step { const char* key=nullptr; int idx=-1; QJsonValue item; Step() = default; };
//
QStack<Step> steps = QStack<Step>();
//
};
class
JsonVisitor
:
public
QMovedResult
<
JsonVisitor
,
BindMode
::
Read
>
{
protected:
JsonVisitor
(
const
JsonVisitor
&
o
)
:
QMovedResult
(),
value
(
o
.
current
()),
steps
(
o
.
steps
)
{}
JsonVisitor
&
operator
=
(
const
JsonVisitor
&
)
=
delete
;
public:
Q_ENABLE_MOVE
(
JsonVisitor
,
std
::
swap
(
value
,
o
.
value
);
std
::
swap
(
steps
,
o
.
steps
);
)
JsonVisitor
(
const
QJsonValue
*
v
)
:
value
(
v
)
{
Q_ASSERT
(
v
);
}
operator
bool
()
{
return
value
;
}
// for QMovedResult
protected:
friend
class
QMovedResult
<
JsonVisitor
,
BindMode
::
Read
>
;
// for slight optimization of fluent interface shortcuts
template
<
class
T_
>
friend
class
Val
;
// calls methods below
bool
_null
()
{
if
(
current
()
->
isNull
())
{
return
true
;
}
return
false
;
}
bool
_record
()
{
if
(
current
()
->
isObject
())
{
steps
.
push
(
Step
());
return
true
;
}
return
false
;
}
bool
_sequence
()
{
if
(
current
()
->
isArray
())
{
steps
.
push
(
Step
());
return
true
;
}
return
false
;
}
bool
_bind
(
QString
&
v
)
{
if
(
current
()
->
isString
())
{
v
=
current
()
->
toString
();
return
true
;
}
return
false
;
}
bool
_bind
(
double
&
v
)
{
if
(
current
()
->
isDouble
())
{
v
=
current
()
->
toDouble
();
return
true
;
}
return
false
;
}
bool
_bind
(
bool
&
v
)
{
if
(
current
()
->
isBool
())
{
v
=
current
()
->
toBool
();
return
true
;
}
return
false
;
}
//template<typename T> bool _bind(T& t, std::enable_if_t<std::is_arithmetic<T>::value>* = nullptr) { double d; if (_bind(d)) { t = d; return true; } return false; }
// Val<TResult> prevents QBind from getting out() of an outer Seq for instance
template
<
typename
T
>
bool
_bind
(
T
&
t
)
{
return
QBind
<
TResult
,
T
>::
bind
(
Val
<
TResult
>
(
TResult
(
*
this
)),
t
);
}
// TODO check why .value() does not work
template
<
class
T_
>
friend
class
Seq
;
// calls methods below
bool
_item
(
const
char
*
key
)
{
steps
.
last
().
key
=
key
;
steps
.
last
().
item
=
current
(
1
)
->
toObject
()[
QString
::
fromUtf8
(
steps
.
last
().
key
)];
return
true
;
}
bool
_item
(
)
{
steps
.
last
().
idx
++
;
steps
.
last
().
item
=
current
(
1
)
->
toArray
()[
steps
.
last
().
idx
];
return
true
;
}
bool
_out
()
{
steps
.
pop
();
return
true
;
}
private:
const
QJsonValue
*
current
(
unsigned
outer
=
0
)
const
{
return
steps
.
size
()
-
outer
<=
0
?
value
:
&
(
steps
[
steps
.
size
()
-
outer
-
1
].
item
);
}
const
QJsonValue
*
value
;
struct
Step
{
const
char
*
key
=
nullptr
;
int
idx
=-
1
;
QJsonValue
item
;
Step
()
=
default
;
};
QStack
<
Step
>
steps
=
QStack
<
Step
>
();
};
class
QJsonWriterImpl
;
class
QJsonWriter
:
public
QScopedResult
<
QJsonWriter
,
QJsonWriterImpl
,
BindMode
::
Write
>
...
...
tests/QBind/main.cpp
View file @
54f34275
...
...
@@ -311,10 +311,11 @@ int main()
QJsonBuilder
(
&
v
).
bind
(
p
);
}
STOP
(
"T>JsonValue"
,
QString
::
fromUtf8
(
QJsonDocument
(
v
.
toArray
()).
toJson
()));
// START {
// JsonVisitor(&v).bind(p);
// }
// STOP("JsonValue>T",QString("[%1,%2,%3,[..%4..]] errors:%5").arg(p.firstName,p.lastName).arg(p.height).arg(p.phones.size()).arg(errors.size()))
START
{
p
=
{};
JsonVisitor
(
&
v
).
bind
(
p
);
}
STOP
(
"JsonValue>T"
,
QString
(
"[%1,%2,%3,[..%4..]] errors:%5"
).
arg
(
p
.
firstName
,
p
.
lastName
).
arg
(
p
.
height
).
arg
(
p
.
phones
.
size
()).
arg
(
errors
.
size
()))
START
{
json
.
seek
(
0
);
v
=
QJsonValue
();
errors
=
QJsonReader
(
&
json
).
bind
(
v
)
->
errors
;
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment