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



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