Boost logo

Boost :

From: Philippe Mori (philippe_mori_at_[hidden])
Date: 2005-01-08 09:54:34


----- Original Message -----
From: "christopher diggins" <cdiggins_at_[hidden]>
Newsgroups: gmane.comp.lib.boost.devel
Sent: Thursday, January 06, 2005 5:12 PM
Subject: Any interest in a library for supporting program reuse?

> Hi all, the compulsive coder here again,
>
> I've just written in a working prototype of a library for writing C++
> programs which can be treated in C++ code like Unix filters, i.e.
> redirected to and from streams, and other programs.
>
> For instance:
>
> fstream f("c:\\tmp.txt");
> stringstream s;
> HelloWorldProgram() > f > UpperCaseProgram() > s;
>

As some has point out, I think that it should not be necessary to
derive from a class with a virtual function. Unnecessary coupling.

I have take a look at the article on CodeProject and here
are my comments:

You might instead want to uses CRTP (Curiously Recurring
Template Pattern) or probably even better would be to uses
boost::function (or something similar).

    boost::function<void (istream &, ostream &)>

In fact, you could have a templatized manager class that
would even allows to specify the original and final stream:

template <typename IStream, typename OStream>
class Manager
{
public:
    // Initialize temporary stream for the next piping operation.
    void SetupStream();

    TmpIStreamType &get_istream() const;
    TmpOStreamType &get_ostream() const;
};

template <typename IStream, typename OStream,
    typename Function>
Manager<IStream, OStream> &operator|(Manager &mgr, Function f)
{
    mgr.SetupStream();
    f(mgr.get_istream(), mgr.get_ostream());
    return mgr;
}

*** Well, I think this should give an idea of a possible design.
In fact, we might need a temporary object so that
we would be able to write to the final stream but
we can also do it in the destructor (although this
might cause problem wwith exception safety if
an error occurs while writing to destination.

A simple solution would be to have an object that
would indicate to flush to the destination. This would
even allows to write to the target in part if after writing
and flushing the stream we leave it open (and the input
stream would be kept at the current position).

An exemple:

    Mgr(params) | Fn1 | Fn2 | Fn3 | Flush | Fn4 | Fn5 | Flush.

This would be similar to the uses of ends for STL stream
but eventually with the possibility of having more than one.

>
> Christopher Diggins


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