SoDaFormat-1.0.0-main:f160581  1.0.0
SoDa::Format Class Reference

Detailed Description

A format object that may be "filled in" with integer, float, double, string, or character values.

The format string

The format string supplied here contains value placeholders of the form "%[0-9]*" That is, a percent sign "%" followed by one or more decimal digits. Any placeholder may occur at any place in the string. A placeholder may appear more than once. So this string

"first val %0 third val %2 second val %1 third val all over again %2\n"

is just fine.

Values are numbered starting at zero, because that's the way we roll here. Values are supplied by invocations of the Format::addX methods, as in

  • addI (integer)
  • addU (unsigned integer)
  • addF (float or double)
  • addS (string)
  • addC (character)

Most of these provide for additional format directives that determine minimum field with and decimal precision.

As each .addX method is invoked, it fills in the associated placeholders. The format string, thus evolves as the placeholders are replaced with the formatted representation of the supplied values.

The format string, like any other string object, follows the "\" escape conventions. In addition, since "%" is used as a special character, the string "%%" may be used to insert a single "%" character.

Other Places, Other Norms

Those in areas where the decimal radix point is not "." or those who simply wish for an alternative may set the radix separator by assigning to the static variable SoDa::Format::separator like this

Friendship and Strings

The output stream operator "<<" is a friend of the SoDa::Format class.

The SoDa::Format::str() method will return a reference to the current state of the format string (with all the placeholders filled in, as far as the process has progressed.)

When good code goes bad

SoDa::Format methods don't return success/failure values to distinguish between good outcomes and errors. If something goes wrong that SoDa::Format feels bad about, it will throw a SoDa::Format::BadFormat exception. This inherits from "std::runtime_exception" so it is moderately well behaved.

SoDa::Format uses exceptions because, after years of reading my code and the code of others, it is a rare and disciplined individual who checks every return value for errors. And their code looks like crap.

Inside Baseball note

More skillful programmers might have overloaded one method name for all five types. However, making a C++ compiler distinguish between add(int v) and add(unsigned int v) – though it seems obvious that this should work – proved beyond my ability, and may be outside of the C++11 definition. LLVM warned about it loudly.

Definition at line 269 of file Format.hxx.

#include <Format.hxx>

Classes

class  BadFormat
 Exception to announce that there was something wrong with the format string, or that too many values were supplied. More...
 

Public Member Functions

 Format (const std::string &fmt_string)
 create a format object with placeholders and all that stuff. More...
 
FormataddI (int v, unsigned int width=0)
 insert a signed integer into the format string More...
 
FormataddU (unsigned int v, unsigned int width=0)
 insert an unsigned integer into the format string More...
 
FormataddF (double v, char fmt='f', unsigned int width=0, unsigned int frac_precision=3)
 insert a float or double into the format string More...
 
FormataddS (const std::string &v, unsigned int width=0)
 insert a string into the format string More...
 
FormataddC (char v)
 insert a character into the format string More...
 
Formatreset ()
 reset the format string to its original value, with all the placeholders restored. More...
 
const std::string & str (bool check_for_filled_out=false) const
 provide a string representing the current state of the format string with all placeholders "filled in" as far as the last invocation of an add method. More...
 

Static Public Attributes

static char separator = '.'
 the radix separator character. More...
 

Constructor & Destructor Documentation

◆ Format()

SoDa::Format::Format ( const std::string &  fmt_string)

create a format object with placeholders and all that stuff.

Parameters
fmt_stringthe format string with placeholders and stuff.

Definition at line 41 of file Format.cxx.

Member Function Documentation

◆ addC()

Format & SoDa::Format::addC ( char  v)

insert a character into the format string

Parameters
vthe character value
Returns
a reference to this SoDa::Format object to allow chaining of method invocations.

Not much to say here.

Definition at line 142 of file Format.cxx.

Referenced by main().

◆ addF()

Format & SoDa::Format::addF ( double  v,
char  fmt = 'f',
unsigned int  width = 0,
unsigned int  frac_precision = 3 
)

insert a float or double into the format string

Parameters
vthe double precision value (floats will be promoted)
fmta choice of representations (see below)
widththe minimum width of the field.
frac_precisionthe number of decimal positions to the right of the radix point.
Returns
a reference to this SoDa::Format object to allow chaining of method invocations.

