FastArduino  v1.8
C++ library to build fast but small Arduino/AVR projects
streambuf.h
Go to the documentation of this file.
1 // Copyright 2016-2021 Jean-Francois Poilpret
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
16 
21 #ifndef STREAMBUF_H
22 #define STREAMBUF_H
23 
24 #include "queue.h"
25 #include "flash.h"
26 #include "virtual.h"
27 
28 namespace streams
29 {
45  class ostreambuf : private containers::Queue<char, char>
46  {
47  private:
48  using QUEUE = Queue<char, char>;
49 
50  public:
51  ostreambuf(const ostreambuf&) = delete;
52  ostreambuf& operator=(const ostreambuf&) = delete;
53 
55 
56  template<uint8_t SIZE>
57  explicit ostreambuf(char (&buffer)[SIZE], CALLBACK callback = nullptr, void* arg = nullptr)
58  : QUEUE{buffer, true}, on_put_callback_{callback, arg} {}
59 
64  void pubsync()
65  {
66  overflow_ = false;
67  while (!empty()) time::yield();
68  }
69 
76  void sputc(char c)
77  {
78  put_(c, true);
79  }
80 
92  void sputn(const char* content, size_t size)
93  {
94  while (size--) put_(*content++, false);
95  on_put();
96  }
97 
109  void sputn(const char* str)
110  {
111  while (*str) put_(*str++, false);
112  on_put();
113  }
114 
132  void sputn(const flash::FlashStorage* str)
133  {
134  uint16_t address = (uint16_t) str;
135  while (char value = pgm_read_byte(address++)) put_(value, false);
136  on_put();
137  }
138 
145  bool overflow() const
146  {
147  return overflow_;
148  }
149 
154  QUEUE& queue()
155  {
156  return *this;
157  }
158 
159  protected:
170  void put_(char c, bool call_on_put = true)
171  {
172  if (!push(c)) overflow_ = true;
173  if (call_on_put) on_put();
174  }
175 
181  {
182  overflow_ = false;
183  }
184 
185  private:
186  void on_put()
187  {
188  on_put_callback_();
189  }
190 
191  bool overflow_ = false;
192  const virtual_support::VirtualMethod on_put_callback_;
193 
194  friend class ios_base;
195  friend class ostream;
196  };
197 
208  class istreambuf : private containers::Queue<char, char>
209  {
210  private:
211  using QUEUE = Queue<char, char>;
212 
213  public:
214  istreambuf(const istreambuf&) = delete;
215  istreambuf& operator=(const istreambuf&) = delete;
216 
220  static const int EOF = -1;
221 
222  template<uint8_t SIZE> explicit istreambuf(char (&buffer)[SIZE]) : QUEUE{buffer, false} {}
223 
227  int in_avail() const
228  {
229  return items();
230  }
231 
236  int sbumpc()
237  {
238  char value;
239  if (pull(value)) return value;
240  return EOF;
241  }
242 
247  int sgetc()
248  {
249  char value;
250  if (peek(value)) return value;
251  return EOF;
252  }
253 
258  QUEUE& queue()
259  {
260  return *this;
261  }
262  };
263 }
264 
265 #endif /* STREAMBUF_H */
266 
streams::ostreambuf::put_
void put_(char c, bool call_on_put=true)
Append a character to the buffer.
Definition: streambuf.h:170
containers::Queue< char, char >::size
uint8_t size() const
Get the maximum size of this queue.
Definition: queue.h:219
streams::istreambuf
Input API based on a ring buffer.
Definition: streambuf.h:209
containers::Queue< char, char >::pull
bool pull(T &item)
Pull an item from the beginning of this queue, if not empty, and copy it into item.
Definition: queue.h:333
containers::Queue
Queue of type T_ items.
Definition: queue.h:59
streams::ostreambuf::sputn
void sputn(const char *str)
Append a string to the buffer.
Definition: streambuf.h:109
virtual_support::VirtualMethod::METHOD
void(*)(void *) METHOD
The type of function that will get the call.
Definition: virtual.h:78
streams::ostreambuf
Output API based on a ring buffer.
Definition: streambuf.h:46
time::yield
void yield()
Utility method used by many FastArduino API in order to "yield" some processor time; concretely it ju...
Definition: time.cpp:22
streams::ostreambuf::reset_overflow
void reset_overflow()
Reset the overflow flag.
Definition: streambuf.h:180
streams::ostreambuf::sputn
void sputn(const char *content, size_t size)
Append several characters to the buffer.
Definition: streambuf.h:92
containers::Queue< char, char >::push
bool push(TREF item)
Push item to the end of this queue, provided there is still available space in its ring buffer.
Definition: queue.h:311
streams::istreambuf::sgetc
int sgetc()
Definition: streambuf.h:247
containers::Queue< char, char >::items
uint8_t items() const
Tell the current number of items currently present in this queue.
Definition: queue.h:432
streams::istreambuf::sbumpc
int sbumpc()
Definition: streambuf.h:236
streams::ostreambuf::queue
QUEUE & queue()
Return the underlying queue.
Definition: streambuf.h:154
queue.h
Utility API to handle ring-buffer queue containers.
virtual_support::VirtualMethod
Holder of a "virtual method".
Definition: virtual.h:70
streams::istreambuf::in_avail
int in_avail() const
Definition: streambuf.h:227
streams
Defines C++-like streams API, based on circular buffers for input or output.
Definition: empty_streams.h:34
streams::ostreambuf::sputn
void sputn(const flash::FlashStorage *str)
Append a string, stored on flash memory, to the buffer.
Definition: streambuf.h:132
virtual.h
Small utilities to emulate virtual methods without extra vtabl.
streams::ostreambuf::pubsync
void pubsync()
Wait until all buffer content has been pulled by a consumer.
Definition: streambuf.h:64
streams::istreambuf::EOF
static const int EOF
Special value returned by sbumpc() when buffer is empty.
Definition: streambuf.h:220
streams::ostreambuf::sputc
void sputc(char c)
Append a character to the buffer.
Definition: streambuf.h:76
containers::Queue< char, char >::empty
bool empty() const
Tell if this queue is currently empty.
Definition: queue.h:420
containers::Queue< char, char >::peek
bool peek(T &item) const
Peek an item from the beginning of this queue, if not empty, and copy it into item.
Definition: queue.h:356
streams::istreambuf::queue
QUEUE & queue()
Return the underlying queue.
Definition: streambuf.h:258
streams::ostreambuf::overflow
bool overflow() const
Indicate if a buffer overflow has occurred since last time pubsync() or reset_overflow() was called.
Definition: streambuf.h:145
flash.h
Flash memory utilities.