cs Class Reference

The type of an expression that can be printed using operator <<. More...

#include <fmtio.h>

Collaboration diagram for cs:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 cs (char const *cs)
 Begin a conversion sequence that contains one or more % operators to define the variables of interest. For example:
 operator std::string const & () const
template<class T , int N>
cs const & operator% (T const (&rhs)[N]) const
 Used to print any type. Triggered by the following syntax is used:
cs const & operator% (char const *rhs) const
 Used to print any type. Triggered by the following syntax is used:
template<class T >
cs const & operator% (T const &rhs) const
 Used to print any type. Triggered by the following syntax is used:
size_t size () const
 ~cs ()

Private Member Functions

void advanceFormat () const
 Consume text from the control string until the next '' character and append the text to the output buffer.
 cs (cs const &)
bool isIntegralFormat () const
 Return true if the current expectedFormat_ is an integral format.
template<class T >
void operator<< (T &) const
template<class T >
void output (T const &t, printableType::UserDefined) const
 Used to print user defined types.
template<class T >
void output (T const &t, printableType::DoubleFloat) const
 Used to print doubles.
template<class T >
void output (T const &t, printableType::Float) const
 Used to print floats.
template<class T >
void output (T const &t, printableType::StlString) const
 Used to print std::string.
template<class T >
void output (T const &t, printableType::String) const
 Used to print char const *, and unsigned char const *.
template<class T >
void output (T const &t, printableType::Char) const
 Used to print character data.
template<class T >
void output (T const &t, printableType::Unsigned) const
 Used to output unsigned integral data types like unsigned int, unsigned short, unsigned char, etc.
template<class T >
void output (T const &t, printableType::Integral) const
 Used to output signed integral data types like int, short, long, unsigned, long long, etc.
void outputFloat (double d) const
 Generalized floating point print routine.
void outputIntegral (long long t) const
 General purpose integer output routine.
void reset () const
 Reset the field formatting fields to their default state.

Private Attributes

std::string buffer_
char const * cs_
char expectedFormat_
int fieldWidth_
int fillChar_
int left_
int precision_

Detailed Description

The type of an expression that can be printed using operator <<.

This class behaves similarly to boost::format() but runs about 3 times faster.

Expressions of this type are constructed like this:

      fmtio::cs("%d-%d") % 13 % 14

When printed using cout, you will see text of the form:

      13-14

You can also you expressions of this type to populate string variables:

     string fred = fmtio::cs("%-04x") % 8;

In this case, fred is initialized with 0008.

Variables of this type are only minimally useful -- the general idea is that you use the constructor, cs(char const *), as passed in an cout expression to construct formatted output in a manner that is easier to use than if you had used the iomanip header file contents to do the same job.

For example:

cout << fmtio::cs("Stuff: %-08.4f") % 9.3 << endl;

In this case, the sub-expression, cs(...) % 9.3, is converted into a string which is then printed as: Stuff: 0009.300.

Most builtin types can all be sent to ostreams or converted into strings by this class.

User defined struct and classes can only be processed if there is an operator<<(std::ostream,...) defined for them.

Pointers and arrays are automatically treated as integers. You can use x to print them as hex.

Enumerations are not at this time usable. Convert them to integers. For example:

      #include <iostream>
      #include <fmtio/fmtio.h>
   
      enum stuff { enum1 };
   
      int main()
      {
         std::cout << fmtio::cs("%d") % enum1 << std::endl;  // WILL NOT COMPILE!
   
         std::cout << fmtio::cs("%d") % int(enum1) << std::endl;  // works ok
         
      }

The control strings are simplified form of printf -- basically all you get is this:

sprintf is used to generate most of the strings, but floats and and user defined types are printed using operator<< into a stringstream.

The conversion characters are as follows:

Multiple % operators can be used with a given cs() but multiple % conversions must be specified when this occurs and they must correcly the match the basic data type as detected by the classifer() methods. Here's an example of one that works:

       cout << fmtio::cs("%s %d %x")  %"stringdata" %integerVariable %integerVariable << endl;

And here is an invalid example:

       cout << fmtio::cs("%s %d %x")  %1.0 %integerVariable %integerVariable << endl;

In this invalid example, s is specified but a floating point data type is actually supplied for the first parameter.

