MRPT  2.0.1
serialization_stl/test.cpp
/* +------------------------------------------------------------------------+
| Mobile Robot Programming Toolkit (MRPT) |
| https://www.mrpt.org/ |
| |
| Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
| See: https://www.mrpt.org/Authors - All rights reserved. |
| Released under BSD License. See: https://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 (const std::exception& e)
{
std::cerr << "MRPT error: " << mrpt::exception_to_str(e) << std::endl;
return -1;
}
catch (...)
{
printf("Untyped exception!");
return -1;
}
}



Page generated by Doxygen 1.8.14 for MRPT 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020