Verified Commit 41c9cbd1 authored by Justin Carpentier's avatar Justin Carpentier
Browse files

serialization: load and save from and to string

parent 208535da
......@@ -79,7 +79,7 @@ namespace pinocchio
}
///
/// \brief Loads an object from std::stringstream.
/// \brief Loads an object from a std::stringstream.
///
/// \tparam T Type of the object to deserialize.
///
......@@ -109,6 +109,39 @@ namespace pinocchio
boost::archive::text_oarchive oa(ss);
oa & object;
}
///
/// \brief Loads an object from a std::string
///
/// \tparam T Type of the object to deserialize.
///
/// \param[out] object Object in which the loaded data are copied.
/// \param[in] str string constaining the serialized content of the object.
///
template<typename T>
inline void loadFromString(T & object,
const std::string & str)
{
std::istringstream is(str);
loadFromStringStream(object,is);
}
///
/// \brief Saves an object inside a std::string
///
/// \tparam T Type of the object to deserialize.
///
/// \param[in] object Object in which the loaded data are copied.
/// \param[out] str String constaining the serialized content of the object.
///
template<typename T>
inline void saveToString(const T & object,
std::string & str)
{
std::stringstream ss;
saveToStringStream(object,ss);
str = ss.str();
}
///
/// \brief Loads an object from a XML file.
......
......@@ -51,6 +51,19 @@ void generic_test(const T & object,
BOOST_CHECK(object_loaded == object);
}
// Load and save as string
std::string str_out;
saveToString(object,str_out);
{
T object_loaded;
std::string str_in(str_out);
loadFromString(object_loaded,str_in);
// Check
BOOST_CHECK(object_loaded == object);
}
// Load and save as XML
const std::string xml_filename = filename + ".xml";
saveToXML(object,xml_filename,tag_name);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment