cx::FileOutputStream Class Reference

Stream to write formated data to a file. More...

#include <FileOutputStream.hpp>

Inheritance diagram for cx::FileOutputStream:
cx::DataOutputStream cx::Stream cx::NonCopyable

List of all members.

Public Types

enum  Mode {
  singleThreaded,
  multiThreaded
}
 

Operation mode.

More...

Public Member Functions

 FileOutputStream (const std::string &fileName, Mode mode=singleThreaded, unsigned int chunkSize=DEFAULT_CHUNK_SIZE)
 Constructor.
 FileOutputStream (Filter &filter, const std::string &fileName, Mode mode=singleThreaded, unsigned int chunkSize=DEFAULT_CHUNK_SIZE)
 Constructor with one filter.
 FileOutputStream (Filter &filter1, Filter &filter2, const std::string &fileName, Mode mode=singleThreaded, unsigned int chunkSize=DEFAULT_CHUNK_SIZE)
 Constructor with two filters.
 FileOutputStream (const std::vector< Filter * > &filters, const std::string &fileName, Mode mode=singleThreaded, unsigned int chunkSize=DEFAULT_CHUNK_SIZE)
 Constructor with a vector of filters.
 ~FileOutputStream ()
 Destructor.
void close ()
 Close the stream.
void write (const void *data, std::size_t sizeInBytes)
 Write a generic bunch of data.
void write (const char *data)
 Write a '' terminated char array.
void write (const std::string &data)
 Write a string.
void write (const wchar_t *data)
 Write a '' terminated wide char array.
void write (const std::wstring &data)
 Write a wide string.
void write (const Resource &resource)
 Write a resource object.
void writeBool (bool data)
 Write a boolean value.
void writeInt8 (Int8 data)
 Write a 1 byte integer.
void writeUint8 (Uint8 data)
 Write a 1 byte unsigned integer.
void writeInt16 (Int16 data)
 Write a 2 bytes integer.
void writeUint16 (Uint16 data)
 Write a 2 bytes unsigned integer.
void writeInt32 (Int32 data)
 Write a 4 bytes integer.
void writeUint32 (Uint32 data)
 Write a 4 bytes unsigned integer.
void writeFloat (float data)
 Write a float value.
void writeDouble (double data)
 Write a double value.
DataOutputStreamoperator<< (bool data)
 Write a boolean value.
DataOutputStreamoperator<< (Int8 data)
 Write a 1 byte integer.
DataOutputStreamoperator<< (Uint8 data)
 Write a 1 byte unsigned integer.
DataOutputStreamoperator<< (Int16 data)
 Write a 2 bytes integer.
DataOutputStreamoperator<< (Uint16 data)
 Write a 2 bytes unsigned integer.
DataOutputStreamoperator<< (Int32 data)
 Write a 4 bytes integer.
DataOutputStreamoperator<< (Uint32 data)
 Write a 4 bytes unsigned integer.
DataOutputStreamoperator<< (float data)
 Write a float value.
DataOutputStreamoperator<< (double data)
 Write a double value.
DataOutputStreamoperator<< (const char *data)
 Write a '' terminated char array.
DataOutputStreamoperator<< (const std::string &data)
 Write a string.
DataOutputStreamoperator<< (const wchar_t *data)
 Write a '' terminated wide char array.
DataOutputStreamoperator<< (const std::wstring &data)
 Write a wide string.
DataOutputStreamoperator<< (const Resource &resource)
 Write a resource object.
void wait ()
 Wait for stream termination.

Static Public Attributes

static const std::size_t DEFAULT_CHUNK_SIZE
 Default buffer size.

Protected Member Functions

void init (Consumer &consumer)
 Initialization by consumer.
void init (Filter &filter1, Consumer &consumer)
 Initialization with one filter.
void init (Filter &filter1, Filter &filter2, Consumer &consumer)
 Initialization with two filters.
void init (const std::vector< Filter * > &filters, Consumer &consumer)
 Initialization with a vector of filters.
