Skip to content

Commit 5bb365f

Browse files
Added checks to make sure headers are self-contained; addresses #528
1 parent 72a7214 commit 5bb365f

36 files changed

Lines changed: 205 additions & 145 deletions

CMakeLists.txt

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ option(REFLECTCPP_YAML "Enable YAML support" ${REFLECTCPP_ALL_FORMATS})
2222

2323
option(REFLECTCPP_BUILD_BENCHMARKS "Build benchmarks" OFF)
2424
option(REFLECTCPP_BUILD_TESTS "Build tests" OFF)
25+
option(REFLECTCPP_CHECK_HEADERS "Make sure that all headers are self-contained" OFF)
2526

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

@@ -55,7 +56,7 @@ if(REFLECTCPP_BUILD_BENCHMARKS)
5556
set(REFLECTCPP_YAML ON CACHE BOOL "" FORCE)
5657
endif()
5758

58-
if (REFLECTCPP_BUILD_TESTS OR REFLECTCPP_BUILD_BENCHMARKS OR
59+
if (REFLECTCPP_BUILD_TESTS OR REFLECTCPP_BUILD_BENCHMARKS OR REFLECTCPP_CHECK_HEADERS OR
5960
(REFLECTCPP_JSON AND NOT REFLECTCPP_USE_BUNDLED_DEPENDENCIES) OR REFLECTCPP_AVRO OR
6061
REFLECTCPP_BSON OR REFLECTCPP_CAPNPROTO OR REFLECTCPP_CBOR OR REFLECTCPP_CSV OR
6162
REFLECTCPP_FLEXBUFFERS OR REFLECTCPP_MSGPACK OR REFLECTCPP_PARQUET OR REFLECTCPP_XML OR
@@ -73,67 +74,67 @@ endif()
7374
option(REFLECTCPP_USE_VCPKG "Use VCPKG to download and build dependencies" ${REFLECTCPP_USE_VCPKG_DEFAULT})
7475

7576
if (REFLECTCPP_USE_VCPKG)
76-
if (REFLECTCPP_AVRO)
77+
if (REFLECTCPP_AVRO OR REFLECTCPP_CHECK_HEADERS)
7778
list(APPEND VCPKG_MANIFEST_FEATURES "avro")
7879
endif()
7980

80-
if (REFLECTCPP_BSON)
81+
if (REFLECTCPP_BSON OR REFLECTCPP_CHECK_HEADERS)
8182
list(APPEND VCPKG_MANIFEST_FEATURES "bson")
8283
endif()
8384

84-
if (REFLECTCPP_BUILD_BENCHMARKS)
85+
if (REFLECTCPP_BUILD_BENCHMARKS OR REFLECTCPP_CHECK_HEADERS)
8586
list(APPEND VCPKG_MANIFEST_FEATURES "benchmarks")
8687
endif()
8788

88-
if (REFLECTCPP_BUILD_TESTS)
89+
if (REFLECTCPP_BUILD_TESTS OR REFLECTCPP_CHECK_HEADERS)
8990
list(APPEND VCPKG_MANIFEST_FEATURES "tests")
9091
endif()
9192

92-
if (REFLECTCPP_CAPNPROTO)
93+
if (REFLECTCPP_CAPNPROTO OR REFLECTCPP_CHECK_HEADERS)
9394
list(APPEND VCPKG_MANIFEST_FEATURES "capnproto")
9495
endif()
9596

96-
if (REFLECTCPP_CBOR)
97+
if (REFLECTCPP_CBOR OR REFLECTCPP_CHECK_HEADERS)
9798
list(APPEND VCPKG_MANIFEST_FEATURES "cbor")
9899
endif()
99100

100-
if (REFLECTCPP_CSV)
101+
if (REFLECTCPP_CSV OR REFLECTCPP_CHECK_HEADERS)
101102
list(APPEND VCPKG_MANIFEST_FEATURES "csv")
102103
endif()
103104

104105
if (NOT REFLECTCPP_USE_BUNDLED_DEPENDENCIES)
105106
list(APPEND VCPKG_MANIFEST_FEATURES "ctre")
106107
endif()
107108

108-
if (REFLECTCPP_FLEXBUFFERS)
109+
if (REFLECTCPP_FLEXBUFFERS OR REFLECTCPP_CHECK_HEADERS)
109110
list(APPEND VCPKG_MANIFEST_FEATURES "flexbuffers")
110111
endif()
111112

112113
if (_REFLECTCPP_NEEDS_JSON_IMPL AND NOT REFLECTCPP_USE_BUNDLED_DEPENDENCIES)
113114
list(APPEND VCPKG_MANIFEST_FEATURES "json")
114115
endif()
115116

116-
if (REFLECTCPP_MSGPACK)
117+
if (REFLECTCPP_MSGPACK OR REFLECTCPP_CHECK_HEADERS)
117118
list(APPEND VCPKG_MANIFEST_FEATURES "msgpack")
118119
endif()
119120

120-
if (REFLECTCPP_PARQUET)
121+
if (REFLECTCPP_PARQUET OR REFLECTCPP_CHECK_HEADERS)
121122
list(APPEND VCPKG_MANIFEST_FEATURES "parquet")
122123
endif()
123124

124-
if (REFLECTCPP_TOML)
125+
if (REFLECTCPP_TOML OR REFLECTCPP_CHECK_HEADERS)
125126
list(APPEND VCPKG_MANIFEST_FEATURES "toml")
126127
endif()
127128

128-
if (REFLECTCPP_UBJSON)
129+
if (REFLECTCPP_UBJSON OR REFLECTCPP_CHECK_HEADERS)
129130
list(APPEND VCPKG_MANIFEST_FEATURES "ubjson")
130131
endif()
131132

132-
if (REFLECTCPP_XML)
133+
if (REFLECTCPP_XML OR REFLECTCPP_CHECK_HEADERS)
133134
list(APPEND VCPKG_MANIFEST_FEATURES "xml")
134135
endif()
135136

136-
if (REFLECTCPP_YAML)
137+
if (REFLECTCPP_YAML OR REFLECTCPP_CHECK_HEADERS)
137138
list(APPEND VCPKG_MANIFEST_FEATURES "yaml")
138139
endif()
139140

@@ -237,7 +238,7 @@ if (_REFLECTCPP_NEEDS_JSON_IMPL)
237238
endif ()
238239
endif ()
239240

240-
if (REFLECTCPP_AVRO)
241+
if (REFLECTCPP_AVRO OR REFLECTCPP_CHECK_HEADERS)
241242
list(APPEND REFLECT_CPP_SOURCES
242243
src/reflectcpp_avro.cpp
243244
)
@@ -270,7 +271,7 @@ if (REFLECTCPP_AVRO)
270271
target_link_libraries(reflectcpp PRIVATE jansson::jansson)
271272
endif ()
272273

273-
if (REFLECTCPP_BSON)
274+
if (REFLECTCPP_BSON OR REFLECTCPP_CHECK_HEADERS)
274275
list(APPEND REFLECT_CPP_SOURCES
275276
src/reflectcpp_bson.cpp
276277
)
@@ -280,7 +281,7 @@ if (REFLECTCPP_BSON)
280281
target_link_libraries(reflectcpp PUBLIC $<IF:$<TARGET_EXISTS:mongo::bson_static>,mongo::bson_static,mongo::bson_shared>)
281282
endif ()
282283

283-
if (REFLECTCPP_CAPNPROTO)
284+
if (REFLECTCPP_CAPNPROTO OR REFLECTCPP_CHECK_HEADERS)
284285
list(APPEND REFLECT_CPP_SOURCES
285286
src/reflectcpp_capnproto.cpp
286287
)
@@ -295,7 +296,7 @@ if (REFLECTCPP_CAPNPROTO)
295296
target_link_libraries(reflectcpp PUBLIC CapnProto::kj CapnProto::capnp ${CAPNPC_LIB} CapnProto::kj-gzip)
296297
endif ()
297298

298-
if (REFLECTCPP_CBOR)
299+
if (REFLECTCPP_CBOR OR REFLECTCPP_CHECK_HEADERS)
299300
list(APPEND REFLECT_CPP_SOURCES
300301
src/reflectcpp_cbor.cpp
301302
)
@@ -305,7 +306,7 @@ if (REFLECTCPP_CBOR)
305306
include_directories(PUBLIC ${jsoncons_INCLUDE_DIRS})
306307
endif ()
307308

308-
if (REFLECTCPP_CSV)
309+
if (REFLECTCPP_CSV OR REFLECTCPP_CHECK_HEADERS)
309310
if (NOT TARGET Arrow)
310311
find_package(Arrow CONFIG REQUIRED)
311312
endif()
@@ -316,7 +317,7 @@ if (REFLECTCPP_CSV)
316317
endif()
317318
endif ()
318319

319-
if (REFLECTCPP_FLEXBUFFERS)
320+
if (REFLECTCPP_FLEXBUFFERS OR REFLECTCPP_CHECK_HEADERS)
320321
list(APPEND REFLECT_CPP_SOURCES
321322
src/reflectcpp_flexbuf.cpp
322323
)
@@ -326,7 +327,7 @@ if (REFLECTCPP_FLEXBUFFERS)
326327
target_link_libraries(reflectcpp PUBLIC flatbuffers::flatbuffers)
327328
endif ()
328329

329-
if (REFLECTCPP_MSGPACK)
330+
if (REFLECTCPP_MSGPACK OR REFLECTCPP_CHECK_HEADERS)
330331
list(APPEND REFLECT_CPP_SOURCES
331332
src/reflectcpp_msgpack.cpp
332333
)
@@ -336,7 +337,7 @@ if (REFLECTCPP_MSGPACK)
336337
target_link_libraries(reflectcpp PUBLIC msgpack-c)
337338
endif ()
338339

339-
if (REFLECTCPP_PARQUET)
340+
if (REFLECTCPP_PARQUET OR REFLECTCPP_CHECK_HEADERS)
340341
if (NOT TARGET Arrow)
341342
find_package(Arrow CONFIG REQUIRED)
342343
endif()
@@ -351,7 +352,7 @@ if (REFLECTCPP_PARQUET)
351352
endif()
352353
endif ()
353354

354-
if (REFLECTCPP_TOML)
355+
if (REFLECTCPP_TOML OR REFLECTCPP_CHECK_HEADERS)
355356
list(APPEND REFLECT_CPP_SOURCES
356357
src/reflectcpp_toml.cpp
357358
)
@@ -361,7 +362,7 @@ if (REFLECTCPP_TOML)
361362
target_link_libraries(reflectcpp PUBLIC tomlplusplus::tomlplusplus)
362363
endif()
363364

364-
if (REFLECTCPP_UBJSON)
365+
if (REFLECTCPP_UBJSON OR REFLECTCPP_CHECK_HEADERS)
365366
list(APPEND REFLECT_CPP_SOURCES
366367
src/reflectcpp_ubjson.cpp
367368
)
@@ -371,7 +372,7 @@ if (REFLECTCPP_UBJSON)
371372
include_directories(PUBLIC ${jsoncons_INCLUDE_DIRS})
372373
endif ()
373374

374-
if (REFLECTCPP_XML)
375+
if (REFLECTCPP_XML OR REFLECTCPP_CHECK_HEADERS)
375376
list(APPEND REFLECT_CPP_SOURCES
376377
src/reflectcpp_xml.cpp
377378
)
@@ -381,7 +382,7 @@ if (REFLECTCPP_XML)
381382
target_link_libraries(reflectcpp PUBLIC pugixml::pugixml)
382383
endif ()
383384

384-
if (REFLECTCPP_YAML)
385+
if (REFLECTCPP_YAML OR REFLECTCPP_CHECK_HEADERS)
385386
list(APPEND REFLECT_CPP_SOURCES
386387
src/reflectcpp_yaml.cpp
387388
)
@@ -415,6 +416,32 @@ if (REFLECTCPP_BUILD_BENCHMARKS)
415416
add_subdirectory(benchmarks)
416417
endif ()
417418

419+
if(REFLECTCPP_CHECK_HEADERS)
420+
file(GLOB_RECURSE PROJECT_HEADERS "include/*.hpp")
421+
422+
set(TEST_DIR "${CMAKE_CURRENT_BINARY_DIR}/self_contained_tests")
423+
file(MAKE_DIRECTORY "${TEST_DIR}")
424+
425+
foreach(HEADER_FILE ${PROJECT_HEADERS})
426+
string(MAKE_C_IDENTIFIER ${HEADER_FILE} HEADER_NAME)
427+
set(TEST_SOURCE_FILE "${TEST_DIR}/test_${HEADER_NAME}.cpp")
428+
429+
file(GENERATE
430+
OUTPUT ${TEST_SOURCE_FILE}
431+
CONTENT "#include \"${HEADER_FILE}\"\n"
432+
)
433+
434+
add_library(check_header_${HEADER_NAME} "${TEST_SOURCE_FILE}")
435+
436+
target_include_directories(check_header_${HEADER_NAME} PRIVATE
437+
${CMAKE_CURRENT_SOURCE_DIR}
438+
${ADDITIONAL_INCLUDE_DIRS}
439+
)
440+
441+
add_custom_target(check_${HEADER_NAME} ALL DEPENDS check_header_${HEADER_NAME})
442+
endforeach()
443+
endif()
444+
418445
if (REFLECTCPP_INSTALL)
419446
include(GNUInstallDirs)
420447
include(CMakePackageConfigHelpers)
@@ -451,9 +478,6 @@ if (REFLECTCPP_INSTALL)
451478
)
452479
endif ()
453480

454-
455-
# CPack configuration
456-
457481
# Set general package information
458482
set(CPACK_PACKAGE_NAME "reflectcpp")
459483
set(CPACK_PACKAGE_VENDOR "Reflect C++ Team")

include/rfl/Processors.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define RFL_INTERNAL_PROCESSORS_HPP_
33

44
#include <type_traits>
5+
#include <utility>
56

67
#include "internal/is_add_tags_to_variants_v.hpp"
78
#include "internal/is_allow_raw_ptrs_v.hpp"
@@ -69,9 +70,9 @@ struct Processors<Head, Tail...> {
6970

7071
template <class T, class NamedTupleType>
7172
static auto process(NamedTupleType&& _named_tuple) {
72-
static_assert(!add_tags_to_variants_ || !add_namespaced_tags_to_variants_,
73-
"You cannot add both rfl::AddTagsToVariants and "
74-
"rfl::AddNamespacedTagsToVariants.");
73+
static_assert(!add_tags_to_variants_ || !add_namespaced_tags_to_variants_,
74+
"You cannot add both rfl::AddTagsToVariants and "
75+
"rfl::AddNamespacedTagsToVariants.");
7576
return Processors<Tail...>::template process<T>(
7677
Head::template process<T>(std::move(_named_tuple)));
7778
}

include/rfl/Timestamp.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <stdexcept>
88
#include <string>
99

10+
#include "Literal.hpp"
1011
#include "Result.hpp"
1112
#include "internal/StringLiteral.hpp"
1213

include/rfl/Tuple.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define RFL_TUPLE_HPP_
33

44
#include <array>
5+
#include <cstddef>
56
#include <tuple>
67
#include <type_traits>
78
#include <utility>
@@ -43,9 +44,7 @@ class Tuple {
4344

4445
Tuple(const Tuple& _other) { copy_from_other(_other, seq_); }
4546

46-
Tuple(Tuple&& _other) noexcept {
47-
move_from_other(std::move(_other), seq_);
48-
}
47+
Tuple(Tuple&& _other) noexcept { move_from_other(std::move(_other), seq_); }
4948

5049
~Tuple() { destroy_if_necessary(seq_); }
5150

@@ -154,8 +153,7 @@ class Tuple {
154153
}
155154

156155
template <int... _is>
157-
void move_from_other(Tuple&& _other,
158-
std::integer_sequence<int, _is...>) {
156+
void move_from_other(Tuple&& _other, std::integer_sequence<int, _is...>) {
159157
const auto move_one = [this]<int _i>(auto&& _other,
160158
std::integral_constant<int, _i>) {
161159
using Type = internal::nth_element_t<_i, Types...>;

include/rfl/avro/Parser.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
#define RFL_AVRO_PARSER_HPP_
33

44
#include "../Generic.hpp"
5+
#include "../NamedTuple.hpp"
56
#include "../Tuple.hpp"
67
#include "../always_false.hpp"
7-
//#include "../parsing/Parser.hpp"
8+
#include "../parsing/AreReaderAndWriter.hpp"
9+
#include "../parsing/Parser.hpp"
810
#include "Reader.hpp"
911
#include "Writer.hpp"
1012

include/rfl/avro/SchemaImpl.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66
#include <string>
77

8-
//#include "../Box.hpp"
9-
//#include "../Result.hpp"
8+
#include "../Box.hpp"
109
#include "../common.hpp"
1110

1211
namespace rfl::avro {

0 commit comments

Comments
 (0)