CACAO
Public Member Functions | Static Public Member Functions | Private Types | Private Member Functions | Private Attributes | Static Private Attributes | Friends
cacao::OStream Class Reference

Simple stream class for formatted output. More...

Public Member Functions

 OStream (FILE *file)
 create a new stream with default flags More...
 
 OStream (const OStream &)
 copy stream More...
 
OStreamoperator<< (char)
 
OStreamoperator<< (bool)
 
OStreamoperator<< (long)
 
OStreamoperator<< (unsigned long)
 
OStreamoperator<< (long long)
 
OStreamoperator<< (unsigned long long)
 
OStreamoperator<< (double)
 
OStreamoperator<< (const void *)
 
OStreamoperator<< (const char *)
 
OStreamoperator<< (unsigned int n)
 
OStreamoperator<< (int n)
 
OStreamoperator<< (const SetWidth &)
 
OStreamoperator<< (const SetZero &)
 
OStreamoperator<< (const SetPrecision &)
 
OStreamoperator<< (const SetIndent &)
 
OStreamoperator<< (const SetPrefix &)
 
OStreamoperator<< (const FillZero &)
 
OStreamoperator<< (const Left &)
 
OStreamoperator<< (const Right &)
 
OStreamoperator<< (const Dec &)
 
OStreamoperator<< (const Oct &)
 
OStreamoperator<< (const Hex &)
 
OStreamoperator<< (const FloatDec &)
 
OStreamoperator<< (const Scientific &)
 
OStreamoperator<< (const FloatHex &)
 
OStreamoperator<< (const Indent &)
 
OStreamoperator<< (const Dedent &)
 
OStreamoperator<< (const Nl &)
 
OStreamoperator<< (const Flush &)
 
OStreamoperator<< (const ThreadId &)
 
OStreamoperator<< (Color)
 
OStreamoperator<< (const ResetColor &)
 
OStreamoperator<< (const Bold &)
 
OStreamoperator<< (const NoBold &)
 
OStreamoperator<< (const Underline &)
 
OStreamoperator<< (const NoUnderline &)
 
void set_file (FILE *file)
 

Static Public Member Functions

static void set_force_color (int)
 force color (0 = disabled, 1 = yes, 0 = no) More...
 

Private Types

enum  IntegerFormat { IntFmt_decimal, IntFmt_octal, IntFmt_hexadecimal }
 
enum  FloatFormat { FloatFmt_decimal, FloatFmt_scientific, FloatFmt_hexadecimal }
 
enum  { Align_left, Align_right }
 Alignment to use when padding text. More...
 

Private Member Functions

void on_newline ()
 
void init_flag_defaults ()
 initialize all format flags to their default value More...
 
void init_transient_flags ()
 initialize all flags that only apply to one write operation More...
 
void init_persistent_flags ()
 initialize all flags that survive a write operation More...
 
bool use_color () const
 supports ansi escape codes More...
 

Private Attributes

FILE * file
 file stream writes to More...
 
bool newline
 true iff we are at the beginning of a new line More...
 
bool _use_color
 supports ansi escape codes More...
 
size_t width
 padding for next write More...
 
int precision
 precision More...
 
bool fill_zero
 fill_zero More...
 
enum cacao::OStream:: { ... }  align
 Alignment to use when padding text. More...
 
IntegerFormat int_fmt
 format used to print integer types More...
 
FloatFormat float_fmt
 format used to print floating point types More...
 
size_t indent_lvl
 indentation level More...
 
const char * prefix
 line prefix More...
 
Color prefix_color
 color line prefix is printed in More...
 

Static Private Attributes

static int force_color = 0
 force color (0 = disabled, 1 = yes, 0 = no) More...
 

Friends

class Logging
 

Detailed Description

Simple stream class for formatted output.

This class is designed for debugging, thus usability trumps performance. It mostly mimics the iostreams library, but requires no global constructors. Interally everything is forwarded to stdio

A stream can contain a prefix or intentions. The stream does not detect if your output contains a '
' (newline) character. You must use the manipulator nl instead. It works like std::endl but does not flush the stream.

Simple examples :

* OStream os(stdout);
*
* os << "Hi there, my name is " << bold << "cacao" << nobold << nl;
* os << "I was born in " << 1996 << nl;
* os << "Test failures are " << underline << red << "BAD" << reset_color << nl;
* os << nl;
* os << "Do you like hex? " << hex << 255 << dec << nl;
* os << "Or floating point hex? " << float_hex << 17.3 << float_dec << nl;
*

