diff --git a/tesseract_common/include/tesseract_common/serialization.h b/tesseract_common/include/tesseract_common/serialization.h index 5c1becbcad4..bd954f86c6a 100644 --- a/tesseract_common/include/tesseract_common/serialization.h +++ b/tesseract_common/include/tesseract_common/serialization.h @@ -113,7 +113,7 @@ struct Serialization { fs::path fp(file_path); if (!fp.has_extension()) - fp = fs::path(file_path + "." + serialization::xml::extension::value); + fp = fs::path(file_path + serialization::xml::extension::value); std::ofstream os(fp.string()); { // Must be scoped because all data is not written until the oost::archive::xml_oarchive goes out of scope @@ -138,7 +138,7 @@ struct Serialization { fs::path fp(file_path); if (!fp.has_extension()) - fp = fs::path(file_path + "." + serialization::binary::extension::value); + fp = fs::path(file_path + serialization::binary::extension::value); std::ofstream os(fp.string(), std::ios_base::binary); { // Must be scoped because all data is not written until the oost::archive::xml_oarchive goes out of scope @@ -222,6 +222,16 @@ struct Serialization return archive_type; } + template + static SerializableType fromArchiveFile(const std::string& file_path) + { + fs::path fp(file_path); + if (fp.extension() == serialization::binary::extension::value) + return fromArchiveFileBinary(file_path); + + return fromArchiveFileXML(file_path); + } + template static SerializableType fromArchiveBinaryData(const std::vector& archive_binary) { diff --git a/tesseract_common/include/tesseract_common/serialization_extensions.h b/tesseract_common/include/tesseract_common/serialization_extensions.h index b15451c92f4..1d93616feb9 100644 --- a/tesseract_common/include/tesseract_common/serialization_extensions.h +++ b/tesseract_common/include/tesseract_common/serialization_extensions.h @@ -47,8 +47,9 @@ struct extension using extension_type = typename U::extenstion; }; - using extension_type = typename boost::mpl:: - eval_if, traits_class_extension, boost::mpl::string<'t', 'r', 's', 'x'>>::type; + using extension_type = typename boost::mpl::eval_if, + traits_class_extension, + boost::mpl::string<'.', 't', 'r', 's', 'x'>>::type; static constexpr const char* value = boost::mpl::c_str::value; }; @@ -65,8 +66,9 @@ struct extension using extension_type = typename U::extenstion; }; - using extension_type = typename boost::mpl:: - eval_if, traits_class_extension, boost::mpl::string<'t', 'r', 's', 'b'>>::type; + using extension_type = typename boost::mpl::eval_if, + traits_class_extension, + boost::mpl::string<'.', 't', 'r', 's', 'b'>>::type; static constexpr const char* value = boost::mpl::c_str::value; }; @@ -75,6 +77,7 @@ struct extension /** * @brief A macro for defining serialization extension for classes + * @details The extension should include the '.', for example .yaml * @param T the class to define extensions for * @param X the xml serialziation extension for the provided class * @param B the binary serialzation extension for the provided class @@ -88,6 +91,7 @@ struct extension struct extension \ { \ static constexpr const char* value = X; \ + static_assert(value[0] == '.', "XML extension value must start with a '.'"); \ }; \ } \ namespace serialization::binary \ @@ -96,6 +100,7 @@ struct extension struct extension \ { \ static constexpr const char* value = B; \ + static_assert(value[0] == '.', "Binary extension value must start with a '.'"); \ }; \ } \ } diff --git a/tesseract_common/test/tesseract_common_serialization_unit.cpp b/tesseract_common/test/tesseract_common_serialization_unit.cpp index 82a551cb19e..c9e7ebc0c52 100644 --- a/tesseract_common/test/tesseract_common_serialization_unit.cpp +++ b/tesseract_common/test/tesseract_common_serialization_unit.cpp @@ -738,7 +738,7 @@ struct ExtensionMacroTestA double a{ 0 }; }; -TESSERACT_CLASS_EXTENSION(ExtensionMacroTestA, "etax", "etab") +TESSERACT_CLASS_EXTENSION(ExtensionMacroTestA, ".etax", ".etab") struct ExtensionMacroTestB { @@ -748,19 +748,19 @@ struct ExtensionMacroTestB TEST(TesseractCommonSerializeUnit, ExtensionXmlMacro) // NOLINT { std::string ext = tesseract_common::serialization::xml::extension::value; - EXPECT_EQ(ext, "etax"); + EXPECT_EQ(ext, ".etax"); std::string default_ext = tesseract_common::serialization::xml::extension::value; - EXPECT_EQ(default_ext, "trsx"); + EXPECT_EQ(default_ext, ".trsx"); } TEST(TesseractCommonSerializeUnit, ExtensionBinaryMacro) // NOLINT { std::string ext = tesseract_common::serialization::binary::extension::value; - EXPECT_EQ(ext, "etab"); + EXPECT_EQ(ext, ".etab"); std::string default_ext = tesseract_common::serialization::binary::extension::value; - EXPECT_EQ(default_ext, "trsb"); + EXPECT_EQ(default_ext, ".trsb"); } int main(int argc, char** argv)