Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
513 changes: 382 additions & 131 deletions CMakeLists.txt

Large diffs are not rendered by default.

28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ reflect-cpp and sqlgen fill important gaps in C++ development. They reduce boile
- [Extra fields](#extra-fields)
- [Reflective programming](#reflective-programming)
- [Standard Library Integration](#support-for-containers)
- [Installation](#installation)
- [The team behind reflect-cpp](#the-team-behind-reflect-cpp)
- [License](#license)

### More in our [documentation](https://munich-data-engineering.github.io/reflect-cpp):
- [Installation ↗](https://munich-data-engineering.github.io/reflect-cpp/install/#option-2-compilation-using-cmake)
- [Installation ↗](https://munich-data-engineering.github.io/reflect-cpp/install/#option-3-compilation-using-cmake)
- [C++26 reflection ↗](https://munich-data-engineering.github.io/reflect-cpp/cpp26_reflection)
- [Benchmarks ↗](https://munich-data-engineering.github.io/reflect-cpp/benchmarks)
- [How to contribute ↗](https://munich-data-engineering.github.io/reflect-cpp/contributing)
Expand Down Expand Up @@ -687,6 +688,29 @@ The following compilers are supported for C++-20:
The following compilers are supported for C++-26:
- GCC 16.2 or higher

### Using CMake FetchContent

JSON-only (zero external dependencies — `yyjson`, `ctre`, and `enchantum` are bundled):

```cmake
include(FetchContent)
FetchContent_Declare(reflectcpp
GIT_REPOSITORY https://github.com/munich-data-engineering/reflect-cpp.git
GIT_TAG main # replace with a release tag containing CMake 3.20 / subproject support
GIT_SHALLOW TRUE
GIT_SUBMODULES "")
FetchContent_MakeAvailable(reflectcpp)
target_link_libraries(your_project PRIVATE reflectcpp::reflectcpp)
```

To enable additional formats (for example YAML), set the corresponding `REFLECTCPP_*` option **before** `FetchContent_MakeAvailable(reflectcpp)` (non-JSON format libraries are resolved via `find_package` from system packages, vcpkg, or Conan because `REFLECTCPP_USE_VCPKG` defaults to `OFF` when consumed as a subproject):

```cmake
set(REFLECTCPP_YAML ON)
FetchContent_MakeAvailable(reflectcpp)
target_link_libraries(your_project PRIVATE reflectcpp::reflectcpp)
```

### Using vcpkg

https://vcpkg.io/en/package/reflectcpp
Expand Down Expand Up @@ -722,7 +746,7 @@ cmake --build build -j 4 # gcc, clang
cmake --build build --config Release -j 4 # MSVC
```

For other installation methods, refer to the [documentation](https://munich-data-engineering.github.io/reflect-cpp/docs-readme).
For other installation methods, refer to the [documentation](https://munich-data-engineering.github.io/reflect-cpp/install).

### Compiling with C++-26 reflection

Expand Down
7 changes: 6 additions & 1 deletion benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O2")
if (NOT MSVC)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O2")
elseif (REFLECTCPP_STRICT_WARNINGS)
# Restore /W3 for benchmark targets after top-level CMakeLists strips /W3 before setting /W4 on reflectcpp.
add_compile_options(/W3)
endif ()

if (MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std:c++20 -O2")
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/all/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project(reflect-cpp-all-format-benchmarks)
project(reflect-cpp-all-format-benchmarks LANGUAGES CXX)

file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "*.cpp")

Expand Down
2 changes: 1 addition & 1 deletion benchmarks/json/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project(reflect-cpp-json-benchmarks)
project(reflect-cpp-json-benchmarks LANGUAGES CXX)

file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "*.cpp")

Expand Down
37 changes: 33 additions & 4 deletions docs/content/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,33 @@ Simply use [this recipe](https://conan.io/center/recipes/reflect-cpp).

## Option 3: Compilation using cmake

### Using FetchContent

To consume reflect-cpp directly from a CMake project with JSON support only (zero external dependencies — `yyjson`, `ctre`, and `enchantum` are bundled):

```cmake
include(FetchContent)
FetchContent_Declare(reflectcpp
GIT_REPOSITORY https://github.com/munich-data-engineering/reflect-cpp.git
GIT_TAG main # replace with a release tag containing CMake 3.20 / subproject support
GIT_SHALLOW TRUE
GIT_SUBMODULES "")
FetchContent_MakeAvailable(reflectcpp)
target_link_libraries(your_project PRIVATE reflectcpp::reflectcpp)
```

To enable additional serialization formats (for example YAML), set the corresponding `REFLECTCPP_*` option **before** `FetchContent_MakeAvailable(reflectcpp)`:

```cmake
set(REFLECTCPP_YAML ON)
FetchContent_MakeAvailable(reflectcpp)
target_link_libraries(your_project PRIVATE reflectcpp::reflectcpp)
```

When reflect-cpp is consumed as a subproject, `REFLECTCPP_USE_VCPKG` defaults to `OFF`, so each non-JSON format's underlying library (such as `yaml-cpp`) is resolved via `find_package` from your system packages, vcpkg toolchain, or Conan generator.

### Building standalone

This will compile reflect-cpp with JSON support only. You can then include reflect-cpp in your project and link to the binary.

```bash
Expand All @@ -44,6 +71,8 @@ cmake --build build -j 4 # gcc, clang
cmake --build build --config Release -j 4 # MSVC
```

> **Tip (Windows multi-config installs):** When installing both `Debug` and `Release` configurations into the same prefix with Visual Studio, pass `-DCMAKE_DEBUG_POSTFIX=d` so Debug (`reflectcppd.lib`) and Release (`reflectcpp.lib`) libraries coexist without overwriting each other.

## Option 4: Include source files into your own build

Simply copy the contents of the folders `include` and `src` into your source repository or add it to your include path and also add `src/reflectcpp.cpp` and `src/reflectcpp_json.cpp` and `src/yyjson.c` to your source files for compilation.
Expand Down Expand Up @@ -72,11 +101,9 @@ cmake --build build -j 4 # gcc, clang
cmake --build build --config Release -j 4 # MSVC
```

To use reflect-cpp in your project:
To use reflect-cpp in your project as a subdirectory, set any optional format flags **before** `add_subdirectory`:

```cmake
add_subdirectory(reflect-cpp) # Add this project as a subdirectory

set(REFLECTCPP_AVRO ON) # Optional
set(REFLECTCPP_BSON ON) # Optional
set(REFLECTCPP_CBOR ON) # Optional
Expand All @@ -87,7 +114,9 @@ set(REFLECTCPP_UBJSON ON) # Optional
set(REFLECTCPP_XML ON) # Optional
set(REFLECTCPP_YAML ON) # Optional

target_link_libraries(your_project PRIVATE reflectcpp) # Link against the library
add_subdirectory(reflect-cpp) # Add this project as a subdirectory

target_link_libraries(your_project PRIVATE reflectcpp::reflectcpp) # Link against the library
```

### Troubleshooting vcpkg
Expand Down
20 changes: 20 additions & 0 deletions include/rfl/parsing/schema/Type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,31 @@ struct RFL_API Type {
*/
Type(const VariantType& _variant);

/**
* @brief Copy constructor.
*/
Type(const Type& _other);

/**
* @brief Move constructor.
*/
Type(Type&& _other) noexcept;

/**
* @brief Destructor.
*/
~Type();

/**
* @brief Copy assignment operator.
*/
Type& operator=(const Type& _other);

/**
* @brief Move assignment operator.
*/
Type& operator=(Type&& _other) noexcept;

/// A type can be determined to be any of the above.
VariantType variant_;
};
Expand Down
7 changes: 4 additions & 3 deletions reflectcpp-config.cmake.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@PACKAGE_INIT@

set(REFLECTCPP_JSON @REFLECTCPP_JSON@)
set(REFLECTCPP_AVRO @REFLECTCPP_AVRO@)
set(REFLECTCPP_BOOST_SERIALIZATION @REFLECTCPP_BOOST_SERIALIZATION@)
set(REFLECTCPP_BSON @REFLECTCPP_BSON@)
set(REFLECTCPP_CAPNPROTO @REFLECTCPP_CAPNPROTO@)
Expand All @@ -17,12 +18,10 @@ set(REFLECTCPP_YAML @REFLECTCPP_YAML@)
set(REFLECTCPP_YAS @REFLECTCPP_YAS@)
set(REFLECTCPP_USE_BUNDLED_DEPENDENCIES @REFLECTCPP_USE_BUNDLED_DEPENDENCIES@)

if(REFLECTCPP_BOOST_SERIALIZATION OR REFLECTCPP_BSON OR REFLECTCPP_CAPNPROTO OR REFLECTCPP_CBOR OR REFLECTCPP_CEREAL OR REFLECTCPP_FLEXBUFFERS OR REFLECTCPP_MSGPACK OR REFLECTCPP_TOML OR REFLECTCPP_UBJSON OR REFLECTCPP_XML OR REFLECTCPP_YAML OR REFLECTCPP_YAS OR (REFLECTCPP_JSON AND NOT REFLECTCPP_USE_BUNDLED_DEPENDENCIES))
if(REFLECTCPP_AVRO OR REFLECTCPP_BOOST_SERIALIZATION OR REFLECTCPP_BSON OR REFLECTCPP_CAPNPROTO OR REFLECTCPP_CBOR OR REFLECTCPP_CEREAL OR REFLECTCPP_CSV OR REFLECTCPP_FLEXBUFFERS OR REFLECTCPP_MSGPACK OR REFLECTCPP_PARQUET OR REFLECTCPP_TOML OR REFLECTCPP_UBJSON OR REFLECTCPP_XML OR REFLECTCPP_YAML OR REFLECTCPP_YAS OR NOT REFLECTCPP_USE_BUNDLED_DEPENDENCIES)
include(CMakeFindDependencyMacro)
endif()

include(${CMAKE_CURRENT_LIST_DIR}/reflectcpp-exports.cmake)

if (NOT REFLECTCPP_USE_BUNDLED_DEPENDENCIES)
find_dependency(ctre)
find_dependency(enchantum)
Expand Down Expand Up @@ -85,4 +84,6 @@ if (REFLECTCPP_YAS)
find_dependency(yas)
endif ()

include(${CMAKE_CURRENT_LIST_DIR}/reflectcpp-exports.cmake)

check_required_components(reflectcpp)
24 changes: 12 additions & 12 deletions src/rfl/env/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,46 +46,46 @@ Writer::Writer() {}

Writer::OutputArrayType Writer::array_as_root(
const size_t /*_size*/) const noexcept {
return OutputArrayType{.prefix = ""};
return OutputArrayType{.prefix = "", .values = {}};
}

Writer::OutputObjectType Writer::object_as_root(
const size_t /*_size*/) const noexcept {
return OutputObjectType{.prefix = ""};
return OutputObjectType{.prefix = "", .fields = {}};
}

Writer::OutputArrayType Writer::add_array_to_array(
const size_t /*_size*/, OutputArrayType* _parent) const {
const auto arr =
OutputArrayType{.prefix = _parent->prefix +
std::to_string(_parent->values->size()) + "_"};
const auto arr = OutputArrayType{
.prefix = _parent->prefix + std::to_string(_parent->values->size()) + "_",
.values = {}};
_parent->values->emplace_back(arr);
return arr;
}

Writer::OutputArrayType Writer::add_array_to_object(
const std::string_view& _name, const size_t /*_size*/,
OutputObjectType* _parent) const {
const auto arr =
OutputArrayType{.prefix = _parent->prefix + std::string(_name) + "_"};
const auto arr = OutputArrayType{
.prefix = _parent->prefix + std::string(_name) + "_", .values = {}};
_parent->fields->emplace(std::string(_name), arr);
return arr;
}

Writer::OutputObjectType Writer::add_object_to_object(
const std::string_view& _name, const size_t /*_size*/,
OutputObjectType* _parent) const {
const auto obj =
OutputObjectType{.prefix = _parent->prefix + std::string(_name) + "_"};
const auto obj = OutputObjectType{
.prefix = _parent->prefix + std::string(_name) + "_", .fields = {}};
_parent->fields->emplace(std::string(_name), obj);
return obj;
}

Writer::OutputObjectType Writer::add_object_to_array(
const size_t /*_size*/, OutputArrayType* _parent) const {
const auto obj =
OutputObjectType{.prefix = _parent->prefix +
std::to_string(_parent->values->size()) + "_"};
const auto obj = OutputObjectType{
.prefix = _parent->prefix + std::to_string(_parent->values->size()) + "_",
.fields = {}};
_parent->values->emplace_back(obj);
return obj;
}
Expand Down
5 changes: 4 additions & 1 deletion src/rfl/json/to_schema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,12 @@ schema::Type handle_validation_type(
}
return handle_validation_type(_type, ValidationType{.variant_ = v},
_no_required);
} else {
return t;
}
} else {
return t;
}
return t;
};

return rfl::visit(handle_size_variant, t.value, _v.size_limit_->variant_);
Expand Down
8 changes: 8 additions & 0 deletions src/rfl/parsing/schema/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ Type::Type() : variant_() {}

Type::Type(const VariantType& _variant) : variant_(_variant) {}

Type::Type(const Type& _other) = default;

Type::Type(Type&& _other) noexcept = default;

Type::~Type() = default;

Type& Type::operator=(const Type& _other) = default;

Type& Type::operator=(Type&& _other) noexcept = default;

} // namespace rfl::parsing::schema
7 changes: 6 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O2")
if (NOT MSVC)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O2")
elseif (REFLECTCPP_STRICT_WARNINGS)
# Restore /W3 for test targets after top-level CMakeLists strips /W3 before setting /W4 on reflectcpp.
add_compile_options(/W3)
endif ()

# Note: Adding -Wno-stringop-overflow is necessary, because of false positive warnings, see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110498
if (MSVC)
Expand Down
2 changes: 1 addition & 1 deletion tests/avro/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project(reflect-cpp-avro-tests)
project(reflect-cpp-avro-tests LANGUAGES CXX)

file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "*.cpp")

Expand Down
2 changes: 1 addition & 1 deletion tests/boost_serialization/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project(reflect-cpp-boost-serialization-tests)
project(reflect-cpp-boost-serialization-tests LANGUAGES CXX)

file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "*.cpp")

Expand Down
2 changes: 1 addition & 1 deletion tests/bson/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project(reflect-cpp-bson-tests)
project(reflect-cpp-bson-tests LANGUAGES CXX)

file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "*.cpp")

Expand Down
2 changes: 1 addition & 1 deletion tests/capnproto/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project(reflect-cpp-capnproto-tests)
project(reflect-cpp-capnproto-tests LANGUAGES CXX)

file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "*.cpp")
list(FILTER SOURCES EXCLUDE REGEX ".*/compile_fail/.*")
Expand Down
2 changes: 1 addition & 1 deletion tests/cbor/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project(reflect-cpp-cbor-tests)
project(reflect-cpp-cbor-tests LANGUAGES CXX)

file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "*.cpp")

Expand Down
2 changes: 1 addition & 1 deletion tests/cereal/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project(reflect-cpp-cereal-tests)
project(reflect-cpp-cereal-tests LANGUAGES CXX)

file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "*.cpp")

Expand Down
2 changes: 1 addition & 1 deletion tests/cli/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project(reflect-cpp-cli-tests)
project(reflect-cpp-cli-tests LANGUAGES CXX)

file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "*.cpp")

