Skip to content

Commit

Permalink
Fix serialization extension marco so they must include . char
Browse files Browse the repository at this point in the history
  • Loading branch information
Levi-Armstrong committed Oct 11, 2024
1 parent 6305a18 commit 4ef61aa
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
14 changes: 12 additions & 2 deletions tesseract_common/include/tesseract_common/serialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ struct Serialization
{
fs::path fp(file_path);
if (!fp.has_extension())
fp = fs::path(file_path + "." + serialization::xml::extension<SerializableType>::value);
fp = fs::path(file_path + serialization::xml::extension<SerializableType>::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
Expand All @@ -138,7 +138,7 @@ struct Serialization
{
fs::path fp(file_path);
if (!fp.has_extension())
fp = fs::path(file_path + "." + serialization::binary::extension<SerializableType>::value);
fp = fs::path(file_path + serialization::binary::extension<SerializableType>::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
Expand Down Expand Up @@ -222,6 +222,16 @@ struct Serialization
return archive_type;
}

template <typename SerializableType>
static SerializableType fromArchiveFile(const std::string& file_path)
{
fs::path fp(file_path);
if (fp.extension() == serialization::binary::extension<SerializableType>::value)
return fromArchiveFileBinary<SerializableType>(file_path);

return fromArchiveFileXML<SerializableType>(file_path);
}

template <typename SerializableType>
static SerializableType fromArchiveBinaryData(const std::vector<std::uint8_t>& archive_binary)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ struct extension
using extension_type = typename U::extenstion;
};

using extension_type = typename boost::mpl::
eval_if<has_member_extension_type<T>, traits_class_extension<T>, boost::mpl::string<'t', 'r', 's', 'x'>>::type;
using extension_type = typename boost::mpl::eval_if<has_member_extension_type<T>,
traits_class_extension<T>,
boost::mpl::string<'.', 't', 'r', 's', 'x'>>::type;

static constexpr const char* value = boost::mpl::c_str<extension::extension_type>::value;
};
Expand All @@ -65,8 +66,9 @@ struct extension
using extension_type = typename U::extenstion;
};

using extension_type = typename boost::mpl::
eval_if<has_member_extension_type<T>, traits_class_extension<T>, boost::mpl::string<'t', 'r', 's', 'b'>>::type;
using extension_type = typename boost::mpl::eval_if<has_member_extension_type<T>,
traits_class_extension<T>,
boost::mpl::string<'.', 't', 'r', 's', 'b'>>::type;

static constexpr const char* value = boost::mpl::c_str<extension::extension_type>::value;
};
Expand All @@ -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
Expand All @@ -88,6 +91,7 @@ struct extension
struct extension<T> \
{ \
static constexpr const char* value = X; \
static_assert(value[0] == '.', "XML extension value must start with a '.'"); \
}; \
} \
namespace serialization::binary \
Expand All @@ -96,6 +100,7 @@ struct extension
struct extension<T> \
{ \
static constexpr const char* value = B; \
static_assert(value[0] == '.', "Binary extension value must start with a '.'"); \
}; \
} \
}
Expand Down
10 changes: 5 additions & 5 deletions tesseract_common/test/tesseract_common_serialization_unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ struct ExtensionMacroTestA
double a{ 0 };
};

TESSERACT_CLASS_EXTENSION(ExtensionMacroTestA, "etax", "etab")
TESSERACT_CLASS_EXTENSION(ExtensionMacroTestA, ".etax", ".etab")

struct ExtensionMacroTestB
{
Expand All @@ -748,19 +748,19 @@ struct ExtensionMacroTestB
TEST(TesseractCommonSerializeUnit, ExtensionXmlMacro) // NOLINT
{
std::string ext = tesseract_common::serialization::xml::extension<ExtensionMacroTestA>::value;
EXPECT_EQ(ext, "etax");
EXPECT_EQ(ext, ".etax");

std::string default_ext = tesseract_common::serialization::xml::extension<ExtensionMacroTestB>::value;
EXPECT_EQ(default_ext, "trsx");
EXPECT_EQ(default_ext, ".trsx");
}

TEST(TesseractCommonSerializeUnit, ExtensionBinaryMacro) // NOLINT
{
std::string ext = tesseract_common::serialization::binary::extension<ExtensionMacroTestA>::value;
EXPECT_EQ(ext, "etab");
EXPECT_EQ(ext, ".etab");

std::string default_ext = tesseract_common::serialization::binary::extension<ExtensionMacroTestB>::value;
EXPECT_EQ(default_ext, "trsb");
EXPECT_EQ(default_ext, ".trsb");
}

int main(int argc, char** argv)
Expand Down

0 comments on commit 4ef61aa

Please sign in to comment.