-
Notifications
You must be signed in to change notification settings - Fork 1
/
buffer.h
43 lines (36 loc) · 979 Bytes
/
buffer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#ifndef BUFFER_H
#define BUFFER_H
#include <mutex>
#include <vector>
class Buffer {
public:
void putData(std::vector<uint8_t> in) {
std::lock_guard<std::mutex>l (_lock);
_data.insert(_data.end(), in.begin(), in.end());
}
bool isEmpty() {
std::lock_guard<std::mutex>l (_lock);
return _data.empty();
}
int size() {
std::lock_guard<std::mutex>l (_lock);
return _data.size();
}
std::vector<uint8_t> pop(int size) {
std::lock_guard<std::mutex>l (_lock);
if(_data.size() >= size) {
auto item = std::vector<uint8_t>(_data.begin(), _data.begin() + size);
_data.erase(_data.begin(), _data.begin() + size);
return item;
}
return std::vector<uint8_t>();
}
void clear() {
std::lock_guard<std::mutex>l (_lock);
_data.clear();
}
private:
std::vector<uint8_t> _data;
std::mutex _lock;
};
#endif // BUFFER_H