Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add binary data serialization support functions #1002

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions tesseract_common/include/tesseract_common/serialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,28 @@ struct Serialization
return true;
}

template <typename SerializableType>
static std::vector<std::uint8_t> toArchiveBinaryData(const SerializableType& archive_type,
const std::string& name = "")
{
std::stringstream ss;
{ // Must be scoped because all data is not written until the oost::archive::xml_oarchive goes out of scope
boost::archive::xml_oarchive oa(ss);

// Boost uses the same function for serialization and deserialization so it requires a non-const reference
// Because we are only serializing here it is safe to cast away const
if (name.empty())
oa << boost::serialization::make_nvp<SerializableType>("archive_type",
const_cast<SerializableType&>(archive_type)); // NOLINT
else
oa << boost::serialization::make_nvp<SerializableType>(name.c_str(),
const_cast<SerializableType&>(archive_type)); // NOLINT
}

std::string data = ss.str();
return std::vector<std::uint8_t>(data.begin(), data.end());
}

template <typename SerializableType>
static SerializableType fromArchiveStringXML(const std::string& archive_xml)
{
Expand Down Expand Up @@ -199,6 +221,21 @@ struct Serialization

return archive_type;
}

template <typename SerializableType>
static SerializableType fromArchiveBinaryData(const std::vector<std::uint8_t>& archive_binary)
{
SerializableType archive_type;

{ // Must be scoped because all data is not written until the oost::archive::xml_oarchive goes out of scope
std::stringstream ss;
std::copy(archive_binary.begin(), archive_binary.end(), std::ostreambuf_iterator<char>(ss));
boost::archive::xml_iarchive ia(ss);
ia >> BOOST_SERIALIZATION_NVP(archive_type);
}

return archive_type;
}
};
} // namespace tesseract_common
#endif // TESSERACT_COMMON_SERIALIZATION_H
34 changes: 34 additions & 0 deletions tesseract_common/include/tesseract_common/unit_test_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ void testSerialization(const SerializableType& object, const std::string& typena
SerializableType nobject{ tesseract_common::Serialization::fromArchiveStringXML<SerializableType>(object_string) };
EXPECT_FALSE(object != nobject); // Using != because it call == for code coverage
}

{ // Archive program to binary data
std::vector<std::uint8_t> object_data =
tesseract_common::Serialization::toArchiveBinaryData<SerializableType>(object, typename_string);
EXPECT_FALSE(object_data.empty());

SerializableType nobject{ tesseract_common::Serialization::fromArchiveBinaryData<SerializableType>(object_data) };
EXPECT_FALSE(object != nobject); // Using != because it call == for code coverage
}
}

/**
Expand Down Expand Up @@ -109,6 +118,17 @@ void testSerializationPtr(const std::shared_ptr<SerializableType>& object, const
tesseract_common::Serialization::fromArchiveStringXML<std::shared_ptr<SerializableType>>(object_string);
EXPECT_FALSE(*object != *nobject); // Using != because it call == for code coverage
}

{ // Archive program to binary data
std::vector<std::uint8_t> object_data =
tesseract_common::Serialization::toArchiveBinaryData<std::shared_ptr<SerializableType>>(object,
typename_string);
EXPECT_FALSE(object_data.empty());

auto nobject =
tesseract_common::Serialization::fromArchiveBinaryData<std::shared_ptr<SerializableType>>(object_data);
EXPECT_FALSE(*object != *nobject); // Using != because it call == for code coverage
}
}

/**
Expand Down Expand Up @@ -161,6 +181,20 @@ void testSerializationDerivedClass(const std::shared_ptr<SerializableTypeBase>&
auto object_derived = std::dynamic_pointer_cast<SerializableTypeDerived>(object);
EXPECT_FALSE(*object_derived != *nobject_derived); // Using != because it call == for code coverage
}

{ // Archive program to binary data
std::vector<std::uint8_t> object_data =
tesseract_common::Serialization::toArchiveBinaryData<std::shared_ptr<SerializableTypeBase>>(object,
typename_string);
EXPECT_FALSE(object_data.empty());

auto nobject =
tesseract_common::Serialization::fromArchiveBinaryData<std::shared_ptr<SerializableTypeBase>>(object_data);
auto nobject_derived = std::dynamic_pointer_cast<SerializableTypeDerived>(nobject);

auto object_derived = std::dynamic_pointer_cast<SerializableTypeDerived>(object);
EXPECT_FALSE(*object_derived != *nobject_derived); // Using != because it call == for code coverage
}
}
} // namespace tesseract_common
#endif // TESSERACT_COMMON_UTILS_H
Loading