Unlike a std::iostream you can copy construct an OStream. A copied OStream will write to the same file but has it's own set of format flags that you can set independent of the original stream. But Colors, bold and underline are shared by all streams for a file, because they are stored in the underlying terminal. You should not write nl to the copied stream since the original will not detect the newline.

Example:

* struct MyLittlePony {
* const char *name;
* Color color;
* };
*
* OStream& operator<<(OStream& os, const MyLittlePony& mlp) {
* OStream os2 = os; // new stream with new flags
*
* os2 << mlp.color;
* os2 << "My little pony is called " << setw(20) << right << mlp.name;
* os2 << hex;
*
* // Forgot to unset hex for os2: no problem, hex flag is not shared
* // Forgot to unset color: big problem, colors are shared!
*
* return os; // always return original stream
* }
*

Definition at line 141 of file OStream.hpp.

Member Enumeration Documentation

anonymous enum
private

Alignment to use when padding text.

default value is OStream::Align_right

Enumerator
Align_left 
Align_right 

Definition at line 280 of file OStream.hpp.

Enumerator
FloatFmt_decimal 
FloatFmt_scientific 
FloatFmt_hexadecimal 

Definition at line 244 of file OStream.hpp.

Enumerator
IntFmt_decimal 
IntFmt_octal 
IntFmt_hexadecimal 

Definition at line 239 of file OStream.hpp.

Constructor & Destructor Documentation

cacao::OStream::OStream ( FILE *  file)

create a new stream with default flags

Definition at line 67 of file OStream.cpp.

cacao::OStream::OStream ( const OStream os)

copy stream

creates a new stream with the same file but default

Definition at line 97 of file OStream.cpp.

Member Function Documentation

void cacao::OStream::init_flag_defaults ( )
private

initialize all format flags to their default value

Definition at line 101 of file OStream.cpp.

void cacao::OStream::init_persistent_flags ( )
private

initialize all flags that survive a write operation

Definition at line 114 of file OStream.cpp.

void cacao::OStream::init_transient_flags ( )
private

initialize all flags that only apply to one write operation

Definition at line 106 of file OStream.cpp.

void cacao::OStream::on_newline ( )
private

Definition at line 125 of file OStream.cpp.

OStream & cacao::OStream::operator<< ( char  c)

Definition at line 209 of file OStream.cpp.

OStream & cacao::OStream::operator<< ( bool  b)

Definition at line 228 of file OStream.cpp.

OStream & cacao::OStream::operator<< ( long  n)

Definition at line 231 of file OStream.cpp.

OStream & cacao::OStream::operator<< ( unsigned long  n)

Definition at line 247 of file OStream.cpp.

OStream & cacao::OStream::operator<< ( long long  n)

Definition at line 239 of file OStream.cpp.

OStream & cacao::OStream::operator<< ( unsigned long long  n)

Definition at line 255 of file OStream.cpp.

OStream & cacao::OStream::operator<< ( double  n)
C++11:
Flag a introduced in C99, disable hex float for now

Definition at line 266 of file OStream.cpp.

OStream & cacao::OStream::operator<< ( const void p)

Definition at line 275 of file OStream.cpp.

OStream & cacao::OStream::operator<< ( const char *  cs)

Definition at line 284 of file OStream.cpp.

OStream& cacao::OStream::operator<< ( unsigned int  n)
inline

Definition at line 163 of file OStream.hpp.

OStream& cacao::OStream::operator<< ( int  n)
inline

Definition at line 167 of file OStream.hpp.

OStream & cacao::OStream::operator<< ( const SetWidth s)

Definition at line 306 of file OStream.cpp.

OStream & cacao::OStream::operator<< ( const SetZero s)

Definition at line 311 of file OStream.cpp.

OStream & cacao::OStream::operator<< ( const SetPrecision s)

Definition at line 316 of file OStream.cpp.

OStream & cacao::OStream::operator<< ( const SetIndent s)

Definition at line 321 of file OStream.cpp.

OStream & cacao::OStream::operator<< ( const SetPrefix s)

Definition at line 326 of file OStream.cpp.

OStream & cacao::OStream::operator<< ( const FillZero )

Definition at line 333 of file OStream.cpp.

OStream & cacao::OStream::operator<< ( const Left )

Definition at line 339 of file OStream.cpp.

