diff --git a/include/rfl.hpp b/include/rfl.hpp index 51b465f6..6b800dd4 100644 --- a/include/rfl.hpp +++ b/include/rfl.hpp @@ -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 diff --git a/include/rfl/MetaField.hpp b/include/rfl/MetaField.hpp new file mode 100644 index 00000000..d55cbb2c --- /dev/null +++ b/include/rfl/MetaField.hpp @@ -0,0 +1,32 @@ +#ifndef RFL_METAFIELD_HPP_ +#define RFL_METAFIELD_HPP_ + +#include + +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_ diff --git a/include/rfl/NamedTuple.hpp b/include/rfl/NamedTuple.hpp index af96dfab..e402992d 100644 --- a/include/rfl/NamedTuple.hpp +++ b/include/rfl/NamedTuple.hpp @@ -286,6 +286,9 @@ class NamedTuple { std::forward(_tail)...); } + /// Returns the size of the named tuple + static constexpr size_t size() { return std::tuple_size_v; } + /// Returns the underlying std::tuple. Values& values() { return values_; } diff --git a/include/rfl/fields.hpp b/include/rfl/fields.hpp new file mode 100644 index 00000000..e3d80cef --- /dev/null +++ b/include/rfl/fields.hpp @@ -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 +auto fields() { + return internal::get_meta_fields>(); +} + +} // namespace rfl + +#endif diff --git a/include/rfl/internal/get_meta_fields.hpp b/include/rfl/internal/get_meta_fields.hpp new file mode 100644 index 00000000..4ff10ba9 --- /dev/null +++ b/include/rfl/internal/get_meta_fields.hpp @@ -0,0 +1,32 @@ +#ifndef RFL_INTERNAL_GETMETAFIELDS_HPP_ +#define RFL_INTERNAL_GETMETAFIELDS_HPP_ + +#include +#include +#include + +#include "rfl/MetaField.hpp" +#include "rfl/type_name_t.hpp" + +namespace rfl { +namespace internal { + +template +auto get_meta_fields(AlreadyExtracted&&... _already_extracted) { + constexpr size_t i = sizeof...(_already_extracted); + if constexpr (NamedTupleType::size() == i) { + return std::array{std::move(_already_extracted)...}; + } else { + using FieldType = std::tuple_element_t; + auto name = typename FieldType::Name().str(); + auto type = type_name_t().str(); + return get_meta_fields( + std::move(_already_extracted)..., + MetaField(std::move(name), std::move(type))); + } +} + +} // namespace internal +} // namespace rfl + +#endif diff --git a/tests/json/CMakeLists.txt b/tests/json/CMakeLists.txt index fea98582..7ba5a0f5 100644 --- a/tests/json/CMakeLists.txt +++ b/tests/json/CMakeLists.txt @@ -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" diff --git a/tests/json/test_meta_fields.cpp b/tests/json/test_meta_fields.cpp new file mode 100644 index 00000000..90d17313 --- /dev/null +++ b/tests/json/test_meta_fields.cpp @@ -0,0 +1,33 @@ +#include "test_meta_fields.hpp" + +#include +#include + +namespace test_meta_fields { + +struct Person { + std::string first_name; + std::string last_name = "Simpson"; + std::string town = "Springfield"; + unsigned int age; + std::vector children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto fields = rfl::fields(); + + /*std::cout << "Fields in " << rfl::type_name_t().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 diff --git a/tests/json/test_meta_fields.hpp b/tests/json/test_meta_fields.hpp new file mode 100644 index 00000000..903af004 --- /dev/null +++ b/tests/json/test_meta_fields.hpp @@ -0,0 +1,4 @@ +namespace test_meta_fields { +void test(); +} + diff --git a/tests/json/tests.cpp b/tests/json/tests.cpp index 6592d4b7..b3e1d272 100644 --- a/tests/json/tests.cpp +++ b/tests/json/tests.cpp @@ -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; }