fmtd< WIDTH, PRECISION, PAD, BASE, ERRCHR > Struct Template Reference

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>

List of all members.

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)

Detailed Description

template<int WIDTH = 0, int PRECISION = 0, int PAD = ' ', int BASE = 10, int ERRCHR = '@'>
struct cxxtls::fmtd< WIDTH, PRECISION, PAD, BASE, ERRCHR >

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:

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.


Member Enumeration Documentation

enum constants
Enumerator:
MIN_WIDTH 
BUFF_SAFETY 
POS_WIDTH 
FIELD_WIDTH 
BUFF_SIZE 
PADDING 
LJUST 

Definition at line 496 of file fmtd.h.


Constructor & Destructor Documentation

fmtd ( T const &  v  ) 

Definition at line 516 of file fmtd.h.

Here is the call graph for this function:

fmtd ( char const *  s  ) 

Definition at line 534 of file fmtd.h.

Here is the call graph for this function:

fmtd ( std::string const &  s  ) 

Definition at line 545 of file fmtd.h.

Here is the call graph for this function:


Member Function Documentation

operator char const * (  )  const

Definition at line 558 of file fmtd.h.

size_t size (  )  const

Definition at line 562 of file fmtd.h.


Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  s,
fmtd< WIDTH, PRECISION, PAD, BASE, ERRCHR > const &  f 
) [friend]

Definition at line 567 of file fmtd.h.


Member Data Documentation

char buffer_[BUFF_SIZE] [private]

Definition at line 507 of file fmtd.h.

size_t size_ [private]

Definition at line 509 of file fmtd.h.


The documentation for this struct was generated from the following file:
Generated on Wed Feb 29 22:55:29 2012 for CXXUtilities by  doxygen 1.6.3