Wednesday, June 2, 2010

format_iterator on Dr Dobb's

Dr. Dobb's have just published a new article, "C++ and format_iterator", describing the design and implementation of the FastFormat library's format_iterator component.

The article describes how FastFormat's flexiblity, expressiveness and type-safety has been combined with the output iterator concept  to produce the fastformat::format_iterator component. Applying STL-extension techniques described in Extended STL, volume 1, the format_iterator() component is able to create an output iterator that can receive arbitrate types, apply a format string or arbitrary complexity, and write to an arbitrary sink, as in:

 #include <fastformat/ff.hpp>
 #include <fastformat/iterators/format_iterator.hpp>
 #include <fastformat/sinks/ostream.hpp>

 int          numbers[] = { -2, -1, 0, 1, 2 };
 char const*  strings[] = { "abc", "def", "ghi" };
 std::string  prefix = "\t";
 char const*  suffix = "\n";

   numbers, numbers + 5
 , ff::format_iterator(std::cout, "[{0}]"));

 // outputs "[-2][-1][0][1][2]"

   strings, strings + 3
 , ff::format_iterator(std::cout, "{1}'{0}'{2}", prefix, suffix));

 // outputs:
 //     'abc'
 //     'def'
 //     'ghi'

This is the fourth article on the FastFormat library, following on from the introductory series of three published in ACCU's Overload magazine:

No comments:

Post a Comment