mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-06 02:36:18 +08:00
Included the meta field retrieval
This commit is contained in:
@@ -34,6 +34,7 @@
|
||||
#include "rfl/define_variant.hpp"
|
||||
#include "rfl/extract_discriminators.hpp"
|
||||
#include "rfl/field_type.hpp"
|
||||
#include "rfl/fields.hpp"
|
||||
#include "rfl/from_named_tuple.hpp"
|
||||
#include "rfl/get.hpp"
|
||||
#include "rfl/make_named_tuple.hpp"
|
||||
@@ -44,6 +45,7 @@
|
||||
#include "rfl/remove_fields.hpp"
|
||||
#include "rfl/replace.hpp"
|
||||
#include "rfl/to_named_tuple.hpp"
|
||||
#include "rfl/type_name_t.hpp"
|
||||
#include "rfl/visit.hpp"
|
||||
|
||||
#ifdef _MSC_VER
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
#ifndef RFL_METAFIELD_HPP_
|
||||
#define RFL_METAFIELD_HPP_
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace rfl {
|
||||
|
||||
/// Contains meta-information about a field in a struct.
|
||||
class MetaField {
|
||||
public:
|
||||
MetaField(const std::string& _name, const std::string& _type)
|
||||
: name_(_name), type_(_type) {}
|
||||
|
||||
~MetaField() = default;
|
||||
|
||||
/// The name of the field we describe.
|
||||
const std::string& name() const { return name_; };
|
||||
|
||||
/// The type of the field we describe.
|
||||
const std::string& type() const { return type_; };
|
||||
|
||||
private:
|
||||
/// The name of the field we describe.
|
||||
std::string name_;
|
||||
|
||||
/// The type of the field we describe.
|
||||
std::string type_;
|
||||
};
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif // RFL_TAGGEDUNION_HPP_
|
||||
@@ -286,6 +286,9 @@ class NamedTuple {
|
||||
std::forward<Tail>(_tail)...);
|
||||
}
|
||||
|
||||
/// Returns the size of the named tuple
|
||||
static constexpr size_t size() { return std::tuple_size_v<Values>; }
|
||||
|
||||
/// Returns the underlying std::tuple.
|
||||
Values& values() { return values_; }
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
#ifndef RFL_FIELDS_HPP_
|
||||
#define RFL_FIELDS_HPP_
|
||||
|
||||
#include "internal/get_meta_fields.hpp"
|
||||
#include "rfl/internal/get_meta_fields.hpp"
|
||||
#include "rfl/named_tuple_t.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
/// Returns meta-information about the fields.
|
||||
template <class T>
|
||||
auto fields() {
|
||||
return internal::get_meta_fields<named_tuple_t<T>>();
|
||||
}
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,32 @@
|
||||
#ifndef RFL_INTERNAL_GETMETAFIELDS_HPP_
|
||||
#define RFL_INTERNAL_GETMETAFIELDS_HPP_
|
||||
|
||||
#include <array>
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
|
||||
#include "rfl/MetaField.hpp"
|
||||
#include "rfl/type_name_t.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class NamedTupleType, class... AlreadyExtracted>
|
||||
auto get_meta_fields(AlreadyExtracted&&... _already_extracted) {
|
||||
constexpr size_t i = sizeof...(_already_extracted);
|
||||
if constexpr (NamedTupleType::size() == i) {
|
||||
return std::array<MetaField, i>{std::move(_already_extracted)...};
|
||||
} else {
|
||||
using FieldType = std::tuple_element_t<i, typename NamedTupleType::Fields>;
|
||||
auto name = typename FieldType::Name().str();
|
||||
auto type = type_name_t<typename FieldType::Type>().str();
|
||||
return get_meta_fields<NamedTupleType>(
|
||||
std::move(_already_extracted)...,
|
||||
MetaField(std::move(name), std::move(type)));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user