MRPT  1.9.9
MT_buffer.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 
11 #include <vector>
12 #include <cstdint>
13 #include <thread>
14 #include <mutex>
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() {}
31  /** Destructor */
32  virtual ~MT_buffer() {}
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 }
79 
virtual ~MT_buffer()
Destructor.
Definition: MT_buffer.h:32
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
GLdouble s
Definition: glext.h:3676
This class is a bulk sequence of bytes with MultiThread (MT)-safe read and write operations.
Definition: MT_buffer.h:22
MT_buffer()
Default constructor.
Definition: MT_buffer.h:30
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 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020