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

Parser for cmnPrintf. More...

#include <cmnPrintf.h>

Public Types

enum  { BUFFER_SIZE = 256 }
 

Public Member Functions

 cmnPrintfParser (std::ostream &output, const cmnPrintf &outputFormat)
 
void RawOutput (const char *text)
 
template<class _argType >
cmnPrintfParseroperator<< (const _argType &dataOut)
 
cmnPrintfParseroperator<< (const int dataOut)
 
cmnPrintfParseroperator<< (const std::string &dataOut)
 
 operator bool (void)
 
template<typename _outputType >
bool MatchOutputWithFormatChar (const _outputType &data)
 
bool NextTypeIdCharIsOneOf (const char *typeIdCharset) const
 
void SuspendOutput (void)
 
const char * GetNextFormatSequence (void) const
 
char GetNextTypeIdCharacter (void) const
 

Static Public Member Functions

static const char * TypeIdString (double CMN_UNUSED(value))
 
static const char * TypeIdString (float CMN_UNUSED(value))
 
static const char * TypeIdString (int CMN_UNUSED(value))
 
static const char * TypeIdString (unsigned int CMN_UNUSED(value))
 
static const char * TypeIdString (long CMN_UNUSED(value))
 
static const char * TypeIdString (unsigned long CMN_UNUSED(value))
 

Static Public Attributes

static const char * TypeIdCharset
 
static const char * IntegerTypeIds
 
static const char * FloatTypeIds
 
static const char * StringTypeIds
 

Detailed Description

Parser for cmnPrintf.

This class performs the actual formatting of the input. It communicates with a cmnPrintf object, parses the format string, and sends formatted inputs to the output. Normally, the user does not deal with this class.

The user may use a cmnPrintfParser object to provide formatting for new types, such as user-defined classes. Note that the collection of `' sequences is specified through the C sprintf set of control characters, and cannot be changed. However, the user may override an existing `' sequence to format new types with that control. For example, the sequence "%d%" can be used to format a single integer or a vector of integers.

To format new classes, the user should provide an overload of the global function cmnTypePrintf, that takes the user's class as an input. For example,

bool cmnTypePrintf(cmnPrintfParser & outputParser, const MyClass data);

The function should return true if the formatting is successful, and false otherwise, although in the current implementation we ignore the return value.

Member Enumeration Documentation

anonymous enum

The size of buffer allocated for formatting each output element. Exceeding this size will truncate the output for that element.

Enumerator
BUFFER_SIZE 

Constructor & Destructor Documentation

cmnPrintfParser::cmnPrintfParser ( std::ostream &  output,
const cmnPrintf outputFormat 
)

Initialize a cmnPrintf object with the destination output stream and a format string.

Parameters
outputthe actual output stream to which text will be printed
outputFormata cmnPrintf object that holds a copy of the format string

Member Function Documentation

const char* cmnPrintfParser::GetNextFormatSequence ( void  ) const
inline

Returns the next format sequence, i.e., '' sequence, which is to be processed next.

char cmnPrintfParser::GetNextTypeIdCharacter ( void  ) const
inline

Returns the type identifying character to be processed next

template<typename _outputType >
bool cmnPrintfParser::MatchOutputWithFormatChar ( const _outputType &  data)
inline

This function matches the "type" of the given data with the set of format characters for that type, by calling TypeIdString for the type of the data. The function returns true of the next format character in the format string matches the type of the data, and false otherwise. For example, if the format text is "%f" and the data is of type double, or float, the function returns true; but if the data is of type in the function returns false.

bool cmnPrintfParser::NextTypeIdCharIsOneOf ( const char *  typeIdCharset) const

This function matches the next type character with the string of id characters given as argument. For example, if the argument is "dxci" and the next type id in the format string was taken from "%10d" the function returns true; but if the next type id is taken from "%7.2f" the function returns false.

cmnPrintfParser::operator bool ( void  )
inline

Overload operator void * to allow similar evaluations of "success" as are available for ostream. E.g.,

if (!std::cout) {
// do something
}

A similar test can be performed for a cmnPrintfParser object, and tests the status of the output channel.

template<class _argType >
cmnPrintfParser& cmnPrintfParser::operator<< ( const _argType &  dataOut)
inline

This is the overloaded operator << that formates the currenct input element and advances to the next one. It calls cmnTypePrintf for the given input type.

cmnPrintfParser& cmnPrintfParser::operator<< ( const int  dataOut)
inline

Overloaded operator for integers. This operator is mostly a helper for compilers which won't convert an anonymous enum as an integer to infer the type used in the templated operator.

This solution works with gcc 4.0.2 but not with gcc 4.0.1. If you get a compilation error like:

error: 'myClass::<anonymous enum>' is/uses anonymous type

You should cast your enum value to int.

cmnPrintfParser& cmnPrintfParser::operator<< ( const std::string &  dataOut)
inline

Overloaded operator for std::string. This operators is required since there is no implicit cast from std::string to const char pointer.

void cmnPrintfParser::RawOutput ( const char *  text)
inline

Dump raw text to the output stream. The user may call this function to output any text to the stream, without internal formatting.

void cmnPrintfParser::SuspendOutput ( void  )
inline
static const char* cmnPrintfParser::TypeIdString ( double   CMN_UNUSEDvalue)
inlinestatic

This auxiliary function returns the set of characters that identify a double variable in the '' sequence, such as 'e', 'f', 'g', etc.

static const char* cmnPrintfParser::TypeIdString ( float   CMN_UNUSEDvalue)
inlinestatic

This auxiliary function returns the set of characters that identify a float variable in the '' sequence, such as 'e', 'f', 'g', etc.

static const char* cmnPrintfParser::TypeIdString ( int   CMN_UNUSEDvalue)
inlinestatic

This auxiliary function returns the set of characters that identify an int variable in the '' sequence, such as 'd', 'x', 'o', etc.

static const char* cmnPrintfParser::TypeIdString ( unsigned int   CMN_UNUSEDvalue)
inlinestatic

This auxiliary function returns the set of characters that identify an int variable in the '' sequence, such as 'd', 'x', 'o', etc.

static const char* cmnPrintfParser::TypeIdString ( long   CMN_UNUSEDvalue)
inlinestatic

This auxiliary function returns the set of characters that identify a long variable in the '' sequence, such as 'd', 'x', 'o', etc.

static const char* cmnPrintfParser::TypeIdString ( unsigned long   CMN_UNUSEDvalue)
inlinestatic

This auxiliary function returns the set of characters that identify an unsigned long variable in the '' sequence, such as 'd', 'x', 'o', etc.

Member Data Documentation

const char* cmnPrintfParser::FloatTypeIds
static

The collection of characters that identify floating-point output

const char* cmnPrintfParser::IntegerTypeIds
static

The collection of characters that identify integer output

const char* cmnPrintfParser::StringTypeIds
static

The collection of characters that identify strings, e.g., 's'

const char* cmnPrintfParser::TypeIdCharset
static

The complete collection of type identifying characters which can be used in printf


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