OStream & cacao::OStream::operator<< ( const Right )

Definition at line 344 of file OStream.cpp.

OStream & cacao::OStream::operator<< ( const Dec )

Definition at line 349 of file OStream.cpp.

OStream & cacao::OStream::operator<< ( const Oct )

Definition at line 354 of file OStream.cpp.

OStream & cacao::OStream::operator<< ( const Hex )

Definition at line 359 of file OStream.cpp.

OStream & cacao::OStream::operator<< ( const FloatDec )

Definition at line 364 of file OStream.cpp.

OStream & cacao::OStream::operator<< ( const Scientific )

Definition at line 369 of file OStream.cpp.

OStream & cacao::OStream::operator<< ( const FloatHex )

Definition at line 374 of file OStream.cpp.

OStream & cacao::OStream::operator<< ( const Indent )

Definition at line 379 of file OStream.cpp.

OStream & cacao::OStream::operator<< ( const Dedent )

Definition at line 384 of file OStream.cpp.

OStream & cacao::OStream::operator<< ( const Nl )

Definition at line 389 of file OStream.cpp.

OStream & cacao::OStream::operator<< ( const Flush )

Definition at line 395 of file OStream.cpp.

OStream & cacao::OStream::operator<< ( const ThreadId )

Definition at line 401 of file OStream.cpp.

OStream & cacao::OStream::operator<< ( Color  c)

Definition at line 405 of file OStream.cpp.

OStream & cacao::OStream::operator<< ( const ResetColor )

Definition at line 432 of file OStream.cpp.

OStream & cacao::OStream::operator<< ( const Bold )

Definition at line 436 of file OStream.cpp.

OStream & cacao::OStream::operator<< ( const NoBold )

Definition at line 440 of file OStream.cpp.

OStream & cacao::OStream::operator<< ( const Underline )

Definition at line 444 of file OStream.cpp.

OStream & cacao::OStream::operator<< ( const NoUnderline )

Definition at line 448 of file OStream.cpp.

void cacao::OStream::set_file ( FILE *  file)
inline

Definition at line 209 of file OStream.hpp.

void cacao::OStream::set_force_color ( int  b)
static

force color (0 = disabled, 1 = yes, 0 = no)

Definition at line 76 of file OStream.cpp.

bool cacao::OStream::use_color ( ) const
inlineprivate

supports ansi escape codes

Definition at line 87 of file OStream.cpp.

Friends And Related Function Documentation

friend class Logging
friend

Definition at line 322 of file OStream.hpp.

Field Documentation

bool cacao::OStream::_use_color
private

supports ansi escape codes

Definition at line 231 of file OStream.hpp.

enum { ... } cacao::OStream::align

Alignment to use when padding text.

default value is OStream::Align_right

FILE* cacao::OStream::file
private

file stream writes to

Definition at line 225 of file OStream.hpp.

bool cacao::OStream::fill_zero
private

fill_zero

! fill_zero is reset to false by all standard write operations !

default value is false

Definition at line 274 of file OStream.hpp.

FloatFormat cacao::OStream::float_fmt
private

format used to print floating point types

default value is decimal

Definition at line 295 of file OStream.hpp.

int cacao::OStream::force_color = 0
staticprivate

force color (0 = disabled, 1 = yes, 0 = no)

Definition at line 237 of file OStream.hpp.

size_t cacao::OStream::indent_lvl
private

indentation level

every new line will start with OStream::Flags::indent_lvl * 4 spaces

default value is 0

Definition at line 303 of file OStream.hpp.

IntegerFormat cacao::OStream::int_fmt
private

format used to print integer types

default value is decimal

Definition at line 289 of file OStream.hpp.

bool cacao::OStream::newline
private

true iff we are at the beginning of a new line

Definition at line 228 of file OStream.hpp.

int cacao::OStream::precision
private

precision

! precision is reset to -1 by all standard write operations !

default value is -1 (i.e. turned off)

Definition at line 266 of file OStream.hpp.

const char* cacao::OStream::prefix
private

line prefix

ignored if NULL will be printed at start of every new line

default value is NULL

Definition at line 312 of file OStream.hpp.

Color cacao::OStream::prefix_color
private

color line prefix is printed in

ignored if negative

default value is -1

Definition at line 320 of file OStream.hpp.

size_t cacao::OStream::width
private

padding for next write

! width is reset to zero by all standard write operations !

default value is 0

Definition at line 258 of file OStream.hpp.


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