void init (Producer &producer, const std::vector< Filter * > &filters, Consumer &consumer, Mode mode)
 Initialize the stream.
void safeWait ()
 Safelly wait for stream termination.
void singleThreadRun (Producer &producer, const std::vector< Filter * > &filters, Consumer &consumer)
 Do the full streaming in singleTreaded mode.
void multiThreadLaunch (Producer &producer, const std::vector< Filter * > &filters, Consumer &consumer)
 Launch the streaming process in multiTreaded mode.
bool singleProcessProducer (Producer &producer, Chunk &chunk)
 Do a producer step in singleTreaded mode.
void singleProcessFilters (const std::vector< Filter * > &filters, Chunk &chunk, bool hasMore)
 Process all filters in singleTreaded mode.
bool singleProcessConsumer (Consumer &consumer, Chunk &chunk, bool hasMore)
 Do a consumer step in singleTreaded mode.

Protected Attributes

std::vector< PipeNode * > pipe
 The pipeline threads (multiTreaded mode only).
bool waited
 True if stream already waited for it's threads (multiTreaded mode only).

Detailed Description

Stream to write formated data to a file.

FileoutputStream derives from cx::DataOutputStream, and internally uses a cx::File as consumer. In other words, FileOutputStream writes structured data to a file.

It can be used like the standard std::ostream to stream native types, strings and resource objects (objects of classes derived from cx::Resource). However, for basic types it is recomended that you use the equivalent writing functions instead of the operator <<. This is because the size of basic types is differen between platforms.

So, instead of this:

 int myInteger = ...
 stream << myInteger;

You should do:

 int myInteger
 stream.writeInt32(myInteger); // this will write an integer of exactly 4 bytes

Alternativelly, use the Codex basic types instead:

 cx::Uint32 myInteger
 stream << myInteger; // this will write an integer of exactly 4 bytes

Usage example:

 cx::FileOutputStream stream("outputFile.dat");
 // write some data
 bool myFlag;
 std::string myString;
 cx::MyResource myRsc; // MyResource derives from cx::Resource
 stream.writeUint16(568); // write an unsigned int of 2 bytes (16 bits).
 stream << myFlag << myString << myRsc;
 ...
 stream.close();

Note: Remember to catch eventual exceptions when using pipeline components (Stream, filters, producers and consumers).

See also:
cx::DataOutputStream
cx::FileInputStream
cx::File

Definition at line 41 of file FileOutputStream.hpp.


Member Enumeration Documentation

enum cx::Stream::Mode [inherited]

Operation mode.

Enumerator:
singleThreaded 

Do not create threads.

multiThreaded 

Use a thread per pipeline node.

Definition at line 55 of file Stream.hpp.


Constructor & Destructor Documentation

cx::FileOutputStream::FileOutputStream ( const std::string &  fileName,
Mode  mode = singleThreaded,
unsigned int  chunkSize = DEFAULT_CHUNK_SIZE 
) [explicit]

Constructor.

Parameters:
fileName Path to the file to write on.
mode Stream operation mode (singleThreaded by default).
chunkSize Size of internal buffer.
cx::FileOutputStream::FileOutputStream ( Filter filter,
const std::string &  fileName,
Mode  mode = singleThreaded,
unsigned int  chunkSize = DEFAULT_CHUNK_SIZE 
)

Constructor with one filter.

Parameters:
filter A filter to process data before consuming it.
fileName Path to the file to write on.
mode Stream operation mode (singleThreaded by default).
chunkSize Size of internal buffer.
cx::FileOutputStream::FileOutputStream ( Filter filter1,
Filter filter2,
const std::string &  fileName,
Mode  mode = singleThreaded,
unsigned int  chunkSize = DEFAULT_CHUNK_SIZE 
)

Constructor with two filters.

Parameters:
filter1 Filter to process the formated data.
filter2 Filter to process the data filtered by
fileName Path to the file to write on. filter1 before writing it on the file.
chunkSize Size of internal buffer.
cx::FileOutputStream::FileOutputStream ( const std::vector< Filter * > &  filters,
const std::string &  fileName,
Mode  mode = singleThreaded,
unsigned int  chunkSize = DEFAULT_CHUNK_SIZE 
)

