#ifndef WRITER_H #define WRITER_H #include struct Writer { template std::basic_ostream& operator()(std::basic_ostream& o,const value_type &l) const { return o << l; } }; struct LineWriter { template std::basic_ostream& operator()(std::basic_ostream& o,const Label &l) const { return o << l << std::endl; } }; template inline std::ios_base::iostate write_range_iostate(O& o,T begin, T end,W writer,bool multiline=false,bool parens=true,char open_paren='(',char close_paren=')',char space=' ') { static const char *const MULTILINE_SEP="\n"; if (parens) { o << open_paren; if (multiline) o << MULTILINE_SEP; } if (multiline) { for (;begin!=end;++begin) { o << space; writer(o,*begin); o << MULTILINE_SEP; } } else { for (T i=begin;i!=end;++i) { if (i!=begin) o< struct range_formatter { Ib i; Ie e; W w; bool multiline; bool parens; range_formatter(Ib i,Ie e,W w=W(),bool multiline=false,bool parens=true) : i(i),e(e),w(w),multiline(multiline),parens(parens) {} template std::basic_ostream & operator()(std::basic_ostream &o) const { write_range_iostate(o,i,e,w,multiline,parens); return o; } template friend inline std::basic_ostream & operator<<(std::basic_ostream &o,range_formatter const& w) { return w(o); } }; template range_formatter wrange(Ib i,Ie e,W const& w,bool multiline=false,bool parens=true) { return range_formatter(i,e,w,multiline,parens); } template range_formatter prange(Ib i,Ie e,bool multiline=false,bool parens=true) { return range_formatter(i,e,Writer(),multiline,parens); } template inline std::basic_ostream & write_range(std::basic_ostream& o,T begin, T end,W writer,bool multiline=false,bool parens=true,char open_paren='(',char close_paren=')') { write_range_iostate(o,begin,end,writer,multiline,parens,open_paren,close_paren); return o; } template inline std::ios_base::iostate print_range(O& o,T begin,T end,bool multiline=false,bool parens=true,char open_paren='(',char close_paren=')') { return write_range_iostate(o,begin,end,Writer(),multiline,parens,open_paren,close_paren); } template inline std::ios_base::iostate print_range_i(O& o,C const&c,unsigned from,unsigned to,bool multiline=false,bool parens=true,char open_paren='(',char close_paren=')') { return write_range_iostate(o,c.begin()+from,c.begin()+to,Writer(),multiline,parens,open_paren,close_paren); } template struct bound_printer { O *po; template void operator()(T const& t) const { *po << t; } }; template bound_printer make_bound_printer(O &o) { bound_printer ret; ret.po=&o; return ret; } template struct bound_writer { W const& w; bound_writer(W const& w) : w(w) {} bound_writer(bound_writer const& o) :w(o.w) {} template void operator()(O &o,V const& v) const { v.print(o,w); } }; template bound_writer make_bound_writer(W const& w) { return bound_writer(w); } #endif