All the parameters must be matched and they must all be correct.

Definition at line 212 of file fmtio.h.


Constructor & Destructor Documentation

cs ( char const *  cs  ) 

Begin a conversion sequence that contains one or more % operators to define the variables of interest. For example:

             cout << cs("%d, %d") %100 %200 << endl;

Will produce "100, 200" to sdout

Definition at line 356 of file fmtio.h.

~cs (  ) 

Definition at line 374 of file fmtio.h.

cs ( cs const &   )  [private]

Member Function Documentation

void advanceFormat (  )  const [private]

Consume text from the control string until the next '' character and append the text to the output buffer.

This function treats %% as a %, per printf standards.

Definition at line 389 of file fmtio.h.

bool isIntegralFormat (  )  const [private]

Return true if the current expectedFormat_ is an integral format.

Definition at line 536 of file fmtio.h.

operator std::string const & (  )  const

Definition at line 352 of file fmtio.h.

cs const& operator% ( T const (&)  rhs[N]  )  const

Used to print any type. Triggered by the following syntax is used:

           long a_long[40];
         
           cout << cs(cs) %a_long;

This member assumes that the cs will have been constructed with the appropriate % conversion character in the correct position for this "%someObject" to match it.

Definition at line 777 of file fmtio.h.

cs const& operator% ( char const *  rhs  )  const

Used to print any type. Triggered by the following syntax is used:

           cout << cs(cs) %"[";

This member assumes that the cs will have been constructed with the appropriate % conversion character in the correct position for this "%someObject" to match it.

Definition at line 753 of file fmtio.h.

cs const& operator% ( T const &  rhs  )  const

Used to print any type. Triggered by the following syntax is used:

           cout << cs(cs) % someObject;

This member assumes that the cs will have been constructed with the appropriate % conversion character in the correct position for this "%someObject" to match it.

Definition at line 730 of file fmtio.h.

Here is the call graph for this function:

void operator<< ( T &   )  const [private]
void output ( T const &  t,
printableType::UserDefined   
) const [private]

Used to print user defined types.

Definition at line 698 of file fmtio.h.

void output ( T const &  t,
printableType::DoubleFloat   
) const [private]

Used to print doubles.

Definition at line 682 of file fmtio.h.

void output ( T const &  t,
printableType::Float   
) const [private]

Used to print floats.

Definition at line 666 of file fmtio.h.

void output ( T const &  t,
printableType::StlString   
) const [private]

Used to print std::string.

Definition at line 616 of file fmtio.h.

void output ( T const &  t,
printableType::String   
) const [private]

Used to print char const *, and unsigned char const *.

Definition at line 600 of file fmtio.h.

void output ( T const &  t,
printableType::Char   
) const [private]

Used to print character data.

Definition at line 580 of file fmtio.h.

void output ( T const &  t,
printableType::Unsigned   
) const [private]

Used to output unsigned integral data types like unsigned int, unsigned short, unsigned char, etc.

Definition at line 565 of file fmtio.h.

void output ( T const &  t,
printableType::Integral   
) const [private]

Used to output signed integral data types like int, short, long, unsigned, long long, etc.

Definition at line 549 of file fmtio.h.

void outputFloat ( double  d  )  const [private]

Generalized floating point print routine.

Definition at line 633 of file fmtio.h.

void outputIntegral ( long long  t  )  const [private]

General purpose integer output routine.

Definition at line 473 of file fmtio.h.

void reset (  )  const [private]

Reset the field formatting fields to their default state.

Definition at line 338 of file fmtio.h.

size_t size (  )  const

Definition at line 802 of file fmtio.h.


Member Data Documentation

std::string buffer_ [mutable, private]

Definition at line 329 of file fmtio.h.

char const* cs_ [mutable, private]

Definition at line 327 of file fmtio.h.

char expectedFormat_ [mutable, private]

Definition at line 325 of file fmtio.h.

int fieldWidth_ [mutable, private]

Definition at line 331 of file fmtio.h.

int fillChar_ [mutable, private]

Definition at line 332 of file fmtio.h.

int left_ [mutable, private]

Definition at line 334 of file fmtio.h.

int precision_ [mutable, private]

Definition at line 333 of file fmtio.h.


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