Rogue Wave banner
Previous fileTop of DocumentContentsIndexNext file

17.4 The Example

Let's return now to our example, in which we are creating a new stream class by derivation.

17.4.1 The Derived Stream Class

Let us derive a new stream type odatstream that has an additional data member fmt_ for storing a date format string, together with a corresponding member function fmt() for setting the date format specification.

//1A date output stream borrows the stream buffer of an already existing output stream, so that the two streams share the stream buffer.

The constructor also takes an optional argument, the date format string. This is always a sequence of tiny characters.
//2The format string is widened or translated into the stream's character type charT. This is because the format string is provided to the time facet of the stream's locale, which expects an array of characters of type charT.
//3This version of function fmt() allows you to set the format string.
//4This version of function fmt() returns the current format string setting.
//5The date stream class needs a destructor that deletes the format string.
//6A pointer to the date format specification is stored as a private data member fmt_.
//7The inserter for dates must access the date format specification. For this reason, we make it a friend of class odatstream.

17.4.2 The Date Inserter

We would like to be able to insert date objects into all kinds of output streams. Whenever the output stream is a date output stream of type odatstream, we would also like to take advantage of its ability to carry additional information for formatting date output. How can this be achieved?

It would be ideal if the inserter for date objects were a virtual member function of all output stream classes that we could implement differently for different types of output streams. For example, when a date object is inserted into an odatstream, the formatting would use the available date formatting string; when inserted into an arbitrary output stream, default formatting would be performed. Unfortunately, we cannot modify the existing output stream classes, since they are part of a library you will not want to modify.

This kind of problem is typically solved using dynamic casts. Since the stream classes have a virtual destructor, inherited from class basic_ios, we can use dynamic casts to achieve the desired virtual behavior.22

Here is the implementation of the date inserter:

//1We perform a dynamic cast in statement //2. A dynamic cast throws an exception in case of mismatch. Naturally, we do not want to confront our user with bad_cast exceptions because the mismatch does not signify an error condition, but only that the default formatting is performed. For this reason, we try to catch the potential bad_cast exception.
//2This is the dynamic cast to find out whether the stream is a date stream or any other kind of output stream.
//3In case of mismatch, we prepare the default date format specification "%x".
//4If the stream is not of type odatstream, the default format specification prepared in the catch clause is used. Otherwise, the format specification is taken from the private data member fmt_.

17.4.3 The Manipulator

The date output stream has a member function for setting the format specification. Analogous to the standard stream format functions, we would like to provide a manipulator for setting the format specification. This manipulator affects only output streams. Therefore, we must define a manipulator base class for output stream manipulators, osmanip, along with the necessary inserter for this manipulator. We do this in the code below. See Section 12.3.3 for a detailed discussion of the technique we are using here:

After these preliminaries, we can now implement the setfmt manipulator itself:

//1The function sfmt() is the function associated with the setfmt manipulator. Its task is to take a format specification and hand it over to the stream. This happens only if the stream is a date output stream; otherwise, nothing is done.
//2We determine the stream's type through a dynamic cast. As it would be rather drastic to let a manipulator call result in an exception thrown, we catch the potential bad_cast exception.
//3In case of mismatch, we don't do anything and simply return.
//4In case the stream actually is a date output stream, we store the format specification by calling the stream's fmt() function.
//5The manipulator itself is a function that creates an output manipulator object.

17.4.4 A Remark on Performance

The solution suggested in Section 17.4.3 uses dynamic casts and exception handling to implement the date inserter and the date format manipulator. Although this technique is elegant and makes proper use of the C++ language, it might introduce some loss in runtime performance due to the use of exception handling. This is particularly true since the dynamic cast expression, and the exception it raises, is used as a sort of branching statement. In other words, the "exceptional" case occurs relatively often and is not really an exception.

If optimal performance is important, you can choose an alternative approach: in the proposed solution that uses dynamic casts, extend the date inserter for arbitrary output streams basic_ostream<charT,Traits>& operator<< (basic_ostream <charT,Traits>&, const date&) so that it formats dates differently, depending on the type of output stream. Alternatively, you can leave the existing date inserter for output streams unchanged and implement an additional date inserter that works for output date streams only; its signature would be odatstream<charT,Traits>& operator<< (odatstream<charT,Traits>&, const date&). Also, you would have two manipulator functions, one for arbitrary output streams and one for output date streams only, that is, basic_ostream<charT,Traits>& sfmt (basic_ostream<charT,Traits>&, const char*) and odatstream<charT,Traits>& sfmt
(odatstream<charT,Traits>&, const char*)
. In each of the functions for date streams, you would replace those operations that are specific for output date streams.

This technique has the drawback of duplicating most of the inserter's code, which in turn might introduce maintenance problems. The advantage is that the runtime performance is likely to be improved.


Previous fileTop of DocumentContentsIndexNext file

OEM Edition, ©Copyright 1999, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.