Sming Framework API
Sming - Open Source framework for high efficiency WiFi SoC ESP8266 native development with C++ language.
CircularBuffer.h
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 #ifndef _SMING_CORE_DATA_BUFFER_CIRCULAR_BUFFER_H_
17 #define _SMING_CORE_DATA_BUFFER_CIRCULAR_BUFFER_H_
18 
19 #include "Stream/ReadWriteStream.h"
20 
28 class CircularBuffer : public ReadWriteStream
30 {
31 public:
32  CircularBuffer(int size) : buffer(new char[size]), readPos(buffer), writePos(buffer), size(size)
33  {
34  }
35 
37  {
38  delete[] buffer;
39  }
40 
45  StreamType getStreamType() const override
46  {
48  }
49 
56  uint16_t readMemoryBlock(char* data, int bufSize) override;
57 
62  bool seek(int len) override;
63 
67  bool isFinished() override
68  {
69  return available() < 1;
70  }
71 
76  int available() override;
77 
82  String id() const override
83  {
84  return String(reinterpret_cast<uint32_t>(&buffer), HEX);
85  }
86 
87  size_t write(uint8_t charToWrite) override;
88 
94  size_t write(const uint8_t* data, size_t size) override;
95 
99  size_t room() const;
100 
101  // Stream::flush()
102  void flush() override
103  {
104  readPos = buffer;
105  writePos = buffer;
106  }
107 
108 private:
109  inline char* wrap(char* ptr) const
110  {
111  return (ptr == buffer + size) ? buffer : ptr;
112  }
113 
114 private:
115  char* buffer = nullptr;
116  char* readPos = nullptr;
117  char* writePos = nullptr;
118  int size = 0;
119 };
120 
122 #endif /* _SMING_CORE_DATA_BUFFER_CIRCULAR_BUFFER_H_ */
bool seek(int len) override
Move read cursor.
Memory data stream.
Definition: DataSourceStream.h:24
The string class.
Definition: WString.h:104
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:29
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:45
String id() const override
Returns unique id of the resource.
Definition: CircularBuffer.h:82
bool isFinished() override
Check if stream is finished.
Definition: CircularBuffer.h:67
Base class for read/write stream.
Definition: ReadWriteStream.h:23