MRPT  1.9.9
archiveFrom_std_streams.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-2019, 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 <iosfwd>
13 #include <istream>
14 #include <ostream>
15 
16 namespace mrpt::serialization
17 {
18 /** Specialization for std::istream */
19 template <>
20 class CArchiveStreamBase<std::istream> : public CArchive
21 {
22  std::istream& m_s;
23 
24  public:
25  CArchiveStreamBase(std::istream& s) : m_s(s) {}
26 
27  protected:
28  size_t write(const void* d, size_t n) override
29  {
30  throw std::runtime_error(
31  "CArchiveStreamBase<std::istream>:"
32  "cannot write to an input stream.");
33  }
34  size_t read(void* d, size_t n) override
35  {
36  if (m_s.read(reinterpret_cast<char*>(d), n))
37  return n;
38  else
39  return 0;
40  }
41 };
42 
43 /** Specialization for std::ostream */
44 template <>
45 class CArchiveStreamBase<std::ostream> : public CArchive
46 {
47  std::ostream& m_s;
48 
49  public:
50  CArchiveStreamBase(std::ostream& s) : m_s(s) {}
51 
52  protected:
53  size_t write(const void* d, size_t n) override
54  {
55  if (m_s.write(reinterpret_cast<const char*>(d), n))
56  return n;
57  else
58  return 0;
59  }
60  size_t read(void* d, size_t n) override
61  {
62  throw std::runtime_error(
63  "CArchiveStreamBase<std::ostream>:"
64  "cannot read from output stream.");
65  }
66 };
67 
68 /** Specialization for std::iostream */
69 template <>
70 class CArchiveStreamBase<std::iostream> : public CArchive
71 {
72  std::iostream& m_s;
73 
74  public:
75  CArchiveStreamBase(std::iostream& s) : m_s(s) {}
76 
77  protected:
78  size_t write(const void* d, size_t n) override
79  {
80  if (m_s.write(reinterpret_cast<const char*>(d), n))
81  return n;
82  else
83  return 0;
84  }
85  size_t read(void* d, size_t n) override
86  {
87  if (m_s.read(reinterpret_cast<char*>(d), n))
88  return n;
89  else
90  return 0;
91  }
92 };
93 
94 } // namespace mrpt::serialization
size_t write(const void *d, size_t n) override
Writes a block of bytes.
GLenum GLsizei n
Definition: glext.h:5136
STL namespace.
GLdouble s
Definition: glext.h:3682
size_t write(const void *d, size_t n) override
Writes a block of bytes.
size_t read(void *d, size_t n) override
Reads a block of bytes.
size_t read(void *d, size_t n) override
Reads a block of bytes.
CArchive for mrpt::io::CStream classes (use as template argument).
Definition: CArchive.h:570
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:54
size_t read(void *d, size_t n) override
Reads a block of bytes.
size_t write(const void *d, size_t n) override
Writes a block of bytes.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 45d659fbb Tue Dec 10 18:21:14 2019 +0100 at mar dic 10 18:30:09 CET 2019