Included the meta field retrieval

This commit is contained in:
Patrick Urbanke
2023-12-08 00:13:14 +01:00
parent 385f6434c5
commit 2c761efdcd
9 changed files with 128 additions and 0 deletions
+2
View File
@@ -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
+32
View File
@@ -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_
+3
View File
@@ -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_; }
+18
View File
@@ -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
+32
View File
@@ -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
+1
View File
@@ -38,6 +38,7 @@ add_executable(
"test_literal_unordered_map.cpp"
"test_map.cpp"
"test_map_with_key_validation.cpp"
"test_meta_fields.cpp"
"test_monster_example.cpp"
"test_move_replace.cpp"
"test_multimap.cpp"
+33
View File
@@ -0,0 +1,33 @@
#include "test_meta_fields.hpp"
#include <iostream>
#include <rfl.hpp>
namespace test_meta_fields {
struct Person {
std::string first_name;
std::string last_name = "Simpson";
std::string town = "Springfield";
unsigned int age;
std::vector<Person> children;
};
void test() {
std::cout << std::source_location::current().function_name() << std::endl;
const auto fields = rfl::fields<Person>();
/*std::cout << "Fields in " << rfl::type_name_t<Person>().str() << ":"
<< std::endl;
for (const auto& f : fields) {
std::cout << "name: " << f.name() << ", type: " << f.type() << std::endl;
}*/
// The exact content is somewhat unpredictable, we just want to make sure it
// compiles.
if (fields.size() == 5) {
std::cout << "OK" << std::endl;
}
}
} // namespace test_meta_fields
+4
View File
@@ -0,0 +1,4 @@
namespace test_meta_fields {
void test();
}
+3
View File
@@ -27,6 +27,7 @@
#include "test_literal_unordered_map.hpp"
#include "test_map.hpp"
#include "test_map_with_key_validation.hpp"
#include "test_meta_fields.hpp"
#include "test_monster_example.hpp"
#include "test_multimap.hpp"
#include "test_multiset.hpp"
@@ -122,5 +123,7 @@ int main() {
test_save_load::test();
test_meta_fields::test();
return 0;
}