AceCommon  1.4.6
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 
106  void flush() /*override*/ {
107  index_ = 0;
108  }
109 
115  const char* cstr() const {
116  buf_[index_] = '\0';
117  return buf_;
118  }
119 
121  const char* getCstr() const { return cstr(); }
122 
124  size_t length() const { return index_; }
125 
126  protected:
133  PrintStrBase(uint16_t size, char* buf):
134  size_(size),
135  buf_(buf) {}
136 
137  private:
138  // Disable copy constructor and assignment operator
139  PrintStrBase(const PrintStrBase&) = delete;
140  PrintStrBase& operator=(const PrintStrBase&) = delete;
141 
142  // These member variables are declared together for more efficient packing
143  // on 32-bit processors.
144  uint16_t const size_;
145  uint16_t index_ = 0;
146 
147  protected:
169  char* const buf_;
170 };
171 
236 template <uint16_t SIZE>
237 class PrintStr: public PrintStrBase {
238  public:
239  PrintStr(): PrintStrBase(SIZE, actualBuf_) {}
240 
241  private:
242  char actualBuf_[SIZE];
243 };
244 
264 class PrintStrN: public PrintStrBase {
265  public:
267  PrintStrN(uint16_t size):
268  PrintStrBase(size, new char[size]) {}
269 
275  delete[] buf_;
276  }
277 };
278 
279 }
280 
281 #endif
ace_common::PrintStrN
An alternate implementation of PrintStr that allocates the character array in the heap,...
Definition: PrintStr.h:264
ace_common::PrintStrN::~PrintStrN
~PrintStrN()
Delete the internal buffer on the heap.
Definition: PrintStr.h:274
ace_common::PrintStr
An implementation of Print that writes to an in-memory buffer supporting strings less than 65535 in l...
Definition: PrintStr.h:237
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:133
ace_common::PrintStrBase::getCstr
const char * getCstr() const
Backwards compatible version of cstr().
Definition: PrintStr.h:121
ace_common::PrintStrBase::length
size_t length() const
Return the length of the internal c-string buffer.
Definition: PrintStr.h:124
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:267
ace_common::PrintStrBase::cstr
const char * cstr() const
Return the NUL terminated c-string buffer.
Definition: PrintStr.h:115
ace_common::PrintStrBase::buf_
char *const buf_
This is the pointer to the character array buffer.
Definition: PrintStr.h:169
ace_common::PrintStrBase
Base class for all template instances of the PrintStr<SIZE> class.
Definition: PrintStr.h:63
ace_common::PrintStrBase::flush
void flush()
Clear the internal buffer.
Definition: PrintStr.h:106