get horizontal and vertical alignment up to spec, handle enum serialization more correctly for #90

This commit is contained in:
Thomas Fussell 2016-12-10 18:22:26 +00:00
parent 888f081fb7
commit d0ef4333b4
5 changed files with 91 additions and 79 deletions

View File

@ -54,7 +54,7 @@ enum class XLNT_API relationship_type
{
unknown,
// Package
// Package parts
core_properties,
extended_properties,
custom_properties,
@ -62,7 +62,7 @@ enum class XLNT_API relationship_type
thumbnail,
printer_settings,
// SpreadsheetML
// SpreadsheetML parts
calculation_chain,
chartsheet,
comments,
@ -90,6 +90,7 @@ enum class XLNT_API relationship_type
volatile_dependencies,
worksheet,
// Worksheet parts
hyperlink,
image
};

View File

@ -33,13 +33,14 @@ namespace xlnt {
/// </summary>
enum class XLNT_API horizontal_alignment
{
none,
general,
left,
right,
center,
right,
fill,
justify,
center_continuous,
justify
distributed
};
} // namespace xlnt

View File

@ -33,11 +33,11 @@ namespace xlnt {
/// </summary>
enum class XLNT_API vertical_alignment
{
none,
bottom,
top,
center,
justify
bottom,
justify,
distributed
};
} // namespace xlnt

View File

@ -15,10 +15,9 @@ std::string to_string(font::underline_style style)
case font::underline_style::single: return "single";
case font::underline_style::single_accounting: return "singleAccounting";
case font::underline_style::none: return "none";
#ifdef WIN32
default: throw xlnt::exception("unhandled");
#endif
}
default_case("single");
}
@ -99,11 +98,9 @@ std::string to_string(relationship_type t)
return "volatile-dependencies";
case relationship_type::image:
return "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image";
#ifdef WIN32
default:
throw xlnt::exception("unhandled");
#endif
}
default_case("unknown");
}
std::string to_string(pattern_fill_type fill_type)
@ -129,10 +126,9 @@ std::string to_string(pattern_fill_type fill_type)
case pattern_fill_type::mediumgray: return "mediumgray";
case pattern_fill_type::solid: return "solid";
case pattern_fill_type::none: return "none";
#ifdef WIN32
default: throw xlnt::exception("unhandled");
#endif
}
default_case("none");
}
std::string to_string(gradient_fill_type fill_type)
@ -158,42 +154,40 @@ std::string to_string(border_style style)
case border_style::thick: return "thick";
case border_style::thin: return "thin";
case border_style::none: return "none";
#ifdef WIN32
default: throw xlnt::exception("unhandled");
#endif
}
default_case("none");
}
std::string to_string(vertical_alignment alignment)
{
switch (alignment)
{
case vertical_alignment::bottom: return "bottom";
case vertical_alignment::center: return "center";
case vertical_alignment::justify: return "justify";
case vertical_alignment::top: return "top";
case vertical_alignment::none: return "none";
#ifdef WIN32
default: throw xlnt::exception("unhandled");
#endif
case vertical_alignment::center: return "center";
case vertical_alignment::bottom: return "bottom";
case vertical_alignment::justify: return "justify";
case vertical_alignment::distributed: return "distributed";
}
default_case("top");
}
std::string to_string(horizontal_alignment alignment)
{
switch (alignment)
{
case horizontal_alignment::center: return "center";
case horizontal_alignment::center_continuous: return "center-continous";
case horizontal_alignment::general: return "general";
case horizontal_alignment::justify: return "justify";
case horizontal_alignment::left: return "left";
case horizontal_alignment::center: return "center";
case horizontal_alignment::right: return "right";
case horizontal_alignment::none: return "none";
#ifdef WIN32
default: throw xlnt::exception("unhandled");
#endif
case horizontal_alignment::fill: return "fill";
case horizontal_alignment::justify: return "justify";
case horizontal_alignment::center_continuous: return "centerContinuous";
case horizontal_alignment::distributed: return "distributed";
}
default_case("general");
}
std::string to_string(border_side side)
@ -207,10 +201,9 @@ std::string to_string(border_side side)
case border_side::horizontal: return "horizontal";
case border_side::vertical: return "vertical";
case border_side::diagonal: return "diagonal";
#ifdef WIN32
default: throw xlnt::exception("unhandled");
#endif
}
default_case("top");
}
} // namespace detail

