I have benefited greatly from criticism, and at no time have I suffered a lack thereof.
Winston Churchill
I'm so gorgeous, there's a six-month waiting list for birds to suddenly appear, every time I am near!
Cat, Red Dwarf
Introduction
Do you ever find yourself wanting to output the contents of a sequence where the elements are to be indented, perhaps by a tab space ('\ t'
), as in the following example?
Header Files: H:\ freelibs\ b64\ current\ include\ b64\ b64.h H:\ freelibs\ b64\ current\ include\ b64\ cpp\ b64.hpp Implementation Files: H:\ freelibs\ b64\ current\ src\ b64.c H:\ freelibs\ b64\ current\ test\C\C.c H:\ freelibs\ b64\ current\ test\Cpp\ Cpp.cpp
Using std::ostream_iterator
, this is disproportionately difficult and inelegant to achieve. Consider that we're searching for source files under the current directory, using the recls/STL library (itself an adaptation of a collection in what should now be, after reading Part II, characteristic STL extension style). recls/STL provides the recls::stl::search_sequence
collection (a typedef of recls::stl::basic_search_sequence<char>
), to which we pass the search directory, pattern, and flags. We can use this in combination with std::copy()
and std::ostream_iterator
, as shown in Listing One, to achieve the desired output.
Listing One: Formatting Output Using std::ostream_iterator
typedef recls::stl::search_sequence srchseq_t; using recls::RECLS_F_RECURSIVE; srchseq_t headers(".", "*.h|*.hpp", RECLS_F_RECURSIVE); srchseq_t impls(".", "*.c|*.cpp", RECLS_F_RECURSIVE); std::cout << "Header Files:" << std::endl << "\ t"; std::copy(headers.begin(), headers.end() , std::ostream_iterator<srchseq_t::value_type>(std::cout, "\ n\ t")); std::cout << "\ r"; std::cout << "Implementation Files:" << std::endl << "\ t"; std::copy(impls.begin(), impls.end() , std::ostream_iterator<srchseq_t::value_type>(std::cout, "\ n\ t")); std::cout << "\n";
Obviously, there's a degree of mess here in that the formatting we seek to apply on lines 89 and 1314 leaks out into lines 7, 10, 12. and 15. I'm certain you can imagine how, in more complex cases, this can lead to convoluted and fragile code, something that would just not happen were std::ostream_iterator
a tiny bit smarter. This chapter describes how std::ostream_iterator
can be enhanced in a simple but crucial way, in the form of stlsoft::ostream_ iterator
.
Before we look in depth at the problem and the simple solution, let's see that solution in action (Listing Two).
Listing Two: Formatting Output Using stlsoft::ostream_iterator
typedef recls::stl::search_sequence srchseq_t; using recls::RECLS_F_RECURSIVE; srchseq_t headers(".", "*.h|*.hpp", RECLS_F_RECURSIVE); srchseq_t impls(".", "*.c|*.cpp", RECLS_F_RECURSIVE); std::cout << "Header Files:" << std::endl; std::copy(headers.begin(), headers.end() , stlsoft::ostream_iterator<srchseq_t::value_type>(std::cout , "\ t", "\ n")); std::cout << "Implementation Files:" << std::endl; std::copy(impls.begin(), impls.end() , stlsoft::ostream_iterator<srchseq_t::value_type>(std::cout , "\t","\ n"));
Now the formatting is entirely located where it should be, in the invocation of the iterator's constructor. Naturally, this component can be used easily in formatted output that employs different levels of indentation.
(If we wanted to be especially pious with regards to DRY SPOT, we might declare a single instance of stlsoft::ostream_iterator
with the required prefix and suffix and pass it to the two invocations of std::copy()
. But that's not as clear-cut as you might think, as we'll shortly see.)