Skip to content

Commit

Permalink
Making proto write functions more flexible then they were previously
Browse files Browse the repository at this point in the history
Proto write functions now no longer return their data, instead they get to directly write their data to the proto itself. There are two variants for scalar and non-scalar, these apply 1-1 to the set() and set_allocated() functions and were pretty unavoidable.
  • Loading branch information
AsherGlick committed Nov 4, 2023
1 parent cb4f817 commit 29de4fb
Show file tree
Hide file tree
Showing 47 changed files with 601 additions and 416 deletions.
17 changes: 10 additions & 7 deletions xml_converter/generators/cpp_templates/attribute_template.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

#include <string>
#include <vector>
#include <functional>

#include "../rapidxml-1.13/rapidxml.hpp"
{% if type == "Enum":%}
{% if type == "Enum" %}
#include "waypoint.pb.h"

class XMLError;
Expand All @@ -14,7 +15,7 @@
{{attribute_variable.attribute_name}},
{% endfor %}
};
{% else: %}
{% else %}
class XMLError;
{{proto_field_cpp_type_prototype}}

Expand All @@ -35,9 +36,11 @@ void xml_attribute_to_{{attribute_name}}(
{{class_name}}* value,
bool* is_set);
std::string {{attribute_name}}_to_xml_attribute(const std::string& attribute_name, const {{class_name}}* value);
{% if type == "Enum":%}
{{proto_field_cpp_type}} to_proto_{{attribute_name}}({{class_name}} attribute_value);
{% else: %}
{{proto_field_cpp_type}}* to_proto_{{attribute_name}}({{class_name}} attribute_value);
{% endif %}

{{class_name}} from_proto_{{attribute_name}}({{proto_field_cpp_type}} proto_{{attribute_name}});

{% if type == "Enum" %}
void {{attribute_name}}_to_proto({{class_name}} value, std::function<void({{proto_field_cpp_type}})> setter);
{% else %}
void {{attribute_name}}_to_proto({{class_name}} value, std::function<void({{proto_field_cpp_type}}*)> setter);
{% endif %}
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,18 @@ void xml_attribute_to_{{attribute_name}}(
}
{% endif %}

{{proto_field_cpp_type}}* to_proto_{{attribute_name}}({{class_name}} attribute_value) {
{{proto_field_cpp_type}}* proto_{{attribute_name}} = new {{proto_field_cpp_type}}();
{% for attribute_variable in attribute_variables %}
proto_{{attribute_name}}->set_{{attribute_variable.protobuf_field}}(attribute_value.{{attribute_variable.attribute_name}});
{% endfor %}
return proto_{{attribute_name}};
}

{{class_name}} from_proto_{{attribute_name}}({{proto_field_cpp_type}} proto_{{attribute_name}}) {
{{class_name}} {{attribute_name}};
{% for attribute_variable in attribute_variables: %}
{{attribute_name}}.{{attribute_variable.attribute_name}} = proto_{{attribute_name}}.{{attribute_variable.protobuf_field}}();
{% endfor %}
return {{attribute_name}};
}

void {{attribute_name}}_to_proto({{class_name}} value, std::function<void({{proto_field_cpp_type}}*)> setter) {
{{proto_field_cpp_type}}* proto_{{attribute_name}} = new {{proto_field_cpp_type}}();
{% for attribute_variable in attribute_variables %}
proto_{{attribute_name}}->set_{{attribute_variable.protobuf_field}}(value.{{attribute_variable.attribute_name}});
{% endfor %}
setter(proto_{{attribute_name}});
}
22 changes: 12 additions & 10 deletions xml_converter/generators/cpp_templates/attribute_template_enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,26 @@ string {{attribute_name}}_to_xml_attribute(const std::string& attribute_name, co
}
}

