Program Listing for File serialization.hpp
↰ Return to documentation for file (include/fuse_core/serialization.hpp
)
/*
* Software License Agreement (BSD License)
*
* Copyright (c) 2019, Locus Robotics
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef FUSE_CORE__SERIALIZATION_HPP_
#define FUSE_CORE__SERIALIZATION_HPP_
#include <Eigen/Core>
#include <ios>
#include <vector>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/iostreams/categories.hpp>
#include <boost/serialization/access.hpp>
#include <boost/serialization/array.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/export.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/uuid/uuid_serialize.hpp>
#include <fuse_core/uuid.hpp>
#include <rclcpp/time.hpp>
namespace fuse_core
{
using BinaryInputArchive = boost::archive::binary_iarchive;
using BinaryOutputArchive = boost::archive::binary_oarchive;
using TextInputArchive = boost::archive::text_iarchive;
using TextOutputArchive = boost::archive::text_oarchive;
class MessageBufferStreamSource
{
public:
typedef char char_type;
typedef boost::iostreams::source_tag category;
explicit MessageBufferStreamSource(const std::vector<unsigned char> & data);
MessageBufferStreamSource operator=(const MessageBufferStreamSource &) = delete;
std::streamsize read(char_type * s, std::streamsize n);
private:
const std::vector<unsigned char> & data_;
size_t index_;
};
class MessageBufferStreamSink
{
public:
typedef char char_type;
typedef boost::iostreams::sink_tag category;
explicit MessageBufferStreamSink(std::vector<unsigned char> & data);
MessageBufferStreamSink operator=(const MessageBufferStreamSink &) = delete;
std::streamsize write(const char_type * s, std::streamsize n);
private:
std::vector<unsigned char> & data_;
};
} // namespace fuse_core
namespace boost
{
namespace serialization
{
template<class Archive>
void serialize(Archive & archive, rclcpp::Time & stamp, const unsigned int /* version */)
{
auto nanoseconds = stamp.nanoseconds();
auto clock_type = stamp.get_clock_type();
archive & nanoseconds;
archive & clock_type;
stamp = rclcpp::Time(nanoseconds, clock_type);
}
template<class Archive, typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
inline void serialize(
Archive & archive,
Eigen::Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols> & matrix,
const unsigned int /* version */)
{
Eigen::Index rows = matrix.rows();
Eigen::Index cols = matrix.cols();
archive & rows;
archive & cols;
if (rows != matrix.rows() || cols != matrix.cols()) {
matrix.resize(rows, cols);
}
if (matrix.size() != 0) {
archive & boost::serialization::make_array(matrix.data(), rows * cols);
}
}
} // namespace serialization
} // namespace boost
#endif // FUSE_CORE__SERIALIZATION_HPP_