Added support for raw pointers; #218 (#244)

This commit is contained in:
Dr. Patrick Urbanke (劉自成)
2024-11-05 00:14:56 +01:00
committed by GitHub
parent 458ca09198
commit 2ef144c041
7 changed files with 156 additions and 8 deletions
+1
View File
@@ -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"
+18
View File
@@ -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 <class StructType>
static auto process(auto&& _named_tuple) {
return _named_tuple;
}
};
} // namespace rfl
#endif
+6
View File
@@ -3,6 +3,7 @@
#include <type_traits>
#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 <class Head, class... Tail>
struct Processors<Head, Tail...> {
static constexpr bool allow_raw_ptrs_ =
std::disjunction_v<internal::is_allow_raw_ptrs<Head>,
internal::is_allow_raw_ptrs<Tail>...>;
static constexpr bool all_required_ =
std::disjunction_v<internal::is_no_optionals<Head>,
internal::is_no_optionals<Tail>...>;
@@ -0,0 +1,29 @@
#ifndef RFL_INTERNAL_ISALLOWRAWPTRS_HPP_
#define RFL_INTERNAL_ISALLOWRAWPTRS_HPP_
#include <tuple>
#include <type_traits>
#include <utility>
#include "../AllowRawPtrs.hpp"
namespace rfl {
namespace internal {
template <class T>
class is_allow_raw_ptrs;
template <class T>
class is_allow_raw_ptrs : public std::false_type {};
template <>
class is_allow_raw_ptrs<AllowRawPtrs> : public std::true_type {};
template <class T>
constexpr bool is_allow_raw_ptrs_v =
is_allow_raw_ptrs<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;
} // namespace internal
} // namespace rfl
#endif
+22 -8
View File
@@ -22,14 +22,28 @@ struct Parser<R, W, T*, ProcessorsType> {
using ParentType = Parent<W>;
/// Expresses the variables as type T.
static Result<T*> read(const R&, const InputVarType&) noexcept {
static_assert(always_false_v<T>,
"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<T*> read(const R& _r, const InputVarType& _var) noexcept {
if constexpr (!ProcessorsType::allow_raw_ptrs_) {
static_assert(
always_false_v<T>,
"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<R, W, T, ProcessorsType>::read(_r, _var).transform(
[](T&& _t) { return new T(std::move(_t)); });
}
}
template <class P>