From 41a506688a00ca1730b14cc4152ad7c7db735751 Mon Sep 17 00:00:00 2001 From: Filip Sykala Date: Tue, 8 Mar 2022 08:35:26 +0100 Subject: [PATCH] Change 2 map to bimap in TextConfigurationSerialization --- src/libslic3r/Format/3mf.cpp | 44 ++++++++++++++++---------- tests/libslic3r/libslic3r_tests.cpp | 48 +++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 16 deletions(-) diff --git a/src/libslic3r/Format/3mf.cpp b/src/libslic3r/Format/3mf.cpp index 745d1310ae..7535553d93 100644 --- a/src/libslic3r/Format/3mf.cpp +++ b/src/libslic3r/Format/3mf.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -26,6 +27,9 @@ #include #include +#include +#include + #include #include #include @@ -36,7 +40,6 @@ namespace pt = boost::property_tree; #include "miniz_extension.hpp" #include "TextConfiguration.hpp" -#include "MapUtils.hpp" #include @@ -1812,12 +1815,23 @@ namespace Slic3r { { public: TextConfigurationSerialization() = delete; - static const std::map to_string; - static const std::map to_type; - static FontItem::Type get_type(const std::string &type_string) { - auto it = to_type.find(type_string); - return (it != to_type.end()) ? it->second : FontItem::Type::undefined; + static const boost::bimap type_to_name; + + static FontItem::Type get_type(std::string_view type) { + const auto& to_type = TextConfigurationSerialization::type_to_name.right; + auto type_item = to_type.find(type); + assert(type_item != to_type.end()); + if (type_item == to_type.end()) return FontItem::Type::undefined; + return type_item->second; + } + + static std::string_view get_name(FontItem::Type type) { + const auto& to_name = TextConfigurationSerialization::type_to_name.left; + auto type_name = to_name.find(type); + assert(type_name != to_name.end()); + if (type_name == to_name.end()) return "unknown type"; + return type_name->second; } static void to_xml(std::stringstream &stream, const TextConfiguration &tc); @@ -3294,15 +3308,13 @@ bool store_3mf(const char* path, Model* model, const DynamicPrintConfig* config, /// /// TextConfiguration serialization /// -const std::map TextConfigurationSerialization::to_string = { - {FontItem::Type::file_path, "file_name"}, - {FontItem::Type::wx_win_font_descr, "wxFontDescriptor_Windows"}, - {FontItem::Type::wx_lin_font_descr, "wxFontDescriptor_Linux"}, - {FontItem::Type::wx_mac_font_descr, "wxFontDescriptor_MacOsX"} -}; - -const std::map TextConfigurationSerialization::to_type = -MapUtils::create_oposit(TextConfigurationSerialization::to_string); +using TypeToName = boost::bimap; +const TypeToName TextConfigurationSerialization::type_to_name = + boost::assign::list_of + (FontItem::Type::file_path, "file_name") + (FontItem::Type::wx_win_font_descr, "wxFontDescriptor_Windows") + (FontItem::Type::wx_lin_font_descr, "wxFontDescriptor_Linux") + (FontItem::Type::wx_mac_font_descr, "wxFontDescriptor_MacOsX"); void TextConfigurationSerialization::to_xml(std::stringstream &stream, const TextConfiguration &tc) { @@ -3312,7 +3324,7 @@ void TextConfigurationSerialization::to_xml(std::stringstream &stream, const Tex // font item const FontItem &fi = tc.font_item; stream << FONT_DESCRIPTOR_ATTR << "=\"" << xml_escape_double_quotes_attribute_value(fi.path) << "\" "; - stream << FONT_DESCRIPTOR_TYPE_ATTR << "=\"" << TextConfigurationSerialization::to_string.at(fi.type) << "\" "; + stream << FONT_DESCRIPTOR_TYPE_ATTR << "=\"" << TextConfigurationSerialization::get_name(fi.type) << "\" "; // font property const FontProp &fp = tc.font_item.prop; diff --git a/tests/libslic3r/libslic3r_tests.cpp b/tests/libslic3r/libslic3r_tests.cpp index f4dcab42a0..a97667dcca 100644 --- a/tests/libslic3r/libslic3r_tests.cpp +++ b/tests/libslic3r/libslic3r_tests.cpp @@ -2,6 +2,12 @@ #include "libslic3r/Utils.hpp" +// bimap test +#include +#include +#include + + namespace { TEST_CASE("sort_remove_duplicates", "[utils]") { @@ -38,4 +44,46 @@ TEST_CASE("string_printf", "[utils]") { } } +TEST_CASE("Bimap duplicity behavior") { + enum class number { + one = 1, + three = 3, + tri = 3 // ONLY alias + }; + + using BimapType = boost::bimap; + BimapType bimap = boost::assign::list_of + ("one", number::one) + ("three", number::three) + ("tri", number::tri) // no matter if it is there + ; + + const auto& to_type = bimap.left; + + auto item_number1 = to_type.find("one"); + REQUIRE(item_number1 != to_type.end()); + CHECK(item_number1->second == number::one); + + auto item_number3 = to_type.find("three"); + REQUIRE(item_number3 != to_type.end()); + CHECK(item_number3->second == number::three); + + // to_type.find("tri"); // not in map + + const auto &to_name = bimap.right; + + auto it1 = to_name.find(number::one); + REQUIRE(it1 != to_name.end()); + CHECK(it1->second == "one"); + + auto it2 = to_name.find(number::three); + REQUIRE(it2 != to_name.end()); + CHECK(it2->second == "three"); + + auto it3 = to_name.find(number::tri); + REQUIRE(it3 != to_name.end()); + REQUIRE(number::three == number::tri); + CHECK(it3->second == "three"); } + +} // end namespace