AceUtils  0.1
Small and light Arduino utilties and libraries.
List of all members
print_string::PrintString< SIZE > Class Template Reference

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

#include <PrintString.h>

Inheritance diagram for print_string::PrintString< SIZE >:
Inheritance graph
[legend]
Collaboration diagram for print_string::PrintString< SIZE >:
Collaboration graph
[legend]

Additional Inherited Members

- Public Member Functions inherited from print_string::PrintStringBase
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_string::PrintStringBase
 PrintStringBase (uint16_t size, char *buf)
 

Detailed Description

template<uint16_t SIZE>
class print_string::PrintString< 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 'PrintString' 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 PrintString 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 PrintString object is alive. The pointer should never be passed to another part of the program if the PrintString object is destroyed before the pointer is used.

Usage:

* #include <print_string.h>
*
* using namespace print_string;
*
* void fillStringA(PrintString& message) {
*   message.print("hello, ");
*   message.println("world!");
* }
*
* void fillStringB(PrintString& message) {
*   message.print("There are ");
*   message.print(42);
*   message.println(" apples");
* }
*
* void someFunction() {
*   PrintString<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 204 of file PrintString.h.


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