Constructor with a vector of filters.

Parameters:
filters Filters to be used before consumption.
fileName Path to the file to write on.
mode Stream operation mode (singleThreaded by default).
chunkSize Size of internal buffer.
cx::FileOutputStream::~FileOutputStream (  ) 

Destructor.

Close the stream.

See also:
close

Member Function Documentation

void cx::FileOutputStream::close (  )  [virtual]

Close the stream.

Reimplemented from cx::DataOutputStream.

void cx::Stream::init ( Producer producer,
const std::vector< Filter * > &  filters,
Consumer consumer,
Mode  mode 
) [protected, inherited]

Initialize the stream.

Parameters:
producer The stream data producer.
filters Filters to be used between the producer and the consumer.
consumer The stream data consumer.
mode Operation mode (singleThreaded by default).
void cx::DataOutputStream::init ( const std::vector< Filter * > &  filters,
Consumer consumer 
) [protected, inherited]

Initialization with a vector of filters.

Parameters:
filters Filters to be used before consumption.
consumer The data consumer.
void cx::DataOutputStream::init ( Filter filter1,
Filter filter2,
Consumer consumer 
) [protected, inherited]

Initialization with two filters.

Parameters:
f1 A filter to process the source data.
f2 A filter to process data after f1, before consumption.
consumer The data consumer.
void cx::DataOutputStream::init ( Filter filter1,
Consumer consumer 
) [protected, inherited]

Initialization with one filter.

Parameters:
f1 A filter to process data before consumption.
consumer The data consumer.
void cx::DataOutputStream::init ( Consumer consumer  )  [protected, inherited]

Initialization by consumer.

Parameters:
consumer The data consumer.
void cx::Stream::multiThreadLaunch ( Producer producer,
const std::vector< Filter * > &  filters,
Consumer consumer 
) [protected, inherited]

Launch the streaming process in multiTreaded mode.

Parameters:
producer The stream data producer.
filters Filters to be used between the producer and the consumer.
consumer The stream data consumer.
DataOutputStream& cx::DataOutputStream::operator<< ( const Resource resource  )  [inherited]

Write a resource object.

DataOutputStream& cx::DataOutputStream::operator<< ( const std::wstring &  data  )  [inherited]

Write a wide string.

DataOutputStream& cx::DataOutputStream::operator<< ( const wchar_t *  data  )  [inherited]

Write a '' terminated wide char array.

DataOutputStream& cx::DataOutputStream::operator<< ( const std::string &  data  )  [inherited]

Write a string.

DataOutputStream& cx::DataOutputStream::operator<< ( const char *  data  )  [inherited]

Write a '' terminated char array.

DataOutputStream& cx::DataOutputStream::operator<< ( double  data  )  [inherited]

Write a double value.

DataOutputStream& cx::DataOutputStream::operator<< ( float  data  )  [inherited]

Write a float value.

DataOutputStream& cx::DataOutputStream::operator<< ( Uint32  data  )  [inherited]

Write a 4 bytes unsigned integer.

DataOutputStream& cx::DataOutputStream::operator<< ( Int32  data  )  [inherited]

Write a 4 bytes integer.

DataOutputStream& cx::DataOutputStream::operator<< ( Uint16  data  )  [inherited]

Write a 2 bytes unsigned integer.

DataOutputStream& cx::DataOutputStream::operator<< ( Int16  data  )  [inherited]

Write a 2 bytes integer.

DataOutputStream& cx::DataOutputStream::operator<< ( Uint8  data  )  [inherited]

Write a 1 byte unsigned integer.

DataOutputStream& cx::DataOutputStream::operator<< ( Int8  data  )  [inherited]

Write a 1 byte integer.

DataOutputStream& cx::DataOutputStream::operator<< ( bool  data  )  [inherited]

