The end of my streambuf road...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher Benson-Manica

    The end of my streambuf road...

    Unless there is something wrong with the following complete C++ file,
    I think my quest to stream-ize our code will be at an end :(

    #include <sstream>
    #include <iostream>

    using namespace std;

    class linebuf : public streambuf
    {
    protected:
    string buffer;

    public:
    virtual char overflow(char c) {if(c!=EOF) buffer+=c; return c;}
    virtual streamsize xsputn(const char *s, streamsize num)
    {
    buffer.append(s ,num); return num;
    }
    virtual int sync()=0;
    };

    typedef unsigned int uint;
    typedef bool (* FTYPE)(const void *,uint,uint,uin t);

    class sendbuf : public linebuf
    {
    public:
    virtual int sync() {cout << buffer << endl; buffer.erase(); return 0;}
    };

    class mystream : public std::ostream
    {
    protected:
    sendbuf buf;

    public:
    mystream() : std::ostream(&b uf) {}
    };

    int main(void)
    {
    mystream s;

    s << "Hello, " << "world!" << flush;
    return 0;
    }

    g++, faithful and true companion that it is, compiles and runs this as
    expected with all warnings enabled. Our Borland setup, however,
    compiles this into an executable that crashes. So I figure either I'm
    invoking some wicked implementation-defined (or undefined!) behavior
    here, or our Borland setup is fatally flawed in ways that I cannot
    begin to fathom. Thanks to everyone who provided assistance during my
    travails - it was a great learning experience if nothing else!

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
  • Leor Zolman

    #2
    Re: The end of my streambuf road...

    On Fri, 27 Feb 2004 18:40:03 +0000 (UTC), Christopher Benson-Manica
    <ataru@nospam.c yberspace.org> wrote:
    [color=blue]
    >Unless there is something wrong with the following complete C++ file,
    >I think my quest to stream-ize our code will be at an end :(
    >
    >#include <sstream>
    >#include <iostream>
    >
    >using namespace std;
    >
    >class linebuf : public streambuf
    >{
    > protected:
    > string buffer;
    >
    > public:
    > virtual char overflow(char c) {if(c!=EOF) buffer+=c; return c;}
    > virtual streamsize xsputn(const char *s, streamsize num)
    > {
    > buffer.append(s ,num); return num;
    > }
    > virtual int sync()=0;
    >};
    >
    >typedef unsigned int uint;
    >typedef bool (* FTYPE)(const void *,uint,uint,uin t);
    >
    >class sendbuf : public linebuf
    >{
    > public:
    > virtual int sync() {cout << buffer << endl; buffer.erase(); return 0;}
    >};
    >
    >class mystream : public std::ostream
    >{
    > protected:
    > sendbuf buf;
    >
    > public:
    > mystream() : std::ostream(&b uf) {}
    >};
    >
    >int main(void)
    >{
    > mystream s;
    >
    > s << "Hello, " << "world!" << flush;
    > return 0;
    >}
    >
    >g++, faithful and true companion that it is, compiles and runs this as
    >expected with all warnings enabled. Our Borland setup, however,
    >compiles this into an executable that crashes. So I figure either I'm
    >invoking some wicked implementation-defined (or undefined!) behavior
    >here, or our Borland setup is fatally flawed in ways that I cannot
    >begin to fathom. Thanks to everyone who provided assistance during my
    >travails - it was a great learning experience if nothing else![/color]

    Here's what Comeau says:


    stream.cpp(13): warning: function "basic_streambu f<char,
    char_traits<cha r>>::overflow(
    basic_streambuf <char, char_traits<cha r>>::int_type) " is hidden by
    "linebuf::overf low" --
    virtual function override intended?
    virtual char overflow(char c) {if(c!=EOF) buffer+=c; return
    c;}
    ^

    (the caret is pointing at the start of the word "overflowa" in the last
    line). Any help?
    -leor


    Leor Zolman
    BD Software
    leor@bdsoft.com
    www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
    C++ users: Download BD Software's free STL Error Message
    Decryptor at www.bdsoft.com/tools/stlfilt.html

    Comment

    • Christopher Benson-Manica

      #3
      Re: The end of my streambuf road...

      Leor Zolman <leor@bdsoft.co m> spoke thus:
      [color=blue]
      > stream.cpp(13): warning: function "basic_streambu f<char,
      > char_traits<cha r>>::overflow(
      > basic_streambuf <char, char_traits<cha r>>::int_type) " is hidden by
      > "linebuf::overf low" --
      > virtual function override intended?
      > virtual char overflow(char c) {if(c!=EOF) buffer+=c; return
      > c;}
      > ^[/color]
      [color=blue]
      > (the caret is pointing at the start of the word "overflowa" in the last
      > line). Any help?[/color]

      It is int_type in the code compiled by Borland - I changed it because
      g++ complained...

      --
      Christopher Benson-Manica | I *should* know what I'm talking about - if I
      ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

      Comment

      • Dietmar Kuehl

        #4
        Re: The end of my streambuf road...

        Christopher Benson-Manica wrote:[color=blue]
        > class linebuf : public streambuf
        > {
        > protected:
        > string buffer;[/color]

        There is no point in making member variables protected: you could as
        well make them public they become part of your interface anyway (well,
        given that the size changes with a type change, in some sense even
        private variables are part of the interface...). However, this is not
        really your problem...
        [color=blue]
        > public:
        > virtual char overflow(char c) {if(c!=EOF) buffer+=c; return c;}[/color]

        This is not an override but an overload! You should use 'int_type'
        or, if this causes problems, 'std::char_trai ts<char>::int_t ype'.
        Using 'char' will definitely cause problems. The 'overflow()'
        function would look something like this:

        int_type overflow(int_ty pe c) {
        if (!traits::eq_in t_type(c, traits::eof()))
        buffer += traits::to_char _type(c);
        return traits::not_eof (c);
        }
        --
        <mailto:dietmar _kuehl@yahoo.co m> <http://www.dietmar-kuehl.de/>
        Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.co m/>

        Comment

        Working...