Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cppgen: improved support of fields of type 'any' #893

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 39 additions & 18 deletions schema_salad/cpp_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,31 +700,52 @@ 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);
}
inline auto toYaml(std::string const& v) {
return YAML::Node{v};
}

template <typename T, typename ...Args>
auto anyToYaml_impl(std::any const& a) {
if (auto v = std::any_cast<T const>(&a)) {
return toYaml(*v);
}
if constexpr (sizeof...(Args) > 0) {
return anyToYaml_impl<Args...>(a);
}
return toYaml(std::monostate{});
}

inline auto toYaml(std::any const& a) {
return anyToYaml_impl<bool,
float,
double,
char,
int8_t,
uint8_t,
int16_t,
uint16_t,
int32_t,
uint32_t,
int64_t,
uint64_t,
std::string>(a);
}

// declaring fromYaml
inline void fromYaml(YAML::Node const& n, bool& v) {
v = n.as<bool>();
Expand Down
57 changes: 39 additions & 18 deletions schema_salad/tests/cpp_tests/01_single_record.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,52 @@ 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);
}
inline auto toYaml(std::string const& v) {
return YAML::Node{v};
}

template <typename T, typename ...Args>
auto anyToYaml_impl(std::any const& a) {
if (auto v = std::any_cast<T const>(&a)) {
return toYaml(*v);
}
if constexpr (sizeof...(Args) > 0) {
return anyToYaml_impl<Args...>(a);
}
return toYaml(std::monostate{});
}

inline auto toYaml(std::any const& a) {
return anyToYaml_impl<bool,
float,
double,
char,
int8_t,
uint8_t,
int16_t,
uint16_t,
int32_t,
uint32_t,
int64_t,
uint64_t,
std::string>(a);
}

// declaring fromYaml
inline void fromYaml(YAML::Node const& n, bool& v) {
v = n.as<bool>();
Expand Down
57 changes: 39 additions & 18 deletions schema_salad/tests/cpp_tests/02_two_records.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,52 @@ 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);
}
inline auto toYaml(std::string const& v) {
return YAML::Node{v};
}

template <typename T, typename ...Args>
auto anyToYaml_impl(std::any const& a) {
if (auto v = std::any_cast<T const>(&a)) {
return toYaml(*v);
}
if constexpr (sizeof...(Args) > 0) {
return anyToYaml_impl<Args...>(a);
}
return toYaml(std::monostate{});
}

inline auto toYaml(std::any const& a) {
return anyToYaml_impl<bool,
float,
double,
char,
int8_t,
uint8_t,
int16_t,
uint16_t,
int32_t,
uint32_t,
int64_t,
uint64_t,
std::string>(a);
}

// declaring fromYaml
inline void fromYaml(YAML::Node const& n, bool& v) {
v = n.as<bool>();
Expand Down
57 changes: 39 additions & 18 deletions schema_salad/tests/cpp_tests/03_simple_inheritance.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,52 @@ 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);
}
inline auto toYaml(std::string const& v) {
return YAML::Node{v};
}

template <typename T, typename ...Args>
auto anyToYaml_impl(std::any const& a) {
if (auto v = std::any_cast<T const>(&a)) {
return toYaml(*v);
}
if constexpr (sizeof...(Args) > 0) {
return anyToYaml_impl<Args...>(a);
}
return toYaml(std::monostate{});
}

inline auto toYaml(std::any const& a) {
return anyToYaml_impl<bool,
float,
double,
char,
int8_t,
uint8_t,
int16_t,
uint16_t,
int32_t,
uint32_t,
int64_t,
uint64_t,
std::string>(a);
}

// declaring fromYaml
inline void fromYaml(YAML::Node const& n, bool& v) {
v = n.as<bool>();
Expand Down
57 changes: 39 additions & 18 deletions schema_salad/tests/cpp_tests/04_abstract_inheritance.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,52 @@ 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);
}
inline auto toYaml(std::string const& v) {
return YAML::Node{v};
}

template <typename T, typename ...Args>
auto anyToYaml_impl(std::any const& a) {
if (auto v = std::any_cast<T const>(&a)) {
return toYaml(*v);
}
if constexpr (sizeof...(Args) > 0) {
return anyToYaml_impl<Args...>(a);
}
return toYaml(std::monostate{});
}

inline auto toYaml(std::any const& a) {
return anyToYaml_impl<bool,
float,
double,
char,
int8_t,
uint8_t,
int16_t,
uint16_t,
int32_t,
uint32_t,
int64_t,
uint64_t,
std::string>(a);
}

// declaring fromYaml
inline void fromYaml(YAML::Node const& n, bool& v) {
v = n.as<bool>();
Expand Down
Loading
Loading