Main MRPT website > C++ reference for MRPT 1.9.9
archiveFrom_std_vector.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 <vector>
13 #include <cstdint>
14 #include <cstring>
15 
16 namespace mrpt
17 {
18 namespace serialization
19 {
20 /** Specialization for std::vector, suitable for read/write, using the
21  * `vector<uint8_t>` as the underlaying stream container. Writing always happen
22  * at the end of the vector. Reading starts at the beggining upon construction
23  * of this wrapper class (via `archiveFrom()`).
24  */
25 template <>
26 class CArchiveStreamBase<std::vector<uint8_t>> : public CArchive
27 {
28  std::vector<uint8_t>& m_v;
29  int m_pos_read{0};
30 
31  public:
32  CArchiveStreamBase(std::vector<uint8_t>& v) : m_v(v) {}
33  protected:
34  size_t write(const void* d, size_t n) override
35  {
36  const size_t idx = m_v.size();
37  m_v.resize(idx + n);
38  ::memcpy(&m_v[idx], d, n);
39  return n;
40  }
41  size_t read(void* d, size_t n) override
42  {
43  const int avail = static_cast<int>(m_v.size()) - m_pos_read;
44  if (avail < static_cast<int>(n))
45  throw std::runtime_error(
46  "CArchiveStreamBase: EOF reading from std::vector!");
47  ::memcpy(d, &m_v[m_pos_read], n);
48  m_pos_read += n;
49  return n;
50  };
51 };
52 /** Read-only version of the wrapper. See archiveFrom() */
53 template <>
54 class CArchiveStreamBase<const std::vector<uint8_t>> : public CArchive
55 {
56  const std::vector<uint8_t>& m_v;
57  int m_pos_read{0};
58 
59  public:
60  CArchiveStreamBase(const std::vector<uint8_t>& v) : m_v(v) {}
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 serialization
79 } // namespace mrpt
n
GLenum GLsizei n
Definition: glext.h:5074
mrpt::serialization::CArchiveStreamBase< std::vector< uint8_t > >::write
size_t write(const void *d, size_t n) override
Writes a block of bytes.
Definition: archiveFrom_std_vector.h:34
mrpt::serialization::CArchiveStreamBase
CArchive for mrpt::io::CStream classes (use as template argument).
Definition: CArchive.h:546
mrpt::serialization::CArchiveStreamBase< const std::vector< uint8_t > >::read
size_t read(void *d, size_t n) override
Reads a block of bytes.
Definition: archiveFrom_std_vector.h:67
mrpt::serialization::CArchiveStreamBase< std::vector< uint8_t > >::CArchiveStreamBase
CArchiveStreamBase(std::vector< uint8_t > &v)
Definition: archiveFrom_std_vector.h:32
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::serialization::CArchive
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:48
v
const GLdouble * v
Definition: glext.h:3678
mrpt::serialization::CArchiveStreamBase< const std::vector< uint8_t > >::m_v
const std::vector< uint8_t > & m_v
Definition: archiveFrom_std_vector.h:56
mrpt::serialization::CArchiveStreamBase< const std::vector< uint8_t > >::write
size_t write(const void *d, size_t n) override
Writes a block of bytes.
Definition: archiveFrom_std_vector.h:62
mrpt::serialization::CArchiveStreamBase< std::vector< uint8_t > >::read
size_t read(void *d, size_t n) override
Reads a block of bytes.
Definition: archiveFrom_std_vector.h:41
CArchive.h
mrpt::serialization::CArchiveStreamBase< std::vector< uint8_t > >::m_v
std::vector< uint8_t > & m_v
Definition: archiveFrom_std_vector.h:28
mrpt::system::os::memcpy
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:356
mrpt::serialization::CArchiveStreamBase< const std::vector< uint8_t > >::CArchiveStreamBase
CArchiveStreamBase(const std::vector< uint8_t > &v)
Definition: archiveFrom_std_vector.h:60



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST