Skip to content

Commit

Permalink
fix: wrong move for operator=. and add more operator= for array and o…
Browse files Browse the repository at this point in the history
…bject
  • Loading branch information
MistEO committed Feb 24, 2024
1 parent 98408e1 commit 35f1b14
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
5 changes: 5 additions & 0 deletions include/common/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ class basic_array

basic_array<string_t>& operator=(const basic_array<string_t>&) = default;
basic_array<string_t>& operator=(basic_array<string_t>&&) noexcept = default;
template <typename value_t, std::enable_if_t<std::is_convertible_v<value_t, basic_array<string_t>>, bool> = true>
basic_array<string_t>& operator=(value_t rhs)
{
return *this = basic_array<string_t>(std::move(rhs));
}

bool operator==(const basic_array<string_t>& rhs) const;
bool operator!=(const basic_array<string_t>& rhs) const { return !(*this == rhs); }
Expand Down
5 changes: 5 additions & 0 deletions include/common/object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ class basic_object

basic_object<string_t>& operator=(const basic_object<string_t>&) = default;
basic_object<string_t>& operator=(basic_object<string_t>&&) = default;
template <typename value_t, std::enable_if_t<std::is_convertible_v<value_t, basic_object<string_t>>, bool> = true>
basic_object<string_t>& operator=(value_t rhs)
{
return *this = basic_object<string_t>(std::move(rhs));
}

bool operator==(const basic_object<string_t>& rhs) const;
bool operator!=(const basic_object<string_t>& rhs) const { return !(*this == rhs); }
Expand Down
7 changes: 1 addition & 6 deletions include/common/value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,7 @@ class basic_value
basic_value<string_t>& operator=(const basic_value<string_t>& rhs);
basic_value<string_t>& operator=(basic_value<string_t>&&) noexcept;
template <typename value_t, std::enable_if_t<std::is_convertible_v<value_t, basic_value<string_t>>, bool> = true>
basic_value<string_t>& operator=(const value_t& rhs)
{
return *this = basic_value<string_t>(rhs);
}
template <typename value_t, std::enable_if_t<std::is_convertible_v<value_t, basic_value<string_t>>, bool> = true>
basic_value<string_t>& operator=(value_t&& rhs)
basic_value<string_t>& operator=(value_t rhs)
{
return *this = basic_value<string_t>(std::move(rhs));
}
Expand Down
25 changes: 25 additions & 0 deletions test/serializing_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,31 @@ bool serializing()
root["arr"].emplace(5);
root["arr"] += json::array { 6, 7 };

std::string str = "hello";
root["str"] = str;
if (str != root["str"].as_string()) {
std::cerr << "error: " << root["str"].as_string() << std::endl;
return false;
}
std::vector<int> vec_op = { 1, 2, 3, 4, 5 };
root["arr from vec"] = vec_op;
json::array vec_obj = vec_op;
if (vec_obj.as_collection<int>() != root["arr from vec"].as_collection<int>()) {
std::cerr << "error: " << root["arr from vec"].as_string() << std::endl;
return false;
}

std::map<std::string, int> map_op {
{ "key1", 1 },
{ "key2", 2 },
};
root["obj from map"] = map_op;
json::object map_obj = map_op;
if (map_obj.as_map<int>() != root["obj from map"].as_map<int>()) {
std::cerr << "error: " << root["obj from map"].as_string() << std::endl;
return false;
}

auto jarr = root.get("arr", json::array());
if (jarr.size() != 7) {
std::cerr << "error: " << jarr.size() << std::endl;
Expand Down

0 comments on commit 35f1b14

Please sign in to comment.