Need help with my delimited stream wrapper class

Discussion in 'C++' started by rep_movsd, Aug 17, 2011.

  1. rep_movsd

    rep_movsd Guest

    I have a utility class that lets you do something like this :

    TDelimStream<char> ds(cerr, ", ");
    ds << 1 << 2 << 3 << 4;

    prints :
    1, 2, 3, 4

    Now I wish I could have it put an std::endl automatically, I have a
    hunch there's no way to do this.... There seem to be no suitable
    operators that have less precedence than <<

    Any ideas?

    Here is the implementation :

    template<typename ELEM = char>
    struct TDelimStream
    {
    typedef std::basic_ostream<ELEM, std::char_traits<ELEM> > ostream;
    ostream &m_stream;
    string m_sDelim;
    TUntil<1> m_once;

    TDelimStream(ostream &stream, const string& sDelim) :
    m_stream(stream), m_sDelim(sDelim)
    {
    }
    //////////////////////////////////////////////////////////////////////////
    };

    template<typename T, typename T2>
    TDelimStream<T> &operator<<(TDelimStream<T> &p, const T2& val)
    {
    if(p.m_once.occurred())
    {
    p.m_stream << p.m_sDelim;
    }
    p.m_stream << val;
    return p;
    }
    //////////////////////////////////////////////////////////////////////////
     
    rep_movsd, Aug 17, 2011
    #1
    1. Advertisements

  2. On 8/17/2011 1:14 PM, rep_movsd wrote:
    > I have a utility class that lets you do something like this :
    >
    > TDelimStream<char> ds(cerr, ", ");
    > ds<< 1<< 2<< 3<< 4;
    >
    > prints :
    > 1, 2, 3, 4
    >
    > Now I wish I could have it put an std::endl automatically, I have a
    > hunch there's no way to do this.... There seem to be no suitable
    > operators that have less precedence than<<
    >
    > Any ideas?


    How about instead of TDelimStream& your operator<< creates a temporary
    object of some proxy class that also has operator<< which simply
    forwards it to the same stream, and returns itself? Then the destructor
    of that proxy object could forward the endl to the stream just before
    going out of existence...

    >
    > Here is the implementation :
    >
    > template<typename ELEM = char>
    > struct TDelimStream
    > {
    > typedef std::basic_ostream<ELEM, std::char_traits<ELEM> > ostream;
    > ostream&m_stream;
    > string m_sDelim;
    > TUntil<1> m_once;
    >
    > TDelimStream(ostream&stream, const string& sDelim) :
    > m_stream(stream), m_sDelim(sDelim)
    > {
    > }
    > //////////////////////////////////////////////////////////////////////////
    > };
    >
    > template<typename T, typename T2>
    > TDelimStream<T> &operator<<(TDelimStream<T> &p, const T2& val)
    > {
    > if(p.m_once.occurred())
    > {
    > p.m_stream<< p.m_sDelim;
    > }
    > p.m_stream<< val;
    > return p;
    > }
    > //////////////////////////////////////////////////////////////////////////
    >
    >


    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 17, 2011
    #2
    1. Advertisements

  3. rep_movsd

    rep_movsd Guest

    On Aug 17, 10:35 pm, Victor Bazarov <> wrote:

    > How about instead of TDelimStream& your operator<< creates a temporary
    > object of some proxy class that also has operator<< which simply
    > forwards it to the same stream, and returns itself?  Then the destructor
    > of that proxy object could forward the endl to the stream just before
    > going out of existence...
    >


    Thanks Victor, your suggestion made me remember temporaries with short
    lives!

    The solution was simple - I just added :

    ~TDelimStream()
    {
    cerr << endl;
    }

    and

    inline TDelimStream<char> print()
    {
    return TDelimStream<char>(cerr, ", ");
    }

    and call it like this :


    print() << 1 << 2 << 3 << 4;

    And it works fine!

    Just when you thing something is impossible in C++ there's always a
    way!!!


    Thanks
    Vivek
     
    rep_movsd, Aug 17, 2011
    #3
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brendan Duffy
    Replies:
    0
    Views:
    5,050
    Brendan Duffy
    Jul 25, 2003
  2. Julian
    Replies:
    1
    Views:
    822
    Roland
    May 24, 2005
  3. E11
    Replies:
    1
    Views:
    5,757
    Thomas Weidenfeller
    Oct 12, 2005
  4. vidalsasoon
    Replies:
    4
    Views:
    1,559
    Ioannis Vranos
    Apr 12, 2005
  5. P Boy
    Replies:
    0
    Views:
    320
    P Boy
    Mar 3, 2006
  6. baldyeti

    wanted: stream wrapper

    baldyeti, Nov 17, 2006, in forum: C++
    Replies:
    1
    Views:
    350
    dasjotre
    Nov 17, 2006
  7. RyanL
    Replies:
    6
    Views:
    1,007
    Paul McGuire
    Aug 28, 2007
  8. mike
    Replies:
    2
    Views:
    218
    Anthony Jones
    Oct 3, 2006
Loading...