From 2ef144c04191b709aeec1f7d0d9e08e5694bd49a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dr=2E=20Patrick=20Urbanke=20=28=E5=8A=89=E8=87=AA=E6=88=90?= =?UTF-8?q?=29?= Date: Tue, 5 Nov 2024 00:14:56 +0100 Subject: [PATCH] Added support for raw pointers; #218 (#244) --- docs/processors.md | 36 ++++++++++++++++ include/rfl.hpp | 1 + include/rfl/AllowRawPtrs.hpp | 18 ++++++++ include/rfl/Processors.hpp | 6 +++ include/rfl/internal/is_allow_raw_ptrs_v.hpp | 29 +++++++++++++ include/rfl/parsing/Parser_ptr.hpp | 30 +++++++++---- tests/json/test_pointer_fields.cpp | 44 ++++++++++++++++++++ 7 files changed, 156 insertions(+), 8 deletions(-) create mode 100644 include/rfl/AllowRawPtrs.hpp create mode 100644 include/rfl/internal/is_allow_raw_ptrs_v.hpp create mode 100644 tests/json/test_pointer_fields.cpp diff --git a/docs/processors.md b/docs/processors.md index 973d79fa..cebafe38 100644 --- a/docs/processors.md +++ b/docs/processors.md @@ -34,6 +34,7 @@ The resulting JSON string looks like this: reflect-cpp currently supports the following processors: - `rfl::AddStructName` +- `rfl::AllowRawPtrs` - `rfl::DefaultIfMissing` - `rfl::NoExtraFields` - `rfl::NoFieldNames` @@ -60,6 +61,41 @@ The resulting JSON string looks like this: {"type":"Person","first_name":"Homer","last_name":"Simpson","age":45} ``` +### `rfl::AllowRawPtrs` + +By default, reflect-cpp does not allow *reading into* raw pointers. (*Writing from* raw pointers is never a problem.) This is because reading into raw pointers means that the library will allocate memory that the user then has to manually delete. This can lead to misunderstandings and memory leaks. + +You might want to consider using some alternatives, such as `std::unique_ptr`, `rfl::Box`, +`std::shared_ptr`, `rfl::Ref` or `std::optional`. But if you absolutely have to use raw pointers, you can pass `rfl::AllowRawPtrs` to `read`: + +```cpp +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::vector* children;` +}; + +const auto person = + rfl::json::read(json_str); +``` + +However, you must keep in mind that it is now **YOUR** responsibility +to clean up. Otherwise, there **WILL** be a memory leak. + +```cpp +void delete_raw_pointers(const Person& _person) { + if (!_person.children) { + return; + } + for (const auto& child: _person.children) { + delete_raw_pointers(child); + } + delete _person.children; +} + +delete_raw_pointers(person); +``` + ### `rfl::DefaultIfMissing` The `rfl::DefaultIfMissing` processor is only relevant for reading data. For writing data, it will make no difference. diff --git a/include/rfl.hpp b/include/rfl.hpp index 04892f1a..9b290516 100644 --- a/include/rfl.hpp +++ b/include/rfl.hpp @@ -9,6 +9,7 @@ #include "rfl/AddStructName.hpp" #include "rfl/AllOf.hpp" +#include "rfl/AllowRawPtrs.hpp" #include "rfl/AnyOf.hpp" #include "rfl/Attribute.hpp" #include "rfl/Binary.hpp" diff --git a/include/rfl/AllowRawPtrs.hpp b/include/rfl/AllowRawPtrs.hpp new file mode 100644 index 00000000..ed9576a7 --- /dev/null +++ b/include/rfl/AllowRawPtrs.hpp @@ -0,0 +1,18 @@ +#ifndef RFL_ALLOWRAWPTRS_HPP_ +#define RFL_ALLOWRAWPTRS_HPP_ + +namespace rfl { + +/// This is a "fake" processor - it doesn't do much in itself, but its +/// inclusion instructs the parsers to allow raw pointers. +struct AllowRawPtrs { + public: + template + static auto process(auto&& _named_tuple) { + return _named_tuple; + } +}; + +} // namespace rfl + +#endif diff --git a/include/rfl/Processors.hpp b/include/rfl/Processors.hpp index abe05700..f2d95d30 100644 --- a/include/rfl/Processors.hpp +++ b/include/rfl/Processors.hpp @@ -3,6 +3,7 @@ #include +#include "internal/is_allow_raw_ptrs_v.hpp" #include "internal/is_default_if_missing_v.hpp" #include "internal/is_no_extra_fields_v.hpp" #include "internal/is_no_field_names_v.hpp" @@ -16,6 +17,7 @@ struct Processors; template <> struct Processors<> { + static constexpr bool allow_raw_ptrs_ = false; static constexpr bool all_required_ = false; static constexpr bool default_if_missing_ = false; static constexpr bool no_extra_fields_ = false; @@ -30,6 +32,10 @@ struct Processors<> { template struct Processors { + static constexpr bool allow_raw_ptrs_ = + std::disjunction_v, + internal::is_allow_raw_ptrs...>; + static constexpr bool all_required_ = std::disjunction_v, internal::is_no_optionals...>; diff --git a/include/rfl/internal/is_allow_raw_ptrs_v.hpp b/include/rfl/internal/is_allow_raw_ptrs_v.hpp new file mode 100644 index 00000000..d29e1430 --- /dev/null +++ b/include/rfl/internal/is_allow_raw_ptrs_v.hpp @@ -0,0 +1,29 @@ +#ifndef RFL_INTERNAL_ISALLOWRAWPTRS_HPP_ +#define RFL_INTERNAL_ISALLOWRAWPTRS_HPP_ + +#include +#include +#include + +#include "../AllowRawPtrs.hpp" + +namespace rfl { +namespace internal { + +template +class is_allow_raw_ptrs; + +template +class is_allow_raw_ptrs : public std::false_type {}; + +template <> +class is_allow_raw_ptrs : public std::true_type {}; + +template +constexpr bool is_allow_raw_ptrs_v = + is_allow_raw_ptrs>>::value; + +} // namespace internal +} // namespace rfl + +#endif diff --git a/include/rfl/parsing/Parser_ptr.hpp b/include/rfl/parsing/Parser_ptr.hpp index 20950796..d2e68ffa 100644 --- a/include/rfl/parsing/Parser_ptr.hpp +++ b/include/rfl/parsing/Parser_ptr.hpp @@ -22,14 +22,28 @@ struct Parser { using ParentType = Parent; /// Expresses the variables as type T. - static Result read(const R&, const InputVarType&) noexcept { - static_assert(always_false_v, - "Reading into raw pointers is dangerous and " - "therefore unsupported. " - "Please consider using std::unique_ptr, rfl::Box, " - "std::shared_ptr, " - "rfl::Ref or std::optional instead."); - return Error("Unsupported."); + static Result read(const R& _r, const InputVarType& _var) noexcept { + if constexpr (!ProcessorsType::allow_raw_ptrs_) { + static_assert( + always_false_v, + "Reading into raw pointers is dangerous and " + "therefore unsupported by default. " + "Please consider using std::unique_ptr, rfl::Box, " + "std::shared_ptr, " + "rfl::Ref or std::optional instead. " + "If you absolutely must use raw pointers, " + "you can pass the rfl::AllowRawPtrs processor. " + "Please note that it is then YOUR responsibility " + "to delete the allocated memory. Please also refer " + "to the related documentation (in the section on processors)."); + return Error("Unsupported."); + } else { + if (_r.is_empty(_var)) { + return nullptr; + } + return Parser::read(_r, _var).transform( + [](T&& _t) { return new T(std::move(_t)); }); + } } template diff --git a/tests/json/test_pointer_fields.cpp b/tests/json/test_pointer_fields.cpp new file mode 100644 index 00000000..de1461b9 --- /dev/null +++ b/tests/json/test_pointer_fields.cpp @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_pointer_fields { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::vector* children; +}; + +TEST(json, test_pointer_fields) { + const auto bart = Person{.first_name = "Bart", .children = nullptr}; + + const auto lisa = Person{.first_name = "Lisa", .children = nullptr}; + + const auto maggie = Person{.first_name = "Maggie", .children = nullptr}; + + const auto homer = + Person{.first_name = "Homer", + .children = new std::vector({bart, lisa, maggie})}; + + const std::string expected = + R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson","children":null},{"firstName":"Lisa","lastName":"Simpson","children":null},{"firstName":"Maggie","lastName":"Simpson","children":null}]})"; + + const auto json_str = rfl::json::write(homer); + + EXPECT_EQ(json_str, expected); + + const auto homer2 = + rfl::json::read(json_str).value(); + const auto json_str2 = rfl::json::write(homer2); + + EXPECT_EQ(json_str2, expected); + + delete homer.children; + delete homer2.children; +} +} // namespace test_pointer_fields