AceUtils  0.2.1
Useful Arduino utilties which are too small for separate libraries.
List of all members
print_str::PrintStr< SIZE > Class Template Reference

An implementation of Print that writes to an in-memory buffer supporting strings less than 65535 in length. More...

#include <PrintStr.h>

Inheritance diagram for print_str::PrintStr< SIZE >:
Inheritance graph
[legend]
Collaboration diagram for print_str::PrintStr< SIZE >:
Collaboration graph
[legend]

Additional Inherited Members

- Public Member Functions inherited from print_str::PrintStrBase
size_t write (uint8_t c) override
 
size_t write (const uint8_t *buf, size_t size) override
 
void flush () override
 
const char * getCstr () const
 Return the NUL terminated c-string buffer. More...
 
size_t length () const
 Return the length of the internal c-string buffer.
 
- Protected Member Functions inherited from print_str::PrintStrBase
 PrintStrBase (uint16_t size, char *buf)
 
- Protected Attributes inherited from print_str::PrintStrBase
char *const buf_
 This is the pointer to the character array buffer. More...
 

Detailed Description

template<uint16_t SIZE>
class print_str::PrintStr< SIZE >

An implementation of Print that writes to an in-memory buffer supporting strings less than 65535 in length.

It is intended to be an alternative to the String class to help avoid heap fragmentation due to repeated creation and deletion of small String objects. The 'PrintStr' object inherit the methods from the 'Print' interface which can be used to build an internal string representation of various objects. Instead of using the operator+=() or the concat() method, use the print(), println() (or sometimes the printf() method) of the Print class. After the internal string is built, the NUL-terminated c-string representation can be retrieved using getCstr().

This object is expected to be created on the stack instead of the heap to avoid heap fragmentation. The SIZE parameter is a compile time constant, to allow the internal string buffer to be created on the stack. The object will be destroyed automatically when the stack is unwound after returning from the function where this is used.

An instance of PrintStr can be reused by calling the flush() method which causes the internal buffer to be cleared to an empty string. An instance of this object could be created statically and reused across different calling sites, but this was not the main intended use of this class.

Warning: The contents of getCstr() are valid only as long as the PrintStr object is alive. The pointer should never be passed to another part of the program if the PrintStr object is destroyed before the pointer is used.

Usage:

* #include <PrintStr.h>
*
* using namespace print_str;
*
* void fillStringA(PrintStr& message) {
*   message.print("hello, ");
*   message.println("world!");
* }
*
* void fillStringB(PrintStr& message) {
*   message.print("There are ");
*   message.print(42);
*   message.println(" apples");
* }
*
* void someFunction() {
*   PrintStr<32> message;
*   fillStringA(message)
*   const char* cstr = message.getCstr();
*   // do stuff with cstr
*
*   message.flush();
*   fillStringB(message);
*   cstr = message.getCstr();
*   // do more stuff with cstr
* }
* 
Template Parameters
SIZEsize of internal string buffer including the NUL terminator character, the maximum is 65535, which means the maximum string length is 65534 characters.

Definition at line 219 of file PrintStr.h.


The documentation for this class was generated from the following file: