Boost logo

Boost :

From: Jeremy Siek (jsiek_at_[hidden])
Date: 2001-03-07 13:01:07


I should point out one other difference. The function_output_iterator is
really a generalization of your os_iter, since the
function_output_iterator does not assume that the output is going to a
std::ostream, it just "output"'s each item to the function object. You
could implement your os_iter using the function_output_iterator by
creating a function object that uses a std::ostream& internally.

On Wed, 7 Mar 2001, Jeremy Siek wrote:

jsiek> Hi Craig,
jsiek>
jsiek> I think what you are describing is something I recently added to the soon
jsiek> to be released iterator adaptors library: function_output_iterator.
jsiek>
jsiek> In the boost CVS, see boost/boost/function_output_iterator.hpp
jsiek> and for documentation see boost/libs/utility/function_output_iterator.htm
jsiek>
jsiek> This version currently does not have a default function that uses
jsiek> operator<<. I'm not sure what the utility of that is, since there is
jsiek> already an ostream_iterator adaptor.
jsiek>
jsiek> Also it does not have the third template parameter that your class does.
jsiek> It seems to me that the splitting into two functions is not really
jsiek> necessary at the function_output_iterator level... after all, in your
jsiek> example, you could remove the FT and just use the FI.
jsiek>
jsiek> Cheers,
jsiek> Jeremy
jsiek>
jsiek>
jsiek> On Thu, 8 Mar 2001, Craig Hicks wrote:
jsiek>
jsiek> hicks> Hello
jsiek> hicks>
jsiek> hicks> The following class, "os_iter", expounds upon the idea of
jsiek> hicks> std::ostream_iterator,
jsiek> hicks> which can be used with std::copy for writing to an output stream.
jsiek> hicks>
jsiek> hicks> std::copy(x.begin(), x.end(), std::ostream_iterator<T>(os));
jsiek> hicks> std::copy(x.begin(), x.end(), os_iter<T, FT, FI>(os));
jsiek> hicks>
jsiek> hicks> The main template argument T of ostream_iterator is a type to print, and
jsiek> hicks> operator << (etc)
jsiek> hicks> must be defined for T.
jsiek> hicks>
jsiek> hicks> The first template argument of os_iter is again T.
jsiek> hicks>
jsiek> hicks> The second, FT, is a functional to print out T. It defaults to
jsiek> hicks> "os_iter_def_ft", which just calls operator << (etc).
jsiek> hicks> I have found this very useful because my compiler often cannot see the
jsiek> hicks> declaration of operator << (etc)
jsiek> hicks> from inside a template function. This is especially a problem when using
jsiek> hicks> namespaces. Also, it allows custom
jsiek> hicks> output formatting for that special occasion.
jsiek> hicks>
jsiek> hicks> The third template parameter, FI. is used to print things around T, like a
jsiek> hicks> line number (e.g., [3]) at the line start
jsiek> hicks> and newline at the end. (The default functional "os_iter_def_fi" does
jsiek> hicks> exactly this.)
jsiek> hicks> This is more general than passing a single end of line character or string.
jsiek> hicks>
jsiek> hicks> Here is typical output:
jsiek> hicks> [0] 23
jsiek> hicks> [1] 13
jsiek> hicks> [2] 16
jsiek> hicks> etc
jsiek> hicks> where [n] is output by FI and 23, 13, 16 are output by FT.
jsiek> hicks>
jsiek> hicks> Important details such as
jsiek> hicks> value_type;
jsiek> hicks> char_type;
jsiek> hicks> traits_type;
jsiek> hicks> ostream_type;
jsiek> hicks> are all missing in this version, although I am able to use it successfully
jsiek> hicks> without them.
jsiek> hicks>
jsiek> hicks> If anyone has thoughts/code along similar lines please let me know.
jsiek> hicks>
jsiek> hicks> Respectfully Yours
jsiek> hicks>
jsiek> hicks> Craig Hicks
jsiek> hicks> hicks_at_[hidden]
jsiek> hicks>
jsiek> hicks>
jsiek> hicks> CODE FROM HERE::
jsiek> hicks>
jsiek> hicks> #ifndef stdex_os_iterH
jsiek> hicks> #define stdex_os_iterH
jsiek> hicks>
jsiek> hicks> #include <iostream>
jsiek> hicks>
jsiek> hicks>
jsiek> hicks>
jsiek> hicks> template <class T>
jsiek> hicks> struct os_iter_def_ft
jsiek> hicks> {
jsiek> hicks> typedef T value_type;
jsiek> hicks> void operator () (std::ostream& os, const typename T& t)
jsiek> hicks> {
jsiek> hicks> os << t;
jsiek> hicks> }
jsiek> hicks> };
jsiek> hicks>
jsiek> hicks> template <class T, class FT=os_iter_def_ft<T> >
jsiek> hicks> struct os_iter_def_fi
jsiek> hicks> {
jsiek> hicks> FT ft;
jsiek> hicks> int i;
jsiek> hicks> void operator () (std::ostream& os, const typename T& t)
jsiek> hicks> {
jsiek> hicks> os << "[" << i++ << "]";
jsiek> hicks> ft(os, t);
jsiek> hicks> os << std::endl;
jsiek> hicks> }
jsiek> hicks> os_iter_def_fi() : i(0) {}
jsiek> hicks> };
jsiek> hicks>
jsiek> hicks>
jsiek> hicks> template
jsiek> hicks> <class T,
jsiek> hicks> class FT=os_iter_def_ft<T>,
jsiek> hicks> class FI=os_iter_def_fi<T, FT> >
jsiek> hicks> class os_iter
jsiek> hicks> {
jsiek> hicks> std::ostream& os;
jsiek> hicks> FI fi;
jsiek> hicks>
jsiek> hicks> public:
jsiek> hicks> typedef T value_type;
jsiek> hicks>
jsiek> hicks>
jsiek> hicks> os_iter(std::ostream& _os) : os(_os) {}
jsiek> hicks> os_iter<T,FT,FI>& operator= (const T& value)
jsiek> hicks> {
jsiek> hicks> fi(os, value);
jsiek> hicks> return *this;
jsiek> hicks> }
jsiek> hicks>
jsiek> hicks> os_iter<T,FT,FI>& operator* () { return *this; }
jsiek> hicks> os_iter<T,FT,FI>& operator++ () { return *this; }
jsiek> hicks> os_iter<T,FT,FI>& operator++ (int) { return *this; }
jsiek> hicks> };
jsiek> hicks>
jsiek> hicks>
jsiek> hicks>
jsiek> hicks> #endif
jsiek> hicks>
jsiek> hicks>
jsiek> hicks>
jsiek> hicks> List-Unsubscribe: <mailto:boost-unsubscribe_at_[hidden]>
jsiek> hicks>
jsiek> hicks>
jsiek> hicks> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
jsiek> hicks>
jsiek> hicks>
jsiek> hicks>
jsiek>
jsiek> ----------------------------------------------------------------------
jsiek> Jeremy Siek www: http://www.lsc.nd.edu/~jsiek/
jsiek> Ph.D. Candidate email: jsiek_at_[hidden]
jsiek> Univ. of Notre Dame work phone: (219) 631-3906
jsiek> ----------------------------------------------------------------------
jsiek>
jsiek>
jsiek>
jsiek> List-Unsubscribe: <mailto:boost-unsubscribe_at_[hidden]>
jsiek>
jsiek>
jsiek> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
jsiek>
jsiek>
jsiek>

----------------------------------------------------------------------
 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