diff --git a/include/curves/serialization/CMakeLists.txt b/include/curves/serialization/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..5961ad6d802763da650e61ee75540c7e60f70ef5 --- /dev/null +++ b/include/curves/serialization/CMakeLists.txt @@ -0,0 +1,10 @@ +SET(${PROJECT_NAME}_SERIALIZATION_HEADERS + archive.hpp + eigen-matrix.hpp + fwd.hpp + ) + +INSTALL(FILES + ${${PROJECT_NAME}_SERIALIZATION_HEADERS} + DESTINATION include/curves/serialization + ) \ No newline at end of file diff --git a/include/curves/serialization/archive.hpp b/include/curves/serialization/archive.hpp new file mode 100644 index 0000000000000000000000000000000000000000..7da8c76aec2584e57323e3c6de7a9b340920caad --- /dev/null +++ b/include/curves/serialization/archive.hpp @@ -0,0 +1,136 @@ +// Copyright (c) 2015-2018, CNRS +// Authors: Justin Carpentier <jcarpent@laas.fr> + +#ifndef __multicontact_api_serialization_archive_hpp__ +#define __multicontact_api_serialization_archive_hpp__ + +#include "serialization/fwd.hpp" + +#include <fstream> +#include <string> +#include <stdexcept> +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp> +#include <boost/archive/xml_iarchive.hpp> +#include <boost/archive/xml_oarchive.hpp> +#include <boost/archive/binary_iarchive.hpp> +#include <boost/archive/binary_oarchive.hpp> + + +namespace curves +{ + namespace serialization + { + + template<class Derived> + struct Serializable + { + private: + Derived & derived() { return *static_cast<Derived*>(this); } + const Derived & derived() const { return *static_cast<const Derived*>(this); } + + public: + /// \brief Loads a Derived object from a text file. + void loadFromText(const std::string & filename) throw (std::invalid_argument) + { + std::ifstream ifs(filename.c_str()); + if(ifs) + { + boost::archive::text_iarchive ia(ifs); + ia >> derived(); + } + else + { + const std::string exception_message(filename + " does not seem to be a valid file."); + throw std::invalid_argument(exception_message); + } + } + + /// \brief Saved a Derived object as a text file. + void saveAsText(const std::string & filename) const throw (std::invalid_argument) + { + std::ofstream ofs(filename.c_str()); + if(ofs) + { + boost::archive::text_oarchive oa(ofs); + oa << derived(); + } + else + { + const std::string exception_message(filename + " does not seem to be a valid file."); + throw std::invalid_argument(exception_message); + } + } + + /// \brief Loads a Derived object from an XML file. + void loadFromXML(const std::string & filename, const std::string & tag_name) throw (std::invalid_argument) + { + assert(!tag_name.empty()); + std::ifstream ifs(filename.c_str()); + if(ifs) + { + boost::archive::xml_iarchive ia(ifs); + ia >> boost::serialization::make_nvp(tag_name.c_str(),derived()); + } + else + { + const std::string exception_message(filename + " does not seem to be a valid file."); + throw std::invalid_argument(exception_message); + } + } + + /// \brief Saved a Derived object as an XML file. + void saveAsXML(const std::string & filename, const std::string & tag_name) const throw (std::invalid_argument) + { + assert(!tag_name.empty()); + std::ofstream ofs(filename.c_str()); + if(ofs) + { + boost::archive::xml_oarchive oa(ofs); + oa << boost::serialization::make_nvp(tag_name.c_str(),derived()); + } + else + { + const std::string exception_message(filename + " does not seem to be a valid file."); + throw std::invalid_argument(exception_message); + } + } + + /// \brief Loads a Derived object from an binary file. + void loadFromBinary(const std::string & filename) throw (std::invalid_argument) + { + std::ifstream ifs(filename.c_str()); + if(ifs) + { + boost::archive::binary_iarchive ia(ifs); + ia >> derived(); + } + else + { + const std::string exception_message(filename + " does not seem to be a valid file."); + throw std::invalid_argument(exception_message); + } + } + + /// \brief Saved a Derived object as an binary file. + void saveAsBinary(const std::string & filename) const throw (std::invalid_argument) + { + std::ofstream ofs(filename.c_str()); + if(ofs) + { + boost::archive::binary_oarchive oa(ofs); + oa << derived(); + } + else + { + const std::string exception_message(filename + " does not seem to be a valid file."); + throw std::invalid_argument(exception_message); + } + } + }; + + } + +} + +#endif // ifndef __multicontact_api_serialization_archive_hpp__ diff --git a/include/curves/serialization/eigen-matrix.hpp b/include/curves/serialization/eigen-matrix.hpp new file mode 100644 index 0000000000000000000000000000000000000000..ca84a5fde37685ad55d03f44b3145af70f3b3111 --- /dev/null +++ b/include/curves/serialization/eigen-matrix.hpp @@ -0,0 +1,68 @@ +// Copyright (c) 2015-2018, CNRS +// Authors: Justin Carpentier <jcarpent@laas.fr> + +/* + Code adapted from: https://gist.githubusercontent.com/mtao/5798888/raw/5be9fa9b66336c166dba3a92c0e5b69ffdb81501/eigen_boost_serialization.hpp + Copyright (c) 2015 Michael Tao + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + */ + + +#ifndef __curves_serialization_eigen_matrix_hpp__ +#define __curves_serialization_eigen_matrix_hpp__ + +#include <Eigen/Dense> +#include <boost/serialization/split_free.hpp> +#include <boost/serialization/vector.hpp> + +namespace boost{ + + namespace serialization{ + + template <class Archive, typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols> + void save(Archive & ar, const Eigen::Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols> & m, const unsigned int /*version*/) + { + Eigen::DenseIndex rows(m.rows()), cols(m.cols()); + ar & BOOST_SERIALIZATION_NVP(rows); + ar & BOOST_SERIALIZATION_NVP(cols); + ar & make_nvp("data",make_array(m.data(), (size_t)m.size())); + } + + template <class Archive, typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols> + void load(Archive & ar, Eigen::Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols> & m, const unsigned int /*version*/) + { + Eigen::DenseIndex rows,cols; + ar >> BOOST_SERIALIZATION_NVP(rows); + ar >> BOOST_SERIALIZATION_NVP(cols); + m.resize(rows,cols); +// if(m.size() > 0) + ar >> make_nvp("data",make_array(m.data(), (size_t)m.size())); + } + + template <class Archive, typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols> + void serialize(Archive & ar, Eigen::Matrix<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols> & m, const unsigned int version) + { + split_free(ar,m,version); + } + + } +} + +#endif // ifndef __multicontact_api_serialization_eigen_matrix_hpp__ diff --git a/include/curves/serialization/fwd.hpp b/include/curves/serialization/fwd.hpp new file mode 100644 index 0000000000000000000000000000000000000000..20dffe3d712ef56d45e9559bcab1015a56a32f58 --- /dev/null +++ b/include/curves/serialization/fwd.hpp @@ -0,0 +1,11 @@ +// Copyright (c) 2015-2018, CNRS +// Authors: Justin Carpentier <jcarpent@laas.fr> + +#ifndef __curves_serialization_fwd_hpp__ +#define __curves_api_serialization_fwd_hpp__ + +#include <boost/serialization/nvp.hpp> + +#define BOOST_SERIALIZATION_MAKE_NVP(member) boost::serialization::make_nvp(##member,member) + +#endif // ifndef __multicontact_api_serialization_fwd_hpp__ diff --git a/include/curves/serialize_test_class.h b/include/curves/serialize_test_class.h new file mode 100644 index 0000000000000000000000000000000000000000..35cbb78f54930d5a5a2354075bde8a3e399a9ca4 --- /dev/null +++ b/include/curves/serialize_test_class.h @@ -0,0 +1,71 @@ +/** +* \file polynomial.h +* \brief Definition of a cubic spline. +* \author Steve T. +* \version 0.1 +* \date 06/17/2013 +* +* This file contains definitions for the polynomial struct. +* It allows the creation and evaluation of natural +* smooth splines of arbitrary dimension and order +*/ + + +#ifndef _STRUCT_TEST_SER +#define _STRUCT_TEST_SER + +#include "MathDefs.h" + +#include "curve_abc.h" + +#include <iostream> +#include <algorithm> +#include <functional> +#include <stdexcept> + +#include <fstream> +#include <string> +#include <stdexcept> +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp> + +#include "serialization/archive.hpp" +#include "serialization/eigen-matrix.hpp" + +namespace curves +{ +struct serialize_test_class : public serialization::Serializable< serialize_test_class > +{ + public: + + serialize_test_class(int a) :a_(a) {} + + int a_; + + void serialize_to_file(std::string path) + { + std::ofstream ofile(path.c_str()); + boost::archive::text_oarchive oTextArchive(ofile); + oTextArchive << (*this); // sérialisation de d + } + + void deserialize_from_file(std::string path) + { + std::ifstream ifile(path.c_str()); + boost::archive::text_iarchive iTextArchive(ifile); + iTextArchive >> (*this); // désérialisation dans d + } + + // Serialization of the class + friend class boost::serialization::access; + + template<class Archive> + void serialize(Archive & ar, const unsigned int version) + { + ar & BOOST_SERIALIZATION_NVP(a_); + } + +}; //class polynomial +} // namespace curves +#endif //_STRUCT_POLYNOMIAL +