AceUtils
0.1
Small and light Arduino utilties and libraries.
|
Base class for all template instances of the PrintString<SIZE> class. More...
#include <PrintString.h>
Public Member Functions | |
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 | |
PrintStringBase (uint16_t size, char *buf) | |
Base class for all template instances of the PrintString<SIZE> class.
A common base class reduces the code size because only one copy of the core code is included across all possible template instances. Otherwise, PrintString<10> is a different class than PrintString<20> and would pull in duplicate copies of the code.
Usually the Print
base class can be used to accept instances of the PrintString<SIZE>
objects. However, if you need access to the lenth()
method, then you need to use the PrintStringBase
class instead, since the Print
class does not have a length()
method.
Here are the actual numbers. I modified tests/CommonTest.ino program to use 2 template instances, PrintString<10> and PrintString<20> instead of just PrintString<10>. Without this base class optimization, the sketch uses:
After inserting this PrintStringBase class in the class hierarchy, the same sketch uses:
So we save 94 bytes of flash memory, and 12 bytes of static RAM. And even better, the program and RAM size was the same as using 2 PrintString<10> instances. In other words, the amount of flash and static RAM remains constant no matter how many template instances we create.
Definition at line 63 of file PrintString.h.
|
inline |
Return the NUL terminated c-string buffer.
After the buffer is no longer needed, the flush() method should be called to reset the internal buffer index to 0.
Definition at line 104 of file PrintString.h.