cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Friends | List of all members
cmnPrintf Class Reference

printf-like formatted output for streams More...

#include <cmnPrintf.h>

Public Member Functions

 cmnPrintf (const std::string &formatStr)
 
 ~cmnPrintf ()
 
 cmnPrintf (const cmnPrintf &other)
 

Friends

class cmnPrintfParser
 

Detailed Description

printf-like formatted output for streams

This is the user interface for output formatting with C++ streams after the fashion of printf.

A usage example:

std::cout << cmnPrintf("Hello %.4f World\n") << 12.345;

prints the text (followed by a
newline)

Hello 12.3450 World

cmnPrintf can be "streamed" into any object derived from the std::ostream base class. The formatting rules are the same as in the standard C routines printf, sprintf, etc. Note that if a cmnPrintf object is streamed out, the rest of the expression no longer behaves like an ordinary "ostream <<", but the cmnPrintf object takes precedence.

In behavior, the format string is parsed and whenever a '' sequence is encountered, a single object is fetched from the corresponding '<<'. That object is formatted and printed. If there are more inputs in the '<<' sequence than `' sequences in the format string, then only the objects that correspond to '' sequences are printed, and the rest are ignored. For example,

std::cout << cmnPrintf("One (%d), two and a half (%f), text (%s)")
<< 1 << 2.5 << "text-input" << 10;

will print:

One (1), two and a half (2.5), text (text-input)

and ignore the input 10.

If there are fewer inputs in the '<<' sequence, then the formatting occurs until the '' sequence that follows the last input object, and stops there. For example,

std::cout << cmnPrintf("One (%d), two and a half (%f), text (%s)")
<< 1 << 2.5;

will print:

One (1), two and a half (2.5), text(

Default formatting is provided for the types double ("%f"), int ("%d") and const char * ("%s"). Other basic types are automatically converted by the compiler to one of these and printed. However, the model can be extended to include more output types. See the documentation of cmnPrintfParser for more information.

Note that special ios manipulators and functions, such as endl, flush, setw, setprecision, etc., have no '' sequence, and therefore CANNOT be used with with cmnPrintf. To print a newline (without flush), one may, for example, include a '
' character in the format stream, as done with the C printf function. To flush, a flush method must be called outside of the cmnPrintf expression.

cmnPrintf is stateless, which means that it does not affect any of the state variables in the output stream, such as width, precision, etc. All the output is done through conversion to character strings.

The allocated space for each output element is defined in cmnPrintfParser::BUFFER_SIZE. If the length of the formatted element is larger than this number, then the output is truncated, and the text '(truncated)' is appended following the first BUFFER_SIZE characters. Unfortunately, there is no standard way to compute the length of a formatted string (snprintf is not portable to Visual Studio), and hence to way for our code to know how much memory to allocate for the result of the formatting.

Implementation-wise, this is a dummy class that just stores a copy of the format string. The real work is done by a cmnPrintfParser object which is returned from operator <<.

The expression

std::cout << cmnPrintf("something")

creates a cmnPrintf object, which is streamed into std::cout. The result of the expression is an invisible cmnPrintfParser object, into which all the following elements are streamed. The cmnPrintfParser object parses the format string, formats the streamed objects, and forwards the formatted result to std::cout.

Constructor & Destructor Documentation

cmnPrintf::cmnPrintf ( const std::string &  formatStr)
inline

Create a cmnPrintf object and provide a format string following the printf specification. The cmnPrintf object stores a copy of the format string.

cmnPrintf::~cmnPrintf ( )
inline

Destructor. Delete any internally allocated memory

cmnPrintf::cmnPrintf ( const cmnPrintf other)
inline

Copy constructor. We would like to have this constructor private but gcc-4.0.2 complains if it is private even thought is doesn't use it.

Friends And Related Function Documentation

friend class cmnPrintfParser
friend

The documentation for this class was generated from the following file: