Skip to content

Commit

Permalink
Bugfix and added test for shared_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzicheng1987 committed Mar 31, 2024
1 parent 1f0dcf5 commit 4fa84ff
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
8 changes: 3 additions & 5 deletions include/rfl/parsing/Parser_shared_ptr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
#include "Parser_base.hpp"
#include "schema/Type.hpp"

namespace rfl {
namespace parsing {
namespace rfl::parsing {

template <class R, class W, class T>
requires AreReaderAndWriter<R, W, std::shared_ptr<T>>
Expand All @@ -25,7 +24,7 @@ struct Parser<R, W, std::shared_ptr<T>> {

static Result<std::shared_ptr<T>> read(const R& _r,
const InputVarType& _var) noexcept {
if (_r.is_empty(*_var)) {
if (_r.is_empty(_var)) {
return std::shared_ptr<T>();
}
const auto to_ptr = [](auto&& _t) {
Expand Down Expand Up @@ -53,7 +52,6 @@ struct Parser<R, W, std::shared_ptr<T>> {
}
};

} // namespace parsing
} // namespace rfl
} // namespace rfl::parsing

#endif
35 changes: 35 additions & 0 deletions tests/json/test_shared_ptr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <iostream>
#include <memory>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <source_location>
#include <string>
#include <vector>

#include "test_unique_ptr.hpp"
#include "write_and_read.hpp"

namespace test_shared_ptr {

struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name = "Simpson";
std::shared_ptr<std::vector<Person>> children;
};

void test() {
std::cout << std::source_location::current().function_name() << std::endl;

auto children = std::make_shared<std::vector<Person>>();
children->emplace_back(Person{.first_name = "Bart"});
children->emplace_back(Person{.first_name = "Lisa"});
children->emplace_back(Person{.first_name = "Maggie"});

const auto homer =
Person{.first_name = "Homer", .children = std::move(children)};

write_and_read(
homer,
R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})");
}
} // namespace test_shared_ptr
4 changes: 4 additions & 0 deletions tests/json/test_shared_ptr.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace test_shared_ptr {
void test();
}

2 changes: 2 additions & 0 deletions tests/json/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
#include "test_result.hpp"
#include "test_save_load.hpp"
#include "test_set.hpp"
#include "test_shared_ptr.hpp"
#include "test_size.hpp"
#include "test_std_ref.hpp"
#include "test_string_map.hpp"
Expand All @@ -89,6 +90,7 @@ int main() {
test_optional_fields::test();
test_unique_ptr::test();
test_unique_ptr2::test();
test_shared_ptr::test();
test_literal::test();
test_variant::test();
test_tagged_union::test();
Expand Down

0 comments on commit 4fa84ff

Please sign in to comment.