Skip to content

Commit

Permalink
Add binary data serialization support functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Levi-Armstrong committed May 29, 2024
1 parent 660d42d commit 371e7d5
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
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

0 comments on commit 371e7d5

Please sign in to comment.