MRPT  1.9.9
serialization_stl/test.cpp
/* +------------------------------------------------------------------------+
| Mobile Robot Programming Toolkit (MRPT) |
| http://www.mrpt.org/ |
| |
| Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
| See: http://www.mrpt.org/Authors - All rights reserved. |
| Released under BSD License. See details in http://www.mrpt.org/License |
+------------------------------------------------------------------------+ */
/** \example serialization_stl/test.cpp */
#include <iostream> // cout
template <class CONTAINER>
void printMap(const CONTAINER& m)
{
for (const auto& e : m) std::cout << e.first << "=" << e.second << ", ";
std::cout << std::endl;
}
//! [example]
#include <iostream> // cout
{
// Declare data to be serialized:
std::map<std::string, uint32_t> m1{{"one", 1}, {"two", 2}};
// === Write ===
{
// CStream output:
mrpt::io::CFileOutputStream ofs("file.bin");
auto arch_out = mrpt::serialization::archiveFrom(ofs);
// Use << to serialize in binary form:
arch_out << m1;
}
// === Read ===
std::map<std::string, uint32_t> m2;
{
// CStream output:
mrpt::io::CFileInputStream ifs("file.bin");
auto arch_in = mrpt::serialization::archiveFrom(ifs);
// Use >> to deserialize:
arch_in >> m2;
}
std::cout << "Wrote: ";
printMap(m1);
std::cout << "Read : ";
printMap(m2);
}
//! [example]
//! [example_stdio]
#include <fstream> // io std streams
#include <iostream> // cout
{
// Declare data to be serialized:
std::map<std::string, uint32_t> m1{{"one", 1}, {"two", 2}};
// === Write ===
{
// CStream output:
std::ofstream ofs("file.bin");
auto arch_out = mrpt::serialization::archiveFrom<std::ostream>(ofs);
// Use << to serialize in binary form:
arch_out << m1;
}
// === Read ===
std::map<std::string, uint32_t> m2;
{
// CStream output:
std::ifstream ifs("file.bin");
auto arch_in = mrpt::serialization::archiveFrom<std::istream>(ifs);
// Use >> to deserialize:
arch_in >> m2;
}
std::cout << "Wrote: ";
printMap(m1);
std::cout << "Read : ";
printMap(m2);
}
//! [example_stdio]
// ------------------------------------------------------
// MAIN
// ------------------------------------------------------
int main(int argc, char** argv)
{
try
{
return 0;
}
catch (std::exception& e)
{
std::cout << "Exception: " << e.what() << std::endl;
return -1;
}
catch (...)
{
printf("Untyped exception!");
return -1;
}
}



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020