MRPT
2.0.4
mrpt
containers
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
mrpt::containers::MT_buffer::m_cs
std::mutex m_cs
Definition:
MT_buffer.h:26
mrpt::containers::MT_buffer::MT_buffer
MT_buffer()=default
Default constructor.
mrpt::containers::MT_buffer::clear
void clear()
Empty the buffer.
Definition:
MT_buffer.h:34
mrpt::containers::MT_buffer::readAndClear
void readAndClear(std::vector< uint8_t > &d)
Read the whole buffer and empty it.
Definition:
MT_buffer.h:60
mrpt::containers::MT_buffer::size
size_t size()
Return the number of available bytes at this moment.
Definition:
MT_buffer.h:42
mrpt::containers::MT_buffer::~MT_buffer
virtual ~MT_buffer()=default
Destructor.
mrpt::containers::MT_buffer::appendData
void appendData(const std::vector< uint8_t > &d)
Append new data to the stream.
Definition:
MT_buffer.h:52
mrpt::containers::MT_buffer::read
void read(std::vector< uint8_t > &d)
Read the whole buffer.
Definition:
MT_buffer.h:69
mrpt::containers::MT_buffer::m_data
std::vector< uint8_t > m_data
Definition:
MT_buffer.h:25
mrpt::containers::MT_buffer
This class is a bulk sequence of bytes with MultiThread (MT)-safe read and write operations.
Definition:
MT_buffer.h:23
mrpt::containers
Definition:
bimap.h:15
Page generated by
Doxygen 1.8.18
for MRPT 2.0.4 at Thu Sep 24 07:14:18 UTC 2020