{{proto_field_cpp_type}} to_proto_{{attribute_name}}({{class_name}} attribute_value) {
switch (attribute_value) {
{{class_name}} from_proto_{{attribute_name}}({{proto_field_cpp_type}} proto_{{attribute_name}}) {
switch (proto_{{attribute_name}}) {
{% for attribute_variable in attribute_variables %}
case {{class_name}}::{{attribute_variable.attribute_name}}:
return {{proto_field_cpp_type}}::{{attribute_variable.attribute_name}};
case {{proto_field_cpp_type}}::{{attribute_variable.attribute_name}}:
return {{class_name}}::{{attribute_variable.attribute_name}};
{% endfor %}
default:
return {{proto_field_cpp_type}}::{{attribute_variables[0].attribute_name}};
return {{class_name}}::{{attribute_variables[0].attribute_name}};
}
}

{{class_name}} from_proto_{{attribute_name}}({{proto_field_cpp_type}} proto_{{attribute_name}}) {
switch (proto_{{attribute_name}}) {
void {{attribute_name}}_to_proto({{class_name}} value, std::function<void({{proto_field_cpp_type}})> setter) {
switch (value) {
{% for attribute_variable in attribute_variables %}
case {{proto_field_cpp_type}}::{{attribute_variable.attribute_name}}:
return {{class_name}}::{{attribute_variable.attribute_name}};
case {{class_name}}::{{attribute_variable.attribute_name}}:
setter({{proto_field_cpp_type}}::{{attribute_variable.attribute_name}});
break;
{% endfor %}
default:
return {{class_name}}::{{attribute_variables[0].attribute_name}};
setter({{proto_field_cpp_type}}::{{attribute_variables[0].attribute_name}});
break;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,18 @@ string {{attribute_name}}_to_xml_attribute(const std::string& attribute_name, co
return " " + attribute_name + "=\"" + output + "\"";
}

{{proto_field_cpp_type}}* to_proto_{{attribute_name}}({{class_name}} attribute_value) {
{{proto_field_cpp_type}}* proto_{{attribute_name}} = new {{proto_field_cpp_type}}();
{% for n, attribute_variable in enumerate(attribute_variables)%}
proto_{{attribute_name}}->set_{{attribute_variable.attribute_name}}(attribute_value.{{attribute_variable.attribute_name}});
{% endfor %}
return proto_{{attribute_name}};
}

{{class_name}} from_proto_{{attribute_name}}({{proto_field_cpp_type}} proto_{{attribute_name}}) {
{{class_name}} {{attribute_name}};
{% for n, attribute_variable in enumerate(attribute_variables)%}
{{attribute_name}}.{{attribute_variable.attribute_name}} = proto_{{attribute_name}}.{{attribute_variable.attribute_name}}();
{% endfor %}
return {{attribute_name}};
}

void {{attribute_name}}_to_proto({{class_name}} value, std::function<void({{proto_field_cpp_type}}*)> setter) {
{{proto_field_cpp_type}}* proto_{{attribute_name}} = new {{proto_field_cpp_type}}();
{% for n, attribute_variable in enumerate(attribute_variables)%}
proto_{{attribute_name}}->set_{{attribute_variable.attribute_name}}(value.{{attribute_variable.attribute_name}});
{% endfor %}
setter(proto_{{attribute_name}});
}
5 changes: 3 additions & 2 deletions xml_converter/generators/cpp_templates/class_template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,11 @@ waypoint::{{cpp_class}} {{cpp_class}}::as_protobuf() const {
{% if attribute_variable.is_component == false %}
if (this->{{attribute_variable.attribute_flag_name}}) {
{% if not attribute_variable.is_proto_field_scalar %}
proto_{{cpp_class_header}}.{{attribute_variable.mutable_proto_drilldown_calls}}set_allocated_{{attribute_variable.protobuf_field}}({{attribute_variable.serialize_proto_function}}(this->{{attribute_variable.attribute_name}}));
std::function<void({{attribute_variable.protobuf_cpp_type}}*)> setter = [&proto_{{cpp_class_header}}]({{attribute_variable.protobuf_cpp_type}}* val) { proto_{{cpp_class_header}}.{{attribute_variable.mutable_proto_drilldown_calls}}set_allocated_{{attribute_variable.protobuf_field}}(val); };
{% else %}
proto_{{cpp_class_header}}.{{attribute_variable.mutable_proto_drilldown_calls}}set_{{attribute_variable.protobuf_field}}({{attribute_variable.serialize_proto_function}}(this->{{attribute_variable.attribute_name}}));
std::function<void({{attribute_variable.protobuf_cpp_type}})> setter = [&proto_{{cpp_class_header}}]({{attribute_variable.protobuf_cpp_type}} val) { proto_{{cpp_class_header}}.{{attribute_variable.mutable_proto_drilldown_calls}}set_{{attribute_variable.protobuf_field}}(val); };
{% endif %}
{{attribute_variable.serialize_proto_function}}(this->{{attribute_variable.attribute_name}}, setter);
}
{% endif %}
{% endfor %}
Expand Down
2 changes: 1 addition & 1 deletion xml_converter/generators/generate_cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ def generate_cpp_variable_data(
serialize_xml_side_effects: List[str] = []
deserialize_xml_function: str = "xml_attribute_to_" + class_name
deserialize_xml_side_effects: List[str] = []
serialize_proto_function: str = "to_proto_" + class_name
serialize_proto_function: str = class_name + "_to_proto"
serialize_proto_side_effects: List[str] = []
deserialize_proto_function: str = "from_proto_" + class_name
deserialize_proto_side_effects: List[str] = []
Expand Down
9 changes: 9 additions & 0 deletions xml_converter/src/attribute/bool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,12 @@ string bool_to_xml_attribute(const string& attribute_name, const bool* value) {
return " " + attribute_name + "=\"false\"";
}
}

////////////////////////////////////////////////////////////////////////////////
// bool_to_proto
//
// Writes a bool to a proto using the provided setter function.
////////////////////////////////////////////////////////////////////////////////
void bool_to_proto(bool value, std::function<void(bool)> setter) {
setter(value);
}
7 changes: 4 additions & 3 deletions xml_converter/src/attribute/bool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <string>
#include <vector>
#include <functional>

#include "../rapidxml-1.13/rapidxml.hpp"

Expand All @@ -15,10 +16,10 @@ void xml_attribute_to_bool(

std::string bool_to_xml_attribute(const std::string& attribute_name, const bool* 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;
}

void bool_to_proto(bool value, std::function<void(bool)> setter);
45 changes: 26 additions & 19 deletions xml_converter/src/attribute/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,25 +113,6 @@ string color_to_xml_attribute(const string& attribute_name, const Color* value)
return " " + attribute_name + "=\"" + rgb + "\"";
}

////////////////////////////////////////////////////////////////////////////////
// to_proto_color
//
// Converts a Color into a proto message
////////////////////////////////////////////////////////////////////////////////
waypoint::RGBAColor* to_proto_color(Color attribute_value) {
waypoint::RGBAColor* color = new waypoint::RGBAColor();
// The default RGB in burrito will be 000000 (i.e. black)
// Default value of alpha in Burrito is 1.0 (i.e. 255)
int int_alpha = 255;
// If alpha (float) is not the default value, convert to int
if (attribute_value.alpha != 0) {
int_alpha = convert_color_channel_float_to_int(attribute_value.alpha);
}

uint32_t rgba = ((convert_color_channel_float_to_int(attribute_value.red) & 0xff) << 24) + ((convert_color_channel_float_to_int(attribute_value.green) & 0xff) << 16) + ((convert_color_channel_float_to_int(attribute_value.blue) & 0xff) << 8) + (int_alpha & 0xff);
color->set_rgba_color(rgba);
return color;
}

////////////////////////////////////////////////////////////////////////////////
// from_proto_color
Expand All @@ -150,3 +131,29 @@ Color from_proto_color(waypoint::RGBAColor attribute_value) {

return color;
}


////////////////////////////////////////////////////////////////////////////////
// color_to_proto
//
// Writes a Color to a proto using the provided setter function.
////////////////////////////////////////////////////////////////////////////////
void color_to_proto(Color value, std::function<void(waypoint::RGBAColor*)> setter) {
waypoint::RGBAColor* color = new waypoint::RGBAColor();
// The default RGB in burrito will be 000000 (i.e. black)
// Default value of alpha in Burrito is 1.0 (i.e. 255)
int int_alpha = 255;
// If alpha (float) is not the default value, convert to int
if (value.alpha != 0) {
int_alpha = convert_color_channel_float_to_int(value.alpha);
}

uint32_t rgba =
((convert_color_channel_float_to_int(value.red) & 0xff) << 24)
+ ((convert_color_channel_float_to_int(value.green) & 0xff) << 16)
+ ((convert_color_channel_float_to_int(value.blue) & 0xff) << 8)
+ (int_alpha & 0xff);

color->set_rgba_color(rgba);
setter(color);
}
5 changes: 3 additions & 2 deletions xml_converter/src/attribute/color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <string>
#include <vector>
#include <functional>

#include "../rapidxml-1.13/rapidxml.hpp"

Expand All @@ -26,6 +27,6 @@ void xml_attribute_to_color(

std::string color_to_xml_attribute(const std::string& attribute_name, const Color* value);

waypoint::RGBAColor* to_proto_color(Color attribute_value);

Color from_proto_color(waypoint::RGBAColor attribute_value);

void color_to_proto(Color value, std::function<void(waypoint::RGBAColor*)> setter);
30 changes: 17 additions & 13 deletions xml_converter/src/attribute/cull_chirality_gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,6 @@ string cull_chirality_to_xml_attribute(const std::string& attribute_name, const
}
}

waypoint::CullChirality to_proto_cull_chirality(CullChirality attribute_value) {
switch (attribute_value) {
case CullChirality::none:
return waypoint::CullChirality::none;
case CullChirality::clockwise:
return waypoint::CullChirality::clockwise;
case CullChirality::counter_clockwise:
return waypoint::CullChirality::counter_clockwise;
default:
return waypoint::CullChirality::none;
}
}

CullChirality from_proto_cull_chirality(waypoint::CullChirality proto_cull_chirality) {
switch (proto_cull_chirality) {
case waypoint::CullChirality::none:
Expand All @@ -76,3 +63,20 @@ CullChirality from_proto_cull_chirality(waypoint::CullChirality proto_cull_chira
return CullChirality::none;
}
}

void cull_chirality_to_proto(CullChirality value, std::function<void(waypoint::CullChirality)> setter) {
switch (value) {
case CullChirality::none:
setter(waypoint::CullChirality::none);
break;
case CullChirality::clockwise:
setter(waypoint::CullChirality::clockwise);
break;
case CullChirality::counter_clockwise:
setter(waypoint::CullChirality::counter_clockwise);
break;
default:
setter(waypoint::CullChirality::none);
break;
}
}
5 changes: 4 additions & 1 deletion xml_converter/src/attribute/cull_chirality_gen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <string>
#include <vector>
#include <functional>

#include "../rapidxml-1.13/rapidxml.hpp"
#include "waypoint.pb.h"
Expand All @@ -19,5 +20,7 @@ void xml_attribute_to_cull_chirality(
CullChirality* value,
bool* is_set);
std::string cull_chirality_to_xml_attribute(const std::string& attribute_name, const CullChirality* value);
waypoint::CullChirality to_proto_cull_chirality(CullChirality attribute_value);

CullChirality from_proto_cull_chirality(waypoint::CullChirality proto_cull_chirality);

void cull_chirality_to_proto(CullChirality value, std::function<void(waypoint::CullChirality)> setter);
16 changes: 8 additions & 8 deletions xml_converter/src/attribute/euler_rotation_gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ string euler_rotation_to_xml_attribute(const std::string& attribute_name, const
return " " + attribute_name + "=\"" + output + "\"";
}

waypoint::EulerRotation* to_proto_euler_rotation(EulerRotation attribute_value) {
waypoint::EulerRotation* proto_euler_rotation = new waypoint::EulerRotation();
proto_euler_rotation->set_x(attribute_value.x_rotation);
proto_euler_rotation->set_y(attribute_value.y_rotation);
proto_euler_rotation->set_z(attribute_value.z_rotation);
return proto_euler_rotation;
}

EulerRotation from_proto_euler_rotation(waypoint::EulerRotation proto_euler_rotation) {
EulerRotation euler_rotation;
euler_rotation.x_rotation = proto_euler_rotation.x();
euler_rotation.y_rotation = proto_euler_rotation.y();
euler_rotation.z_rotation = proto_euler_rotation.z();
return euler_rotation;
}

void euler_rotation_to_proto(EulerRotation value, std::function<void(waypoint::EulerRotation*)> setter) {
waypoint::EulerRotation* proto_euler_rotation = new waypoint::EulerRotation();
proto_euler_rotation->set_x(value.x_rotation);
proto_euler_rotation->set_y(value.y_rotation);
proto_euler_rotation->set_z(value.z_rotation);
setter(proto_euler_rotation);
}
5 changes: 4 additions & 1 deletion xml_converter/src/attribute/euler_rotation_gen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <string>
#include <vector>
#include <functional>

#include "../rapidxml-1.13/rapidxml.hpp"
class XMLError;
Expand All @@ -25,5 +26,7 @@ void xml_attribute_to_euler_rotation(
EulerRotation* value,
bool* is_set);
std::string euler_rotation_to_xml_attribute(const std::string& attribute_name, const EulerRotation* value);
waypoint::EulerRotation* to_proto_euler_rotation(EulerRotation attribute_value);

EulerRotation from_proto_euler_rotation(waypoint::EulerRotation proto_euler_rotation);

void euler_rotation_to_proto(EulerRotation value, std::function<void(waypoint::EulerRotation*)> setter);
24 changes: 12 additions & 12 deletions xml_converter/src/attribute/festival_filter_gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,6 @@ string festival_filter_to_xml_attribute(const std::string& attribute_name, const
return " " + attribute_name + "=\"" + output + "\"";
}

waypoint::FestivalFilter* to_proto_festival_filter(FestivalFilter attribute_value) {
waypoint::FestivalFilter* proto_festival_filter = new waypoint::FestivalFilter();
proto_festival_filter->set_dragonbash(attribute_value.dragonbash);
proto_festival_filter->set_festival_of_the_four_winds(attribute_value.festival_of_the_four_winds);
proto_festival_filter->set_halloween(attribute_value.halloween);
proto_festival_filter->set_lunar_new_year(attribute_value.lunar_new_year);
proto_festival_filter->set_super_adventure_festival(attribute_value.super_adventure_festival);
proto_festival_filter->set_wintersday(attribute_value.wintersday);
proto_festival_filter->set_none(attribute_value.none);
return proto_festival_filter;
}

FestivalFilter from_proto_festival_filter(waypoint::FestivalFilter proto_festival_filter) {
FestivalFilter festival_filter;
festival_filter.dragonbash = proto_festival_filter.dragonbash();
Expand All @@ -113,3 +101,15 @@ FestivalFilter from_proto_festival_filter(waypoint::FestivalFilter proto_festiva
festival_filter.none = proto_festival_filter.none();
return festival_filter;
}

void festival_filter_to_proto(FestivalFilter value, std::function<void(waypoint::FestivalFilter*)> setter) {
waypoint::FestivalFilter* proto_festival_filter = new waypoint::FestivalFilter();
proto_festival_filter->set_dragonbash(value.dragonbash);
proto_festival_filter->set_festival_of_the_four_winds(value.festival_of_the_four_winds);
proto_festival_filter->set_halloween(value.halloween);
proto_festival_filter->set_lunar_new_year(value.lunar_new_year);
proto_festival_filter->set_super_adventure_festival(value.super_adventure_festival);
proto_festival_filter->set_wintersday(value.wintersday);
proto_festival_filter->set_none(value.none);
setter(proto_festival_filter);
}
Loading

0 comments on commit 29de4fb

Please sign in to comment.