MRPT  1.9.9
archiveFrom_std_streams.h
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 #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  protected:
27  size_t write(const void* d, size_t n) override
28  {
29  throw std::runtime_error(
30  "CArchiveStreamBase<std::istream>:"
31  "cannot write to an input stream.");
32  }
33  size_t read(void* d, size_t n) override
34  {
35  if (m_s.read(reinterpret_cast<char*>(d), n))
36  return n;
37  else
38  return 0;
39  }
40 };
41 
42 /** Specialization for std::ostream */
43 template <>
44 class CArchiveStreamBase<std::ostream> : public CArchive
45 {
46  std::ostream& m_s;
47 
48  public:
49  CArchiveStreamBase(std::ostream& s) : m_s(s) {}
50  protected:
51  size_t write(const void* d, size_t n) override
52  {
53  if (m_s.write(reinterpret_cast<const char*>(d), n))
54  return n;
55  else
56  return 0;
57  }
58  size_t read(void* d, size_t n) override
59  {
60  throw std::runtime_error(
61  "CArchiveStreamBase<std::ostream>:"
62  "cannot read from output stream.");
63  }
64 };
65 
66 /** Specialization for std::iostream */
67 template <>
68 class CArchiveStreamBase<std::iostream> : public CArchive
69 {
70  std::iostream& m_s;
71 
72  public:
73  CArchiveStreamBase(std::iostream& s) : m_s(s) {}
74  protected:
75  size_t write(const void* d, size_t n) override
76  {
77  if (m_s.write(reinterpret_cast<const char*>(d), n))
78  return n;
79  else
80  return 0;
81  }
82  size_t read(void* d, size_t n) override
83  {
84  if (m_s.read(reinterpret_cast<char*>(d), n))
85  return n;
86  else
87  return 0;
88  }
89 };
90 
91 }
92 
size_t write(const void *d, size_t n) override
Writes a block of bytes.
GLenum GLsizei n
Definition: glext.h:5074
STL namespace.
GLdouble s
Definition: glext.h:3676
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:540
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:52
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: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020