MRPT  1.9.9
archiveFrom_std_vector.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 <cstdint>
13 #include <cstring>
14 #include <vector>
15 
16 namespace mrpt::serialization
17 {
18 /** Specialization for std::vector, suitable for read/write, using the
19  * `vector<uint8_t>` as the underlaying stream container. Writing always happen
20  * at the end of the vector. Reading starts at the beggining upon construction
21  * of this wrapper class (via `archiveFrom()`).
22  */
23 template <>
24 class CArchiveStreamBase<std::vector<uint8_t>> : public CArchive
25 {
26  std::vector<uint8_t>& m_v;
27  int m_pos_read{0};
28 
29  public:
30  CArchiveStreamBase(std::vector<uint8_t>& v) : m_v(v) {}
31 
32  protected:
33  size_t write(const void* d, size_t n) override
34  {
35  const size_t idx = m_v.size();
36  m_v.resize(idx + n);
37  ::memcpy(&m_v[idx], d, n);
38  return n;
39  }
40  size_t read(void* d, size_t n) override
41  {
42  const int avail = static_cast<int>(m_v.size()) - m_pos_read;
43  if (avail < static_cast<int>(n))
44  throw std::runtime_error(
45  "CArchiveStreamBase: EOF reading from std::vector!");
46  ::memcpy(d, &m_v[m_pos_read], n);
47  m_pos_read += n;
48  return n;
49  };
50 };
51 /** Read-only version of the wrapper. See archiveFrom() */
52 template <>
53 class CArchiveStreamBase<const std::vector<uint8_t>> : public CArchive
54 {
55  const std::vector<uint8_t>& m_v;
56  int m_pos_read{0};
57 
58  public:
59  CArchiveStreamBase(const std::vector<uint8_t>& v) : m_v(v) {}
60 
61  protected:
62  size_t write(const void* d, size_t n) override
63  {
64  throw std::runtime_error(
65  "CArchiveStreamBase: Attempt to write to read-only vector.");
66  }
67  size_t read(void* d, size_t n) override
68  {
69  const int avail = static_cast<int>(m_v.size()) - m_pos_read;
70  if (avail < static_cast<int>(n))
71  throw std::runtime_error(
72  "CArchiveStreamBase: EOF reading from std::vector!");
73  ::memcpy(d, &m_v[m_pos_read], n);
74  m_pos_read += n;
75  return n;
76  };
77 };
78 } // namespace mrpt::serialization
size_t read(void *d, size_t n) override
Reads a block of bytes.
GLenum GLsizei n
Definition: glext.h:5136
STL namespace.
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:565
const GLdouble * v
Definition: glext.h:3684
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:53
size_t write(const void *d, size_t n) override
Writes a block of bytes.
size_t write(const void *d, size_t n) override
Writes a block of bytes.
void memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) noexcept
An OS and compiler independent version of "memcpy".
Definition: os.cpp:358



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019