Expand Down
2 changes: 1 addition & 1 deletion tests/csv/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project(reflect-cpp-csv-tests)
project(reflect-cpp-csv-tests LANGUAGES CXX)

file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "*.cpp")

Expand Down
2 changes: 1 addition & 1 deletion tests/env/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project(reflect-cpp-env-tests)
project(reflect-cpp-env-tests LANGUAGES CXX)

file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "*.cpp")

Expand Down
2 changes: 1 addition & 1 deletion tests/flexbuffers/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project(reflect-cpp-flexbuffers-tests)
project(reflect-cpp-flexbuffers-tests LANGUAGES CXX)

file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "*.cpp")

Expand Down
2 changes: 1 addition & 1 deletion tests/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project(reflect-cpp-generic-tests)
project(reflect-cpp-generic-tests LANGUAGES CXX)

file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "*.cpp")
list(FILTER SOURCES EXCLUDE REGEX ".*/compile_fail/.*")
Expand Down
2 changes: 1 addition & 1 deletion tests/json/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project(reflect-cpp-json-tests)
project(reflect-cpp-json-tests LANGUAGES CXX)

file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "*.cpp")

Expand Down
2 changes: 1 addition & 1 deletion tests/json_c_arrays_and_inheritance/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project(reflect-cpp-json-c-arrays-and-inheritance-tests)
project(reflect-cpp-json-c-arrays-and-inheritance-tests LANGUAGES CXX)

file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "*.cpp")

Expand Down
2 changes: 1 addition & 1 deletion tests/msgpack/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project(reflect-cpp-msgpack-tests)
project(reflect-cpp-msgpack-tests LANGUAGES CXX)

file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "*.cpp")

Expand Down
2 changes: 1 addition & 1 deletion tests/parquet/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project(reflect-cpp-parquet-tests)
project(reflect-cpp-parquet-tests LANGUAGES CXX)

file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "*.cpp")

Expand Down
2 changes: 1 addition & 1 deletion tests/toml/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project(reflect-cpp-toml-tests)
project(reflect-cpp-toml-tests LANGUAGES CXX)

file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "*.cpp")

Expand Down
Loading
Loading