MRPT  2.0.1
optional_serialization.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
12 #include <mrpt/typemeta/TTypeName_stl.h> // TTypeName<> for STL templates, needed for serialization of STL templates
13 #include <optional>
14 
15 namespace mrpt::serialization
16 {
17 /** \addtogroup stlext_grp
18  * @{ */
19 
20 /** Template to serialize a std::optional<T> */
21 template <class T>
22 CArchive& operator<<(CArchive& out, const std::optional<T>& obj)
23 {
24  out << std::string("std::optional") << mrpt::typemeta::TTypeName<T>::get();
25  out << obj.has_value();
26  if (obj.has_value()) out << *obj;
27  return out;
28 }
29 
30 /** Template to deserialize a std::optional<T> */
31 template <class T>
32 CArchive& operator>>(CArchive& in, std::optional<T>& obj)
33 {
34  std::string pref, stored_T;
35  in >> pref;
36  if (pref != "std::optional")
38  "Error: serialized std::optional<%s>'s preamble is wrong: '%s'",
39  mrpt::typemeta::TTypeName<T>::get().c_str(), pref.c_str()));
40  in >> stored_T;
41  if (stored_T != std::string(mrpt::typemeta::TTypeName<T>::get().c_str()))
43  "Error: serialized std::optional type %s != %s", stored_T.c_str(),
45 
46  bool has_value;
47  in >> has_value;
48  if (has_value)
49  {
50  T val;
51  in >> val;
52  obj = std::move(val);
53  }
54  else
55  {
56  obj.reset();
57  }
58  return in;
59 }
60 
61 /** @} */ // end of grouping
62 } // namespace mrpt::serialization
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
std::string std::string format(std::string_view fmt, ARGS &&... args)
Definition: format.h:26
A template to obtain the type of its argument as a string at compile time.
Definition: TTypeName.h:69
int val
Definition: mrpt_jpeglib.h:957
CArchive & operator>>(CArchive &s, mrpt::aligned_std_vector< float > &a)
Definition: CArchive.cpp:250
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:54
mrpt::vision::TStereoCalibResults out
static constexpr auto get()
Definition: TTypeName.h:71



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