MRPT  2.0.4
CSchemeArchive.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 
13 #include <memory>
14 #include <optional>
15 #include <sstream>
16 #include <string>
17 
18 namespace mrpt::serialization
19 {
20 /** Base template class for schema-capable "archives", e.g. JSON, YAML,
21  * from which to (de)serialize objects.
22  *
23  * See \ref mrpt_serialization_grp for examples of use.
24  * \ingroup mrpt_serialization_grp
25  * \note Original version by https://github.com/rachit173 for GSoC 2018.
26  */
27 template <typename SCHEME_CAPABLE>
29 {
30  std::optional<SCHEME_CAPABLE> m_own_val;
31 
32  public:
33  /** Ctor that creates an own SCHEME_CAPABLE object. */
34  CSchemeArchive() : m_own_val{SCHEME_CAPABLE()}, m_val{m_own_val.value()} {}
35  /** Ctor that uses user-providen SCHEME_CAPABLE object. */
36  CSchemeArchive(SCHEME_CAPABLE& val) : m_val(val) {}
37  // Virtual assignment operators
39  const int32_t val) override
40  {
41  m_val = val;
42  return *m_parent;
43  }
45  const uint32_t val) override
46  {
47  m_val = val;
48  return *m_parent;
49  }
51  const int64_t val) override
52  {
53  m_val = typename SCHEME_CAPABLE::Int64(val);
54  return *m_parent;
55  }
57  const uint64_t val) override
58  {
59  m_val = typename SCHEME_CAPABLE::UInt64(val);
60  return *m_parent;
61  }
63  {
64  m_val = val;
65  return *m_parent;
66  }
68  const double val) override
69  {
70  m_val = val;
71  return *m_parent;
72  }
74  const std::nullptr_t val) override
75  {
76  m_val = val;
77  return *m_parent;
78  }
80  const std::string val) override
81  {
82  m_val = val;
83  return *m_parent;
84  }
86  {
87  m_val = val;
88  return *m_parent;
89  }
90 
91  explicit operator int32_t() const override { return m_val.asInt(); }
92  explicit operator uint32_t() const override { return m_val.asUInt(); }
93  explicit operator int64_t() const override { return m_val.asInt64(); }
94  explicit operator uint64_t() const override { return m_val.asUInt64(); }
95  explicit operator float() const override { return m_val.asFloat(); }
96  explicit operator double() const override { return m_val.asDouble(); }
97  explicit operator bool() const override { return m_val.asBool(); }
98  explicit operator std::string() const override { return m_val.asString(); }
100  const mrpt::serialization::CSerializable& obj) override
101  {
102  ReadObject(*m_parent, obj);
103  return *m_parent;
104  }
106  {
107  WriteObject(*m_parent, obj);
108  return;
109  }
110 
112  {
114  std::make_unique<CSchemeArchive<SCHEME_CAPABLE>>(m_val[(int)idx]));
115  }
117  {
119  std::make_unique<CSchemeArchive<SCHEME_CAPABLE>>(
120  m_val[std::string(str)]));
121  }
122 
123  std::ostream& writeToStream(std::ostream& out) const override
124  {
125  out << m_val;
126  return out;
127  }
128  std::istream& readFromStream(std::istream& in) override
129  {
130  in >> m_val;
131  return in;
132  }
133 
134  private:
135  SCHEME_CAPABLE& m_val;
136 };
137 
138 /** Returns an archive for reading/writing in JSON format.
139  * This feature requires compiling MRPT with jsoncpp support.
140  * See \ref mrpt_serialization_grp for examples of use.
141  * \ingroup mrpt_serialization_grp */
143 
144 } // namespace mrpt::serialization
CSchemeArchive()
Ctor that creates an own SCHEME_CAPABLE object.
mrpt::serialization::CSchemeArchiveBase & operator=(const bool val) override
Pure virtual class carrying the implementation of CSchemeArchiveBase as per the PIMPL idiom...
mrpt::serialization::CSchemeArchiveBase & operator=(const mrpt::serialization::CSerializable &obj) override
Writes object to archive, with synxtax out["name"] = obj;
void WriteObject(CSchemeArchiveBase &in, CSerializable &obj)
mrpt::serialization::CSchemeArchiveBase & operator=(const uint32_t val) override
mrpt::serialization::CSchemeArchiveBase & operator=(const std::nullptr_t val) override
std::istream & readFromStream(std::istream &in) override
Reads the scheme from a plain-text input.
mrpt::serialization::CSchemeArchiveBase operator[](size_t idx) override
mrpt::serialization::CSchemeArchiveBase & operator=(const int32_t val) override
Virtual base class for "schematic archives" (JSON, XML,...)
mrpt::serialization::CSchemeArchiveBase operator[](std::string str) override
int val
Definition: mrpt_jpeglib.h:957
void readTo(mrpt::serialization::CSerializable &obj) override
Reads object from the archive.
Base template class for schema-capable "archives", e.g.
mrpt::vision::TStereoCalibResults out
std::ostream & writeToStream(std::ostream &out) const override
Writes the scheme to a plain-text output.
mrpt::serialization::CSchemeArchiveBase & operator=(const double val) override
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:30
mrpt::serialization::CSchemeArchiveBase & operator=(const std::string val) override
CSchemeArchive(SCHEME_CAPABLE &val)
Ctor that uses user-providen SCHEME_CAPABLE object.
mrpt::serialization::CSchemeArchiveBase & operator=(const uint64_t val) override
CSchemeArchiveBase archiveJSON()
Returns an archive for reading/writing in JSON format.
std::optional< SCHEME_CAPABLE > m_own_val
mrpt::serialization::CSchemeArchiveBase & operator=(const int64_t val) override
void ReadObject(CSchemeArchiveBase &out, const CSerializable &obj)
mrpt::serialization::CSchemeArchiveBase & operator=(const float val) override



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020