A numeric to ascii string conversion class based on snprintf. The constructor for this class constructs an object containing a char buffer holding the formatted resultant string. More...
#include <fmtd.h>
Public Types | |
enum | constants { MIN_WIDTH = 25, BUFF_SAFETY = 10, POS_WIDTH = (WIDTH < 0) ? -WIDTH : WIDTH, FIELD_WIDTH = (PRECISION > POS_WIDTH ? PRECISION : ( (POS_WIDTH == 0) ? 0 : POS_WIDTH ) ), BUFF_SIZE = FIELD_WIDTH + BUFF_SAFETY, PADDING = (POS_WIDTH != 0) || (PRECISION != 0), LJUST = (WIDTH< 1) } |
Public Member Functions | |
fmtd (std::string const &s) | |
fmtd (char const *s) | |
template<class T > | |
fmtd (T const &v) | |
operator char const * () const | |
size_t | size () const |
Private Attributes | |
char | buffer_ [BUFF_SIZE] |
size_t | size_ |
Friends | |
std::ostream & | operator<< (std::ostream &s, fmtd const &f) |
A numeric to ascii string conversion class based on snprintf. The constructor for this class constructs an object containing a char buffer holding the formatted resultant string.
Template class fmtd<> defines a family of string converters that can be used like this:
cout << "some text" << fmtd<>(37) << endl; cout << "some text" << fmtd<20,4,'0'>(-1e8) << endl; cout << fmtd<0,0,0,16>(20) << endl; cout << fmtd<0,0,0,8>(20) << endl;
In the above cases, the number 37 is coverted to a string using sprintf and is append to the output stream, right after :some text", leading to lines containing:
some text37 some text-00000100000000.0000 14 24
The fmtd template has the following template parameters:
WIDTH | -- defaults to 0, specifies the maximum width of the resultant converted text. But specifying 0 for width means that the output width is variable, not fixed. If negative, the field with is assumed be left justified -- right justified is the default. See the snprintf man page for width and precision. | |
PRECISION | -- varies between integer and float. See the snprintf man page. | |
PAD | -- specifies whether we are using blanks or zeros to pad the resultant output buffer. | |
BASE | -- either 10, 8, 16. Ignored for floating point numbers | |
ERRCHR | -- specifies the text to put in the output field if |
Note also that the data type of the object being converted is not a part of the template signature. Instead, the constructor to the fmtd class is templated and that is where the actual conversion algorithm appears.
Usage advice:
You can use a fmtd<> object as a variable:
fmtd<20> someVar( (unsigned int)(3) );
But generally, this is ill-advised. The whole point of this class it to use the fmtd<parms> constructor in output and other string conversion expressions.
Definition at line 421 of file fmtd.h.
enum constants |
fmtd | ( | T const & | v | ) |
fmtd | ( | char const * | s | ) |
fmtd | ( | std::string const & | s | ) |
std::ostream& operator<< | ( | std::ostream & | s, | |
fmtd< WIDTH, PRECISION, PAD, BASE, ERRCHR > const & | f | |||
) | [friend] |