Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
json
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
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
Libraries
json
Commits
139ef0e7
Commit
139ef0e7
authored
Apr 12, 2015
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
implemented front() and back()
parent
ade49f8b
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
150 additions
and
1 deletion
+150
-1
src/json.hpp
src/json.hpp
+31
-1
src/json.hpp.re2c
src/json.hpp.re2c
+28
-0
test/unit.cpp
test/unit.cpp
+91
-0
No files found.
src/json.hpp
View file @
139ef0e7
...
...
@@ -587,7 +587,7 @@ class basic_json
alloc
.
construct
(
m_value
.
array
,
count
,
other
);
}
// construct a JSON container given an iterator range
//
/
construct a JSON container given an iterator range
template
<
class
T
,
typename
std
::
enable_if
<
std
::
is_same
<
T
,
basic_json
::
iterator
>
::
value
or
...
...
@@ -603,8 +603,10 @@ class basic_json
throw
std
::
runtime_error
(
"iterators are not compatible"
);
}
// set the type
m_type
=
first
.
m_object
->
m_type
;
// check if iterator range is complete for non-compound values
switch
(
m_type
)
{
case
value_t
:
:
number_integer
:
...
...
@@ -1261,6 +1263,34 @@ class basic_json
return
m_value
.
object
->
operator
[](
key
);
}
/// access the first element
inline
reference
front
()
{
return
*
begin
();
}
/// access the first element
inline
const_reference
front
()
const
{
return
*
cbegin
();
}
/// access the last element
inline
reference
back
()
{
auto
tmp
=
end
();
--
tmp
;
return
*
tmp
;
}
/// access the last element
inline
const_reference
back
()
const
{
auto
tmp
=
cend
();
--
tmp
;
return
*
tmp
;
}
/// remove element given an iterator
template
<
class
T
,
typename
std
::
enable_if
<
...
...
src/json.hpp.re2c
View file @
139ef0e7
...
...
@@ -1263,6 +1263,34 @@ class basic_json
return m_value.object->operator[](key);
}
/// access the first element
inline reference front()
{
return *begin();
}
/// access the first element
inline const_reference front() const
{
return *cbegin();
}
/// access the last element
inline reference back()
{
auto tmp = end();
--tmp;
return *tmp;
}
/// access the last element
inline const_reference back() const
{
auto tmp = cend();
--tmp;
return *tmp;
}
/// remove element given an iterator
template <class T, typename
std::enable_if<
...
...
test/unit.cpp
View file @
139ef0e7
...
...
@@ -2471,6 +2471,14 @@ TEST_CASE("element access")
}
}
SECTION
(
"front and back"
)
{
CHECK
(
j
.
front
()
==
json
(
1
));
CHECK
(
j_const
.
front
()
==
json
(
1
));
CHECK
(
j
.
back
()
==
json
({
1
,
2
,
3
}));
CHECK
(
j_const
.
back
()
==
json
({
1
,
2
,
3
}));
}
SECTION
(
"access specified element"
)
{
SECTION
(
"access within bounds"
)
...
...
@@ -2829,6 +2837,16 @@ TEST_CASE("element access")
}
}
SECTION
(
"front and back"
)
{
// "array" is the smallest key
CHECK
(
j
.
front
()
==
json
({
1
,
2
,
3
}));
CHECK
(
j_const
.
front
()
==
json
({
1
,
2
,
3
}));
// "string" is the largest key
CHECK
(
j
.
back
()
==
json
(
"hello world"
));
CHECK
(
j_const
.
back
()
==
json
(
"hello world"
));
}
SECTION
(
"access specified element"
)
{
SECTION
(
"access within bounds"
)
...
...
@@ -3282,6 +3300,79 @@ TEST_CASE("element access")
SECTION
(
"other values"
)
{
SECTION
(
"front and back"
)
{
SECTION
(
"null"
)
{
{
json
j
;
CHECK_THROWS_AS
(
j
.
front
(),
std
::
out_of_range
);
CHECK_THROWS_AS
(
j
.
back
(),
std
::
out_of_range
);
}
{
const
json
j
{};
CHECK_THROWS_AS
(
j
.
front
(),
std
::
out_of_range
);
CHECK_THROWS_AS
(
j
.
back
(),
std
::
out_of_range
);
}
}
SECTION
(
"string"
)
{
{
json
j
=
"foo"
;
CHECK
(
j
.
front
()
==
j
);
CHECK
(
j
.
back
()
==
j
);
}
{
const
json
j
=
"bar"
;
CHECK
(
j
.
front
()
==
j
);
CHECK
(
j
.
back
()
==
j
);
}
}
SECTION
(
"number (boolean)"
)
{
{
json
j
=
false
;
CHECK
(
j
.
front
()
==
j
);
CHECK
(
j
.
back
()
==
j
);
}
{
const
json
j
=
true
;
CHECK
(
j
.
front
()
==
j
);
CHECK
(
j
.
back
()
==
j
);
}
}
SECTION
(
"number (integer)"
)
{
{
json
j
=
17
;
CHECK
(
j
.
front
()
==
j
);
CHECK
(
j
.
back
()
==
j
);
}
{
const
json
j
=
17
;
CHECK
(
j
.
front
()
==
j
);
CHECK
(
j
.
back
()
==
j
);
}
}
SECTION
(
"number (floating point)"
)
{
{
json
j
=
23.42
;
CHECK
(
j
.
front
()
==
j
);
CHECK
(
j
.
back
()
==
j
);
}
{
const
json
j
=
23.42
;
CHECK
(
j
.
front
()
==
j
);
CHECK
(
j
.
back
()
==
j
);
}
}
}
SECTION
(
"erase with one valid iterator"
)
{
SECTION
(
"null"
)
...
...
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