TooN Algorithm Library - tag  0.2
Macros | Functions | Variables
Typesafe, variadic printf
Collaboration diagram for Typesafe, variadic printf:

Macros

#define Printf(A,...)   vPrintf(A, (tag::Fmt,## __VA_ARGS__))
 
#define fPrintf(A, B,...)   vfPrintf(A,B, (tag::Fmt,## __VA_ARGS__))
 
#define sPrintf(A,...)   vsPrintf(A, (tag::Fmt,## __VA_ARGS__))
 

Functions

template<class A , class B , class C , class D >
void tag::vfPrintf (std::basic_ostream< A, B > &o, const std::string fmt, const T_list< C, D > &l)
 
template<class C , class D >
void tag::vPrintf (const std::string fmt, const T_list< C, D > &l)
 
template<class C , class D >
std::string tag::vsPrintf (const std::string &fmt, const T_list< C, D > &l)
 

Variables

static const T_ListEnd tag::Fmt =TupleHead
 

Detailed Description

This group provides a reimplementation of the C standard library printf family of functions in a typesafe manner, using the tag::T_list tuple type.

Macro Definition Documentation

#define fPrintf (   A,
  B,
  ... 
)    vfPrintf(A,B, (tag::Fmt,## __VA_ARGS__))

This is the equivalent to the C-style fprintf: it provides a variadic interface and prints to give ostream. See vfPrintf for details.

Parameters
Athe ostream to write to
Bthe format string
...the arguments
#define Printf (   A,
  ... 
)    vPrintf(A, (tag::Fmt,## __VA_ARGS__))

This is the equivalent to the C-style printf: it provides a variadic interface and prints to cout. See vfPrintf for details.

Parameters
Athe format string
...the arguments
#define sPrintf (   A,
  ... 
)    vsPrintf(A, (tag::Fmt,## __VA_ARGS__))

This is the equivalent to the C-style sprintf: it provides a variadic interface returns the string. See vfPrintf for details.

Parameters
Athe format string
...the arguments
Returns
The retulting application of fmt to the arguments

Function Documentation

template<class A , class B , class C , class D >
void tag::vfPrintf ( std::basic_ostream< A, B > &  o,
const std::string  fmt,
const T_list< C, D > &  l 
)

This function provides the mose generic implementation, where a typelist and an ostream can be provided.

Parameters
oostream to print to
fmtformat string
ltypelist of arguments

The format specifiers work much like the standard printf ones, but with some notable differences. The format is:

%[flags][width][.[precision]]<conversion>
Flags

Flags contains zero or more of:

# Alternate form: Include trailing zeros for float, use 0x or 0 prefix for hex and octal ints
0 Zero pad everything, including (oddly) strings.
' ' Space. Does nothing, I haven't figured out how to do this one.
- Left justify. Overrides 0
+ Always show sign for numbers
Width

The width is optional and must start with a non-zero digit. It seems to work even with overloaded operator<<

Precision
.[precision]

This can mean one of two things:

  • The precision for floating point numbers (affects all numbers in the following operator<<)
  • The maximum width for "string" types. Truncation happens from the right.
Conversion

This is a single alphabetic character. This is the largets departure from the C style printf. This flag affects the "type" of the datum being formatted. Type safety still applies. Uppercase and lowercase conversions behave in exactly the same way, except uppercase ones cause uppercased output (where applicable). If a float looks like 1E6 intead of 1e6

x Make ints come out in hexadecimal
o Make ints come out in octal
f Make floats come out as fixed
g Make floats come out in the general format
e Make floats come out in exponential (scientific) format
b Make bools coma out as "true" or "false"
s Treat next output as string: precision means max width.
k Kill (ignore) the data
Differences campared to C-stype printf

Unlike C, the C++ sytle printf can output values of any type for which operator<< is defined. Further, printf itself will not cause any undefined behaviour. Access beyond the end of the vargs list produces <Missing value> in the output, instead of a segfault. A malformed format string causes <Malformed format> to appear in the output followed by the remaining string including the malformed part. No further conversions are attempted after a malformed format.

Note: length modifiers are not supported.

See also other members of the Printf family:

Usage
vfPrintf(cout, "%s, %s\n", (Fmt, "Hello", "world"));

References tag::print.

Referenced by tag::vPrintf(), and tag::vsPrintf().

template<class C , class D >
void tag::vPrintf ( const std::string  fmt,
const T_list< C, D > &  l 
)

This prints to cout. See vfPrintf for details.

Parameters
fmtformat string
ltypelist of arguments

References tag::vfPrintf().

template<class C , class D >
std::string tag::vsPrintf ( const std::string &  fmt,
const T_list< C, D > &  l 
)

This prints to a string. See vfPrintf for details.

Parameters
fmtformat string
ltypelist of arguments
Returns
The resulting application of fmt to l, returned as a string

References tag::vfPrintf().

Variable Documentation

const T_ListEnd tag::Fmt =TupleHead
static

List head for argument lists. This is a synonym for TupleHead

Argument lists can be made by doing:

(Fmt, arg1, arg2, ...)