basic_oformatstream Template Class

Nov 14, 2001

4 min read

C++

Windows

Visual-Studio

Dev

Advanced

VC++

Author picture

by David 'dex' Schwartz

Contributor

58k Views

Introduction

I'm quite sure there will be some who will ask "Why did you build this?". Yes, it is slower than printf and a little fatter than STL wcout, but...

There are two major reasons I built this template class, apart from the learning experience.

On the one hand, I find the STL manipulators to be cumbersome, on the other, the printf family of functions have a major drawback, they don't allow arbitrary growth of the output string.

I could have wrapped only the ostrstream class but that would have unnecessarily limited the use of this template for other kinds of output, namely file and console.

The main template class, basic_oformatstream, acts as a shim ("something thin placed between two parts to make a fit") between the user and the ostream the object is tied to.

WARNING: There is a bug in the VC++ 6.0 STL implementation from Dinkumware.

See the end of file oformatstream.cpp for a short description of the bug (with code). An initial fix is to make the buffer size much larger in <xlocnum> template num_put functions virtual _OI do_put(_OI _F, ios_base& _X, _E _Fill, double _V) const and virtual _OI do_put(_OI _F, ios_base& _X, _E _Fill, long double _V) const. If making this kind of change to a standard template library file fills you with dread, I don't blame you, so if you can't or won't fix this, then never ever try to output a really big floating point value without using the scientific format, i.e., %e or %g, the other one %f could cause you grief.

So How Do You Use It ?

First, construct an oformatstream by giving it a format and an output stream to write to. Then it is just a matter of outputting values to the resulting oformatstream. The order in which you send the output values is, as you should expect, utterly dependent upon the order that you specify in the format string.

You can change the format string by using the reformat manipulator which takes a basic_formatter object as its argument.

CPP
formatter f("%8d %6.2f");
oformatstream ofs("%8x %5.3e",&std::cout);

ofs <<  reformat(f);

Note that formatter and oformatstream are typedefs similar in purpose to those done for cout and wcout. In case you were wondering, woformatstream and wformatter are the wide equivalents.

CPP
// Example usage:
//
// First a really simple situation: 
oformatstream ofs("[%s] [%8d] [%6.5f]\n", &std::cout);
ofs << "example" << 1 << 3.141592 << setformat;

// Note setformat is used to make sure the last field is output.
// The last field ? Yes the "]\n" text.
// You could also drop the '\n' and use the endl inserter instead.
// This would result in the following code.
oformatstream ofs("[%s] [%8d] [%6.5f]", &std::cout);
ofs << "example" << 1 << 3.141592 << setformat << endl;

// A slightly more complex example: 

// Set up a custom default format specification first 
format_specification fs(12 /*width*/,2/*precision*/,std::ios_base::dec); 

// Generate a formatter with this default format specification 
formatter format("[%s] [%d] [%f]", fs);

 // Construct the oformatstream with this custom formatter 
oformatstream ofs(format, &std::cout); 
ofs << "example" << 1 << 3.141592 << endl;

For some really complex examples, see TestFormat.cpp in the demo.

The following are more detailed descriptions of the template classes and the various implementation classes and functions you will find if you dig around inside oformatstream.hpp.

basic_oformatstream

Constructors

  • The default constructor. Will require a call to tie() before output will work:
    CPP
    basic_oformatstream()
  • Use a format string to construct the internal format field vector:
    CPP
    basic_oformatstream(const std::basic_string<_E,_Tr>& s, _Myostream *os = NULL)
  • Use a preconstructed basic_formatter which allows for a custom default format specification:
    CPP
    basic_oformatstream(const basic_formatter<_E>& f, _Myostream *os = NULL)

Public Methods

  • Inserter operators:
    CPP
    _Myt& operator<<(T) for T= {bool,int,long,...etc.}
  • Sets the internal formatter:
    CPP
    void formatter(constbasic_formatter<_E,_Tr>& f)
  • Returns the internal formatter:
    CPP
    basic_formatter<_E>& formatter()
  • Ties to an output stream:
    CPP
    void tie(_Myostream *os)
  • Returns the tied output stream:
    CPP
    _Myostream* get_ostream()
  • Sets the default format specification:
    CPP
    void default_format_specification(const format_specification& f)
  • Returns the default format specification:
    CPP
    format_specification default_format_specification()

Plus several others which are used internally but need to be public.

basic_formatter

Constructors

  • The default constructor:
    CPP
    basic_formatter()
  • Supply a default format specification:
    CPP
    basic_formatter(const format_specification fs)
  • Use a format string:
    CPP
    basic_formatter(std::basic_string<_E,_Tr> fs)
  • Use a format string and supply a default format specification:
    CPP
    basic_formatter(std::basic_string<_E,_Tr> s, const format_specification& fs)

Implementation Classes

format_flags

Manages changes to a std::ios_base::fmtflags value. Enforces restrictions such as hex, dec and oct being mutually exclusive flags. Provides operators |=, &=, = and various constructors and conversion operators.

format_specification

A single field's format specification corresponds to a single field, i.e., "%7.5f".

TEMPLATE CLASS format_characters

Provides a central place for storing constants required by the parsing routines. Currently _E may be either char or wchar_t.

CPP
TEMPLATE CLASS 

Holds the final results of parsing a single field's format specification. These being a prefix text string and format specification for the field.

CPP
TEMPLATE CLASS 

derived from std::vector< basic_formatterfield<_E,_Tr>>

Parsing Routines

CPP
std::ios_base::fmtflags 

Converts the type character [cdefgisx] into appropriate ios_base flag values.

CPP
bool 

Called by parse_field<_E>() to process a single field's format specification, i.e., everything after the percent (%) symbol.

CPP
bool 

Called by parse_format<_E> to process a format field, i.e., the prefix text followed by a format specification. Calls parse_format_specification<_E>():

CPP
bool 

Used by basic_formatter<_E> constructors to process a full format specification. Calls parse_field<_E>() to build a basic_formatterfield which it then stores in a FormatFieldVector.

Format Output Management Classes

CPP
TEMPLATE CLASS 

The format of each field is controlled by the given format string.

CPP
TEMPLATE CLASS 

Outputs values to the connected stream (does nothing if not). The internal basic_formatter object controls the layout of each output field and the order that they are expected. No exceptions are thrown if the supplied field type does not match the expected format. The output will probably just look awful.

History

  • 14th November, 2001: Initial post
  • 26th September, 2022: Mentioned upgrade on Reddit and wanted to update the code to reflect the effort

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)