View File

@ -14,14 +14,13 @@
namespace xlnt {
namespace detail {
template<typename T>
T default_case(T default_return_value)
{
#define EXCEPT_ON_UNHANDLED_SWITCH_CASE
#ifdef EXCEPT_ON_UNHANDLED_SWITCH_CASE
throw unhandled_switch_case();
#define default_case(default_value) throw xlnt::exception("unhandled case");
#else
#define default_case(default_value) return default_value;
#endif
return default_return_value;
}
/// <summary>
/// Returns the string representation of the underline style.
@ -74,7 +73,7 @@ struct value_traits<xlnt::font::underline_style>
}
}
return xlnt::detail::default_case(font::underline_style::none);
default_case(font::underline_style::none);
}
static std::string serialize(xlnt::font::underline_style underline_style, const serializer &)
@ -92,43 +91,54 @@ struct value_traits<xlnt::relationship_type>
static auto *relationship_types = new std::vector<relationship_type>
{
// Package parts
relationship_type::core_properties,
relationship_type::extended_properties,
relationship_type::custom_properties,
relationship_type::office_document,
relationship_type::thumbnail,
relationship_type::printer_settings,
// SpreadsheetML parts
relationship_type::calculation_chain,
relationship_type::chartsheet,
relationship_type::comments,
relationship_type::connections,
relationship_type::core_properties,
relationship_type::custom_properties,
relationship_type::custom_property,
relationship_type::custom_xml_mappings,
relationship_type::dialogsheet,
relationship_type::drawings,
relationship_type::extended_properties,
relationship_type::external_workbook_references,
relationship_type::hyperlink,
relationship_type::image,
relationship_type::metadata,
relationship_type::office_document,
relationship_type::pivot_table,
relationship_type::pivot_table_cache_definition,
relationship_type::pivot_table_cache_records,
relationship_type::printer_settings,
relationship_type::query_table,
relationship_type::revision_log,
relationship_type::shared_string_table,
relationship_type::shared_workbook,
relationship_type::shared_workbook_revision_headers,
relationship_type::shared_workbook,
relationship_type::theme,
relationship_type::revision_log,
relationship_type::shared_workbook_user_data,
relationship_type::single_cell_table_definitions,
relationship_type::stylesheet,
relationship_type::table_definition,
relationship_type::theme,
relationship_type::thumbnail,
relationship_type::unknown,
relationship_type::vml_drawing,
relationship_type::volatile_dependencies,
relationship_type::worksheet
relationship_type::worksheet,
// Worksheet parts
relationship_type::hyperlink,
relationship_type::image
};
// Core properties relationships can have two different type strings
if (relationship_type_string
== "http://schemas.openxmlformats.org/officedocument/2006/relationships/metadata/core-properties")
{
return relationship_type::core_properties;
}
for (auto type : *relationship_types)
{
if (xlnt::detail::to_string(type) == relationship_type_string)
@ -137,7 +147,7 @@ struct value_traits<xlnt::relationship_type>
}
}
return xlnt::detail::default_case(relationship_type::unknown);
default_case(relationship_type::unknown);
}
static std::string serialize(xlnt::relationship_type type, const serializer &)
@ -184,7 +194,7 @@ struct value_traits<xlnt::pattern_fill_type>
}
}
return xlnt::detail::default_case(pattern_fill_type::none);
default_case(pattern_fill_type::none);
}
static std::string serialize(xlnt::pattern_fill_type fill_type, const serializer &)
@ -214,7 +224,7 @@ struct value_traits<xlnt::gradient_fill_type>
}
}
return xlnt::detail::default_case(gradient_fill_type::linear);
default_case(gradient_fill_type::linear);
}
static std::string serialize(xlnt::gradient_fill_type fill_type, const serializer &)
@ -256,7 +266,7 @@ struct value_traits<xlnt::border_style>
}
}
return xlnt::detail::default_case(border_style::none);
default_case(border_style::none);
}
static std::string
@ -275,11 +285,11 @@ struct value_traits<xlnt::vertical_alignment>
static auto *vertical_alignments = new std::vector<vertical_alignment>
{
vertical_alignment::bottom,
vertical_alignment::top,
vertical_alignment::center,
vertical_alignment::bottom,
vertical_alignment::justify,
vertical_alignment::none,
vertical_alignment::top
vertical_alignment::distributed
};
for (auto alignment : *vertical_alignments)
@ -290,7 +300,7 @@ struct value_traits<xlnt::vertical_alignment>
}
}
return xlnt::detail::default_case(vertical_alignment::none);
default_case(vertical_alignment::none);
}
static std::string serialize (xlnt::vertical_alignment alignment, const serializer &)
@ -308,13 +318,14 @@ struct value_traits<xlnt::horizontal_alignment>
static auto *horizontal_alignments = new std::vector<horizontal_alignment>
{
horizontal_alignment::center,
horizontal_alignment::center_continuous,
horizontal_alignment::general,
horizontal_alignment::justify,
horizontal_alignment::left,
horizontal_alignment::center,
horizontal_alignment::right,
horizontal_alignment::none
horizontal_alignment::fill,
horizontal_alignment::justify,
horizontal_alignment::center_continuous,
horizontal_alignment::distributed
};
for (auto alignment : *horizontal_alignments)
@ -325,7 +336,7 @@ struct value_traits<xlnt::horizontal_alignment>
}
}
return xlnt::detail::default_case(horizontal_alignment::none);
default_case(horizontal_alignment::none);
}
static std::string serialize(xlnt::horizontal_alignment alignment, const serializer &)
@ -359,11 +370,11 @@ struct value_traits<xlnt::border_side>
return side;
}
}
return border_side::top;
default_case(xlnt::border_side::top);
}
static std::string serialize (xlnt::border_side side, const serializer &)
static std::string serialize(xlnt::border_side side, const serializer &)
{
return xlnt::detail::to_string(side);
}
@ -377,7 +388,7 @@ struct value_traits<xlnt::target_mode>
return mode_string == "Internal" ? xlnt::target_mode::internal : xlnt::target_mode::external;
}
static std::string serialize (xlnt::target_mode mode, const serializer &)
static std::string serialize(xlnt::target_mode mode, const serializer &)
{
return mode == xlnt::target_mode::internal ? "Internal" : "External";
}
@ -391,10 +402,11 @@ struct value_traits<xlnt::pane_state>
if (string == "frozen") return xlnt::pane_state::frozen;
else if (string == "frozenSplit") return xlnt::pane_state::frozen_split;
else if (string == "split") return xlnt::pane_state::split;
return xlnt::pane_state::split;
default_case(xlnt::pane_state::frozen);
}
static std::string serialize (xlnt::pane_state state, const serializer &)
static std::string serialize(xlnt::pane_state state, const serializer &)
{
switch (state)
{
@ -402,6 +414,8 @@ struct value_traits<xlnt::pane_state>
case xlnt::pane_state::frozen_split: return "frozenSplit";
case xlnt::pane_state::split: return "split";
}
default_case("frozen");
}
}; // struct value_traits<xlnt::pane_state>
@ -414,10 +428,11 @@ struct value_traits<xlnt::pane_corner>
else if (string == "bottomRight") return xlnt::pane_corner::bottom_right;
else if (string == "topLeft") return xlnt::pane_corner::top_left;
else if (string == "topRight") return xlnt::pane_corner::top_right;
return xlnt::pane_corner::top_left;
default_case(xlnt::pane_corner::top_left);
}
static std::string serialize (xlnt::pane_corner corner, const serializer &)
static std::string serialize(xlnt::pane_corner corner, const serializer &)
{
switch (corner)
{
@ -426,6 +441,8 @@ struct value_traits<xlnt::pane_corner>
case xlnt::pane_corner::top_left: return "topLeft";
case xlnt::pane_corner::top_right: return "topRight";
}
default_case("topLeft");
}
}; // struct value_traits<xlnt::pane_corner>