AceCommon  1.4.5
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 ACE_COMMON_PRINT_STR_H
26 #define ACE_COMMON_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 // Some versions of the Print class do not define a virtual flush() method so
90 // we can't use the 'override' keyword.
91 #if defined(ESP32) || defined(ARDUINO_ARCH_STM32)
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* cstr() const {
109  buf_[index_] = '\0';
110  return buf_;
111  }
112 
114  const char* getCstr() const { return cstr(); }
115 
117  size_t length() const { return index_; }
118 
119  protected:
126  PrintStrBase(uint16_t size, char* buf):
127  size_(size),
128  buf_(buf) {}
129 
130  private:
131  // Disable copy constructor and assignment operator
132  PrintStrBase(const PrintStrBase&) = delete;
133  PrintStrBase& operator=(const PrintStrBase&) = delete;
134 
135  // These member variables are declared together for more efficient packing
136  // on 32-bit processors.
137  uint16_t const size_;
138  uint16_t index_ = 0;
139 
140  protected:
162  char* const buf_;
163 };
164 
229 template <uint16_t SIZE>
230 class PrintStr: public PrintStrBase {
231  public:
232  PrintStr(): PrintStrBase(SIZE, actualBuf_) {}
233 
234  private:
235  char actualBuf_[SIZE];
236 };
237 
257 class PrintStrN: public PrintStrBase {
258  public:
260  PrintStrN(uint16_t size):
261  PrintStrBase(size, new char[size]) {}
262 
268  delete[] buf_;
269  }
270 };
271 
272 }
273 
274 #endif
ace_common::PrintStrN
An alternate implementation of PrintStr that allocates the character array in the heap,...
Definition: PrintStr.h:257
ace_common::PrintStrN::~PrintStrN
~PrintStrN()
Delete the internal buffer on the heap.
Definition: PrintStr.h:267
ace_common::PrintStr
An implementation of Print that writes to an in-memory buffer supporting strings less than 65535 in l...
Definition: PrintStr.h:230
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:126
ace_common::PrintStrBase::getCstr
const char * getCstr() const
Backwards compatible version of cstr().
Definition: PrintStr.h:114
ace_common::PrintStrBase::length
size_t length() const
Return the length of the internal c-string buffer.
Definition: PrintStr.h:117
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:260
ace_common::PrintStrBase::cstr
const char * cstr() const
Return the NUL terminated c-string buffer.
Definition: PrintStr.h:108
ace_common::PrintStrBase::buf_
char *const buf_
This is the pointer to the character array buffer.
Definition: PrintStr.h:162
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