Solved

Overloading streambuf

Posted on 1998-02-05
3
377 Views
Last Modified: 2010-07-27
I need to overload streambuf for use with WIN32 overlapped I/O. I know you have to override the "overflow","underflow", and "sync"
members, but I don't know how they are supposed to act.

Thanks,
Kevin Wooten
0
Comment
Question by:KevinWooten
  • 2
3 Comments
 
LVL 3

Expert Comment

by:q2guo
ID: 1180742
I found this .... , hope it helps.

Terry
-------------------------------

Simple output re-direction by redefining `overflow'
===================================================

   Suppose you have a function `write_to_window' that writes characters
to a `window' object.  If you want to use the ostream function to write
to it, here is one (portable) way to do it.  This depends on the
default buffering (if any).

     #include <iostream.h>
     /* Returns number of characters successfully written to WIN. */
     extern int write_to_window (window* win, char* text, int length);
     
     class windowbuf : public streambuf {
         window* win;
       public:
         windowbuf (window* w) { win = w; }
         int sync ();
         int overflow (int ch);
         // Defining xsputn is an optional optimization.
         // (streamsize was recently added to ANSI C++, not portable yet.)
         streamsize xsputn (char* text, streamsize n);
     };
     
     int windowbuf::sync ()
     { streamsize n = pptr () - pbase ();
       return (n && write_to_window (win, pbase (), n) != n) ? EOF : 0;
     }
     
     int windowbuf::overflow (int ch)
     { streamsize n = pptr () - pbase ();
       if (n && sync ())
         return EOF;
       if (ch != EOF)
         {
           char cbuf[1];
           cbuf[0] = ch;
           if (write_to_window (win, cbuf, 1) != 1)
             return EOF;
         }
       pbump (-n);  // Reset pptr().
       return 0;
     }
     
     streamsize windowbuf::xsputn (char* text, streamsize n)
     { return sync () == EOF ? 0 : write_to_window (win, text, n); }
     
     int
     main (int argc, char**argv)
     {
       window *win = ...;
       windowbuf wbuf(win);
       ostream wstr(&wbuf);
       wstr << "Hello world!\n";
     }

This is Info file iostream.info, produced by Makeinfo-1.55 from the
input file ./iostream.texi.

START-INFO-DIR-ENTRY
* iostream: (iostream).                    The C++ input/output facility.
END-INFO-DIR-ENTRY

   This file describes libio, the GNU library for C++ iostreams and C
stdio.

   libio includes software developed by the University of California,
Berkeley.

   Copyright (C) 1993 Free Software Foundation, Inc.

   Permission is granted to make and distribute verbatim copies of this
manual provided the copyright notice and this permission notice are
preserved on all copies.

   Permission is granted to copy and distribute modified versions of
this manual under the conditions for verbatim copying, provided also
that the entire resulting derived work is distributed under the terms
of a permission notice identical to this one.

   Permission is granted to copy and distribute translations of this
manual into another language, under the above conditions for modified
versions.


0
 

Author Comment

by:KevinWooten
ID: 1180743
I need a little more explanation, on how sync, overflow, and underflow are supposed to work.
0
 
LVL 3

Accepted Solution

by:
q2guo earned 50 total points
ID: 1180744
virtual int sync();
Typically, sync() involves writing out any elements between the beginning and next pointers for the output buffer. It does not involve putting back any elements between the next and end pointers for the input buffer. If the function cannot succeed, it returns -1. The default behavior is to return zero.

virtual int_type underflow();
function underflow() extracts the current element c from the input stream, without advancing the current stream position.
c is the element stored in the read position.

typedef T traits_type
virtual int_type overflow(int_type c = T::eof());

If c does not compare equal to T::eof(), the function inserts the element ::to_char_type(c) into the output stream.

The example is given in my previous answer
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
java inner class, for the sole use of parameter passing 21 77
C++ assignment question 7 199
Least Squares Curve Fitting 4 77
Autosar OS Multicore Share Resources confusion ? 2 37
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question