The default value for the width parameter will take up only as much room as the value requires.

representation choice 'f'

The value is converted to a string in the manner of the C++ stream "std::fixed" format. The width parameter sets the minimum field width, and the frac_precision parameter determines the number of places to the right of the radix point.

The default value for the width parameter will take up only as much room as the value requires.

representation choice 's'

The value is converted to a string in the manner of the C++ stream "std::scientific" format. The width parameter sets the minimum field width, and the frac_precision parameter determines the number of places to the right of the radix point. THe number will be represented as a floating string value in the range 1.0 to 1.999999999...999 followed by an exponent specifier like "e+" or "e-" followed by one or more digits.

The exponent may take on just about any value in the range of the machine's double precision datatype.

The default value for the width parameter will take up only as much room as the value requires.

representation choice 'g'

If you just want to get the job done and don't care how your values appear, the 'g' format will use the default C++ stream conversion to float. It could be fixed. It could be scientific. The width parameter will set the minimum field width.

The default value for the width parameter will take up only as much room as the value requires.

representation choice 'e'

This is similar to the 's' format, but the exponent (power of 10) will always be a multiple of 3. That makes the value useful to those who think in Peta, Tera, Giga, Mega, kilo, milli, micro, nano, and pico. Those left in the CGS world can write their own code.

The width parameter is ignored. The field width is determined by the fmt choice and the frac_precision. The field contain a leading sign, up to three decimal digits, a radix point, frac_precision digits, "e+" or "e-" and one or more exponent digits.

The leading (whole) part of the value will be padded with spaces to the left of the sign.

Definition at line 70 of file Format.cxx.

References separator.

Referenced by main().

◆ addI()

Format & SoDa::Format::addI ( int  v,
unsigned int  width = 0 
)

insert a signed integer into the format string

Parameters
vthe signed integer value
widththe minimum width of the field.
Returns
a reference to this SoDa::Format object to allow chaining of method invocations.

The width is passed along to the setw I/O manipulator, so it is no more useful than that. If the value takes more room than the specified width, the width parameter will be ignored and the field will be as wide as necessary to accommodate the value.

Definition at line 50 of file Format.cxx.

Referenced by main().

◆ addS()

Format & SoDa::Format::addS ( const std::string &  v,
unsigned int  width = 0 
)

insert a string into the format string

Parameters
vthe string value
widththe minimum width of the field.
Returns
a reference to this SoDa::Format object to allow chaining of method invocations.

The width is passed along to the setw I/O manipulator, so it is no more useful than that. If the string is wider than the width, the field will expand to fit the string. If the string is smaller than the width the field will be padded to the left with spaces.

The default value for the width parameter will take up only as much room as the value requires.

Definition at line 132 of file Format.cxx.

Referenced by main().

◆ addU()

Format & SoDa::Format::addU ( unsigned int  v,
unsigned int  width = 0 
)

insert an unsigned integer into the format string

Parameters
vthe unsigned integer value
widththe minimum width of the field.
Returns
a reference to this SoDa::Format object to allow chaining of method invocations.

The width is passed along to the setw I/O manipulator, so it is no more useful than that. If the value takes more room than the specified width, the width parameter will be ignored and the field will be as wide as necessary to accommodate the value.

Definition at line 60 of file Format.cxx.

◆ reset()

Format & SoDa::Format::reset ( )

reset the format string to its original value, with all the placeholders restored.

This method allows a format object to be re-used.

Returns
a reference to a format object to allow chaining.

Definition at line 207 of file Format.cxx.

Referenced by main().

◆ str()

const std::string & SoDa::Format::str ( bool  check_for_filled_out = false) const

provide a string representing the current state of the format string with all placeholders "filled in" as far as the last invocation of an add method.

Parameters
check_for_filled_outIf a placeholder has not yet been replaced, this method may throw a BadFormat exception. It doesn't yet.
Returns
a reference to the format string.

Definition at line 213 of file Format.cxx.

Referenced by operator<<().

Member Data Documentation

◆ separator

char SoDa::Format::separator = '.'
static

the radix separator character.

For those in locales where pi is 3,14159 or so, the separater may be replaced, like this:

Definition at line 464 of file Format.hxx.

Referenced by addF().


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