Skip to content

Commit

Permalink
Merge pull request #189 from AsherGlick/pointer_based_export_import_2
Browse files Browse the repository at this point in the history
Migrating xml attribute reading code
  • Loading branch information
AsherGlick authored Oct 31, 2023
2 parents 3316de7 + 2f66447 commit 73ae206
Show file tree
Hide file tree
Showing 46 changed files with 383 additions and 314 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@
}
};
{% endif %}
{{class_name}} parse_{{attribute_name}}(rapidxml::xml_attribute<>* input, std::vector<XMLError*>* errors);
void xml_attribute_to_{{attribute_name}}(
rapidxml::xml_attribute<>* input,
std::vector<XMLError*>* errors,
{{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":%}
waypoint::{{class_name}} to_proto_{{attribute_name}}({{class_name}} attribute_value);
Expand Down
4 changes: 2 additions & 2 deletions xml_converter/generators/cpp_templates/class_template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ bool {{cpp_class}}::init_xml_attribute(rapidxml::xml_attribute<>* attribute, vec
{% for n, attribute_variable in enumerate(attribute_variables) %}
{% for i, value in enumerate(attribute_variable.xml_fields) %}
{{ "if" if i == n == 0 else "else if" }} (attributename == "{{value}}") {
this->{{attribute_variable.attribute_name}} = parse_{{attribute_variable.class_name}}({{", ".join(attribute_variable.args)}});
this->{{attribute_variable.attribute_flag_name}} = true;
xml_attribute_to_{{attribute_variable.class_name}}({{", ".join(attribute_variable.args)}}, &(this->{{attribute_variable.attribute_name}}), &(this->{{attribute_variable.attribute_flag_name}}));
{% for side_effect in attribute_variable.side_effects %}
// TOOD: this side effects should be passed into the parse function
this->{{side_effect}} = this->{{attribute_variable.class_name}}.side_effect_{{side_effect}};
this->{{side_effect}}_is_set = true;
{% endfor %}
Expand Down
9 changes: 7 additions & 2 deletions xml_converter/generators/cpp_templates/compoundvalue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@

using namespace std;

{{class_name}} parse_{{attribute_name}}(rapidxml::xml_attribute<>* input, vector<XMLError*>*) {
void xml_attribute_to_{{attribute_name}}(
rapidxml::xml_attribute<>* input,
std::vector<XMLError*>* errors,
{{class_name}}* value,
bool* is_set) {
{{class_name}} {{attribute_name}};
vector<string> compound_values;
string attributename;
Expand All @@ -25,7 +29,8 @@ using namespace std;
{{attribute_name}}.{{attribute_variable.attribute_name}} = std::stof(compound_values[{{n}}]);
{% endfor %}
}
return {{attribute_name}};
*value = {{attribute_name}};
*is_set = true;
}
{% if xml_bundled_components != [] %}
string {{attribute_name}}_to_xml_attribute(const std::string& attribute_name, const {{class_name}}* value) {
Expand Down
9 changes: 7 additions & 2 deletions xml_converter/generators/cpp_templates/enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@

using namespace std;

{{class_name}} parse_{{attribute_name}}(rapidxml::xml_attribute<>* input, vector<XMLError*>* errors) {
void xml_attribute_to_{{attribute_name}}(
rapidxml::xml_attribute<>* input,
std::vector<XMLError*>* errors,
{{class_name}}* value,
bool* is_set) {
{{class_name}} {{attribute_name}};
string normalized_value = normalize(get_attribute_value(input));
{% for n, attribute_variable in enumerate(attribute_variables) %}
Expand All @@ -32,7 +36,8 @@ using namespace std;
errors->push_back(new XMLAttributeValueError("Found an invalid value that was not in the Enum {{class_name}}", input));
{{attribute_name}} = {{class_name}}::{{attribute_variables[0].attribute_name}};
}
return {{attribute_name}};
*value = {{attribute_name}};
*is_set = true;
}

string {{attribute_name}}_to_xml_attribute(const std::string& attribute_name, const {{class_name}}* value) {
Expand Down
9 changes: 7 additions & 2 deletions xml_converter/generators/cpp_templates/multiflagvalue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@

using namespace std;

{{class_name}} parse_{{attribute_name}}(rapidxml::xml_attribute<>* input, vector<XMLError*>* errors) {
void xml_attribute_to_{{attribute_name}}(
rapidxml::xml_attribute<>* input,
std::vector<XMLError*>* errors,
{{class_name}}* value,
bool* is_set) {
{{class_name}} {{attribute_name}};
vector<string> flag_values;
flag_values = split(get_attribute_value(input), ",");
Expand Down Expand Up @@ -40,7 +44,8 @@ using namespace std;
continue;
}
}
return {{attribute_name}};
*value = {{attribute_name}};
*is_set = true;
}

string {{attribute_name}}_to_xml_attribute(const std::string& attribute_name, const {{class_name}}* value) {
Expand Down
13 changes: 9 additions & 4 deletions xml_converter/src/attribute/bool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,21 @@ using namespace std;
// evaluated as `true`. 'false' or '0' are evaluated as `false`. Everything is
// is also evaluated as false but appends an error to the errors vector.
////////////////////////////////////////////////////////////////////////////////
bool parse_bool(rapidxml::xml_attribute<>* input, vector<XMLError*>* errors) {
void xml_attribute_to_bool(
rapidxml::xml_attribute<>* input,
std::vector<XMLError*>* errors,
bool* value,
bool* is_set) {
if (get_attribute_value(input) == "0" || get_attribute_value(input) == "false") {
return false;
*value = false;
*is_set = true;
}
else if (get_attribute_value(input) == "1" || get_attribute_value(input) == "true") {
return true;
*value = true;
*is_set = true;
}
else {
errors->push_back(new XMLAttributeValueError("Found a boolean value that was not a '1', '0', 'true', or 'false'", input));
return false;
}
}

Expand Down
6 changes: 5 additions & 1 deletion xml_converter/src/attribute/bool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

class XMLError;

bool parse_bool(rapidxml::xml_attribute<>* input, std::vector<XMLError*>* errors);
void xml_attribute_to_bool(
rapidxml::xml_attribute<>* input,
std::vector<XMLError*>* errors,
bool* value,
bool* is_set);

std::string bool_to_xml_attribute(const std::string& attribute_name, const bool* value);

Expand Down
9 changes: 7 additions & 2 deletions xml_converter/src/attribute/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ int convert_color_channel_float_to_int(float input) {
//
// Parses a Color from the value of a rapidxml::xml_attribute.
////////////////////////////////////////////////////////////////////////////////
Color parse_color(rapidxml::xml_attribute<>* input, vector<XMLError*>* errors) {
void xml_attribute_to_color(
rapidxml::xml_attribute<>* input,
std::vector<XMLError*>* errors,
Color* value,
bool* is_set) {
Color color;
std::string input_string = get_attribute_value(input);
std::string hex_string;
Expand Down Expand Up @@ -82,7 +86,8 @@ Color parse_color(rapidxml::xml_attribute<>* input, vector<XMLError*>* errors) {
else {
errors->push_back(new XMLAttributeValueError("Found a color value not in hex format", input));
}
return color;
*value = color;
*is_set = true;
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
6 changes: 5 additions & 1 deletion xml_converter/src/attribute/color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ class Color {
float alpha;
};

Color parse_color(rapidxml::xml_attribute<>* input, std::vector<XMLError*>* errors);
void xml_attribute_to_color(
rapidxml::xml_attribute<>* input,
std::vector<XMLError*>* errors,
Color* value,
bool* is_set);

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

Expand Down
9 changes: 7 additions & 2 deletions xml_converter/src/attribute/cull_chirality_gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@

using namespace std;

CullChirality parse_cull_chirality(rapidxml::xml_attribute<>* input, vector<XMLError*>* errors) {
void xml_attribute_to_cull_chirality(
rapidxml::xml_attribute<>* input,
std::vector<XMLError*>* errors,
CullChirality* value,
bool* is_set) {
CullChirality cull_chirality;
string normalized_value = normalize(get_attribute_value(input));
if (normalized_value == "none") {
Expand All @@ -28,7 +32,8 @@ CullChirality parse_cull_chirality(rapidxml::xml_attribute<>* input, vector<XMLE
errors->push_back(new XMLAttributeValueError("Found an invalid value that was not in the Enum CullChirality", input));
cull_chirality = CullChirality::none;
}
return cull_chirality;
*value = cull_chirality;
*is_set = true;
}

string cull_chirality_to_xml_attribute(const std::string& attribute_name, const CullChirality* value) {
Expand Down
6 changes: 5 additions & 1 deletion xml_converter/src/attribute/cull_chirality_gen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ enum CullChirality {
counter_clockwise,
none,
};
CullChirality parse_cull_chirality(rapidxml::xml_attribute<>* input, std::vector<XMLError*>* errors);
void xml_attribute_to_cull_chirality(
rapidxml::xml_attribute<>* input,
std::vector<XMLError*>* errors,
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);
9 changes: 7 additions & 2 deletions xml_converter/src/attribute/euler_rotation_gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@

using namespace std;

EulerRotation parse_euler_rotation(rapidxml::xml_attribute<>* input, vector<XMLError*>*) {
void xml_attribute_to_euler_rotation(
rapidxml::xml_attribute<>* input,
std::vector<XMLError*>* errors,
EulerRotation* value,
bool* is_set) {
EulerRotation euler_rotation;
vector<string> compound_values;
string attributename;
Expand All @@ -25,7 +29,8 @@ EulerRotation parse_euler_rotation(rapidxml::xml_attribute<>* input, vector<XMLE
euler_rotation.y_rotation = std::stof(compound_values[1]);
euler_rotation.z_rotation = std::stof(compound_values[2]);
}
return euler_rotation;
*value = euler_rotation;
*is_set = true;
}
string euler_rotation_to_xml_attribute(const std::string& attribute_name, const EulerRotation* value) {
string output;
Expand Down
6 changes: 5 additions & 1 deletion xml_converter/src/attribute/euler_rotation_gen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ class EulerRotation {
return "EulerRotation";
}
};
EulerRotation parse_euler_rotation(rapidxml::xml_attribute<>* input, std::vector<XMLError*>* errors);
void xml_attribute_to_euler_rotation(
rapidxml::xml_attribute<>* input,
std::vector<XMLError*>* errors,
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);
9 changes: 7 additions & 2 deletions xml_converter/src/attribute/festival_filter_gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@

using namespace std;

FestivalFilter parse_festival_filter(rapidxml::xml_attribute<>* input, vector<XMLError*>* errors) {
void xml_attribute_to_festival_filter(
rapidxml::xml_attribute<>* input,
std::vector<XMLError*>* errors,
FestivalFilter* value,
bool* is_set) {
FestivalFilter festival_filter;
vector<string> flag_values;
flag_values = split(get_attribute_value(input), ",");
Expand Down Expand Up @@ -55,7 +59,8 @@ FestivalFilter parse_festival_filter(rapidxml::xml_attribute<>* input, vector<XM
continue;
}
}
return festival_filter;
*value = festival_filter;
*is_set = true;
}

string festival_filter_to_xml_attribute(const std::string& attribute_name, const FestivalFilter* value) {
Expand Down
6 changes: 5 additions & 1 deletion xml_converter/src/attribute/festival_filter_gen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ class FestivalFilter {
return "FestivalFilter";
}
};
FestivalFilter parse_festival_filter(rapidxml::xml_attribute<>* input, std::vector<XMLError*>* errors);
void xml_attribute_to_festival_filter(
rapidxml::xml_attribute<>* input,
std::vector<XMLError*>* errors,
FestivalFilter* value,
bool* is_set);
std::string festival_filter_to_xml_attribute(const std::string& attribute_name, const FestivalFilter* value);
waypoint::FestivalFilter* to_proto_festival_filter(FestivalFilter attribute_value);
FestivalFilter from_proto_festival_filter(waypoint::FestivalFilter proto_festival_filter);
9 changes: 7 additions & 2 deletions xml_converter/src/attribute/float.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ using namespace std;
//
// Parses a float from the value of a rapidxml::xml_attribute.
////////////////////////////////////////////////////////////////////////////////
float parse_float(rapidxml::xml_attribute<>* input, std::vector<XMLError*>*) {
return std::stof(get_attribute_value(input));
void xml_attribute_to_float(
rapidxml::xml_attribute<>* input,
std::vector<XMLError*>* errors,
float* value,
bool* is_set) {
*value = std::stof(get_attribute_value(input));
*is_set = true;
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
6 changes: 5 additions & 1 deletion xml_converter/src/attribute/float.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

class XMLError;

float parse_float(rapidxml::xml_attribute<>* input, std::vector<XMLError*>* errors);
void xml_attribute_to_float(
rapidxml::xml_attribute<>* input,
std::vector<XMLError*>* errors,
float* value,
bool* is_set);

std::string float_to_xml_attribute(const std::string& attribute_name, const float* value);

Expand Down
11 changes: 7 additions & 4 deletions xml_converter/src/attribute/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ using namespace std;
//
// Parses the path to an image from the value of a rapidxml::xml_attribute.
////////////////////////////////////////////////////////////////////////////////
Image parse_image(rapidxml::xml_attribute<>* input, vector<XMLError*>*) {
Image image;
image.path = get_attribute_value(input);
return image;
void xml_attribute_to_image(
rapidxml::xml_attribute<>* input,
std::vector<XMLError*>* errors,
Image* value,
bool* is_set) {
value->path = get_attribute_value(input);
*is_set = true;
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
6 changes: 5 additions & 1 deletion xml_converter/src/attribute/image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ class Image {
std::string path;
};

Image parse_image(rapidxml::xml_attribute<>* input, std::vector<XMLError*>* errors);
void xml_attribute_to_image(
rapidxml::xml_attribute<>* input,
std::vector<XMLError*>* errors,
Image* value,
bool* is_set);

std::string image_to_xml_attribute(const std::string& attribute_name, const Image* value);

Expand Down
12 changes: 8 additions & 4 deletions xml_converter/src/attribute/int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@
using namespace std;

////////////////////////////////////////////////////////////////////////////////
// parse_int
// xml_attribute_to_int
//
// Parses an int from the value of a rapidxml::xml_attribute. Adds an error
// if the value cannot be parsed properly.
////////////////////////////////////////////////////////////////////////////////
int parse_int(rapidxml::xml_attribute<>* input, vector<XMLError*>* errors) {
void xml_attribute_to_int(
rapidxml::xml_attribute<>* input,
std::vector<XMLError*>* errors,
int* value,
bool* is_set) {
try {
return stoi(get_attribute_value(input));
*value = stoi(get_attribute_value(input));
*is_set = true;
}
catch (std::invalid_argument const& exception) {
errors->push_back(new XMLAttributeValueError("Invalid integer value", input));
return 0;
}
// TODO(#99): Do we need an out_of_range exception catch when parsing integers?
// catch(std::out_of_range const& exception) {
Expand Down
6 changes: 5 additions & 1 deletion xml_converter/src/attribute/int.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

class XMLError;

int parse_int(rapidxml::xml_attribute<>* input, std::vector<XMLError*>* errors);
void xml_attribute_to_int(
rapidxml::xml_attribute<>* input,
std::vector<XMLError*>* errors,
int* value,
bool* is_set);

std::string int_to_xml_attribute(const std::string& attribute_name, const int* value);

Expand Down
9 changes: 7 additions & 2 deletions xml_converter/src/attribute/map_type_filter_gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@

using namespace std;

MapTypeFilter parse_map_type_filter(rapidxml::xml_attribute<>* input, vector<XMLError*>* errors) {
void xml_attribute_to_map_type_filter(
rapidxml::xml_attribute<>* input,
std::vector<XMLError*>* errors,
MapTypeFilter* value,
bool* is_set) {
MapTypeFilter map_type_filter;
vector<string> flag_values;
flag_values = split(get_attribute_value(input), ",");
Expand Down Expand Up @@ -120,7 +124,8 @@ MapTypeFilter parse_map_type_filter(rapidxml::xml_attribute<>* input, vector<XML
continue;
}
}
return map_type_filter;
*value = map_type_filter;
*is_set = true;
}

string map_type_filter_to_xml_attribute(const std::string& attribute_name, const MapTypeFilter* value) {
Expand Down
Loading

0 comments on commit 73ae206

Please sign in to comment.