Skip to content

Commit

Permalink
Implemented some measures to reduce the compile time; #86 (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzicheng1987 committed Jun 14, 2024
1 parent 41872c4 commit e369940
Show file tree
Hide file tree
Showing 17 changed files with 629 additions and 571 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ The following table lists the serialization formats currently supported by refle

| Format | Library | Version | License | Remarks |
|--------------|------------------------------------------------------|--------------|------------| -----------------------------------------------------|
| JSON | [yyjson](https://github.com/ibireme/yyjson) | >= 0.8.0 | MIT | out-of-the-box support, included in this repository |
| JSON | [yyjson](https://github.com/ibireme/yyjson) | 0.8.0 | MIT | out-of-the-box support, included in this repository |
| BSON | [libbson](https://github.com/mongodb/libbson) | >= 1.25.1 | Apache 2.0 | JSON-like binary format |
| CBOR | [tinycbor](https://github.com/intel/tinycbor) | >= 0.6.0 | MIT | JSON-like binary format |
| flexbuffers | [flatbuffers](https://github.com/google/flatbuffers) | >= 23.5.26 | Apache 2.0 | Schema-less version of flatbuffers, binary format |
Expand Down
2 changes: 2 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

1.13) [Standard containers](https://github.com/getml/reflect-cpp/blob/main/docs/standard_containers.md) - Describes how reflect-cpp treats containers in the standard library.

1.14) [C arrays and inheritance](https://github.com/getml/reflect-cpp/blob/main/docs/c_arrays_and_inheritance.md) - Describes how reflect-cpp handles C arrays and inheritance.

## 2) Validation

2.1) [Regex patterns](https://github.com/getml/reflect-cpp/blob/main/docs/patterns.md) - For requiring that strings follow used-defined regex patterns.
Expand Down
108 changes: 108 additions & 0 deletions docs/c_arrays_and_inheritance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# C Arrays and inheritance

reflect-cpp supports fixed-size C arrays and some forms of inheritance, but only as an opt-in.

We have made this design decision, because checking for C arrays and inheritance
requires a disproportionate amount of compile time and we believe it is
more important to reduce compile time than to support things out-of-the-box
that most people won't need anyway. Since there are good alternatives for
both of these problems, it is recommended to avoid using C arrays or
inheritance altogether.

Note that C arrays are not the same thing as `std::array`. `std::array` is always
supported and is the recommended alternative.

If you want support for these, you will have to pass the flag `-D REFLECT_CPP_C_ARRAYS_OR_INHERITANCE`
during compilation.

## C arrays

Suppose you have a struct like this:

```cpp
struct Person{
std::string first_name;
std::string last_name;
int[5] post_code;
};
```

In this example, `post_code` is a C array. The best way to handle this
is to simply replace the C array with `std::array`, like this:

```cpp
struct Person{
std::string first_name;
std::string last_name;
std::array<int, 5> post_code;
};
```

However, in some cases, using C arrays is unavoidable. For instance `bson_oid_t` contains
a C array under-the-hood, so if you want to parse a `bson_oid_t` you will have
no choice but to pass the flag mentioned above.

## Inheritance

reflect-cpp supports some form of inheritance. Because we use structured bindings to
retrieve the fields of a struct, there are limitations on what we can do when it comes
to inheritance:

*Every non-static data member of E must be a direct member of E or the same base class of E.*
([https://en.cppreference.com/w/cpp/language/structured_binding](https://en.cppreference.com/w/cpp/language/structured_binding))

In other words, all of the fields must be inside the same struct.

So, this is fine:

```cpp
struct Base {
int x;
};

struct Derived : Base {};
```
This is fine as well:
```cpp
struct Base {};
struct Derived : Base {
int x;
};
```

But this is not fine, because the fields
are spread out over more than one struct:

```cpp
struct Base {
int x;
};

struct Derived : Base {
int y;
};
```
The recommended alternative is to simply use `rfl::Flatten`, which
has no such limitation:
```cpp
struct Base {
int x;
};
struct Derived {
rfl::Flatten<Base> base;
int y;
};
```

Please refer to the section on `rfl::Flatten` in this documentation for
more information.

If for any reason you want to use inheritance patterns like the ones
described above, you will make have to make sure that all of the fields
are inside the same struct and also you will also have to pass the
compile time flag mentioned above.

0 comments on commit e369940

Please sign in to comment.