AceCommon  1.1.2
Arduino library for low-level common functions and features with no external dependencies
List of all members
ace_common::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 ace_common::PrintStr< SIZE >:
Inheritance graph
[legend]
Collaboration diagram for ace_common::PrintStr< SIZE >:
Collaboration graph
[legend]

Additional Inherited Members

- Public Member Functions inherited from ace_common::PrintStrBase
size_t write (uint8_t c) override
 Write a single character into the internal buffer.
 
size_t write (const uint8_t *buf, size_t size) override
 Write the buf string of size into the internal buffer.
 
void flush () override
 Clear the internal buffer.
 
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 ace_common::PrintStrBase
 PrintStrBase (uint16_t size, char *buf)
 Constructor. More...
 
- Protected Attributes inherited from ace_common::PrintStrBase
char *const buf_
 This is the pointer to the character array buffer. More...
 

Detailed Description

template<uint16_t SIZE>
class ace_common::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 229 of file PrintStr.h.


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