|
Evo C++ Library v0.5.1
|
String, Stream, and StreamOut have a common formatting interface, but not a common base class.
Still, common code can use these different types via template parameters.
String, Stream, and StreamOut support this common interface:
Format nested class for formatting with statewrite_out() to call themoperator<<() methods for formatting various typesThis& write_out() – Get reference to parent stream/string (that isn't a Format type)Format type so use write_out() to get stream/string:Size – Number type for data size parameters (always unsigned)Size writetext(const char* buf, Size size) – Write (append) text to stream/stringulong writefmtstr(const char* buf, ulong size, const FmtSetField& field) – Write (append) formatted string with field alignment to stream/stringbool writefmtdump(const FmtDump& fmt, Newline nl=NL) – Write (append) formatted data dump to stream/stringtemplate<class TNum>:bool writenum(TNum num, int base=fDEC) – Append formatted signed number to stream/stringbool writenumu(TNum num, int base=fDEC) – Append formatted unsigned number to stream/stringbool writenumf(TNum num, int precision=fPREC_AUTO) – Append formatted floating point number to stream/stringbool writefmtnum(TNum num, const FmtSetInt &fmt, const FmtSetField *field=NULL) – Append formatted signed number with field alignment to stream/stringbool writefmtnumu(TNum num, const FmtSetInt &fmt, const FmtSetField *field=NULL) – Append formatted unsigned number with field alignment to stream/stringbool writefmtnumf(TNum num, const FmtSetFloat &fmt, const FmtSetField *field=NULL) – Append formatted floating point number with field alignment to stream/stringchar* write_direct(Size size) – Setup to append directly to internal buffer of stream/stringchar* write_direct_multi(Size& available, Size reserve_size) – Setup to append directly to internal buffer of stream/string and allow multiple passes for larger datachar* write_direct_flush(Size& available, Size written_size, Size reserve_size) – Flush data written directly to internal buffer of stream/string and setup to append more with another passbool write_direct_finish(Size size) – Finish appending directly to internal buffer of stream/stringAdditional custom operator<<() support may be defined using global inline functions (see example below).
It can be useful to write a single function that can write formatted output to any Stream or String type. Strings are not a stream type but this is possible with a template function.
This shows a template function that writes directly to a String or Stream buffer.
Example:
Writing larger data sizes may require multiple passes with Stream types since they use a fixed size buffer:
This shows a custom class with a templated format() method that works with all string/stream types and defines a global string/stream output operator that uses the format() method.
1.8.13