473,978 Members | 4,011 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.
Jul 22 '05 #1
3 2313
On Fri, 27 Feb 2004 18:40:03 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.c yberspace.org> wrote:
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!


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
le**@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
Jul 22 '05 #2
Leor Zolman <le**@bdsoft.co m> spoke thus:
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?


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.
Jul 22 '05 #3
Christopher Benson-Manica wrote:
class linebuf : public streambuf
{
protected:
string buffer;
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...
public:
virtual char overflow(char c) {if(c!=EOF) buffer+=c; return c;}


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:di***** ******@yahoo.co m> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.co m/>
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
12100
by: Viktor Lundström | last post by:
Hi! I was planning to wrap a socket inside an iostream, to achieve something like this: TCPSocket s(..); s << "Hello!" << endl; Information on the web seems to be a bit scarce on how to do this. I have understood that there is a relation between streambuf and iostream, where one should be able to extend streambuf and override the underflow/overflow functions (and do send()/recv() for example).
3
2196
by: Christopher Benson-Manica | last post by:
This is starting to seem ridiculous to me :( #include <streambuf> #include <iostream> class TWFileStream : public std::streambuf { private: char cbuf;
9
2735
by: Fred Ma | last post by:
Hello, I posted previously under the thread: How to break this up into streambuf/ostream I've asked our library to get "C++ IOStreams and Locales..." by A. Langer et al. Meantime, I've looked at the C++ standard (ISO/IEC 14882) from the web to get a grip on the relationship between ostream methods and streambuf methods: flush(), operator<<(), sputc(),
9
5772
by: Marcin Kalicinski | last post by:
Hi, I have a set of C-like functions for file access (like fopen, fwrite, fread, fseek etc.). But I want to access the files using C++ stream, not these functions. What I probably need to do is to create custom streambuf class (am I right?). Is there any free source code or tutorial available on how to do that? I have googled for it, but I only found partial and untested solutions. Worse, the solutions were pretty complicated,...
2
2782
by: Raf256 | last post by:
Hello, my custom streambuf works fine with output via << and with input via .get() but fails to input via >> or getline... any idea why? -------------------- A custom stream buffer (for encryption, currently it is doing "nothing")
7
7049
by: smith4894 | last post by:
Hello all, I'm working on writing my own streambuf classes (to use in my custom ostream/isteam classes that will handle reading/writing data to a mmap'd file). When reading from the mmap file, I essentially have a char buffer in my streambuf class, that I'm registering with setp(). on an overflow() call, I simply copy the contents of the buffer into the mmap'd file via memcpy().
0
1722
by: tryptik | last post by:
All- I have been looking at all the info I can find on deriving streambuf objects, but I need some advice. I wish to derive a custom streambuf that filters out c++ style comments. I have looked at the boost libs and have attempted to impliment their filtered streambuf, but it does not seemt o support seekg() and tellg() operations that I need. My query is, are there any good refrerences for deriving a streambuf that will work with...
4
6424
by: rakesh.usenet | last post by:
For a particular application of mine - I need a simulation of byte array output stream. * write data onto a stream * getback the contiguous content as an array later for network transport. My code looks as follows. #include <iostream>
3
4739
by: Raymond Martineau | last post by:
I have the following code segment for a class intended to split output between cout and a file: class SplitStream : public std::streambuf { std::streambuf *x; public: SplitStream() {
0
10182
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11441
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11600
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10095
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8467
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6428
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6574
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
5164
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4750
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.