mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-03 16:56:17 +08:00
Make rfl::num_fields public (#465)
This commit is contained in:
+23
-2
@@ -2,8 +2,9 @@
|
|||||||
|
|
||||||
## `rfl::Reflector`
|
## `rfl::Reflector`
|
||||||
|
|
||||||
If you absolutely do not want to make any changes to your original classes whatsoever,
|
If you absolutely do not want to (or are unable to) make any changes to your
|
||||||
You can create a Reflector template specialization for your type:
|
original classes whatsoever, you can create a Reflector template specialization
|
||||||
|
for your type:
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
namespace rfl {
|
namespace rfl {
|
||||||
@@ -25,6 +26,26 @@ struct Reflector<Person> {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
One way to help make sure that your `ReflType` is kept up to date with your
|
||||||
|
original class is to use the `rfl::num_fields<T>` utility to implement a compile-
|
||||||
|
time assertion to verify that they have the same number of fields. The
|
||||||
|
`rfl::num_fields<T>` utility can be used even in cases where the original
|
||||||
|
class is too complex for `reflect-cpp`'s default reflection logic or
|
||||||
|
`rfl::to_view()` to be able to handle.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
namespace rfl {
|
||||||
|
template <>
|
||||||
|
struct Reflector<Person> {
|
||||||
|
struct ReflType {
|
||||||
|
std::string first_name;
|
||||||
|
std::string last_name;
|
||||||
|
};
|
||||||
|
static_assert(rfl::num_fields<ReflType> == rfl::num_fields<Person>,
|
||||||
|
"ReflType and actual type must have the same number of fields");
|
||||||
|
// ...
|
||||||
|
```
|
||||||
|
|
||||||
It's also fine to define just the `from` method when the original class is
|
It's also fine to define just the `from` method when the original class is
|
||||||
only written, or `to` when the original class is only read:
|
only written, or `to` when the original class is only read:
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -58,7 +58,7 @@
|
|||||||
|
|
||||||
[Custom classes](concepts/custom_classes.md) - For custom classes with private fields.
|
[Custom classes](concepts/custom_classes.md) - For custom classes with private fields.
|
||||||
|
|
||||||
[Custom parsers for your classes](custom_parser.md) - For custom classes with private fields that you want to leave absolutely untouched.
|
[Custom parsers for your classes](custom_parser.md) - For custom classes with private fields that you want (or need) to leave absolutely untouched.
|
||||||
|
|
||||||
## Useful helper functions and classes
|
## Useful helper functions and classes
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
#ifndef RFL_NUM_FIELDS_HPP_
|
||||||
|
#define RFL_NUM_FIELDS_HPP_
|
||||||
|
|
||||||
|
#include "internal/num_fields.hpp"
|
||||||
|
|
||||||
|
namespace rfl {
|
||||||
|
|
||||||
|
/// Returns the number of fields fields.
|
||||||
|
template <class T>
|
||||||
|
constexpr std::size_t num_fields = internal::num_fields<T>;
|
||||||
|
|
||||||
|
} // namespace rfl
|
||||||
|
|
||||||
|
#endif
|
||||||
+11
-5
@@ -3,7 +3,7 @@
|
|||||||
reflect-cpp uses vcpkg for dependency management, including
|
reflect-cpp uses vcpkg for dependency management, including
|
||||||
gtest, which is required for the tests.
|
gtest, which is required for the tests.
|
||||||
|
|
||||||
```bash
|
```shell
|
||||||
# bootstrap vcpkg if you haven't done so already
|
# bootstrap vcpkg if you haven't done so already
|
||||||
git submodule update --init
|
git submodule update --init
|
||||||
./vcpkg/bootstrap-vcpkg.sh # Linux, macOS
|
./vcpkg/bootstrap-vcpkg.sh # Linux, macOS
|
||||||
@@ -15,7 +15,7 @@ git submodule update --init
|
|||||||
|
|
||||||
To compile the tests, do the following:
|
To compile the tests, do the following:
|
||||||
|
|
||||||
```bash
|
```shell
|
||||||
cmake -S . -B build -DCMAKE_CXX_STANDARD=20 -DCMAKE_BUILD_TYPE=Release -DREFLECTCPP_BUILD_TESTS=ON
|
cmake -S . -B build -DCMAKE_CXX_STANDARD=20 -DCMAKE_BUILD_TYPE=Release -DREFLECTCPP_BUILD_TESTS=ON
|
||||||
cmake --build build -j 4 # gcc, clang
|
cmake --build build -j 4 # gcc, clang
|
||||||
cmake --build build --config Release -j 4 # MSVC
|
cmake --build build --config Release -j 4 # MSVC
|
||||||
@@ -23,7 +23,7 @@ cmake --build build --config Release -j 4 # MSVC
|
|||||||
|
|
||||||
To run the tests, do the following:
|
To run the tests, do the following:
|
||||||
|
|
||||||
```
|
```shell
|
||||||
./build/tests/json/reflect-cpp-json-tests
|
./build/tests/json/reflect-cpp-json-tests
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -31,13 +31,19 @@ To run the tests, do the following:
|
|||||||
|
|
||||||
To compile the tests with serialization formats other than JSON, do the following:
|
To compile the tests with serialization formats other than JSON, do the following:
|
||||||
|
|
||||||
```bash
|
```shell
|
||||||
cmake -S . -B build -DCMAKE_CXX_STANDARD=20 -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_AVRO=ON -DREFLECTCPP_BSON=ON -DREFLECTCPP_CAPNPROTO=ON -DREFLECTCPP_CBOR=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_MSGPACK=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_TOML=ON -DREFLECTCPP_UBJSON=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release
|
cmake -S . -B build -DCMAKE_CXX_STANDARD=20 -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_AVRO=ON -DREFLECTCPP_BSON=ON -DREFLECTCPP_CAPNPROTO=ON -DREFLECTCPP_CBOR=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_MSGPACK=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_TOML=ON -DREFLECTCPP_UBJSON=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release
|
||||||
cmake --build build -j 4 # gcc, clang
|
cmake --build build -j 4 # gcc, clang
|
||||||
cmake --build build --config Release -j 4 # MSVC
|
cmake --build build --config Release -j 4 # MSVC
|
||||||
```
|
```
|
||||||
|
|
||||||
To run the tests, do the following:
|
You can run all of the tests via `ctest`, as in:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
ctest --test-dir build --output-on-failure
|
||||||
|
```
|
||||||
|
|
||||||
|
Or you can run tests individually, as in:
|
||||||
|
|
||||||
```
|
```
|
||||||
./build/tests/avro/reflect-cpp-avro-tests
|
./build/tests/avro/reflect-cpp-avro-tests
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <rfl.hpp>
|
#include <rfl.hpp>
|
||||||
|
|
||||||
#include "rfl/internal/num_fields.hpp"
|
#include "rfl/num_fields.hpp"
|
||||||
|
|
||||||
namespace test_inheritance2 {
|
namespace test_inheritance2 {
|
||||||
|
|
||||||
@@ -31,23 +31,28 @@ struct EmptyDerived2 : EmptyBase1, EmptyBase2, BaseX {};
|
|||||||
TEST(json, test_inheritance2) {
|
TEST(json, test_inheritance2) {
|
||||||
Derived1 derived1;
|
Derived1 derived1;
|
||||||
const auto derived1_view = rfl::to_view(derived1);
|
const auto derived1_view = rfl::to_view(derived1);
|
||||||
|
static_assert(rfl::num_fields<Derived1> == 2);
|
||||||
static_assert(derived1_view.size() == 2);
|
static_assert(derived1_view.size() == 2);
|
||||||
|
|
||||||
Derived2 derived2;
|
Derived2 derived2;
|
||||||
const auto derived2_view = rfl::to_view(derived2);
|
const auto derived2_view = rfl::to_view(derived2);
|
||||||
|
static_assert(rfl::num_fields<Derived2> == 3);
|
||||||
static_assert(derived2_view.size() == 3);
|
static_assert(derived2_view.size() == 3);
|
||||||
|
|
||||||
EmptyDerived1 empty_derived0;
|
EmptyDerived0 empty_derived0;
|
||||||
auto empty_derived0_view = rfl::to_view(empty_derived0);
|
auto empty_derived0_view = rfl::to_view(empty_derived0);
|
||||||
|
static_assert(rfl::num_fields<EmptyDerived0> == 2);
|
||||||
static_assert(empty_derived0_view.size() == 2);
|
static_assert(empty_derived0_view.size() == 2);
|
||||||
|
|
||||||
EmptyDerived1 empty_derived1;
|
EmptyDerived1 empty_derived1;
|
||||||
auto empty_derived1_view = rfl::to_view(empty_derived1);
|
auto empty_derived1_view = rfl::to_view(empty_derived1);
|
||||||
|
static_assert(rfl::num_fields<EmptyDerived1> == 2);
|
||||||
static_assert(empty_derived1_view.size() == 2);
|
static_assert(empty_derived1_view.size() == 2);
|
||||||
|
|
||||||
EmptyDerived1 empty_derived2;
|
EmptyDerived2 empty_derived2;
|
||||||
auto empty_derived2_view = rfl::to_view(empty_derived2);
|
auto empty_derived2_view = rfl::to_view(empty_derived2);
|
||||||
static_assert(empty_derived0_view.size() == 2);
|
static_assert(rfl::num_fields<EmptyDerived1> == 2);
|
||||||
|
static_assert(empty_derived2_view.size() == 2);
|
||||||
|
|
||||||
EXPECT_TRUE(true);
|
EXPECT_TRUE(true);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user