diff --git a/Makefile b/Makefile index b906d189..eb0ebf7f 100644 --- a/Makefile +++ b/Makefile @@ -228,6 +228,11 @@ flake8: FORCE schema_salad/metaschema.py: schema_salad/codegen_base.py schema_salad/python_codegen_support.py schema_salad/python_codegen.py schema_salad/metaschema/*.yml schema-salad-tool --codegen python schema_salad/metaschema/metaschema.yml > $@ +vpath %.yml schema_salad/tests/cpp_tests + +schema_salad/tests/cpp_tests/%.h: %.yml + schema-salad-tool --codegen cpp --codegen-target $@ $< + FORCE: # Use this to print the value of a Makefile variable diff --git a/schema_salad/cpp_codegen.py b/schema_salad/cpp_codegen.py index d010588a..3593baa8 100644 --- a/schema_salad/cpp_codegen.py +++ b/schema_salad/cpp_codegen.py @@ -700,24 +700,18 @@ def epilogue(self, root_loader: Optional[TypeDef]) -> None: } // declaring toYaml -inline auto toYaml(bool v) { - return YAML::Node{v}; -} -inline auto toYaml(float v) { - return YAML::Node{v}; -} -inline auto toYaml(double v) { - return YAML::Node{v}; -} -inline auto toYaml(int32_t v) { - return YAML::Node{v}; -} -inline auto toYaml(int64_t v) { - return YAML::Node{v}; -} -inline auto toYaml(std::any const&) { - return YAML::Node{}; -} +inline auto toYaml(bool v) { return YAML::Node{v}; } +inline auto toYaml(float v) { return YAML::Node{v}; } +inline auto toYaml(double v) { return YAML::Node{v}; } +inline auto toYaml(char v) { return YAML::Node{v}; } +inline auto toYaml(int8_t v) { return YAML::Node{v}; } +inline auto toYaml(uint8_t v) { return YAML::Node{v}; } +inline auto toYaml(int16_t v) { return YAML::Node{v}; } +inline auto toYaml(uint16_t v) { return YAML::Node{v}; } +inline auto toYaml(int32_t v) { return YAML::Node{v}; } +inline auto toYaml(uint32_t v) { return YAML::Node{v}; } +inline auto toYaml(int64_t v) { return YAML::Node{v}; } +inline auto toYaml(uint64_t v) { return YAML::Node{v}; } inline auto toYaml(std::monostate const&) { return YAML::Node(YAML::NodeType::Undefined); } @@ -725,6 +719,33 @@ def epilogue(self, root_loader: Optional[TypeDef]) -> None: return YAML::Node{v}; } +template +auto anyToYaml_impl(std::any const& a) { + if (auto v = std::any_cast(&a)) { + return toYaml(*v); + } + if constexpr (sizeof...(Args) > 0) { + return anyToYaml_impl(a); + } + return toYaml(std::monostate{}); +} + +inline auto toYaml(std::any const& a) { + return anyToYaml_impl(a); +} + // declaring fromYaml inline void fromYaml(YAML::Node const& n, bool& v) { v = n.as(); diff --git a/schema_salad/tests/cpp_tests/01_single_record.h b/schema_salad/tests/cpp_tests/01_single_record.h index 4933e3ae..80e6491a 100644 --- a/schema_salad/tests/cpp_tests/01_single_record.h +++ b/schema_salad/tests/cpp_tests/01_single_record.h @@ -25,24 +25,18 @@ inline auto mergeYaml(YAML::Node n1, YAML::Node n2) { } // declaring toYaml -inline auto toYaml(bool v) { - return YAML::Node{v}; -} -inline auto toYaml(float v) { - return YAML::Node{v}; -} -inline auto toYaml(double v) { - return YAML::Node{v}; -} -inline auto toYaml(int32_t v) { - return YAML::Node{v}; -} -inline auto toYaml(int64_t v) { - return YAML::Node{v}; -} -inline auto toYaml(std::any const&) { - return YAML::Node{}; -} +inline auto toYaml(bool v) { return YAML::Node{v}; } +inline auto toYaml(float v) { return YAML::Node{v}; } +inline auto toYaml(double v) { return YAML::Node{v}; } +inline auto toYaml(char v) { return YAML::Node{v}; } +inline auto toYaml(int8_t v) { return YAML::Node{v}; } +inline auto toYaml(uint8_t v) { return YAML::Node{v}; } +inline auto toYaml(int16_t v) { return YAML::Node{v}; } +inline auto toYaml(uint16_t v) { return YAML::Node{v}; } +inline auto toYaml(int32_t v) { return YAML::Node{v}; } +inline auto toYaml(uint32_t v) { return YAML::Node{v}; } +inline auto toYaml(int64_t v) { return YAML::Node{v}; } +inline auto toYaml(uint64_t v) { return YAML::Node{v}; } inline auto toYaml(std::monostate const&) { return YAML::Node(YAML::NodeType::Undefined); } @@ -50,6 +44,33 @@ inline auto toYaml(std::string const& v) { return YAML::Node{v}; } +template +auto anyToYaml_impl(std::any const& a) { + if (auto v = std::any_cast(&a)) { + return toYaml(*v); + } + if constexpr (sizeof...(Args) > 0) { + return anyToYaml_impl(a); + } + return toYaml(std::monostate{}); +} + +inline auto toYaml(std::any const& a) { + return anyToYaml_impl(a); +} + // declaring fromYaml inline void fromYaml(YAML::Node const& n, bool& v) { v = n.as(); diff --git a/schema_salad/tests/cpp_tests/02_two_records.h b/schema_salad/tests/cpp_tests/02_two_records.h index 8d4b2e72..7af34fad 100644 --- a/schema_salad/tests/cpp_tests/02_two_records.h +++ b/schema_salad/tests/cpp_tests/02_two_records.h @@ -25,24 +25,18 @@ inline auto mergeYaml(YAML::Node n1, YAML::Node n2) { } // declaring toYaml -inline auto toYaml(bool v) { - return YAML::Node{v}; -} -inline auto toYaml(float v) { - return YAML::Node{v}; -} -inline auto toYaml(double v) { - return YAML::Node{v}; -} -inline auto toYaml(int32_t v) { - return YAML::Node{v}; -} -inline auto toYaml(int64_t v) { - return YAML::Node{v}; -} -inline auto toYaml(std::any const&) { - return YAML::Node{}; -} +inline auto toYaml(bool v) { return YAML::Node{v}; } +inline auto toYaml(float v) { return YAML::Node{v}; } +inline auto toYaml(double v) { return YAML::Node{v}; } +inline auto toYaml(char v) { return YAML::Node{v}; } +inline auto toYaml(int8_t v) { return YAML::Node{v}; } +inline auto toYaml(uint8_t v) { return YAML::Node{v}; } +inline auto toYaml(int16_t v) { return YAML::Node{v}; } +inline auto toYaml(uint16_t v) { return YAML::Node{v}; } +inline auto toYaml(int32_t v) { return YAML::Node{v}; } +inline auto toYaml(uint32_t v) { return YAML::Node{v}; } +inline auto toYaml(int64_t v) { return YAML::Node{v}; } +inline auto toYaml(uint64_t v) { return YAML::Node{v}; } inline auto toYaml(std::monostate const&) { return YAML::Node(YAML::NodeType::Undefined); } @@ -50,6 +44,33 @@ inline auto toYaml(std::string const& v) { return YAML::Node{v}; } +template +auto anyToYaml_impl(std::any const& a) { + if (auto v = std::any_cast(&a)) { + return toYaml(*v); + } + if constexpr (sizeof...(Args) > 0) { + return anyToYaml_impl(a); + } + return toYaml(std::monostate{}); +} + +inline auto toYaml(std::any const& a) { + return anyToYaml_impl(a); +} + // declaring fromYaml inline void fromYaml(YAML::Node const& n, bool& v) { v = n.as(); diff --git a/schema_salad/tests/cpp_tests/03_simple_inheritance.h b/schema_salad/tests/cpp_tests/03_simple_inheritance.h index fe80d0b3..9e8c70ff 100644 --- a/schema_salad/tests/cpp_tests/03_simple_inheritance.h +++ b/schema_salad/tests/cpp_tests/03_simple_inheritance.h @@ -25,24 +25,18 @@ inline auto mergeYaml(YAML::Node n1, YAML::Node n2) { } // declaring toYaml -inline auto toYaml(bool v) { - return YAML::Node{v}; -} -inline auto toYaml(float v) { - return YAML::Node{v}; -} -inline auto toYaml(double v) { - return YAML::Node{v}; -} -inline auto toYaml(int32_t v) { - return YAML::Node{v}; -} -inline auto toYaml(int64_t v) { - return YAML::Node{v}; -} -inline auto toYaml(std::any const&) { - return YAML::Node{}; -} +inline auto toYaml(bool v) { return YAML::Node{v}; } +inline auto toYaml(float v) { return YAML::Node{v}; } +inline auto toYaml(double v) { return YAML::Node{v}; } +inline auto toYaml(char v) { return YAML::Node{v}; } +inline auto toYaml(int8_t v) { return YAML::Node{v}; } +inline auto toYaml(uint8_t v) { return YAML::Node{v}; } +inline auto toYaml(int16_t v) { return YAML::Node{v}; } +inline auto toYaml(uint16_t v) { return YAML::Node{v}; } +inline auto toYaml(int32_t v) { return YAML::Node{v}; } +inline auto toYaml(uint32_t v) { return YAML::Node{v}; } +inline auto toYaml(int64_t v) { return YAML::Node{v}; } +inline auto toYaml(uint64_t v) { return YAML::Node{v}; } inline auto toYaml(std::monostate const&) { return YAML::Node(YAML::NodeType::Undefined); } @@ -50,6 +44,33 @@ inline auto toYaml(std::string const& v) { return YAML::Node{v}; } +template +auto anyToYaml_impl(std::any const& a) { + if (auto v = std::any_cast(&a)) { + return toYaml(*v); + } + if constexpr (sizeof...(Args) > 0) { + return anyToYaml_impl(a); + } + return toYaml(std::monostate{}); +} + +inline auto toYaml(std::any const& a) { + return anyToYaml_impl(a); +} + // declaring fromYaml inline void fromYaml(YAML::Node const& n, bool& v) { v = n.as(); diff --git a/schema_salad/tests/cpp_tests/04_abstract_inheritance.h b/schema_salad/tests/cpp_tests/04_abstract_inheritance.h index 4530fd40..d5feb1b4 100644 --- a/schema_salad/tests/cpp_tests/04_abstract_inheritance.h +++ b/schema_salad/tests/cpp_tests/04_abstract_inheritance.h @@ -25,24 +25,18 @@ inline auto mergeYaml(YAML::Node n1, YAML::Node n2) { } // declaring toYaml -inline auto toYaml(bool v) { - return YAML::Node{v}; -} -inline auto toYaml(float v) { - return YAML::Node{v}; -} -inline auto toYaml(double v) { - return YAML::Node{v}; -} -inline auto toYaml(int32_t v) { - return YAML::Node{v}; -} -inline auto toYaml(int64_t v) { - return YAML::Node{v}; -} -inline auto toYaml(std::any const&) { - return YAML::Node{}; -} +inline auto toYaml(bool v) { return YAML::Node{v}; } +inline auto toYaml(float v) { return YAML::Node{v}; } +inline auto toYaml(double v) { return YAML::Node{v}; } +inline auto toYaml(char v) { return YAML::Node{v}; } +inline auto toYaml(int8_t v) { return YAML::Node{v}; } +inline auto toYaml(uint8_t v) { return YAML::Node{v}; } +inline auto toYaml(int16_t v) { return YAML::Node{v}; } +inline auto toYaml(uint16_t v) { return YAML::Node{v}; } +inline auto toYaml(int32_t v) { return YAML::Node{v}; } +inline auto toYaml(uint32_t v) { return YAML::Node{v}; } +inline auto toYaml(int64_t v) { return YAML::Node{v}; } +inline auto toYaml(uint64_t v) { return YAML::Node{v}; } inline auto toYaml(std::monostate const&) { return YAML::Node(YAML::NodeType::Undefined); } @@ -50,6 +44,33 @@ inline auto toYaml(std::string const& v) { return YAML::Node{v}; } +template +auto anyToYaml_impl(std::any const& a) { + if (auto v = std::any_cast(&a)) { + return toYaml(*v); + } + if constexpr (sizeof...(Args) > 0) { + return anyToYaml_impl(a); + } + return toYaml(std::monostate{}); +} + +inline auto toYaml(std::any const& a) { + return anyToYaml_impl(a); +} + // declaring fromYaml inline void fromYaml(YAML::Node const& n, bool& v) { v = n.as(); diff --git a/schema_salad/tests/cpp_tests/05_specialization.h b/schema_salad/tests/cpp_tests/05_specialization.h index e20a4fd4..b64b1a13 100644 --- a/schema_salad/tests/cpp_tests/05_specialization.h +++ b/schema_salad/tests/cpp_tests/05_specialization.h @@ -25,24 +25,18 @@ inline auto mergeYaml(YAML::Node n1, YAML::Node n2) { } // declaring toYaml -inline auto toYaml(bool v) { - return YAML::Node{v}; -} -inline auto toYaml(float v) { - return YAML::Node{v}; -} -inline auto toYaml(double v) { - return YAML::Node{v}; -} -inline auto toYaml(int32_t v) { - return YAML::Node{v}; -} -inline auto toYaml(int64_t v) { - return YAML::Node{v}; -} -inline auto toYaml(std::any const&) { - return YAML::Node{}; -} +inline auto toYaml(bool v) { return YAML::Node{v}; } +inline auto toYaml(float v) { return YAML::Node{v}; } +inline auto toYaml(double v) { return YAML::Node{v}; } +inline auto toYaml(char v) { return YAML::Node{v}; } +inline auto toYaml(int8_t v) { return YAML::Node{v}; } +inline auto toYaml(uint8_t v) { return YAML::Node{v}; } +inline auto toYaml(int16_t v) { return YAML::Node{v}; } +inline auto toYaml(uint16_t v) { return YAML::Node{v}; } +inline auto toYaml(int32_t v) { return YAML::Node{v}; } +inline auto toYaml(uint32_t v) { return YAML::Node{v}; } +inline auto toYaml(int64_t v) { return YAML::Node{v}; } +inline auto toYaml(uint64_t v) { return YAML::Node{v}; } inline auto toYaml(std::monostate const&) { return YAML::Node(YAML::NodeType::Undefined); } @@ -50,6 +44,33 @@ inline auto toYaml(std::string const& v) { return YAML::Node{v}; } +template +auto anyToYaml_impl(std::any const& a) { + if (auto v = std::any_cast(&a)) { + return toYaml(*v); + } + if constexpr (sizeof...(Args) > 0) { + return anyToYaml_impl(a); + } + return toYaml(std::monostate{}); +} + +inline auto toYaml(std::any const& a) { + return anyToYaml_impl(a); +} + // declaring fromYaml inline void fromYaml(YAML::Node const& n, bool& v) { v = n.as();