|
Boost : |
Subject: Re: [boost] [explore] Library Proposal: Container Streaming
From: Michael Caisse (boost_at_[hidden])
Date: 2009-12-01 01:00:02
Jeffrey Faust wrote:
> Jared McIntyre, Danny Havenith, and myself, with support from Jeff
> Garland have developed a library to stream standard containers to output
> streams. We would like to submit this for review. The requirements for
> this library were gathered at the BoostCon 2007 Library in a Week
> sessions. An initial review was held and further requirements were
> gathered at BoostCon 2008.
>
>
I've been trying to think of when I might use such a library. Without
the symmetry (input as well as output) I can think of few instances
where this functionality might come in handy. Other than potential debug
output, what else do you envision? What am I missing?
I'm also finding little improvement over the following example
based on your samples. Here I am using Karma to produce the same
output as Explore. It is a bit more verbose; however, it is infinitely
more flexible.
Best regards -
michael
-------------------------------------------------------------------
#include <boost/config/warning_disable.hpp>
#include <iostream>
#include <vector>
#include <map>
#include <list>
#include <boost/array.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/fusion/include/std_pair.hpp>
#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/karma_format.hpp>
#include <boost/spirit/include/karma_stream.hpp>
using boost::spirit::stream;
using boost::spirit::karma::format;
int main()
{
// simple vector example
std::vector<int> vi;
vi.push_back(1);
vi.push_back(2);
vi.push_back(3);
std::cout << format('[' << (stream % ", " ) << ']' , vi ); // prints [1, 2, 3]
// lets do some containers in containers
std::vector<std::vector<int> > vvi;
vvi.push_back(vi);
vvi.push_back(vi);
std::cout << format('[' << *( '[' << (stream % ", " ) << ']' ) << ']' , vvi); // prints [[1, 2, 3][1, 2, 3]]
// associative containers
std::map<std::string, int> si_map;
si_map["hello"] = 1;
si_map["world"] = 2;
std::cout << format('[' << ( (stream << ':' << stream) % ", " ) << ']' , si_map); // prints [hello:1, world:2]
// containers of complex types
using namespace boost::gregorian;
std::list<date> dl; // date from boost::gregorian
dl.push_back(date(2007, Jan, 1));
dl.push_back(date(2007, Jan, 3));
std::cout << format('[' << stream % ", " << ']' , dl); // prints [2007-Jan-1, 2007-Jan-3]
// how about some boost container types:
boost::array<std::string, 2> sa2;
sa2[0] = "one";
sa2[1] = "two";
std::cout << format( '[' << stream % ", " << ']' , sa2); // prints [one, two]
}
-- ---------------------------------- Michael Caisse Object Modeling Designs www.objectmodelingdesigns.com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk