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
8549e3db
Unverified
Commit
8549e3db
authored
Dec 18, 2018
by
Milo Yip
Committed by
GitHub
Dec 18, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1431 from ylavic/pointer_swap
Allow to (std::)Swap two pointers.
parents
66eb6067
2ce91b82
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
63 additions
and
2 deletions
+63
-2
pointer.h
include/rapidjson/pointer.h
+30
-0
pointertest.cpp
test/unittest/pointertest.cpp
+33
-2
No files found.
include/rapidjson/pointer.h
View file @
8549e3db
...
...
@@ -200,6 +200,36 @@ public:
return
*
this
;
}
//! Swap the content of this pointer with an other.
/*!
\param other The pointer to swap with.
\note Constant complexity.
*/
GenericPointer
&
Swap
(
GenericPointer
&
other
)
RAPIDJSON_NOEXCEPT
{
internal
::
Swap
(
allocator_
,
other
.
allocator_
);
internal
::
Swap
(
ownAllocator_
,
other
.
ownAllocator_
);
internal
::
Swap
(
nameBuffer_
,
other
.
nameBuffer_
);
internal
::
Swap
(
tokens_
,
other
.
tokens_
);
internal
::
Swap
(
tokenCount_
,
other
.
tokenCount_
);
internal
::
Swap
(
parseErrorOffset_
,
other
.
parseErrorOffset_
);
internal
::
Swap
(
parseErrorCode_
,
other
.
parseErrorCode_
);
return
*
this
;
}
//! free-standing swap function helper
/*!
Helper function to enable support for common swap implementation pattern based on \c std::swap:
\code
void swap(MyClass& a, MyClass& b) {
using std::swap;
swap(a.pointer, b.pointer);
// ...
}
\endcode
\see Swap()
*/
friend
inline
void
swap
(
GenericPointer
&
a
,
GenericPointer
&
b
)
RAPIDJSON_NOEXCEPT
{
a
.
Swap
(
b
);
}
//@}
//!@name Append token
...
...
test/unittest/pointertest.cpp
View file @
8549e3db
...
...
@@ -16,6 +16,7 @@
#include "rapidjson/pointer.h"
#include "rapidjson/stringbuffer.h"
#include <sstream>
#include <algorithm>
using
namespace
rapidjson
;
...
...
@@ -529,6 +530,36 @@ TEST(Pointer, Assignment) {
}
}
TEST
(
Pointer
,
Swap
)
{
Pointer
p
(
"/foo/0"
);
Pointer
q
(
&
p
.
GetAllocator
());
q
.
Swap
(
p
);
EXPECT_EQ
(
&
q
.
GetAllocator
(),
&
p
.
GetAllocator
());
EXPECT_TRUE
(
p
.
IsValid
());
EXPECT_TRUE
(
q
.
IsValid
());
EXPECT_EQ
(
0u
,
p
.
GetTokenCount
());
EXPECT_EQ
(
2u
,
q
.
GetTokenCount
());
EXPECT_EQ
(
3u
,
q
.
GetTokens
()[
0
].
length
);
EXPECT_STREQ
(
"foo"
,
q
.
GetTokens
()[
0
].
name
);
EXPECT_EQ
(
1u
,
q
.
GetTokens
()[
1
].
length
);
EXPECT_STREQ
(
"0"
,
q
.
GetTokens
()[
1
].
name
);
EXPECT_EQ
(
0u
,
q
.
GetTokens
()[
1
].
index
);
// std::swap compatibility
std
::
swap
(
p
,
q
);
EXPECT_EQ
(
&
p
.
GetAllocator
(),
&
q
.
GetAllocator
());
EXPECT_TRUE
(
q
.
IsValid
());
EXPECT_TRUE
(
p
.
IsValid
());
EXPECT_EQ
(
0u
,
q
.
GetTokenCount
());
EXPECT_EQ
(
2u
,
p
.
GetTokenCount
());
EXPECT_EQ
(
3u
,
p
.
GetTokens
()[
0
].
length
);
EXPECT_STREQ
(
"foo"
,
p
.
GetTokens
()[
0
].
name
);
EXPECT_EQ
(
1u
,
p
.
GetTokens
()[
1
].
length
);
EXPECT_STREQ
(
"0"
,
p
.
GetTokens
()[
1
].
name
);
EXPECT_EQ
(
0u
,
p
.
GetTokens
()[
1
].
index
);
}
TEST
(
Pointer
,
Append
)
{
{
Pointer
p
;
...
...
@@ -867,7 +898,7 @@ TEST(Pointer, Set_NoAllocator) {
#endif
}
TEST
(
Pointer
,
Swap
)
{
TEST
(
Pointer
,
Swap
_Value
)
{
Document
d
;
d
.
Parse
(
kJson
);
Document
::
AllocatorType
&
a
=
d
.
GetAllocator
();
...
...
@@ -876,7 +907,7 @@ TEST(Pointer, Swap) {
EXPECT_STREQ
(
"bar"
,
d
[
"foo"
][
1
].
GetString
());
}
TEST
(
Pointer
,
Swap_NoAllocator
)
{
TEST
(
Pointer
,
Swap_
Value_
NoAllocator
)
{
Document
d
;
d
.
Parse
(
kJson
);
Pointer
(
"/foo/0"
).
Swap
(
d
,
*
Pointer
(
"/foo/1"
).
Get
(
d
));
...
...
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