Skip to content

Commit d55c30f

Browse files
committed
Move to inline constexpr to avoid duplication of symbols
1 parent b34d892 commit d55c30f

File tree

6 files changed

+40
-23
lines changed

6 files changed

+40
-23
lines changed

src/json2cpp.cpp

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ std::string compile(const nlohmann::json &value, std::size_t &obj_count, std::ve
4040
"value_pair_t{{{}, {{{}}}}},", json_string(itr.key()), compile(*itr, obj_count, lines)));
4141
}
4242

43-
lines.push_back(fmt::format("static constexpr std::array<value_pair_t, {}> object_data_{} = {{",
43+
lines.push_back(fmt::format("inline constexpr std::array<value_pair_t, {}> object_data_{} = {{",
4444
pairs.size(),
4545
current_object_number));
4646

@@ -58,7 +58,7 @@ std::string compile(const nlohmann::json &value, std::size_t &obj_count, std::ve
5858
});
5959

6060

61-
lines.push_back(fmt::format("static constexpr std::array<json, {}> object_data_{} = {{{{",
61+
lines.push_back(fmt::format("inline constexpr std::array<json, {}> object_data_{} = {{{{",
6262
entries.size(),
6363
current_object_number));
6464

@@ -99,7 +99,7 @@ compile_results compile(const std::string_view document_name, const nlohmann::js
9999
results.hpp.emplace_back("#include <json2cpp/json2cpp.hpp>");
100100

101101
results.hpp.push_back(fmt::format("namespace compiled_json::{} {{", document_name));
102-
results.hpp.push_back(fmt::format(" const json2cpp::json &get_{}();", document_name));
102+
results.hpp.push_back(fmt::format(" const json2cpp::json &get();", document_name));
103103
results.hpp.emplace_back("}");
104104

105105
results.hpp.emplace_back("#endif");
@@ -112,16 +112,32 @@ compile_results compile(const std::string_view document_name, const nlohmann::js
112112

113113
results.impl.emplace_back("#include <json2cpp/json2cpp.hpp>");
114114

115-
results.impl.push_back(fmt::format("namespace compiled_json::{} {{\nusing json = json2cpp::basic_json<char>;\nusing data_t=json2cpp::data_variant<char>;\nusing string_view=std::basic_string_view<char>;\nusing array_t=json2cpp::basic_array_t<char>;\nusing object_t=json2cpp::basic_object_t<char>;\nusing value_pair_t=json2cpp::basic_value_pair_t<char>;\n", document_name));
115+
results.impl.push_back(
116+
fmt::format(R"(
117+
namespace compiled_json::{}::impl {{
118+
119+
using json = json2cpp::basic_json<char>;
120+
using data_t=json2cpp::data_variant<char>;
121+
using string_view=std::basic_string_view<char>;
122+
using array_t=json2cpp::basic_array_t<char>;
123+
using object_t=json2cpp::basic_object_t<char>;
124+
using value_pair_t=json2cpp::basic_value_pair_t<char>;
125+
126+
)", document_name));
116127

117128

118129
const auto last_obj_name = compile(json, obj_count, results.impl);
119130

120-
results.impl.push_back(fmt::format("static constexpr auto document = json{{{{{}}}}};", last_obj_name));
131+
results.impl.push_back(fmt::format(R"(
132+
inline constexpr auto document = json{{{{{}}}}};
133+
134+
135+
}}
136+
137+
#endif
138+
139+
)", last_obj_name));
121140

122-
results.impl.push_back(fmt::format("const json2cpp::json &get_{}() {{ return document; }}", document_name));
123-
results.impl.emplace_back("}");
124-
results.impl.emplace_back("#endif");
125141

126142
spdlog::info("{} JSON objects processed.", obj_count);
127143

@@ -162,6 +178,7 @@ void write_compilation([[maybe_unused]] std::string_view document_name,
162178

163179
std::ofstream cpp(cpp_name);
164180
cpp << fmt::format("#include \"{}\"\n", impl_name.filename().string());
181+
cpp << fmt::format("namespace compiled_json::{} {{\nconst json2cpp::json &get() {{ return compiled_json::{}::impl::document; }}\n}}\n", document_name, document_name);
165182
}
166183

167184
void compile_to(const std::string_view document_name,

src/schema_validator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ bool validate_internal(const std::filesystem::path &file_to_validate)
114114
spdlog::info("Creating SchemaParser object");
115115
SchemaParser parser;
116116
spdlog::info("Creating json2cppJsonAdapter object");
117-
json2cppJsonAdapter mySchemaAdapter(compiled_json::energyplus_schema::get_energyplus_schema());
117+
json2cppJsonAdapter mySchemaAdapter(compiled_json::energyplus_schema::get());
118118
spdlog::info("parser.populateSchema object");
119119
parser.populateSchema(mySchemaAdapter, mySchema);
120120

@@ -195,7 +195,7 @@ int main(int argc, const char **argv)
195195

196196
if (args.at("--walk").asBool()) {
197197
if (args.at("--internal").asBool()) {
198-
walk(compiled_json::energyplus_schema::get_energyplus_schema());
198+
walk(compiled_json::energyplus_schema::get());
199199
} else {
200200
std::filesystem::path schema_file_name = args.at("<schema_file>").asString();
201201
spdlog::info("Creating nlohmann::json object");

test/constexpr_schema_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
TEST_CASE("Basic test of very large energyplus JSON schema")
66
{
7-
constexpr auto &document = compiled_json::schema::document;// NOLINT No, I'm not going to mark this `const`
7+
constexpr auto &document = compiled_json::schema::impl::document;// NOLINT No, I'm not going to mark this `const`
88

99
using namespace std::literals::string_view_literals;
1010

test/constexpr_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
TEST_CASE("Can read object size")
66
{
7-
constexpr auto &document = compiled_json::test_json::document;// NOLINT No, I'm not going to mark this `const`
7+
constexpr auto &document = compiled_json::test_json::impl::document;// NOLINT No, I'm not going to mark this `const`
88

99
STATIC_REQUIRE(document.size() == 1);
1010
}
1111

1212
constexpr auto count_elements()
1313
{
14-
constexpr auto &document = compiled_json::test_json::document;// NOLINT No, I'm not going to mark this `const`
14+
constexpr auto &document = compiled_json::test_json::impl::document;// NOLINT No, I'm not going to mark this `const`
1515

1616
std::size_t elements = 0;
1717
for (const auto &json : document) {
@@ -23,11 +23,11 @@ constexpr auto count_elements()
2323
return elements;
2424
}
2525

26-
TEST_CASE("Can iterate object") { STATIC_REQUIRE(count_elements() == compiled_json::test_json::document.size()); }
26+
TEST_CASE("Can iterate object") { STATIC_REQUIRE(count_elements() == compiled_json::test_json::impl::document.size()); }
2727

2828
TEST_CASE("Can read iterator key")
2929
{
30-
constexpr auto &document = compiled_json::test_json::document;// NOLINT No, I'm not going to mark this `const`
30+
constexpr auto &document = compiled_json::test_json::impl::document;// NOLINT No, I'm not going to mark this `const`
3131

3232
STATIC_REQUIRE(document.begin().key() == "glossary");
3333
}

test/tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33

44
TEST_CASE("Can read object size")
55
{
6-
const auto &document = compiled_json::test_json::get_test_json();
6+
const auto &document = compiled_json::test_json::get();
77

88
REQUIRE(document.size() == 1);
99
}
1010

1111

1212
TEST_CASE("Can iterate object")
1313
{
14-
const auto &document = compiled_json::test_json::get_test_json();
14+
const auto &document = compiled_json::test_json::get();
1515

1616
std::size_t elements = 0;
1717
for (const auto &json : document) {
@@ -24,6 +24,6 @@ TEST_CASE("Can iterate object")
2424

2525
TEST_CASE("Can read iterator key")
2626
{
27-
const auto &document = compiled_json::test_json::get_test_json();
27+
const auto &document = compiled_json::test_json::get();
2828
REQUIRE(document.begin().key() == "glossary");
2929
}

test/valijson_tests.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ TEST_CASE("Can load a valijson schema")
2020
Schema mySchema;
2121
SchemaParser parser;
2222
json2cppJsonAdapter mySchemaAdapter(
23-
compiled_json::allof_integers_and_numbers_schema::get_allof_integers_and_numbers_schema());
23+
compiled_json::allof_integers_and_numbers_schema::get());
2424
CHECK_NOTHROW(parser.populateSchema(mySchemaAdapter, mySchema));
2525
}
2626

@@ -37,11 +37,11 @@ TEST_CASE("Validation fails where expected")
3737
Schema mySchema;
3838
SchemaParser parser;
3939
json2cppJsonAdapter mySchemaAdapter(
40-
compiled_json::allof_integers_and_numbers_schema::get_allof_integers_and_numbers_schema());
40+
compiled_json::allof_integers_and_numbers_schema::get());
4141
CHECK_NOTHROW(parser.populateSchema(mySchemaAdapter, mySchema));
4242

4343
Validator validator;
44-
json2cppJsonAdapter myTargetAdapter(compiled_json::array_doubles_10_20_30_40::get_array_doubles_10_20_30_40());
44+
json2cppJsonAdapter myTargetAdapter(compiled_json::array_doubles_10_20_30_40::get());
4545

4646
REQUIRE_FALSE(validator.validate(mySchema, myTargetAdapter, nullptr));
4747
}
@@ -59,11 +59,11 @@ TEST_CASE("Can validate a document")
5959
Schema mySchema;
6060
SchemaParser parser;
6161
json2cppJsonAdapter mySchemaAdapter(
62-
compiled_json::allof_integers_and_numbers_schema::get_allof_integers_and_numbers_schema());
62+
compiled_json::allof_integers_and_numbers_schema::get());
6363
CHECK_NOTHROW(parser.populateSchema(mySchemaAdapter, mySchema));
6464

6565
Validator validator;
66-
json2cppJsonAdapter myTargetAdapter(compiled_json::array_integers_10_20_30_40::get_array_integers_10_20_30_40());
66+
json2cppJsonAdapter myTargetAdapter(compiled_json::array_integers_10_20_30_40::get());
6767

6868
REQUIRE(validator.validate(mySchema, myTargetAdapter, nullptr));
6969
}

0 commit comments

Comments
 (0)