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
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ option(REFLECTCPP_YAML "Enable YAML support" ${REFLECTCPP_ALL_FORMATS})
option(REFLECTCPP_BUILD_BENCHMARKS "Build benchmarks" OFF)
option(REFLECTCPP_BUILD_TESTS "Build tests" OFF)
option(REFLECTCPP_CHECK_HEADERS "Make sure that all headers are self-contained" OFF)
option(REFLECTCPP_BUILD_MODULES "Build reflectcpp as a C++ module" OFF)

option(REFLECTCPP_USE_BUNDLED_DEPENDENCIES "Use the bundled dependencies" ON)

Expand Down Expand Up @@ -446,6 +447,10 @@ if(REFLECTCPP_CHECK_HEADERS)
endforeach()
endif()

if (REFLECTCPP_BUILD_MODULES)
add_subdirectory(src/modules)
endif()

if (REFLECTCPP_INSTALL)
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)
Expand Down
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,33 @@ In addition, it supports the following custom containers:

Finally, it is very easy to extend full support to your own classes, refer to the [documentation](https://rfl.getml.com/docs-readme) for details.

### Module support

reflect-cpp has support for C++20 modules. To enable, pass `REFLECTCPP_BUILD_MODULES` to CMake. You must have CMake 3.28 or higher, and any build system that supports modules (such as Ninja).

```cpp
import std;
import rfl;

using std::string;

struct Person {
string first_name;
string last_name;
int age;
};

const Person homer = Person{
.first_name = "Homer",
.last_name = "Simpson",
.age = 45
};

// We can now write into and read from a JSON string.
const string json_string = rfl::json::write(homer);
auto homer2 = rfl::json::read<Person>(json_string).value();
```


## Installation

Expand Down
2 changes: 1 addition & 1 deletion include/rfl/capnproto/schema/CapnProtoTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct CapnProtoTypes {
std::map<std::string, schema::Type> unions_;
};

const char* MAP_DEFINITION = R"(
inline const char* MAP_DEFINITION = R"(
struct Map(Value) {
entries @0 :List(Entry);
struct Entry {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

#ifndef RFL_PARSING_CALL_DESTRUCTORS_ON_ARRAY_WHERE_NECESSARY_HPP_
#define RFL_PARSING_CALL_DESTRUCTORS_ON_ARRAY_WHERE_NECESSARY_HPP_

Expand Down
97 changes: 97 additions & 0 deletions src/modules/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
cmake_minimum_required(VERSION 3.28)

add_library(reflectcpp_module)

set(MODULE_FILES)

if(REFLECTCPP_JSON)
list(APPEND MODULE_FILES rfl.json.cppm)
target_compile_definitions(reflectcpp_module PUBLIC REFLECTCPP_JSON)
endif()

if(REFLECTCPP_AVRO)
list(APPEND MODULE_FILES rfl.avro.cppm)
target_compile_definitions(reflectcpp_module PUBLIC REFLECTCPP_AVRO)
endif()

if(REFLECTCPP_BSON)
list(APPEND MODULE_FILES rfl.bson.cppm)
target_compile_definitions(reflectcpp_module PUBLIC REFLECTCPP_BSON)
endif()

if(REFLECTCPP_CAPNPROTO)
list(APPEND MODULE_FILES rfl.capnproto.cppm)
target_compile_definitions(reflectcpp_module PUBLIC REFLECTCPP_CAPNPROTO)
endif()

if(REFLECTCPP_CBOR)
list(APPEND MODULE_FILES rfl.cbor.cppm)
target_compile_definitions(reflectcpp_module PUBLIC REFLECTCPP_CBOR)
endif()

if(REFLECTCPP_CSV)
list(APPEND MODULE_FILES rfl.csv.cppm)
target_compile_definitions(reflectcpp_module PUBLIC REFLECTCPP_CSV)
endif()

if(REFLECTCPP_FLEXBUFFERS)
list(APPEND MODULE_FILES rfl.flexbuf.cppm)
target_compile_definitions(reflectcpp_module PUBLIC REFLECTCPP_FLEXBUFFERS)
endif()

if(REFLECTCPP_MSGPACK)
list(APPEND MODULE_FILES rfl.msgpack.cppm)
target_compile_definitions(reflectcpp_module PUBLIC REFLECTCPP_MSGPACK)
endif()

if(REFLECTCPP_PARQUET)
list(APPEND MODULE_FILES rfl.parquet.cppm)
target_compile_definitions(reflectcpp_module PUBLIC REFLECTCPP_PARQUET)
endif()

if(REFLECTCPP_TOML)
list(APPEND MODULE_FILES rfl.toml.cppm)
target_compile_definitions(reflectcpp_module PUBLIC REFLECTCPP_TOML)
endif()

if(REFLECTCPP_UBJSON)
list(APPEND MODULE_FILES rfl.ubjson.cppm)
target_compile_definitions(reflectcpp_module PUBLIC REFLECTCPP_UBJSON)
endif()

if(REFLECTCPP_XML)
list(APPEND MODULE_FILES rfl.xml.cppm)
target_compile_definitions(reflectcpp_module PUBLIC REFLECTCPP_XML)
endif()

if(REFLECTCPP_YAML)
list(APPEND MODULE_FILES rfl.yaml.cppm)
target_compile_definitions(reflectcpp_module PUBLIC REFLECTCPP_YAML)
endif()

target_sources(reflectcpp_module
PUBLIC
FILE_SET CXX_MODULES FILES ${MODULE_FILES}
)

list(APPEND MODULE_FILES rfl.module.cppm)

target_compile_features(reflectcpp_module PUBLIC cxx_std_23)

target_include_directories(reflectcpp_module PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)

target_link_libraries(reflectcpp_module PUBLIC reflectcpp::reflectcpp)

add_library(reflectcpp::module ALIAS reflectcpp_module)

# Installation
install(TARGETS reflectcpp_module
EXPORT ${PROJECT_NAME}Targets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
FILE_SET CXX_MODULES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/src/module
)
30 changes: 30 additions & 0 deletions src/modules/rfl.avro.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module;

#ifdef REFLECTCPP_AVRO
#include "rfl/avro.hpp"
#include "rfl/avro/schema/Type.hpp"
#endif

export module rfl:avro;

export namespace rfl::avro {
#ifdef REFLECTCPP_AVRO
using rfl::avro::Parser;
using rfl::avro::Reader;
using rfl::avro::Schema;
using rfl::avro::Writer;
using rfl::avro::InputObjectType;
using rfl::avro::InputVarType;

using rfl::avro::load;
using rfl::avro::read;
using rfl::avro::save;
using rfl::avro::to_json_representation;
using rfl::avro::to_schema;
using rfl::avro::write;

namespace schema {
using rfl::avro::schema::Type;
}
#endif
}
22 changes: 22 additions & 0 deletions src/modules/rfl.bson.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module;

#ifdef REFLECTCPP_BSON
#include "rfl/bson.hpp"
#endif

export module rfl:bson;

export namespace rfl::bson {
#ifdef REFLECTCPP_BSON
using rfl::bson::Parser;
using rfl::bson::Reader;
using rfl::bson::Writer;
using rfl::bson::InputObjectType;
using rfl::bson::InputVarType;

using rfl::bson::load;
using rfl::bson::read;
using rfl::bson::save;
using rfl::bson::write;
#endif
}
41 changes: 41 additions & 0 deletions src/modules/rfl.capnproto.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module;

#ifdef REFLECTCPP_CAPNPROTO
#include "rfl/capnproto.hpp"
#include "rfl/capnproto/is_named_type.hpp"
#include "rfl/capnproto/schema/CapnProtoTypes.hpp"
#endif

export module rfl:capnproto;

export namespace rfl::capnproto {
#ifdef REFLECTCPP_CAPNPROTO
using rfl::capnproto::Parser;
using rfl::capnproto::Reader;
using rfl::capnproto::Schema;
using rfl::capnproto::SchemaImpl;
using rfl::capnproto::SchemaHolder;
using rfl::capnproto::Writer;
using rfl::capnproto::InputObjectType;
using rfl::capnproto::InputVarType;

using rfl::capnproto::load;
using rfl::capnproto::read;
using rfl::capnproto::save;
using rfl::capnproto::to_schema;
using rfl::capnproto::write;
using rfl::capnproto::get_root_name;
using rfl::capnproto::is_named_type;
using rfl::capnproto::to_string_representation;
using rfl::capnproto::to_schema;

namespace schema {
using rfl::capnproto::schema::CapnProtoTypes;
using rfl::capnproto::schema::Type;

using rfl::capnproto::schema::MAP_DEFINITION;

using rfl::capnproto::schema::operator<<;
}
#endif
}
22 changes: 22 additions & 0 deletions src/modules/rfl.cbor.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module;

#ifdef REFLECTCPP_CBOR
#include "rfl/cbor.hpp"
#endif

export module rfl:cbor;

export namespace rfl::cbor {
#ifdef REFLECTCPP_CBOR
using rfl::cbor::Parser;
using rfl::cbor::Reader;
using rfl::cbor::Writer;
using rfl::cbor::InputObjectType;
using rfl::cbor::InputVarType;

using rfl::cbor::load;
using rfl::cbor::read;
using rfl::cbor::save;
using rfl::cbor::write;
#endif
}
Loading
Loading