-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathMemoryStream.hpp
More file actions
66 lines (49 loc) · 1.57 KB
/
MemoryStream.hpp
File metadata and controls
66 lines (49 loc) · 1.57 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// StreamUtils - github.com/bblanchon/ArduinoStreamUtils
// Copyright Benoit Blanchon 2019-2024
// MIT License
#pragma once
#include <Stream.h>
#include "../Buffers/CircularBuffer.hpp"
#include "../Configuration.hpp"
#include "../Ports/DefaultAllocator.hpp"
namespace StreamUtils {
template <typename TAllocator>
class BasicMemoryStream : public Stream {
public:
BasicMemoryStream(size_t capacity, TAllocator allocator = TAllocator())
: _buffer(capacity, allocator) {}
BasicMemoryStream(const BasicMemoryStream &src) : _buffer(src._buffer) {}
int available() override {
return static_cast<int>(_buffer.available());
}
int peek() override {
return _buffer.isEmpty() ? -1 : static_cast<unsigned char>(_buffer.peek());
}
int read() override {
return _buffer.isEmpty() ? -1 : static_cast<unsigned char>(_buffer.read());
}
#if STREAMUTILS_STREAM_READBYTES_IS_VIRTUAL
size_t readBytes(char *data, size_t size) override {
return _buffer.readBytes(data, size);
}
#endif
size_t write(uint8_t data) override {
return _buffer.isFull() ? 0 : _buffer.write(data);
}
#if STREAMUTILS_PRINT_WRITE_VOID_UINT32
size_t write(const void *p, uint32 size) override {
const uint8_t *data = reinterpret_cast<const uint8_t *>(p);
#else
size_t write(const uint8_t *data, size_t size) override {
#endif
return _buffer.write(data, size);
}
using Stream::write;
void flush() override {
_buffer.clear();
}
private:
CircularBuffer<TAllocator> _buffer;
};
using MemoryStream = BasicMemoryStream<DefaultAllocator>;
} // namespace StreamUtils