mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-11-29 14:26:19 +08:00
Added compile-time checks disallowing pointers
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
|
||||
#include "rfl/Field.hpp"
|
||||
#include "rfl/Flatten.hpp"
|
||||
#include "rfl/always_false.hpp"
|
||||
#include "rfl/internal/StringLiteral.hpp"
|
||||
|
||||
namespace rfl {
|
||||
@@ -12,18 +13,39 @@ namespace internal {
|
||||
|
||||
template <internal::StringLiteral _name, class Type>
|
||||
inline auto to_ptr_field(Field<_name, Type>& _field) {
|
||||
if constexpr (std::is_pointer_v<Type>) {
|
||||
static_assert(always_false_v<Type>,
|
||||
"Writing from raw pointers is dangerous and "
|
||||
"therefore unsupported. "
|
||||
"Please consider using std::unique_ptr, rfl::Box, "
|
||||
"std::shared_ptr, rfl::Ref or std::optional instead.");
|
||||
}
|
||||
using T = std::remove_reference_t<Type>;
|
||||
return Field<_name, T*>(&_field.value_);
|
||||
}
|
||||
|
||||
template <internal::StringLiteral _name, class Type>
|
||||
inline auto to_ptr_field(const Field<_name, Type>& _field) {
|
||||
if constexpr (std::is_pointer_v<Type>) {
|
||||
static_assert(always_false_v<Type>,
|
||||
"Writing from raw pointers is dangerous and "
|
||||
"therefore unsupported. "
|
||||
"Please consider using std::unique_ptr, rfl::Box, "
|
||||
"std::shared_ptr, rfl::Ref or std::optional instead.");
|
||||
}
|
||||
using T = std::remove_cvref_t<Type>;
|
||||
return Field<_name, const T*>(&_field.value_);
|
||||
}
|
||||
|
||||
template <class Type>
|
||||
inline auto to_ptr_field(Flatten<Type>& _field) {
|
||||
if constexpr (std::is_pointer_v<Type>) {
|
||||
static_assert(always_false_v<Type>,
|
||||
"Writing from raw pointers is dangerous and "
|
||||
"therefore unsupported. "
|
||||
"Please consider using std::unique_ptr, rfl::Box, "
|
||||
"std::shared_ptr, rfl::Ref or std::optional instead.");
|
||||
}
|
||||
using T = std::remove_reference_t<Type>;
|
||||
return Flatten<T*>(&_field.value_);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user