Write a boolean value.

void cx::Stream::safeWait (  )  [protected, inherited]

Safelly wait for stream termination.

Any exception caught is written on std::cerr.

bool cx::Stream::singleProcessConsumer ( Consumer consumer,
Chunk chunk,
bool  hasMore 
) [protected, inherited]

Do a consumer step in singleTreaded mode.

Parameters:
Consumer The consumer being processed.
chunk The chunk of data to be consumed.
hasMore Flag telling if the producer has more data (i.e., this isn't the last chunk).
Returns:
true if the consumer can acept more data.
void cx::Stream::singleProcessFilters ( const std::vector< Filter * > &  filters,
Chunk chunk,
bool  hasMore 
) [protected, inherited]

Process all filters in singleTreaded mode.

Parameters:
filters The filters that will process the data chunk.
chunk The chunk of data to be filtered.
hasMore Flag telling if the producer has more data (i.e., this isn't the last chunk).
bool cx::Stream::singleProcessProducer ( Producer producer,
Chunk chunk 
) [protected, inherited]

Do a producer step in singleTreaded mode.

Parameters:
producer The data producer being processed.
[out] chunk The produced data.
Returns:
true if the producer has more data to produce.
void cx::Stream::singleThreadRun ( Producer producer,
const std::vector< Filter * > &  filters,
Consumer consumer 
) [protected, inherited]

Do the full streaming in singleTreaded mode.

Parameters:
producer The stream data producer.
filters Filters to be used between the producer and the consumer.
consumer The stream data consumer.
void cx::Stream::wait (  )  [inherited]

Wait for stream termination.

Only applycable when the stream is running in multiThreaded mode.

void cx::DataOutputStream::write ( const Resource resource  )  [inherited]

Write a resource object.

void cx::DataOutputStream::write ( const std::wstring &  data  )  [inherited]

Write a wide string.

void cx::DataOutputStream::write ( const wchar_t *  data  )  [inherited]

Write a '' terminated wide char array.

void cx::DataOutputStream::write ( const std::string &  data  )  [inherited]

Write a string.

void cx::DataOutputStream::write ( const char *  data  )  [inherited]

Write a '' terminated char array.

void cx::DataOutputStream::write ( const void *  data,
std::size_t  sizeInBytes 
) [inherited]

Write a generic bunch of data.

Parameters:
data A pointer to the object to be written.
sizeInBytes Size of the object in bytes.
void cx::DataOutputStream::writeBool ( bool  data  )  [inherited]

Write a boolean value.

void cx::DataOutputStream::writeDouble ( double  data  )  [inherited]

Write a double value.

void cx::DataOutputStream::writeFloat ( float  data  )  [inherited]

Write a float value.

void cx::DataOutputStream::writeInt16 ( Int16  data  )  [inherited]

Write a 2 bytes integer.

void cx::DataOutputStream::writeInt32 ( Int32  data  )  [inherited]

Write a 4 bytes integer.

void cx::DataOutputStream::writeInt8 ( Int8  data  )  [inherited]

Write a 1 byte integer.

void cx::DataOutputStream::writeUint16 ( Uint16  data  )  [inherited]

Write a 2 bytes unsigned integer.

void cx::DataOutputStream::writeUint32 ( Uint32  data  )  [inherited]

Write a 4 bytes unsigned integer.

void cx::DataOutputStream::writeUint8 ( Uint8  data  )  [inherited]

Write a 1 byte unsigned integer.


Member Data Documentation

const std::size_t cx::DataOutputStream::DEFAULT_CHUNK_SIZE [static, inherited]

Default buffer size.

Definition at line 248 of file DataOutputStream.hpp.

std::vector<PipeNode*> cx::Stream::pipe [protected, inherited]

The pipeline threads (multiTreaded mode only).

Definition at line 222 of file Stream.hpp.

bool cx::Stream::waited [protected, inherited]

True if stream already waited for it's threads (multiTreaded mode only).

Definition at line 223 of file Stream.hpp.


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