Skip to content

Commit 9137f57

Browse files
Add support for C++ modules
1 parent c0e26fb commit 9137f57

19 files changed

Lines changed: 648 additions & 1 deletion

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ option(REFLECTCPP_YAML "Enable YAML support" ${REFLECTCPP_ALL_FORMATS})
2323
option(REFLECTCPP_BUILD_BENCHMARKS "Build benchmarks" OFF)
2424
option(REFLECTCPP_BUILD_TESTS "Build tests" OFF)
2525
option(REFLECTCPP_CHECK_HEADERS "Make sure that all headers are self-contained" OFF)
26+
option(REFLECTCPP_BUILD_MODULES "Build reflectcpp as a C++ module" OFF)
2627

2728
option(REFLECTCPP_USE_BUNDLED_DEPENDENCIES "Use the bundled dependencies" ON)
2829

@@ -446,6 +447,10 @@ if(REFLECTCPP_CHECK_HEADERS)
446447
endforeach()
447448
endif()
448449

450+
if (REFLECTCPP_BUILD_MODULES)
451+
add_subdirectory(src/modules)
452+
endif()
453+
449454
if (REFLECTCPP_INSTALL)
450455
include(GNUInstallDirs)
451456
include(CMakePackageConfigHelpers)

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,33 @@ In addition, it supports the following custom containers:
587587
588588
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.
589589
590+
### Module support
591+
592+
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).
593+
594+
```cpp
595+
import std;
596+
import rfl;
597+
598+
using std::string;
599+
600+
struct Person {
601+
string first_name;
602+
string last_name;
603+
int age;
604+
};
605+
606+
const Person homer = Person{
607+
.first_name = "Homer",
608+
.last_name = "Simpson",
609+
.age = 45
610+
};
611+
612+
// We can now write into and read from a JSON string.
613+
const string json_string = rfl::json::write(homer);
614+
auto homer2 = rfl::json::read<Person>(json_string).value();
615+
```
616+
590617

591618
## Installation
592619

include/rfl/capnproto/schema/CapnProtoTypes.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ struct CapnProtoTypes {
1616
std::map<std::string, schema::Type> unions_;
1717
};
1818

19-
const char* MAP_DEFINITION = R"(
19+
inline const char* MAP_DEFINITION = R"(
2020
struct Map(Value) {
2121
entries @0 :List(Entry);
2222
struct Entry {

include/rfl/parsing/call_destructors_on_array_where_necessary.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
#ifndef RFL_PARSING_CALL_DESTRUCTORS_ON_ARRAY_WHERE_NECESSARY_HPP_
23
#define RFL_PARSING_CALL_DESTRUCTORS_ON_ARRAY_WHERE_NECESSARY_HPP_
34

src/modules/CMakeLists.txt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
cmake_minimum_required(VERSION 3.28)
2+
3+
add_library(reflectcpp_module)
4+
5+
target_sources(reflectcpp_module
6+
PUBLIC
7+
FILE_SET CXX_MODULES FILES
8+
rfl.cppm
9+
rfl.avro.cppm
10+
rfl.bson.cppm
11+
rfl.capnproto.cppm
12+
rfl.cbor.cppm
13+
rfl.csv.cppm
14+
rfl.flexbuf.cppm
15+
rfl.json.cppm
16+
rfl.msgpack.cppm
17+
rfl.parquet.cppm
18+
rfl.toml.cppm
19+
rfl.ubjson.cppm
20+
rfl.xml.cppm
21+
rfl.yaml.cppm
22+
)
23+
24+
target_compile_features(reflectcpp_module PUBLIC cxx_std_23)
25+
26+
target_include_directories(reflectcpp_module PUBLIC
27+
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
28+
$<INSTALL_INTERFACE:include>
29+
)
30+
31+
target_link_libraries(reflectcpp_module PUBLIC reflectcpp::reflectcpp)
32+
33+
add_library(reflectcpp::module ALIAS reflectcpp_module)
34+
35+
# Installation
36+
install(TARGETS reflectcpp_module
37+
EXPORT ${PROJECT_NAME}Targets
38+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
39+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
40+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
41+
FILE_SET CXX_MODULES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/src/module
42+
)

src/modules/rfl.avro.cppm

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module;
2+
3+
#include "rfl/avro.hpp"
4+
#include "rfl/avro/schema/Type.hpp"
5+
6+
export module rfl:avro;
7+
8+
export namespace rfl::avro {
9+
using rfl::avro::Parser;
10+
using rfl::avro::Reader;
11+
using rfl::avro::Schema;
12+
using rfl::avro::Writer;
13+
using rfl::avro::InputObjectType;
14+
using rfl::avro::InputVarType;
15+
16+
using rfl::avro::load;
17+
using rfl::avro::read;
18+
using rfl::avro::save;
19+
using rfl::avro::to_json_representation;
20+
using rfl::avro::to_schema;
21+
using rfl::avro::write;
22+
23+
namespace schema {
24+
using rfl::avro::schema::Type;
25+
}
26+
}

src/modules/rfl.bson.cppm

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module;
2+
3+
#include "rfl/bson.hpp"
4+
5+
export module rfl:bson;
6+
7+
export namespace rfl::bson {
8+
using rfl::bson::Parser;
9+
using rfl::bson::Reader;
10+
using rfl::bson::Writer;
11+
using rfl::bson::InputObjectType;
12+
using rfl::bson::InputVarType;
13+
14+
using rfl::bson::load;
15+
using rfl::bson::read;
16+
using rfl::bson::save;
17+
using rfl::bson::write;
18+
}

src/modules/rfl.capnproto.cppm

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module;
2+
3+
#include "rfl/capnproto.hpp"
4+
#include "rfl/capnproto/is_named_type.hpp"
5+
#include "rfl/capnproto/schema/CapnProtoTypes.hpp"
6+
7+
export module rfl:capnproto;
8+
9+
export namespace rfl::capnproto {
10+
using rfl::capnproto::Parser;
11+
using rfl::capnproto::Reader;
12+
using rfl::capnproto::Schema;
13+
using rfl::capnproto::SchemaImpl;
14+
using rfl::capnproto::SchemaHolder;
15+
using rfl::capnproto::Writer;
16+
using rfl::capnproto::InputObjectType;
17+
using rfl::capnproto::InputVarType;
18+
19+
using rfl::capnproto::load;
20+
using rfl::capnproto::read;
21+
using rfl::capnproto::save;
22+
using rfl::capnproto::to_schema;
23+
using rfl::capnproto::write;
24+
using rfl::capnproto::get_root_name;
25+
using rfl::capnproto::is_named_type;
26+
using rfl::capnproto::to_string_representation;
27+
using rfl::capnproto::to_schema;
28+
29+
namespace schema {
30+
using rfl::capnproto::schema::CapnProtoTypes;
31+
using rfl::capnproto::schema::Type;
32+
33+
using rfl::capnproto::schema::MAP_DEFINITION;
34+
35+
using rfl::capnproto::schema::operator<<;
36+
}
37+
}

src/modules/rfl.cbor.cppm

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module;
2+
3+
#include "rfl/cbor.hpp"
4+
5+
export module rfl:cbor;
6+
7+
export namespace rfl::cbor {
8+
using rfl::cbor::Parser;
9+
using rfl::cbor::Reader;
10+
using rfl::cbor::Writer;
11+
using rfl::cbor::InputObjectType;
12+
using rfl::cbor::InputVarType;
13+
14+
using rfl::cbor::load;
15+
using rfl::cbor::read;
16+
using rfl::cbor::save;
17+
using rfl::cbor::write;
18+
}

0 commit comments

Comments
 (0)