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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019