AceCommon  1.1.2
Arduino library for low-level common functions and features with no external dependencies
PrintStr.h
1 /*
2 MIT License
3 
4 Copyright (c) 2020 Brian T. Park
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 
25 #ifndef PRINT_STR_PRINT_STR_H
26 #define PRINT_STR_PRINT_STR_H
27 
28 #include <stddef.h> // size_t
29 #include <Print.h>
30 
31 namespace ace_common {
32 
63 class PrintStrBase: public Print {
64  public:
66  size_t write(uint8_t c) override {
67  if (index_ < size_ - 1) {
68  buf_[index_] = c;
69  index_++;
70  return 1;
71  } else {
72  return 0;
73  }
74  }
75 
77  size_t write(const uint8_t *buf, size_t size) override {
78  if (buf == nullptr) return 0;
79 
80  size_t n = 0;
81  while (size-- > 0) {
82  size_t ret = write(*buf++);
83  if (ret == 0) break;
84  n++;
85  }
86  return n;
87  }
88 
89 // ESP32 version of Print class does not define a virtual flush() method so
90 // we can't use the 'override' keyword.
91 #ifdef ESP32
92 
93  void flush() {
94  index_ = 0;
95  }
96 #else
97 
98  void flush() override {
99  index_ = 0;
100  }
101 #endif
102 
108  const char* getCstr() const {
109  buf_[index_] = '\0';
110  return buf_;
111  }
112 
114  size_t length() const {
115  return index_;
116  }
117 
118  protected:
125  PrintStrBase(uint16_t size, char* buf):
126  size_(size),
127  buf_(buf) {}
128 
129  private:
130  // Disable copy constructor and assignment operator
131  PrintStrBase(const PrintStrBase&) = delete;
132  PrintStrBase& operator=(const PrintStrBase&) = delete;
133 
134  // These member variables are declared together for more efficient packing
135  // on 32-bit processors.
136  uint16_t const size_;
137  uint16_t index_ = 0;
138 
139  protected:
161  char* const buf_;
162 };
163 
228 template <uint16_t SIZE>
229 class PrintStr: public PrintStrBase {
230  public:
231  PrintStr(): PrintStrBase(SIZE, actualBuf_) {}
232 
233  private:
234  char actualBuf_[SIZE];
235 };
236 
256 class PrintStrN: public PrintStrBase {
257  public:
259  PrintStrN(uint16_t size):
260  PrintStrBase(size, new char[size]) {}
261 
267  delete[] buf_;
268  }
269 };
270 
271 }
272 
273 #endif
ace_common::PrintStrN
An alternate implementation of PrintStr that allocates the character array in the heap,...
Definition: PrintStr.h:256
ace_common::PrintStrN::~PrintStrN
~PrintStrN()
Delete the internal buffer on the heap.
Definition: PrintStr.h:266
ace_common::PrintStr
An implementation of Print that writes to an in-memory buffer supporting strings less than 65535 in l...
Definition: PrintStr.h:229
ace_common::PrintStrBase::write
size_t write(const uint8_t *buf, size_t size) override
Write the buf string of size into the internal buffer.
Definition: PrintStr.h:77
ace_common::PrintStrBase::PrintStrBase
PrintStrBase(uint16_t size, char *buf)
Constructor.
Definition: PrintStr.h:125
ace_common::PrintStrBase::getCstr
const char * getCstr() const
Return the NUL terminated c-string buffer.
Definition: PrintStr.h:108
ace_common::PrintStrBase::length
size_t length() const
Return the length of the internal c-string buffer.
Definition: PrintStr.h:114
ace_common::PrintStrBase::write
size_t write(uint8_t c) override
Write a single character into the internal buffer.
Definition: PrintStr.h:66
ace_common::PrintStrN::PrintStrN
PrintStrN(uint16_t size)
Create an instance with an internal buffer of size on the heap.
Definition: PrintStr.h:259
ace_common::PrintStrBase::buf_
char *const buf_
This is the pointer to the character array buffer.
Definition: PrintStr.h:161
ace_common::PrintStrBase
Base class for all template instances of the PrintStr<SIZE> class.
Definition: PrintStr.h:63
ace_common::PrintStrBase::flush
void flush() override
Clear the internal buffer.
Definition: PrintStr.h:98