Skip to content

Commit 850d856

Browse files
committed
🔨 cleanup after #663
1 parent 3d67ec4 commit 850d856

2 files changed

Lines changed: 44 additions & 45 deletions

File tree

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,9 @@ I deeply appreciate the help of the following people.
849849
- [Steffen](https://github.com/koemeet) fixed a potential issue with MSVC and `std::min`.
850850
- [Mike Tzou](https://github.com/Chocobo1) fixed some typos.
851851
- [amrcode](https://github.com/amrcode) noted a missleading documentation about comparison of floats.
852-
- [Oleg Endo](https://github.com/olegendo) reduced the memory consumption by replacing `<iostream>` with `<iosfwd>`
852+
- [Oleg Endo](https://github.com/olegendo) reduced the memory consumption by replacing `<iostream>` with `<iosfwd>`.
853+
- [dan-42](https://github.com/dan-42) cleaned up the CMake files to simplify including/reusing of the library.
854+
- [Nikita Ofitserov](https://github.com/himikof) allowed for moving values from initializer lists.
853855
854856
855857
Thanks a lot for helping out! Please [let me know](mailto:mail@nlohmann.me) if I forgot someone.
@@ -912,7 +914,7 @@ $ make json_unit -Ctest
912914
$ ./test/json_unit "*"
913915
914916
===============================================================================
915-
All tests passed (13391115 assertions in 49 test cases)
917+
All tests passed (14504461 assertions in 48 test cases)
916918
```
917919

918920
Alternatively, you can use [CMake](https://cmake.org) and run

src/json.hpp

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ struct external_constructor<value_t::string>
577577
}
578578

579579
template<typename BasicJsonType>
580-
static void construct(BasicJsonType& j, typename BasicJsonType::string_t&& s)
580+
static void construct(BasicJsonType& j, typename BasicJsonType::string_t&& s)
581581
{
582582
j.m_type = value_t::string;
583583
j.m_value = std::move(s);
@@ -633,7 +633,7 @@ struct external_constructor<value_t::array>
633633
}
634634

635635
template<typename BasicJsonType>
636-
static void construct(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
636+
static void construct(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
637637
{
638638
j.m_type = value_t::array;
639639
j.m_value = std::move(arr);
@@ -679,7 +679,7 @@ struct external_constructor<value_t::object>
679679
}
680680

681681
template<typename BasicJsonType>
682-
static void construct(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
682+
static void construct(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
683683
{
684684
j.m_type = value_t::object;
685685
j.m_value = std::move(obj);
@@ -875,7 +875,7 @@ void to_json(BasicJsonType& j, const CompatibleString& s)
875875
}
876876

877877
template <typename BasicJsonType>
878-
void to_json(BasicJsonType& j, typename BasicJsonType::string_t&& s)
878+
void to_json(BasicJsonType& j, typename BasicJsonType::string_t&& s)
879879
{
880880
external_constructor<value_t::string>::construct(j, std::move(s));
881881
}
@@ -931,7 +931,7 @@ void to_json(BasicJsonType& j, const CompatibleArrayType& arr)
931931
}
932932

933933
template <typename BasicJsonType>
934-
void to_json(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
934+
void to_json(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
935935
{
936936
external_constructor<value_t::array>::construct(j, std::move(arr));
937937
}
@@ -946,7 +946,7 @@ void to_json(BasicJsonType& j, const CompatibleObjectType& obj)
946946
}
947947

948948
template <typename BasicJsonType>
949-
void to_json(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
949+
void to_json(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
950950
{
951951
external_constructor<value_t::object>::construct(j, std::move(obj));
952952
}
@@ -6843,63 +6843,61 @@ class serializer
68436843
};
68446844

68456845
template<typename BasicJsonType>
6846-
struct json_ref
6846+
class json_ref
68476847
{
6848-
typedef BasicJsonType value_type;
6848+
public:
6849+
using value_type = BasicJsonType;
68496850

6850-
json_ref(value_type&& value)
6851-
: owned_value_(std::move(value))
6852-
, is_rvalue_(true)
6853-
{
6854-
value_ref_ = &owned_value_;
6855-
}
6851+
json_ref(value_type&& value)
6852+
: owned_value(std::move(value)),
6853+
value_ref(&owned_value),
6854+
is_rvalue(true)
6855+
{}
68566856

68576857
json_ref(const value_type& value)
6858-
: value_ref_(const_cast<value_type*>(&value))
6859-
, is_rvalue_(false)
6858+
: value_ref(const_cast<value_type*>(&value)),
6859+
is_rvalue(false)
68606860
{}
68616861

68626862
json_ref(std::initializer_list<json_ref> init)
6863-
: owned_value_(init)
6864-
, is_rvalue_(true)
6865-
{
6866-
value_ref_ = &owned_value_;
6867-
}
6863+
: owned_value(init),
6864+
value_ref(&owned_value),
6865+
is_rvalue(true)
6866+
{}
68686867

68696868
template <class... Args>
68706869
json_ref(Args... args)
6871-
: owned_value_(std::forward<Args>(args)...)
6872-
, is_rvalue_(true)
6873-
{
6874-
value_ref_ = &owned_value_;
6875-
}
6870+
: owned_value(std::forward<Args>(args)...),
6871+
value_ref(&owned_value),
6872+
is_rvalue(true)
6873+
{}
68766874

68776875
value_type moved_or_copied() const
68786876
{
6879-
if (is_rvalue_)
6877+
if (is_rvalue)
68806878
{
6881-
return std::move(*value_ref_);
6879+
return std::move(*value_ref);
68826880
}
68836881
else
68846882
{
6885-
return *value_ref_;
6883+
return *value_ref;
68866884
}
68876885
}
68886886

68896887
value_type const& operator*() const
68906888
{
6891-
return *static_cast<value_type const*>(value_ref_);
6889+
return *static_cast<value_type const*>(value_ref);
68926890
}
68936891

68946892
value_type const* operator->() const
68956893
{
6896-
return static_cast<value_type const*>(value_ref_);
6894+
return static_cast<value_type const*>(value_ref);
68976895
}
68986896

68996897
private:
6900-
value_type * value_ref_;
6901-
mutable value_type owned_value_;
6902-
bool is_rvalue_;
6898+
mutable value_type owned_value;
6899+
value_type* value_ref = nullptr;
6900+
const bool is_rvalue;
69036901
};
69046902

69056903
} // namespace detail
@@ -8130,7 +8128,7 @@ class basic_json
81308128
}
81318129

81328130
/// constructor for rvalue strings
8133-
json_value(string_t&& value)
8131+
json_value(string_t&& value)
81348132
{
81358133
string = create<string_t>(std::move(value));
81368134
}
@@ -8142,7 +8140,7 @@ class basic_json
81428140
}
81438141

81448142
/// constructor for rvalue objects
8145-
json_value(object_t&& value)
8143+
json_value(object_t&& value)
81468144
{
81478145
object = create<object_t>(std::move(value));
81488146
}
@@ -8154,7 +8152,7 @@ class basic_json
81548152
}
81558153

81568154
/// constructor for rvalue arrays
8157-
json_value(array_t&& value)
8155+
json_value(array_t&& value)
81588156
{
81598157
array = create<array_t>(std::move(value));
81608158
}
@@ -8510,7 +8508,7 @@ class basic_json
85108508

85118509
std::for_each(init.begin(), init.end(), [this](const detail::json_ref<basic_json>& element_ref)
85128510
{
8513-
basic_json element = element_ref.moved_or_copied();
8511+
auto element = element_ref.moved_or_copied();
85148512
m_value.object->emplace(
85158513
std::move(*((*element.m_value.array)[0].m_value.string)),
85168514
std::move((*element.m_value.array)[1]));
@@ -8775,8 +8773,7 @@ class basic_json
87758773

87768774
basic_json(const detail::json_ref<basic_json>& ref)
87778775
: basic_json(ref.moved_or_copied())
8778-
{
8779-
}
8776+
{}
87808777

87818778
/*!
87828779
@brief copy constructor
@@ -11578,7 +11575,7 @@ class basic_json
1157811575
@brief add an object to an array
1157911576
@copydoc push_back(basic_json&&)
1158011577
*/
11581-
reference operator+=(basic_json && val)
11578+
reference operator+=(basic_json&& val)
1158211579
{
1158311580
push_back(std::move(val));
1158411581
return *this;
@@ -11697,9 +11694,9 @@ class basic_json
1169711694
{
1169811695
if (is_object() and init.size() == 2 and (*init.begin())->is_string())
1169911696
{
11700-
basic_json&& key = init.begin()->moved_or_copied();
11697+
basic_json&& key = init.begin()->moved_or_copied();
1170111698
push_back(typename object_t::value_type(
11702-
std::move(key.get_ref<string_t&>()), (init.begin() + 1)->moved_or_copied()));
11699+
std::move(key.get_ref<string_t&>()), (init.begin() + 1)->moved_or_copied()));
1170311700
}
1170411701
else
1170511702
{

0 commit comments

Comments
 (0)