Skip to content

Commit

Permalink
Implementing copy ctor and equality operator between easyjson objects
Browse files Browse the repository at this point in the history
  • Loading branch information
gbittoun committed Nov 16, 2017
1 parent 2527d8a commit 2280cbf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
17 changes: 16 additions & 1 deletion picojson.h
Original file line number Diff line number Diff line change
Expand Up @@ -1225,13 +1225,22 @@ class easyjson
holder(std::make_unique<value_ptr_holder>(d)) {
}

// Copy constructor

easyjson(const easyjson & other) :
holder(std::make_unique<value_ptr_holder>()) {
get_value() = other.get_value();
}


// picojson::value accessor

value & get_value()
value & get_value() const
{
return holder->get();
}


// Equality operators

easyjson & operator=(bool value) {
Expand Down Expand Up @@ -1264,6 +1273,12 @@ class easyjson
return *this;
}

easyjson & operator=(const easyjson & other) {
holder = std::make_unique<value_ptr_holder>();
get_value() = other.get_value();
return *this;
}

// Assignment operators

easyjson operator[](const std::string & s) {
Expand Down
17 changes: 17 additions & 0 deletions test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -391,5 +391,22 @@ int main(void)
_ok(v[12].get_value().get<std::string>() == "foo", "equals value");
}

{
picojson::easyjson v, v_equal;
v["foo"] = "bar";
v_equal = v;
picojson::easyjson v_copy(v);

_ok(v["foo"].get_value().get<std::string>() == v_copy["foo"].get_value().get<std::string>(), "copy equals original");
_ok(v["foo"].get_value().get<std::string>() == v_equal["foo"].get_value().get<std::string>(), "equality assigned to original");

v_copy["foo"] = "baz";
v_equal["foo"] = "picojson rocks !";

_ok(v["foo"].get_value().get<std::string>() == "bar", "original not modified");
_ok(v_copy["foo"].get_value().get<std::string>() == "baz", "changed value in copy is OK");
_ok(v_equal["foo"].get_value().get<std::string>() == "picojson rocks !", "changed value after equality is OK");
}

return done_testing();
}

0 comments on commit 2280cbf

Please sign in to comment.