Main MRPT website > C++ reference for MRPT 1.9.9
test.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 /** \example serialization_stl/test.cpp */
10 
11 #include <iostream> // cout
12 
13 template <class CONTAINER>
14 void printMap(const CONTAINER& m)
15 {
16  for (const auto& e : m) std::cout << e.first << "=" << e.second << ", ";
17  std::cout << std::endl;
18 }
19 
20 //! [example]
25 
26 #include <iostream> // cout
27 
29 {
30  // Declare data to be serialized:
31  std::map<std::string, uint32_t> m1{{"one", 1}, {"two", 2}};
32 
33  // === Write ===
34  {
35  // CStream output:
36  mrpt::io::CFileOutputStream ofs("file.bin");
37  auto arch_out = mrpt::serialization::archiveFrom(ofs);
38  // Use << to serialize in binary form:
39  arch_out << m1;
40  }
41 
42  // === Read ===
43  std::map<std::string, uint32_t> m2;
44  {
45  // CStream output:
46  mrpt::io::CFileInputStream ifs("file.bin");
47  auto arch_in = mrpt::serialization::archiveFrom(ifs);
48  // Use >> to deserialize:
49  arch_in >> m2;
50  }
51 
52  std::cout << "Wrote: ";
53  printMap(m1);
54  std::cout << "Read : ";
55  printMap(m2);
56 }
57 //! [example]
58 
59 //! [example_stdio]
63 #include <fstream> // io std streams
64 
65 #include <iostream> // cout
66 
68 {
69  // Declare data to be serialized:
70  std::map<std::string, uint32_t> m1{{"one", 1}, {"two", 2}};
71 
72  // === Write ===
73  {
74  // CStream output:
75  std::ofstream ofs("file.bin");
76  auto arch_out = mrpt::serialization::archiveFrom<std::ostream>(ofs);
77  // Use << to serialize in binary form:
78  arch_out << m1;
79  }
80 
81  // === Read ===
82  std::map<std::string, uint32_t> m2;
83  {
84  // CStream output:
85  std::ifstream ifs("file.bin");
86  auto arch_in = mrpt::serialization::archiveFrom<std::istream>(ifs);
87  // Use >> to deserialize:
88  arch_in >> m2;
89  }
90 
91  std::cout << "Wrote: ";
92  printMap(m1);
93  std::cout << "Read : ";
94  printMap(m2);
95 }
96 //! [example_stdio]
97 
98 // ------------------------------------------------------
99 // MAIN
100 // ------------------------------------------------------
101 int main(int argc, char** argv)
102 {
103  try
104  {
107  return 0;
108  }
109  catch (std::exception& e)
110  {
111  std::cout << "Exception: " << e.what() << std::endl;
112  return -1;
113  }
114  catch (...)
115  {
116  printf("Untyped exception!");
117  return -1;
118  }
119 }
CFileOutputStream.h
stl_serialization.h
CFileInputStream.h
WriteAndReadExample
void WriteAndReadExample()
[example]
Definition: vision_stereo_rectify/test.cpp:28
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
mrpt::io::CFileInputStream
This CStream derived class allow using a file as a read-only, binary stream.
Definition: io/CFileInputStream.h:23
WriteAndReadExampleStdIO
void WriteAndReadExampleStdIO()
[example]
Definition: vision_stereo_rectify/test.cpp:67
mrpt::io::CFileOutputStream
This CStream derived class allow using a file as a write-only, binary stream.
Definition: io/CFileOutputStream.h:24
mrpt::serialization::archiveFrom
CArchiveStreamBase< STREAM > archiveFrom(STREAM &s)
Helper function to create a templatized wrapper CArchive object for a: MRPT's CStream,...
Definition: CArchive.h:561
printMap
void printMap(const CONTAINER &m)
Definition: vision_stereo_rectify/test.cpp:14
CArchive.h
archiveFrom_std_streams.h



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST