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
2786103a
Commit
2786103a
authored
May 12, 2015
by
miloyip
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Value::XXXMember(...) overloads for std::string
parent
7eb117a2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
72 additions
and
0 deletions
+72
-0
document.h
include/rapidjson/document.h
+50
-0
valuetest.cpp
test/unittest/valuetest.cpp
+22
-0
No files found.
include/rapidjson/document.h
View file @
2786103a
...
...
@@ -844,6 +844,12 @@ public:
template
<
typename
SourceAllocator
>
const
GenericValue
&
operator
[](
const
GenericValue
<
Encoding
,
SourceAllocator
>&
name
)
const
{
return
const_cast
<
GenericValue
&>
(
*
this
)[
name
];
}
#if RAPIDJSON_HAS_STDSTRING
//! Get a value from an object associated with name (string object).
GenericValue
&
operator
[](
const
std
::
basic_string
<
Ch
>&
name
)
{
return
(
*
this
)[
GenericValue
(
StringRef
(
name
))];
}
const
GenericValue
&
operator
[](
const
std
::
basic_string
<
Ch
>&
name
)
const
{
return
(
*
this
)[
GenericValue
(
StringRef
(
name
))];
}
#endif
//! Const member iterator
/*! \pre IsObject() == true */
ConstMemberIterator
MemberBegin
()
const
{
RAPIDJSON_ASSERT
(
IsObject
());
return
ConstMemberIterator
(
data_
.
o
.
members
);
}
...
...
@@ -867,6 +873,18 @@ public:
*/
bool
HasMember
(
const
Ch
*
name
)
const
{
return
FindMember
(
name
)
!=
MemberEnd
();
}
#if RAPIDJSON_HAS_STDSTRING
//! Check whether a member exists in the object with string object.
/*!
\param name Member name to be searched.
\pre IsObject() == true
\return Whether a member with that name exists.
\note It is better to use FindMember() directly if you need the obtain the value as well.
\note Linear time complexity.
*/
bool
HasMember
(
const
std
::
basic_string
<
Ch
>&
name
)
const
{
return
FindMember
(
name
)
!=
MemberEnd
();
}
#endif
//! Check whether a member exists in the object with GenericValue name.
/*!
This version is faster because it does not need a StrLen(). It can also handle string with null character.
...
...
@@ -923,6 +941,18 @@ public:
}
template
<
typename
SourceAllocator
>
ConstMemberIterator
FindMember
(
const
GenericValue
<
Encoding
,
SourceAllocator
>&
name
)
const
{
return
const_cast
<
GenericValue
&>
(
*
this
).
FindMember
(
name
);
}
#if RAPIDJSON_HAS_STDSTRING
//! Find member by string object name.
/*!
\param name Member name to be searched.
\pre IsObject() == true
\return Iterator to member, if it exists.
Otherwise returns \ref MemberEnd().
*/
MemberIterator
FindMember
(
const
std
::
basic_string
<
Ch
>&
name
)
{
return
FindMember
(
StringRef
(
name
));
}
ConstMemberIterator
FindMember
(
const
std
::
basic_string
<
Ch
>&
name
)
const
{
return
FindMember
(
StringRef
(
name
));
}
#endif
//! Add a member (name-value pair) to the object.
/*! \param name A string value as name of member.
\param value Value of any type.
...
...
@@ -969,6 +999,22 @@ public:
return
AddMember
(
name
,
v
,
allocator
);
}
#if RAPIDJSON_HAS_STDSTRING
//! Add a string object as member (name-value pair) to the object.
/*! \param name A string value as name of member.
\param value constant string reference as value of member.
\param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator().
\return The value itself for fluent API.
\pre IsObject()
\note This overload is needed to avoid clashes with the generic primitive type AddMember(GenericValue&,T,Allocator&) overload below.
\note Amortized Constant time complexity.
*/
GenericValue
&
AddMember
(
GenericValue
&
name
,
std
::
basic_string
<
Ch
>&
value
,
Allocator
&
allocator
)
{
GenericValue
v
(
value
,
allocator
);
return
AddMember
(
name
,
v
,
allocator
);
}
#endif
//! Add any primitive value as member (name-value pair) to the object.
/*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t
\param name A string value as name of member.
...
...
@@ -1087,6 +1133,10 @@ public:
return
RemoveMember
(
n
);
}
#if RAPIDJSON_HAS_STDSTRING
bool
RemoveMember
(
const
std
::
basic_string
<
Ch
>&
name
)
{
return
RemoveMember
(
GenericValue
(
StringRef
(
name
)));
}
#endif
template
<
typename
SourceAllocator
>
bool
RemoveMember
(
const
GenericValue
<
Encoding
,
SourceAllocator
>&
name
)
{
MemberIterator
m
=
FindMember
(
name
);
...
...
test/unittest/valuetest.cpp
View file @
2786103a
...
...
@@ -957,6 +957,19 @@ TEST(Value, Object) {
EXPECT_EQ
(
2u
,
o
.
MemberCount
());
}
#if RAPIDJSON_HAS_STDSTRING
{
// AddMember(StringRefType, const std::string&, Allocator)
Value
o
(
kObjectType
);
o
.
AddMember
(
"b"
,
std
::
string
(
"Banana"
),
allocator
);
EXPECT_STREQ
(
"Banana"
,
o
[
"b"
].
GetString
());
// RemoveMember(const std::string&)
o
.
RemoveMember
(
std
::
string
(
"b"
));
EXPECT_TRUE
(
o
.
ObjectEmpty
());
}
#endif
#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
// AddMember(GenericValue&&, ...) variants
{
...
...
@@ -986,6 +999,10 @@ TEST(Value, Object) {
EXPECT_TRUE
(
y
.
HasMember
(
"A"
));
EXPECT_TRUE
(
y
.
HasMember
(
"B"
));
#if RAPIDJSON_HAS_STDSTRING
EXPECT_TRUE
(
x
.
HasMember
(
std
::
string
(
"A"
)));
#endif
name
.
SetString
(
"C
\0
D"
);
EXPECT_TRUE
(
x
.
HasMember
(
name
));
EXPECT_TRUE
(
y
.
HasMember
(
name
));
...
...
@@ -1009,6 +1026,11 @@ TEST(Value, Object) {
EXPECT_STREQ
(
"Banana"
,
y
[
"B"
].
GetString
());
EXPECT_STREQ
(
"CherryD"
,
y
[
C0D
].
GetString
());
#if RAPIDJSON_HAS_STDSTRING
EXPECT_STREQ
(
"Apple"
,
x
[
"A"
].
GetString
());
EXPECT_STREQ
(
"Apple"
,
y
[
std
::
string
(
"A"
)].
GetString
());
#endif
// member iterator
Value
::
MemberIterator
itr
=
x
.
MemberBegin
();
EXPECT_TRUE
(
itr
!=
x
.
MemberEnd
());
...
...
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