DGD Library
Version: 0.1.0.14 | Updated: Thu Aug 10 16:59:33 2006
Home
DGD News
FAQ
Code Documentation
DGD Installation Guide
Bugs-n-Features
DGD at Sourceforge.net
Download DGD
Subversion
Code Documentation
Documentation
Namespaces
Class Hierarchy
Class List
File List
Namespace Members
Functions
Globals
Tutorial
Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

DGD Basics - channelbuf

DGD::channelbuf is a basic class in the library. In fact, DGD is started from the single class. Channelbuf can be used as stand-alone std::streambuf for formatting output. Lets consider the example:
// format output with channelbuf

#include <dgChannelBuf.h>
#include <dgStream.h>

main( int argc, char** argv ) {
   DGD::channelbuf formatter;

   formatter.pubsetbuf( NULL, 100 );
   formatter.assoc( &std::cout );

   std::ostream formatting_stream( &formatter );

   formatting_stream << "Hello! This is actally very very very " 
                     << "very very very very very very very very " 
                     << "very very very very very very very long line!" 
                     << std::endl;
   return 0;
}
For this example the formatter is declared as a local variable. In practice this is usually not so good idea because std::streambuf is usually registered in some std::ostream. The channelbuf constructor does not allocate any memory. So line
   formatter.pubsetbuf( NULL, 100 );
allocates buffer of 100 bytes in size. The first NULL parameter will cause internal memory allocation, while non-NULL first parameter will be treated as a pointer to the already allocated buffer.

Note that formatter makes no physical output, it just makes formatting. To achieve the physical output we must associate channelbuf with physical stream, for example std::cout:

   formatter.assoc( &std::cout );

Now, when the formatter is defined and associated with standard output we can make some output. But channelbuf can't do output itself. So we need an output stream:

   std::ostream formatting_stream( &formatter );
The next line will produce the following:
 Hello! This is actally very very very very very very very very very very
 very very very very very very very very long line!

Note how the formatter split the line in two. By default it has word wrapping turned on and maximum line length 79. You can easily change the defaults. Read the next chapter to learn that.


Generated on Thu Aug 10 16:48:29 2006 for DGD Library by doxygen1.3

SourceForge.net Logo Powered by Mason Powered by Perl
Web design derived from Pasilda design found on www.oswd.org.
Copyright (c) 2002, 2003. Dimitry Kloper <kloper@users.sourceforge.net>