Skip to content

Commit 3d9bf8e

Browse files
author
Jakob Widauer
authored
feat: adds front and back methods to Value type (#1458)
Value::front and Value::back
1 parent 8190e06 commit 3d9bf8e

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

include/json/value.h

+24
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,22 @@ class JSON_API Value {
585585
iterator begin();
586586
iterator end();
587587

588+
/// \brief Returns a reference to the first element in the `Value`.
589+
/// Requires that this value holds an array or json object, with at least one element.
590+
const Value& front() const;
591+
592+
/// \brief Returns a reference to the first element in the `Value`.
593+
/// Requires that this value holds an array or json object, with at least one element.
594+
Value& front();
595+
596+
/// \brief Returns a reference to the last element in the `Value`.
597+
/// Requires that value holds an array or json object, with at least one element.
598+
const Value& back() const;
599+
600+
/// \brief Returns a reference to the last element in the `Value`.
601+
/// Requires that this value holds an array or json object, with at least one element.
602+
Value& back();
603+
588604
// Accessors for the [start, limit) range of bytes within the JSON text from
589605
// which this value was parsed, if any.
590606
void setOffsetStart(ptrdiff_t start);
@@ -925,6 +941,14 @@ class JSON_API ValueIterator : public ValueIteratorBase {
925941

926942
inline void swap(Value& a, Value& b) { a.swap(b); }
927943

944+
inline const Value& Value::front() const { return *begin(); }
945+
946+
inline Value& Value::front() { return *begin(); }
947+
948+
inline const Value& Value::back() const { return *(--end()); }
949+
950+
inline Value& Value::back() { return *(--end()); }
951+
928952
} // namespace Json
929953

930954
#pragma pack(pop)

src/test_lib_json/main.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,14 @@ JSONTEST_FIXTURE_LOCAL(ValueTest, arrays) {
310310
const Json::Value& constArray = array1_;
311311
JSONTEST_ASSERT_EQUAL(Json::Value(1234), constArray[index0]);
312312
JSONTEST_ASSERT_EQUAL(Json::Value(1234), constArray[0]);
313+
JSONTEST_ASSERT_EQUAL(Json::Value(1234), constArray.front());
314+
JSONTEST_ASSERT_EQUAL(Json::Value(1234), constArray.back());
313315

314316
// Access through non-const reference
315317
JSONTEST_ASSERT_EQUAL(Json::Value(1234), array1_[index0]);
316318
JSONTEST_ASSERT_EQUAL(Json::Value(1234), array1_[0]);
319+
JSONTEST_ASSERT_EQUAL(Json::Value(1234), array1_.front());
320+
JSONTEST_ASSERT_EQUAL(Json::Value(1234), array1_.back());
317321

318322
array1_[2] = Json::Value(17);
319323
JSONTEST_ASSERT_EQUAL(Json::Value(), array1_[1]);
@@ -356,6 +360,8 @@ JSONTEST_FIXTURE_LOCAL(ValueTest, resizePopulatesAllMissingElements) {
356360
v.resize(n);
357361
JSONTEST_ASSERT_EQUAL(n, v.size());
358362
JSONTEST_ASSERT_EQUAL(n, std::distance(v.begin(), v.end()));
363+
JSONTEST_ASSERT_EQUAL(v.front(), Json::Value{});
364+
JSONTEST_ASSERT_EQUAL(v.back(), Json::Value{});
359365
for (const Json::Value& e : v)
360366
JSONTEST_ASSERT_EQUAL(e, Json::Value{});
361367
}
@@ -406,13 +412,17 @@ JSONTEST_FIXTURE_LOCAL(ValueTest, arrayInsertAtRandomIndex) {
406412
JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]); // check append
407413
JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[1]);
408414
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]);
415+
JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array.front());
416+
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array.back());
409417

410418
// insert lvalue at the head
411419
JSONTEST_ASSERT(array.insert(0, str1));
412420
JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[0]);
413421
JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[1]);
414422
JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[2]);
415423
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[3]);
424+
JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array.front());
425+
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array.back());
416426
// checking address
417427
for (Json::ArrayIndex i = 0; i < 3; i++) {
418428
JSONTEST_ASSERT_EQUAL(vec[i], &array[i]);
@@ -425,6 +435,8 @@ JSONTEST_FIXTURE_LOCAL(ValueTest, arrayInsertAtRandomIndex) {
425435
JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[2]);
426436
JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[3]);
427437
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[4]);
438+
JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array.front());
439+
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array.back());
428440
// checking address
429441
for (Json::ArrayIndex i = 0; i < 4; i++) {
430442
JSONTEST_ASSERT_EQUAL(vec[i], &array[i]);
@@ -438,6 +450,8 @@ JSONTEST_FIXTURE_LOCAL(ValueTest, arrayInsertAtRandomIndex) {
438450
JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[3]);
439451
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[4]);
440452
JSONTEST_ASSERT_EQUAL(Json::Value("index5"), array[5]);
453+
JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array.front());
454+
JSONTEST_ASSERT_EQUAL(Json::Value("index5"), array.back());
441455
// checking address
442456
for (Json::ArrayIndex i = 0; i < 5; i++) {
443457
JSONTEST_ASSERT_EQUAL(vec[i], &array[i]);

0 commit comments

Comments
 (0)