MRPT  2.0.1
MT_buffer.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 
11 #include <cstdint>
12 #include <mutex>
13 #include <thread>
14 #include <vector>
15 
16 namespace mrpt::containers
17 {
18 /** This class is a bulk sequence of bytes with MultiThread (MT)-safe read and
19  * write operations.
20  * \ingroup stlext_grp
21  */
22 class MT_buffer
23 {
24  private:
25  std::vector<uint8_t> m_data;
26  std::mutex m_cs;
27 
28  public:
29  /** Default constructor */
30  MT_buffer() = default;
31  /** Destructor */
32  virtual ~MT_buffer() = default;
33  /** Empty the buffer */
34  void clear()
35  {
36  m_cs.lock();
37  m_data.clear();
38  m_cs.unlock();
39  }
40 
41  /** Return the number of available bytes at this moment. */
42  size_t size()
43  {
44  size_t s;
45  m_cs.lock();
46  s = m_data.size();
47  m_cs.unlock();
48  return s;
49  }
50 
51  /** Append new data to the stream */
52  void appendData(const std::vector<uint8_t>& d)
53  {
54  m_cs.lock();
55  m_data.insert(m_data.begin(), d.begin(), d.end());
56  m_cs.unlock();
57  }
58 
59  /** Read the whole buffer and empty it. */
60  void readAndClear(std::vector<uint8_t>& d)
61  {
62  m_cs.lock();
63  d.clear();
64  m_data.swap(d);
65  m_cs.unlock();
66  }
67 
68  /** Read the whole buffer. */
69  void read(std::vector<uint8_t>& d)
70  {
71  m_cs.lock();
72  d = m_data;
73  m_cs.unlock();
74  }
75 
76 }; // end of MT_buffer
77 
78 } // namespace mrpt::containers
virtual ~MT_buffer()=default
Destructor.
void appendData(const std::vector< uint8_t > &d)
Append new data to the stream.
Definition: MT_buffer.h:52
void read(std::vector< uint8_t > &d)
Read the whole buffer.
Definition: MT_buffer.h:69
std::vector< uint8_t > m_data
Definition: MT_buffer.h:25
This class is a bulk sequence of bytes with MultiThread (MT)-safe read and write operations.
Definition: MT_buffer.h:22
MT_buffer()=default
Default constructor.
void readAndClear(std::vector< uint8_t > &d)
Read the whole buffer and empty it.
Definition: MT_buffer.h:60
void clear()
Empty the buffer.
Definition: MT_buffer.h:34
size_t size()
Return the number of available bytes at this moment.
Definition: MT_buffer.h:42



Page generated by Doxygen 1.8.14 for MRPT 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020