Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
R
rapidjson
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
rapidjson
Commits
46dc8e92
Commit
46dc8e92
authored
Feb 18, 2016
by
Milo Yip
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add implicit constructors of GenericValue for GenercArray|Object
Also remove SetArray|Object(…)
parent
d13be6c7
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
68 additions
and
12 deletions
+68
-12
document.h
include/rapidjson/document.h
+26
-10
valuetest.cpp
test/unittest/valuetest.cpp
+42
-2
No files found.
include/rapidjson/document.h
View file @
46dc8e92
...
...
@@ -481,8 +481,8 @@ struct TypeHelper<ValueType, typename ValueType::Array> {
typedef
typename
ValueType
::
Array
ArrayType
;
static
bool
Is
(
const
ValueType
&
v
)
{
return
v
.
IsArray
();
}
static
ArrayType
Get
(
ValueType
&
v
)
{
return
v
.
GetArray
();
}
static
ValueType
&
Set
(
ValueType
&
v
,
ArrayType
data
)
{
return
v
.
SetArray
(
data
)
;
}
static
ValueType
&
Set
(
ValueType
&
v
,
ArrayType
data
,
typename
ValueType
::
AllocatorType
&
)
{
return
v
.
SetArray
(
data
)
;
}
static
ValueType
&
Set
(
ValueType
&
v
,
ArrayType
data
)
{
return
v
=
data
;
}
static
ValueType
&
Set
(
ValueType
&
v
,
ArrayType
data
,
typename
ValueType
::
AllocatorType
&
)
{
return
v
=
data
;
}
};
template
<
typename
ValueType
>
...
...
@@ -497,8 +497,8 @@ struct TypeHelper<ValueType, typename ValueType::Object> {
typedef
typename
ValueType
::
Object
ObjectType
;
static
bool
Is
(
const
ValueType
&
v
)
{
return
v
.
IsObject
();
}
static
ObjectType
Get
(
ValueType
&
v
)
{
return
v
.
GetObject
();
}
static
ValueType
&
Set
(
ValueType
&
v
,
ObjectType
data
)
{
return
v
.
SetObject
(
data
)
;
}
static
ValueType
&
Set
(
ValueType
&
v
,
ObjectType
data
,
typename
ValueType
::
AllocatorType
&
)
{
return
v
.
SetObject
(
data
)
;
}
static
ValueType
&
Set
(
ValueType
&
v
,
ObjectType
data
)
{
return
v
=
data
;
}
static
ValueType
&
Set
(
ValueType
&
v
,
ObjectType
data
,
typename
ValueType
::
AllocatorType
&
)
{
v
=
data
;
}
};
template
<
typename
ValueType
>
...
...
@@ -681,6 +681,28 @@ public:
GenericValue
(
const
std
::
basic_string
<
Ch
>&
s
,
Allocator
&
allocator
)
:
data_
(),
flags_
()
{
SetStringRaw
(
StringRef
(
s
),
allocator
);
}
#endif
//! Constructor for Array.
/*!
\param a An array obtained by \c GetArray().
\note \c Array is always pass-by-value.
\note the source array is moved into this value and the sourec array becomes empty.
*/
GenericValue
(
Array
a
)
:
data_
(
a
.
value_
.
data_
),
flags_
(
a
.
value_
.
flags_
)
{
a
.
value_
.
data_
=
Data
();
a
.
value_
.
flags_
=
kArrayFlag
;
}
//! Constructor for Object.
/*!
\param o An object obtained by \c GetObject().
\note \c Object is always pass-by-value.
\note the source object is moved into this value and the sourec object becomes empty.
*/
GenericValue
(
Object
o
)
:
data_
(
o
.
value_
.
data_
),
flags_
(
o
.
value_
.
flags_
)
{
o
.
value_
.
data_
=
Data
();
o
.
value_
.
flags_
=
kObjectFlag
;
}
//! Destructor.
/*! Need to destruct elements of array, members of object, or copy-string.
*/
...
...
@@ -970,9 +992,6 @@ public:
/*! \post IsObject() == true */
GenericValue
&
SetObject
()
{
this
->~
GenericValue
();
new
(
this
)
GenericValue
(
kObjectType
);
return
*
this
;
}
//! Set this value with an object.
GenericValue
&
SetObject
(
Object
&
o
)
{
return
*
this
=
o
.
value_
;
}
//! Get the number of members in the object.
SizeType
MemberCount
()
const
{
RAPIDJSON_ASSERT
(
IsObject
());
return
data_
.
o
.
size
;
}
...
...
@@ -1431,9 +1450,6 @@ public:
/*! \post IsArray == true */
GenericValue
&
SetArray
()
{
this
->~
GenericValue
();
new
(
this
)
GenericValue
(
kArrayType
);
return
*
this
;
}
//! Set this value with an array.
GenericValue
&
SetArray
(
Array
&
a
)
{
return
*
this
=
a
.
value_
;
}
//! Get the number of elements in array.
SizeType
Size
()
const
{
RAPIDJSON_ASSERT
(
IsArray
());
return
data_
.
a
.
size
;
}
...
...
test/unittest/valuetest.cpp
View file @
46dc8e92
...
...
@@ -1059,9 +1059,32 @@ TEST(Value, ArrayHelper) {
Value
x2
;
x2
.
Set
<
Value
::
Array
>
(
a
);
EXPECT_TRUE
(
x
.
Is
Null
());
EXPECT_TRUE
(
x
.
Is
Array
());
// IsArray() is invariant after moving.
EXPECT_EQ
(
1
,
x2
.
Get
<
Value
::
Array
>
()[
0
].
GetInt
());
}
{
Value
y
(
kArrayType
);
y
.
PushBack
(
123
,
allocator
);
Value
x
(
y
.
GetArray
());
// Construct value form array.
EXPECT_TRUE
(
x
.
IsArray
());
EXPECT_EQ
(
123
,
x
[
0
].
GetInt
());
EXPECT_TRUE
(
y
.
IsArray
());
// Invariant
EXPECT_TRUE
(
y
.
Empty
());
}
{
Value
x
(
kArrayType
);
Value
y
(
kArrayType
);
y
.
PushBack
(
123
,
allocator
);
x
.
PushBack
(
y
.
GetArray
(),
allocator
);
// Implicit constructor to convert Array to GenericValue
EXPECT_EQ
(
1
,
x
.
Size
());
EXPECT_EQ
(
123
,
x
[
0
][
0
].
GetInt
());
EXPECT_TRUE
(
y
.
IsArray
());
EXPECT_TRUE
(
y
.
Empty
());
}
}
#if RAPIDJSON_HAS_CXX11_RANGE_FOR
...
...
@@ -1413,9 +1436,26 @@ TEST(Value, ObjectHelper) {
Value
x2
;
x2
.
Set
<
Value
::
Object
>
(
o
);
EXPECT_TRUE
(
x
.
Is
Null
());
EXPECT_TRUE
(
x
.
Is
Object
());
// IsObject() is invariant after moving
EXPECT_EQ
(
1
,
x2
.
Get
<
Value
::
Object
>
()[
"1"
].
GetInt
());
}
{
Value
x
(
kObjectType
);
x
.
AddMember
(
"a"
,
"apple"
,
allocator
);
Value
y
(
x
.
GetObject
());
EXPECT_STREQ
(
"apple"
,
y
[
"a"
].
GetString
());
EXPECT_TRUE
(
x
.
IsObject
());
// Invariant
}
{
Value
x
(
kObjectType
);
x
.
AddMember
(
"a"
,
"apple"
,
allocator
);
Value
y
(
kObjectType
);
y
.
AddMember
(
"fruits"
,
x
.
GetObject
(),
allocator
);
EXPECT_STREQ
(
"apple"
,
y
[
"fruits"
][
"a"
].
GetString
());
EXPECT_TRUE
(
x
.
IsObject
());
// Invariant
}
}
#if RAPIDJSON_HAS_CXX11_RANGE_FOR
...
...
Write
Preview
Markdown
is supported
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