MRPT  2.0.2
CSchemeArchiveBase.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 <cstdint>
13 #include <memory>
14 #include <stdexcept>
15 #include <string>
16 #include <string_view>
17 #include <vector>
18 
19 namespace mrpt::serialization
20 {
21 /** Pure virtual class carrying the implementation
22  * of CSchemeArchiveBase as per the PIMPL idiom.
23  * \ingroup mrpt_serialization_grp
24  */
26 {
27  public:
28  virtual ~CSchemeArchiveBase_impl() = default;
29  virtual CSchemeArchiveBase& operator=(const int32_t) = 0;
30  virtual CSchemeArchiveBase& operator=(const uint32_t) = 0;
31  virtual CSchemeArchiveBase& operator=(const int64_t) = 0;
32  virtual CSchemeArchiveBase& operator=(const uint64_t) = 0;
33  virtual CSchemeArchiveBase& operator=(const float) = 0;
34  virtual CSchemeArchiveBase& operator=(const double) = 0;
35  virtual CSchemeArchiveBase& operator=(const std::nullptr_t) = 0;
36  virtual CSchemeArchiveBase& operator=(const std::string) = 0;
37  virtual CSchemeArchiveBase& operator=(bool) = 0;
38 
39  // Type conversion methods
40  virtual explicit operator int32_t() const = 0;
41  virtual explicit operator uint32_t() const = 0;
42  virtual explicit operator int64_t() const = 0;
43  virtual explicit operator uint64_t() const = 0;
44  virtual explicit operator float() const = 0;
45  virtual explicit operator double() const = 0;
46  virtual explicit operator bool() const = 0;
47  virtual explicit operator std::string() const = 0;
48  /** Reads object from the archive */
49  virtual void readTo(CSerializable& obj) = 0;
50  /** Writes object to archive, with synxtax `out["name"] = obj;`*/
53 
54  /** Writes the scheme to a plain-text output */
55  virtual std::ostream& writeToStream(std::ostream& out) const = 0;
56  /** Reads the scheme from a plain-text input */
57  virtual std::istream& readFromStream(std::istream& in) = 0;
58 
59  // List accessor
60  virtual CSchemeArchiveBase operator[](size_t) = 0;
61  // Dict accessor
62  virtual CSchemeArchiveBase operator[](std::string) = 0;
63 
64  public: // should make it private by virtue of friend class
65  void setParent(CSchemeArchiveBase* parent) { m_parent = parent; }
66 
67  protected:
71 };
72 /** Virtual base class for "schematic archives" (JSON, XML,...)
73  * \ingroup mrpt_serialization_grp
74  */
76 {
77  public:
80  /** @name Serialization API for schema based "archives"
81  * @{ */
82  // Constructor
83  CSchemeArchiveBase(std::unique_ptr<CSchemeArchiveBase_impl> ptr)
84  : pimpl(std::move(ptr))
85  {
86  pimpl->setParent(this);
87  }
88  // Note: Due to the definition of this one, the compiler should not
89  // automatically define copy ctor, so we define it next.
90  // It's better to have the dtor defined here since we'll have derived
91  // classes.
92  virtual ~CSchemeArchiveBase() = default;
94 
96  {
97  return (*pimpl).operator=(val);
98  }
99  CSchemeArchiveBase& operator=(const uint32_t val)
100  {
101  return (*pimpl).operator=(val);
102  }
104  {
105  return (*pimpl).operator=(val);
106  }
108  {
109  return (*pimpl).operator=(val);
110  }
112  {
113  return (*pimpl).operator=(val);
114  }
116  {
117  return (*pimpl).operator=(val);
118  }
119  CSchemeArchiveBase& operator=(const std::nullptr_t val)
120  {
121  return (*pimpl).operator=(val);
122  }
123  CSchemeArchiveBase& operator=(const std::string val)
124  {
125  return (*pimpl).operator=(val);
126  }
127  CSchemeArchiveBase& operator=(bool val) { return (*pimpl).operator=(val); }
128  // Type conversion methods
129  explicit operator int32_t() const { return static_cast<int32_t>(*pimpl); }
130  explicit operator uint32_t() const { return static_cast<uint32_t>(*pimpl); }
131  explicit operator int64_t() const { return static_cast<int64_t>(*pimpl); }
132  explicit operator uint64_t() const { return static_cast<uint64_t>(*pimpl); }
133  explicit operator float() const { return static_cast<float>(*pimpl); }
134  explicit operator double() const { return static_cast<double>(*pimpl); }
135  explicit operator bool() const { return static_cast<bool>(*pimpl); }
136  explicit operator std::string() const
137  {
138  return static_cast<std::string>(*pimpl);
139  }
140  void readTo(CSerializable& obj) { pimpl->readTo(obj); }
142  {
143  return (*pimpl).operator=(obj);
144  }
145  // List accessor
147  {
148  return (*pimpl).operator[](val);
149  }
150  // Dict accessor
152  {
153  return (*pimpl).operator[](val);
154  }
155 
156  // class CSchemeArchiveBase_impl;
157  protected:
158  // Read Object
159  static void ReadObject(CSchemeArchiveBase& out, const CSerializable& obj)
160  {
161  obj.serializeTo(out);
162  }
163  // Write Object
165  {
166  obj.serializeFrom(in);
167  }
168 
169  private:
170  std::unique_ptr<CSchemeArchiveBase_impl> pimpl;
171  friend std::ostream& operator<<(
172  std::ostream& out, const CSchemeArchiveBase& a);
173  friend std::istream& operator>>(std::istream& in, CSchemeArchiveBase& a);
174 };
175 
176 inline std::ostream& operator<<(std::ostream& out, const CSchemeArchiveBase& a)
177 {
178  a.pimpl->writeToStream(out);
179  return out;
180 }
181 inline std::istream& operator>>(std::istream& in, CSchemeArchiveBase& a)
182 {
183  a.pimpl->readFromStream(in);
184  return in;
185 }
186 
187 } // namespace mrpt::serialization
CSchemeArchiveBase & operator=(const int32_t val)
virtual void serializeTo(CArchive &out) const =0
Pure virtual method for writing (serializing) to an abstract archive.
CSchemeArchiveBase & operator=(const float val)
virtual std::ostream & writeToStream(std::ostream &out) const =0
Writes the scheme to a plain-text output.
Pure virtual class carrying the implementation of CSchemeArchiveBase as per the PIMPL idiom...
friend std::ostream & operator<<(std::ostream &out, const CSchemeArchiveBase &a)
static void ReadObject(CSchemeArchiveBase &out, const CSerializable &obj)
virtual void serializeFrom(CArchive &in, uint8_t serial_version)=0
Pure virtual method for reading (deserializing) from an abstract archive.
spimpl::impl_ptr< T > pimpl
Definition: pimpl.h:15
CSchemeArchiveBase & operator=(const std::string val)
void WriteObject(CSchemeArchiveBase &in, CSerializable &obj)
STL namespace.
CSchemeArchiveBase & operator=(const uint32_t val)
CSchemeArchiveBase & operator=(const mrpt::serialization::CSerializable &obj)
Virtual base class for "schematic archives" (JSON, XML,...)
virtual CSchemeArchiveBase operator[](size_t)=0
int val
Definition: mrpt_jpeglib.h:957
virtual std::istream & readFromStream(std::istream &in)=0
Reads the scheme from a plain-text input.
friend std::istream & operator>>(std::istream &in, CSchemeArchiveBase &a)
CSchemeArchiveBase & operator=(const std::nullptr_t val)
CSchemeArchiveBase & operator=(const int64_t val)
CArchive & operator>>(CArchive &s, mrpt::aligned_std_vector< float > &a)
Definition: CArchive.cpp:250
void setParent(CSchemeArchiveBase *parent)
CSchemeArchiveBase(std::unique_ptr< CSchemeArchiveBase_impl > ptr)
CSchemeArchiveBase operator[](std::string val)
mrpt::vision::TStereoCalibResults out
CSchemeArchiveBase & operator=(bool val)
CArchive & operator<<(CArchive &s, const mrpt::aligned_std_vector< float > &a)
Definition: CArchive.cpp:199
virtual void readTo(CSerializable &obj)=0
Reads object from the archive.
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:30
CSchemeArchiveBase operator[](size_t val)
static void WriteObject(CSchemeArchiveBase &in, CSerializable &obj)
CSchemeArchiveBase & operator=(const double val)
std::unique_ptr< CSchemeArchiveBase_impl > pimpl
virtual CSchemeArchiveBase & operator=(const int32_t)=0
CSchemeArchiveBase & operator=(const uint64_t val)
void ReadObject(CSchemeArchiveBase &out, const CSerializable &obj)



Page generated by Doxygen 1.8.14 for MRPT 2.0.2 Git: 9b4fd2465 Mon May 4 16:59:08 2020 +0200 at lun may 4 17:26:07 CEST 2020