CircularBuffer.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * CircularBuffer.h
8  *
9  * Initial code done by Ivan Grokhotkov as part of the esp8266 core for Arduino environment.
10  * https://github.com/esp8266/Arduino/blob/master/cores/esp8266/cbuf.h
11  *
12  * Adapted for Sming by Slavey Karadzhov <slaff@attachix.com>
13  *
14  ****/
15 
16 #pragma once
17 
19 
27 class CircularBuffer : public ReadWriteStream
29 {
30 public:
31  CircularBuffer(int size) : buffer(new char[size]), readPos(buffer), writePos(buffer), size(size)
32  {
33  }
34 
36  {
37  delete[] buffer;
38  }
39 
44  StreamType getStreamType() const override
45  {
47  }
48 
55  uint16_t readMemoryBlock(char* data, int bufSize) override;
56 
61  bool seek(int len) override;
62 
66  bool isFinished() override
67  {
68  return available() < 1;
69  }
70 
75  int available() override;
76 
81  String id() const override
82  {
83  return String(reinterpret_cast<uint32_t>(&buffer), HEX);
84  }
85 
86  size_t write(uint8_t charToWrite) override;
87 
93  size_t write(const uint8_t* data, size_t size) override;
94 
98  size_t room() const;
99 
100  // Stream::flush()
101  void flush() override
102  {
103  readPos = buffer;
104  writePos = buffer;
105  }
106 
107 private:
108  inline char* wrap(char* ptr) const
109  {
110  return (ptr == buffer + size) ? buffer : ptr;
111  }
112 
113 private:
114  char* buffer = nullptr;
115  char* readPos = nullptr;
116  char* writePos = nullptr;
117  int size = 0;
118 };
119 
bool seek(int len) override
Move read cursor.
Memory data stream.
Definition: DataSourceStream.h:24
#define HEX
Definition: WConstants.h:66
The String class.
Definition: WString.h:136
int available() override
Return the total length of the stream.
uint16_t readMemoryBlock(char *data, int bufSize) override
Read a block of memory.
size_t room() const
Get the maximum number of bytes for which write() will succeed.
Circular stream class.
Definition: CircularBuffer.h:28
~CircularBuffer()
Definition: CircularBuffer.h:35
CircularBuffer(int size)
Definition: CircularBuffer.h:31
StreamType
Data stream type.
Definition: DataSourceStream.h:22
size_t write(uint8_t charToWrite) override
Writes a single character to output stream.
StreamType getStreamType() const override
Get the stream type.
Definition: CircularBuffer.h:44
String id() const override
Returns unique id of the resource.
Definition: CircularBuffer.h:81
void flush() override
Definition: CircularBuffer.h:101
bool isFinished() override
Check if stream is finished.
Definition: CircularBuffer.h:66
Base class for read/write stream.
Definition: ReadWriteStream.h:22