Skip to content

Commit

Permalink
Merge pull request #176 from AsherGlick/zero_cost_proto_wrappers
Browse files Browse the repository at this point in the history
Adding zero cost wrappers to the proto serializers
  • Loading branch information
AsherGlick authored Oct 9, 2023
2 parents c3fcd2a + 8e327e8 commit 4289183
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 124 deletions.
33 changes: 9 additions & 24 deletions xml_converter/generators/cpp_templates/class_template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,13 @@ waypoint::{{cpp_class}} {{cpp_class}}::as_protobuf() const {
waypoint::{{cpp_class}} proto_{{cpp_class_header}};
{% for attribute_variable in attribute_variables %}
{% if attribute_variable.is_component == false %}
{% if (attribute_variable.attribute_type in ["MultiflagValue", "CompoundValue", "Custom", "CompoundCustomClass"])%}
if (this->{{attribute_variable.attribute_flag_name}}) {
if (this->{{attribute_variable.attribute_flag_name}}) {
{% if (attribute_variable.attribute_type in ["MultiflagValue", "CompoundValue", "Custom", "CompoundCustomClass"]) %}
proto_{{cpp_class_header}}.{{attribute_variable.mutable_proto_drilldown_calls}}set_allocated_{{attribute_variable.protobuf_field}}(to_proto_{{attribute_variable.class_name}}(this->{{attribute_variable.attribute_name}}));
}
{% elif (attribute_variable.attribute_type == "Enum")%}
if (this->{{attribute_variable.attribute_flag_name}}) {
{% else %}
proto_{{cpp_class_header}}.{{attribute_variable.mutable_proto_drilldown_calls}}set_{{attribute_variable.protobuf_field}}(to_proto_{{attribute_variable.class_name}}(this->{{attribute_variable.attribute_name}}));
}
{% else: %}
if (this->{{attribute_variable.attribute_flag_name}}) {
proto_{{cpp_class_header}}.{{attribute_variable.mutable_proto_drilldown_calls}}set_{{attribute_variable.protobuf_field}}(this->{{attribute_variable.attribute_name}});
}
{% endif %}
{% endif %}
}
{% endif %}
{% endfor %}
return proto_{{cpp_class_header}};
Expand All @@ -120,25 +114,16 @@ void {{cpp_class}}::parse_protobuf(waypoint::{{cpp_class}} proto_{{cpp_class_hea
{% if attribute_variable.is_component == false %}
{% if (attribute_variable.attribute_type in ["MultiflagValue", "CompoundValue", "Custom", "CompoundCustomClass"]) %}
if (proto_{{cpp_class_header}}{{attribute_variable.proto_drilldown_calls}}.has_{{attribute_variable.protobuf_field}}()) {
this->{{attribute_variable.attribute_name}} = from_proto_{{attribute_variable.class_name}}(proto_{{cpp_class_header}}{{attribute_variable.proto_drilldown_calls}}.{{attribute_variable.protobuf_field}}());
this->{{attribute_variable.attribute_flag_name}} = true;
}
{% elif (attribute_variable.class_name == "string") %}{# TODO: why is this .class_name when the others are .attribute_name #}
{% elif (attribute_variable.attribute_type == "String") %}
if (proto_{{cpp_class_header}}{{attribute_variable.proto_drilldown_calls}}.{{attribute_variable.protobuf_field}}() != "") {
this->{{attribute_variable.attribute_name}} = proto_{{cpp_class_header}}{{attribute_variable.proto_drilldown_calls}}.{{attribute_variable.protobuf_field}}();
this->{{attribute_variable.attribute_flag_name}} = true;
}
{% elif (attribute_variable.attribute_type == "Enum") %}
if (proto_{{cpp_class_header}}{{attribute_variable.proto_drilldown_calls}}.{{attribute_variable.protobuf_field}}() != 0) {
this->{{attribute_variable.attribute_name}} = from_proto_{{attribute_variable.class_name}}(proto_{{cpp_class_header}}{{attribute_variable.proto_drilldown_calls}}.{{attribute_variable.protobuf_field}}());
this->{{attribute_variable.attribute_flag_name}} = true;
}
{% else %}
if (proto_{{cpp_class_header}}{{attribute_variable.proto_drilldown_calls}}.{{attribute_variable.protobuf_field}}() != 0) {
this->{{attribute_variable.attribute_name}} = proto_{{cpp_class_header}}{{attribute_variable.proto_drilldown_calls}}.{{attribute_variable.protobuf_field}}();
this->{{attribute_variable.attribute_flag_name}} = true;
}
{% endif %}
this->{{attribute_variable.attribute_name}} = from_proto_{{attribute_variable.class_name}}(proto_{{cpp_class_header}}{{attribute_variable.proto_drilldown_calls}}.{{attribute_variable.protobuf_field}}());
this->{{attribute_variable.attribute_flag_name}} = true;
}
{% endif %}
{% endfor %}
}
8 changes: 8 additions & 0 deletions xml_converter/src/attribute/bool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@ class XMLError;
bool parse_bool(rapidxml::xml_attribute<>* input, std::vector<XMLError*>* errors);

std::string stringify_bool(bool attribute_value);

// Zero Cost Abstraction identity functions to make parsing and writing protobufs more uniform
inline bool const& from_proto_bool(const bool& x) {
return x;
}
inline bool const& to_proto_bool(const bool& x) {
return x;
}
8 changes: 8 additions & 0 deletions xml_converter/src/attribute/float.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@ class XMLError;
float parse_float(rapidxml::xml_attribute<>* input, std::vector<XMLError*>* errors);

std::string stringify_float(float attribute_value);

// Zero Cost Abstraction identity functions to make parsing and writing protobufs more uniform
inline float const& from_proto_float(const float& x) {
return x;
}
inline float const& to_proto_float(const float& x) {
return x;
}
8 changes: 8 additions & 0 deletions xml_converter/src/attribute/int.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@ class XMLError;
int parse_int(rapidxml::xml_attribute<>* input, std::vector<XMLError*>* errors);

std::string stringify_int(int attribute_value);

// Zero Cost Abstraction identity functions to make parsing and writing protobufs more uniform
inline int const& from_proto_int(const int& x) {
return x;
}
inline int const& to_proto_int(const int& x) {
return x;
}
8 changes: 8 additions & 0 deletions xml_converter/src/attribute/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@ class XMLError;
std::string parse_string(rapidxml::xml_attribute<>* input, std::vector<XMLError*>* errors);

std::string stringify_string(std::string attribute_value);

// Zero Cost Abstraction identity functions to make parsing and writing protobufs more uniform
inline std::string const& from_proto_string(const std::string& x) {
return x;
}
inline std::string const& to_proto_string(const std::string& x) {
return x;
}
20 changes: 10 additions & 10 deletions xml_converter/src/category_gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,42 +98,42 @@ vector<string> Category::as_xml() const {
waypoint::Category Category::as_protobuf() const {
waypoint::Category proto_category;
if (this->default_visibility_is_set) {
proto_category.set_default_visibility(this->default_visibility);
proto_category.set_default_visibility(to_proto_bool(this->default_visibility));
}
if (this->display_name_is_set) {
proto_category.set_display_name(this->display_name);
proto_category.set_display_name(to_proto_string(this->display_name));
}
if (this->is_separator_is_set) {
proto_category.set_is_separator(this->is_separator);
proto_category.set_is_separator(to_proto_bool(this->is_separator));
}
if (this->name_is_set) {
proto_category.set_name(this->name);
proto_category.set_name(to_proto_string(this->name));
}
if (this->tooltip_description_is_set) {
proto_category.set_tip_description(this->tooltip_description);
proto_category.set_tip_description(to_proto_string(this->tooltip_description));
}
return proto_category;
}

void Category::parse_protobuf(waypoint::Category proto_category) {
if (proto_category.default_visibility() != 0) {
this->default_visibility = proto_category.default_visibility();
this->default_visibility = from_proto_bool(proto_category.default_visibility());
this->default_visibility_is_set = true;
}
if (proto_category.display_name() != "") {
this->display_name = proto_category.display_name();
this->display_name = from_proto_string(proto_category.display_name());
this->display_name_is_set = true;
}
if (proto_category.is_separator() != 0) {
this->is_separator = proto_category.is_separator();
this->is_separator = from_proto_bool(proto_category.is_separator());
this->is_separator_is_set = true;
}
if (proto_category.name() != "") {
this->name = proto_category.name();
this->name = from_proto_string(proto_category.name());
this->name_is_set = true;
}
if (proto_category.tip_description() != "") {
this->tooltip_description = proto_category.tip_description();
this->tooltip_description = from_proto_string(proto_category.tip_description());
this->tooltip_description_is_set = true;
}
}
Loading

0 comments on commit 4289183

Please sign in to comment.