Boost logo

Boost :

From: Jeremy Siek (jsiek_at_[hidden])
Date: 2001-03-07 12:51:33


Hi Craig,

I think what you are describing is something I recently added to the soon
to be released iterator adaptors library: function_output_iterator.

In the boost CVS, see boost/boost/function_output_iterator.hpp
and for documentation see boost/libs/utility/function_output_iterator.htm

This version currently does not have a default function that uses
operator<<. I'm not sure what the utility of that is, since there is
already an ostream_iterator adaptor.

Also it does not have the third template parameter that your class does.
It seems to me that the splitting into two functions is not really
necessary at the function_output_iterator level... after all, in your
example, you could remove the FT and just use the FI.

Cheers,
Jeremy

On Thu, 8 Mar 2001, Craig Hicks wrote:

hicks> Hello
hicks>
hicks> The following class, "os_iter", expounds upon the idea of
hicks> std::ostream_iterator,
hicks> which can be used with std::copy for writing to an output stream.
hicks>
hicks> std::copy(x.begin(), x.end(), std::ostream_iterator<T>(os));
hicks> std::copy(x.begin(), x.end(), os_iter<T, FT, FI>(os));
hicks>
hicks> The main template argument T of ostream_iterator is a type to print, and
hicks> operator << (etc)
hicks> must be defined for T.
hicks>
hicks> The first template argument of os_iter is again T.
hicks>
hicks> The second, FT, is a functional to print out T. It defaults to
hicks> "os_iter_def_ft", which just calls operator << (etc).
hicks> I have found this very useful because my compiler often cannot see the
hicks> declaration of operator << (etc)
hicks> from inside a template function. This is especially a problem when using
hicks> namespaces. Also, it allows custom
hicks> output formatting for that special occasion.
hicks>
hicks> The third template parameter, FI. is used to print things around T, like a
hicks> line number (e.g., [3]) at the line start
hicks> and newline at the end. (The default functional "os_iter_def_fi" does
hicks> exactly this.)
hicks> This is more general than passing a single end of line character or string.
hicks>
hicks> Here is typical output:
hicks> [0] 23
hicks> [1] 13
hicks> [2] 16
hicks> etc
hicks> where [n] is output by FI and 23, 13, 16 are output by FT.
hicks>
hicks> Important details such as
hicks> value_type;
hicks> char_type;
hicks> traits_type;
hicks> ostream_type;
hicks> are all missing in this version, although I am able to use it successfully
hicks> without them.
hicks>
hicks> If anyone has thoughts/code along similar lines please let me know.
hicks>
hicks> Respectfully Yours
hicks>
hicks> Craig Hicks
hicks> hicks_at_[hidden]
hicks>
hicks>
hicks> CODE FROM HERE::
hicks>
hicks> #ifndef stdex_os_iterH
hicks> #define stdex_os_iterH
hicks>
hicks> #include <iostream>
hicks>
hicks>
hicks>
hicks> template <class T>
hicks> struct os_iter_def_ft
hicks> {
hicks> typedef T value_type;
hicks> void operator () (std::ostream& os, const typename T& t)
hicks> {
hicks> os << t;
hicks> }
hicks> };
hicks>
hicks> template <class T, class FT=os_iter_def_ft<T> >
hicks> struct os_iter_def_fi
hicks> {
hicks> FT ft;
hicks> int i;
hicks> void operator () (std::ostream& os, const typename T& t)
hicks> {
hicks> os << "[" << i++ << "]";
hicks> ft(os, t);
hicks> os << std::endl;
hicks> }
hicks> os_iter_def_fi() : i(0) {}
hicks> };
hicks>
hicks>
hicks> template
hicks> <class T,
hicks> class FT=os_iter_def_ft<T>,
hicks> class FI=os_iter_def_fi<T, FT> >
hicks> class os_iter
hicks> {
hicks> std::ostream& os;
hicks> FI fi;
hicks>
hicks> public:
hicks> typedef T value_type;
hicks>
hicks>
hicks> os_iter(std::ostream& _os) : os(_os) {}
hicks> os_iter<T,FT,FI>& operator= (const T& value)
hicks> {
hicks> fi(os, value);
hicks> return *this;
hicks> }
hicks>
hicks> os_iter<T,FT,FI>& operator* () { return *this; }
hicks> os_iter<T,FT,FI>& operator++ () { return *this; }
hicks> os_iter<T,FT,FI>& operator++ (int) { return *this; }
hicks> };
hicks>
hicks>
hicks>
hicks> #endif
hicks>
hicks>
hicks>
hicks> List-Unsubscribe: <mailto:boost-unsubscribe_at_[hidden]>
hicks>
hicks>
hicks> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
hicks>
hicks>
hicks>

----------------------------------------------------------------------
 Jeremy Siek www: http://www.lsc.nd.edu/~jsiek/
 Ph.D. Candidate email: jsiek_at_[hidden]
 Univ. of Notre Dame work phone: (219) 631-3906
----------------------------------------------------------------------


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk