mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-03 00:26:19 +08:00
Extended the README
This commit is contained in:
@@ -2,18 +2,22 @@
|
||||
|
||||

|
||||
|
||||
**reflect-cpp** is a C++-20 library for compile-time reflection, similar to [serde](https://github.com/serde-rs) in Rust, [pydantic](https://github.com/pydantic/pydantic) in Python or [encoding](https://github.com/golang/go/tree/master/src/encoding) in Go. As these libraries are among the most widely used libraries in the respective languages, reflect-cpp fills an important gap in C++ development.
|
||||
**reflect-cpp** is a C++-20 library for fast serialization and deserialization using compile-time reflection, similar to [serde](https://github.com/serde-rs) in Rust, [pydantic](https://github.com/pydantic/pydantic) in Python, [encoding](https://github.com/golang/go/tree/master/src/encoding) in Go or [aeson](https://github.com/haskell/aeson/tree/master) in Haskell.
|
||||
|
||||
As the aformentioned libraries are among the most widely used in the respective languages, reflect-cpp fills an important gap in C++ development. It reduces boilerplate code and increases code safety.
|
||||
|
||||
Design principles for reflect-cpp include:
|
||||
|
||||
- Close integration with containers in the C++ library
|
||||
- Close integration with C++ idioms
|
||||
- Close integration with containers from the C++ standard library
|
||||
- Close adherence to C++ idioms
|
||||
- Out-of-the-box support for JSON, no dependencies
|
||||
- Simple installation: If no JSON support is required, reflect-cpp is header-only. For JSON support, only a single source file needs to be included.
|
||||
- Simple extendability to other formats
|
||||
- Simple installation: If no JSON support is required, reflect-cpp is header-only. For JSON support, only a single source file needs to be compiled.
|
||||
- Simple extendability to other serialization formats
|
||||
- Simple extendability to custom classes
|
||||
- No macros
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <rfl>
|
||||
#include <rfl/json>
|
||||
|
||||
@@ -39,8 +43,8 @@ const auto lisa = Person{
|
||||
.children = rfl::default_value // same as std::vector<Person>()
|
||||
};
|
||||
|
||||
// We only need to replace the first name to get from Lisa
|
||||
// to Maggie.
|
||||
// Returns a deep copy of the original object,
|
||||
// replacing first_name.
|
||||
const auto maggie = rfl::replace(
|
||||
lisa, rfl::make_field<"firstName">("Maggie"));
|
||||
|
||||
@@ -53,10 +57,62 @@ const auto homer = Person{
|
||||
// We can now transform this into a JSON string.
|
||||
const std::string json_string = rfl::json::write(homer);
|
||||
|
||||
// And we can retrieve the result.
|
||||
const auto homer2 = rfl::json::read<Person>(json_string).value();
|
||||
// And we can parse the string to recreate the struct.
|
||||
auto homer2 = rfl::json::read<Person>(json_string).value();
|
||||
|
||||
// Fields can be accessed like this:
|
||||
std::cout << "Hello, my name is " << homer.first_name() << " " << homer.last_name() << "." << std::endl;
|
||||
|
||||
// Since homer2 is mutable, we can also change the values like this:
|
||||
homer2.first_name = "Marge";
|
||||
```
|
||||
|
||||
## Support for containers
|
||||
|
||||
reflect-cpp supports the following containers from the C++ standard library:
|
||||
|
||||
- `std::array` (TODO)
|
||||
- `std::deque` (TODO)
|
||||
- `std::forward_list` (TODO)
|
||||
- `std::map` (TODO)
|
||||
- `std::list` (TODO)
|
||||
- `std::optional`
|
||||
- `std::pair`
|
||||
- `std::set`
|
||||
- `std::shared_ptr`
|
||||
- `std::string`
|
||||
- `std::tuple`
|
||||
- `std::unique_ptr`
|
||||
- `std::unordered_map` (TODO)
|
||||
- `std::unordered_set` (TODO)
|
||||
- `std::variant`
|
||||
- `std::vector`
|
||||
|
||||
In addition, it includes the following custom containers:
|
||||
|
||||
- `rfl::Box`: Similar to `std::unique_ptr`, but guaranteed to never be null.
|
||||
- `rfl::Enum`: Similar to `std::variant`, but with explicit tags that make parsing more efficient.
|
||||
- `rfl::Literal`: An explicitly enumerated string.
|
||||
- `rfl::NamedTuple`: Similar to `std::tuple`, but with named fields that can be retrieved via their name at compile time.
|
||||
- `rfl::Ref`: Similar to `std::shared_ptr`, but guaranteed to never be null.
|
||||
- `rfl::TaggedUnion`: Similar to `std::variant`, but with explicit tags that make parsing more efficient, results in a different serialization format than `rfl::Enum`.
|
||||
|
||||
Finally, it is very easy to extend full support to your own custom classes, refer to the [documentation](TODO) for details.
|
||||
|
||||
## Serialization formats
|
||||
|
||||
reflect-cpp currently supports the following serialization formats:
|
||||
|
||||
- **JSON**: Out-of-the-box support, no additional dependencies required.
|
||||
- **XML**: Requires [libxml2](https://github.com/GNOME/libxml2) (TODO).
|
||||
- **flexbuffers**: Requires [flatbuffers](https://github.com/google/flatbuffers).
|
||||
|
||||
reflect-cpp is deliberately designed in a very modular format, using [concepts](https://en.cppreference.com/w/cpp/language/constraints), to make it as easy as possible to support additional serialization formats. Refer to the [documentation](TODO) for details. PRs related to serialization formats are welcome.
|
||||
|
||||
## Installation
|
||||
|
||||
### Using
|
||||
...TODO...
|
||||
|
||||
## Authors
|
||||
|
||||
reflect-cpp has been developed by [scaleML](https://www.scaleml.de), a company specializing in software engineering and machine learning for enterprise applications. It is extensively used in [getML](https://getml.com), a software for automated feature engineering using relational learning.
|
||||
Reference in New Issue
Block a user