From 18db49201da4f8221aa6fb7aca1c2c2f39f8ebbe Mon Sep 17 00:00:00 2001 From: Alexis Glass Date: Sat, 5 Nov 2022 20:28:31 +0100 Subject: [PATCH] Make json serialisation methods inline (#136) Inline serialization functions to avoid non-used function warnings if they are enabled. --- .../cpp-headers/all_datatypes_json+json.hpp | 4 +- .../cpp-headers/enum_data+json.hpp | 4 +- .../cpp-headers/my_flags+json.hpp | 4 +- .../cpp-headers/my_enum+json.hpp | 4 +- .../cpp-headers/my_flags+json.hpp | 4 +- .../cpp-headers/my_record+json.hpp | 4 +- src/main/scala/djinni/CppGenerator.scala | 57 ++++++++++--------- 7 files changed, 41 insertions(+), 40 deletions(-) diff --git a/src/it/resources/expected/all_datatypes_json/cpp-headers/all_datatypes_json+json.hpp b/src/it/resources/expected/all_datatypes_json/cpp-headers/all_datatypes_json+json.hpp index 5e0186f2..2ac3e7c4 100644 --- a/src/it/resources/expected/all_datatypes_json/cpp-headers/all_datatypes_json+json.hpp +++ b/src/it/resources/expected/all_datatypes_json/cpp-headers/all_datatypes_json+json.hpp @@ -15,7 +15,7 @@ #include #include -static void from_json(const nlohmann::json & j, all_datatypes_json& result) { +inline void from_json(const nlohmann::json & j, all_datatypes_json& result) { if (j.contains("booleanData")) { j.at("booleanData").get_to(result.booleanData); } @@ -65,7 +65,7 @@ static void from_json(const nlohmann::json & j, all_datatypes_json& result) { j.at("myFlags").get_to(result.myFlags); } } -static void to_json(nlohmann::json & j, const all_datatypes_json & item) { +inline void to_json(nlohmann::json & j, const all_datatypes_json & item) { j = nlohmann::json { {"booleanData", item.booleanData}, {"integer8Data", item.integer8Data}, diff --git a/src/it/resources/expected/all_datatypes_json/cpp-headers/enum_data+json.hpp b/src/it/resources/expected/all_datatypes_json/cpp-headers/enum_data+json.hpp index e7e4cd6b..d6dbf612 100644 --- a/src/it/resources/expected/all_datatypes_json/cpp-headers/enum_data+json.hpp +++ b/src/it/resources/expected/all_datatypes_json/cpp-headers/enum_data+json.hpp @@ -8,7 +8,7 @@ #include #include -static void to_json(nlohmann::json& j, enum_data e) +inline void to_json(nlohmann::json& j, enum_data e) { static const std::pair m[] = {{enum_data::FIRSTENUMVALUE,"FirstEnumValue"},{enum_data::SECONDENUMVALUE,"SecondEnumValue"}}; auto it = std::find_if(std::begin(m), std::end(m), @@ -18,7 +18,7 @@ static void to_json(nlohmann::json& j, enum_data e) }); j = ((it != std::end(m)) ? it : std::begin(m))->second; } -static void from_json(const nlohmann::json& j, enum_data& e) +inline void from_json(const nlohmann::json& j, enum_data& e) { static const std::pair m[] = {{enum_data::FIRSTENUMVALUE,"FirstEnumValue"},{enum_data::SECONDENUMVALUE,"SecondEnumValue"}}; auto it = std::find_if(std::begin(m), std::end(m), diff --git a/src/it/resources/expected/all_datatypes_json/cpp-headers/my_flags+json.hpp b/src/it/resources/expected/all_datatypes_json/cpp-headers/my_flags+json.hpp index 9a26c32c..79037e2b 100644 --- a/src/it/resources/expected/all_datatypes_json/cpp-headers/my_flags+json.hpp +++ b/src/it/resources/expected/all_datatypes_json/cpp-headers/my_flags+json.hpp @@ -8,7 +8,7 @@ #include #include -static void to_json(nlohmann::json& j, my_flags e) +inline void to_json(nlohmann::json& j, my_flags e) { static const std::pair m[] = {{my_flags::FLAG1,"flag1"},{my_flags::FLAG2,"flag2"},{my_flags::FLAG3,"flag3"}}; j = nlohmann::json::array(); @@ -20,7 +20,7 @@ static void to_json(nlohmann::json& j, my_flags e) } } } -static void from_json(const nlohmann::json& j, my_flags& e) +inline void from_json(const nlohmann::json& j, my_flags& e) { static const std::pair m[] = {{my_flags::FLAG1,"flag1"},{my_flags::FLAG2,"flag2"},{my_flags::FLAG3,"flag3"}}; e = static_cast(0); diff --git a/src/it/resources/expected/all_json_specialized_datatypes_in_custom_namespace/cpp-headers/my_enum+json.hpp b/src/it/resources/expected/all_json_specialized_datatypes_in_custom_namespace/cpp-headers/my_enum+json.hpp index 7084b2de..154e3e31 100644 --- a/src/it/resources/expected/all_json_specialized_datatypes_in_custom_namespace/cpp-headers/my_enum+json.hpp +++ b/src/it/resources/expected/all_json_specialized_datatypes_in_custom_namespace/cpp-headers/my_enum+json.hpp @@ -10,7 +10,7 @@ namespace custom_namespace { -static void to_json(nlohmann::json& j, my_enum e) +inline void to_json(nlohmann::json& j, my_enum e) { static const std::pair m[] = {{my_enum::FIRSTENUMVALUE,"FirstEnumValue"},{my_enum::SECONDENUMVALUE,"SecondEnumValue"}}; auto it = std::find_if(std::begin(m), std::end(m), @@ -20,7 +20,7 @@ static void to_json(nlohmann::json& j, my_enum e) }); j = ((it != std::end(m)) ? it : std::begin(m))->second; } -static void from_json(const nlohmann::json& j, my_enum& e) +inline void from_json(const nlohmann::json& j, my_enum& e) { static const std::pair m[] = {{my_enum::FIRSTENUMVALUE,"FirstEnumValue"},{my_enum::SECONDENUMVALUE,"SecondEnumValue"}}; auto it = std::find_if(std::begin(m), std::end(m), diff --git a/src/it/resources/expected/all_json_specialized_datatypes_in_custom_namespace/cpp-headers/my_flags+json.hpp b/src/it/resources/expected/all_json_specialized_datatypes_in_custom_namespace/cpp-headers/my_flags+json.hpp index 62b112cf..46e1c18c 100644 --- a/src/it/resources/expected/all_json_specialized_datatypes_in_custom_namespace/cpp-headers/my_flags+json.hpp +++ b/src/it/resources/expected/all_json_specialized_datatypes_in_custom_namespace/cpp-headers/my_flags+json.hpp @@ -10,7 +10,7 @@ namespace custom_namespace { -static void to_json(nlohmann::json& j, my_flags e) +inline void to_json(nlohmann::json& j, my_flags e) { static const std::pair m[] = {{my_flags::FLAG1,"flag1"},{my_flags::FLAG2,"flag2"}}; j = nlohmann::json::array(); @@ -22,7 +22,7 @@ static void to_json(nlohmann::json& j, my_flags e) } } } -static void from_json(const nlohmann::json& j, my_flags& e) +inline void from_json(const nlohmann::json& j, my_flags& e) { static const std::pair m[] = {{my_flags::FLAG1,"flag1"},{my_flags::FLAG2,"flag2"}}; e = static_cast(0); diff --git a/src/it/resources/expected/all_json_specialized_datatypes_in_custom_namespace/cpp-headers/my_record+json.hpp b/src/it/resources/expected/all_json_specialized_datatypes_in_custom_namespace/cpp-headers/my_record+json.hpp index 13badbc1..b63ac1e3 100644 --- a/src/it/resources/expected/all_json_specialized_datatypes_in_custom_namespace/cpp-headers/my_record+json.hpp +++ b/src/it/resources/expected/all_json_specialized_datatypes_in_custom_namespace/cpp-headers/my_record+json.hpp @@ -10,7 +10,7 @@ namespace custom_namespace { -static void from_json(const nlohmann::json & j, my_record& result) { +inline void from_json(const nlohmann::json & j, my_record& result) { if (j.contains("myEnum")) { j.at("myEnum").get_to(result.myEnum); } @@ -18,7 +18,7 @@ static void from_json(const nlohmann::json & j, my_record& result) { j.at("myFlags").get_to(result.myFlags); } } -static void to_json(nlohmann::json & j, const my_record & item) { +inline void to_json(nlohmann::json & j, const my_record & item) { j = nlohmann::json { {"myEnum", item.myEnum}, {"myFlags", item.myFlags} diff --git a/src/main/scala/djinni/CppGenerator.scala b/src/main/scala/djinni/CppGenerator.scala index 47d13c86..31e55980 100644 --- a/src/main/scala/djinni/CppGenerator.scala +++ b/src/main/scala/djinni/CppGenerator.scala @@ -217,37 +217,38 @@ class CppGenerator(spec: Spec) extends Generator(spec) { ) .mkString(",") - w.wl(s"static void to_json(nlohmann::json& j, ${ident.name} e)") - .braced { - if (e.flags) { - w.wl( - s"static const std::pair<${ident.name}, nlohmann::json> m[] = {$enumInitializer};" - ) - w.wl("j = nlohmann::json::array();") - w.wl("for(const auto& flagOption : m)").braced { - w.wl( - "if(static_cast(e & flagOption.first) != 0)" - ).braced { - w.wl("j.push_back(flagOption.second);") - } - } - } else { - w.wl( - s"static const std::pair<${ident.name}, nlohmann::json> m[] = {$enumInitializer};" - ) - w.wl("auto it = std::find_if(std::begin(m), std::end(m),") + w.wl( + s"inline void to_json(nlohmann::json& j, ${ident.name} e)" + ).braced { + if (e.flags) { + w.wl( + s"static const std::pair<${ident.name}, nlohmann::json> m[] = {$enumInitializer};" + ) + w.wl("j = nlohmann::json::array();") + w.wl("for(const auto& flagOption : m)").braced { w.wl( - s" [e](const std::pair<${ident.name}, nlohmann::json>& ej_pair) -> bool" - ).bracedEnd(");") { - w.wl("return ej_pair.first == e;") + "if(static_cast(e & flagOption.first) != 0)" + ).braced { + w.wl("j.push_back(flagOption.second);") } - w.wl( - "j = ((it != std::end(m)) ? it : std::begin(m))->second;" - ) } + } else { + w.wl( + s"static const std::pair<${ident.name}, nlohmann::json> m[] = {$enumInitializer};" + ) + w.wl("auto it = std::find_if(std::begin(m), std::end(m),") + w.wl( + s" [e](const std::pair<${ident.name}, nlohmann::json>& ej_pair) -> bool" + ).bracedEnd(");") { + w.wl("return ej_pair.first == e;") + } + w.wl( + "j = ((it != std::end(m)) ? it : std::begin(m))->second;" + ) } + } w.wl( - s"static void from_json(const nlohmann::json& j, ${ident.name}& e)" + s"inline void from_json(const nlohmann::json& j, ${ident.name}& e)" ).braced { if (e.flags) { w.wl( @@ -530,7 +531,7 @@ class CppGenerator(spec: Spec) extends Generator(spec) { val recordSelf = ident.name // From JSON w.w( - s"static void from_json(const nlohmann::json & j, ${recordSelf}& result) " + s"inline void from_json(const nlohmann::json & j, ${recordSelf}& result) " ).braced { for (i <- fields.indices) { val name = idCpp.field(fields(i).ident) @@ -557,7 +558,7 @@ class CppGenerator(spec: Spec) extends Generator(spec) { } // To JSON w.w( - s"static void to_json(nlohmann::json & j, const $recordSelf & item) " + s"inline void to_json(nlohmann::json & j, const $recordSelf & item) " ).braced { w.w(s"j = nlohmann::json").bracedEnd(";") { for (i <- fields.indices) {