| OpenMS 2.5.0

Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Friends | List of all members
StreamHandler Class Reference
Concept

Provides a central class to register globally used output streams. Currently supported streams are. More...

`#include <OpenMS/CONCEPT/StreamHandler.h>`

## Public Types

enum | StreamType { FILE, STRING }
| Defines the type of the stream that should be handled. More...

## Public Member Functions

| StreamHandler ()
| Default constructor. More...
virtual | ~StreamHandler ()
| destructor More...
Int | registerStream (StreamType const type, const String &stream_name)
| Creates a stream of type `type` and with name `stream_name`. More...
void | unregisterStream (StreamType const type, const String &stream_name)
| De-registers a stream of type `type` and with name `stream_name` from the handler. More...
std::ostream & | getStream (StreamType const type, const String &stream_name)
| Returns a reference to the stream of type `type` and with name `stream_name`. More...
bool | hasStream (const StreamType type, const String &stream_name)
| Returns true if the stream `stream_name` with type `type` is registered. More...

## Protected Member Functions

std::ostream * | createStream_ (const StreamType type, const String &stream_name)
| Creates a stream with the given type and the given name. More...

## Protected Attributes

std::map< String, std::ostream * > | name_to_stream_map_
| Maps all registered stream names to the corresponding std::ostream. More...
std::map< String, StreamType > | name_to_type_map_
| Maps all registered stream names to the corresponding StreamHandler::StreamType. More...
std::map< String, Size > | name_to_counter_map_
| Maps all registered stream names to the number of times it was registered. If the counter goes to zero, the stream will be closed and removed. More...

## Private Member Functions

| StreamHandler (const StreamHandler &source)
| copy constructor More...
virtual StreamHandler & | operator= (const StreamHandler &source)
| assignment operator More...

## Friends

std::ostream & | operator<< (std::ostream &os, StreamHandler const &stream_handler)
| Overload for the insertion operator (operator<<) to have a formatted output of the StreamHandler. More...

## Detailed Description

Provides a central class to register globally used output streams. Currently supported streams are.

- std::ofstream
- std::ostringstream

You can simply request a stream from StreamHandler and it will manage the process of construction and destruction (if the stream is not needed any more).

A normal scenario would be

`STREAM_HANDLER.registerStream(StreamHandler::FILE, "name_of_the_output_file");
STREAM_HANDLER.getStream(StreamHandler::FILE, "name_of_the_output_file") << "This will be send to the file" << std::endl;
// ...
// you do not need the file any more
STREAM_HANDLER.unregisterStream(StreamHandler::FILE, "name_of_the_output_file");
// StreamHandler will close the file stream if no other part of your program requested the same stream`

## Member Enumeration Documentation

## ◆ StreamType

enum StreamType

Defines the type of the stream that should be handled.

Enumerator
FILE |
STRING |

## Constructor & Destructor Documentation

## ◆ StreamHandler() [1/2]

StreamHandler | ( |  | ) |

Default constructor.

## ◆ ~StreamHandler()

virtual ~StreamHandler | ( |  | ) |

virtual

destructor

## ◆ StreamHandler() [2/2]

StreamHandler | ( | const StreamHandler & | source | ) |

private

copy constructor

## Member Function Documentation

## ◆ createStream_()

std::ostream* createStream_ | ( | const StreamType | type,
|  | const String & | stream_name
| ) |  |

protected

Creates a stream with the given type and the given name.

Parameters

  type | Type of the stream (e.g. FILE)
  stream_name | Name of the stream (e.g. the file name for a file stream).

Returns
  A pointer to the created stream.

## ◆ getStream()

std::ostream& getStream | ( | StreamType const | type,
|  | const String & | stream_name
| ) |  |

Returns a reference to the stream of type `type` and with name `stream_name`.

If the stream was not registered before an ElementNotFoundException will be thrown.

Parameters

  type | Type of the stream (e.g. FILE)
  stream_name | Name of the stream (e.g. the file name for a file stream).

Exceptions

  ElementNotFoundException |

Returns
  A reference to the requested stream.

## ◆ hasStream()

bool hasStream | ( | const StreamType | type,
|  | const String & | stream_name
| ) |  |

Returns true if the stream `stream_name` with type `type` is registered.

Parameters

  type | Type of the stream (e.g. FILE)
  stream_name | Name of the stream (e.g. the file name for a file stream).

Returns
  bool indication if the stream is known.

## ◆ operator=()

virtual StreamHandler& operator= | ( | const StreamHandler & | source | ) |

private virtual

assignment operator

## ◆ registerStream()

Int registerStream | ( | StreamType const | type,
|  | const String & | stream_name
| ) |  |

Creates a stream of type `type` and with name `stream_name`.

If the stream is already registered the reference counter is increased.

Note
  The stream name must be unique. You cannot register the same stream name with two different types.

Parameters

  type | Type of the stream (e.g. FILE)
  stream_name | Name of the stream (e.g. the file name for a file stream).

Returns
  An integer indicating if the operation was completed successfully (`value` != 1 means a failure occurred).

## ◆ unregisterStream()

void unregisterStream | ( | StreamType const | type,
|  | const String & | stream_name
| ) |  |

De-registers a stream of type `type` and with name `stream_name` from the handler.

It also decreases the reference counter for the named stream. If the counter reaches 0. The stream will be closed.

Parameters

  type | Type of the stream (e.g. FILE)
  stream_name | Name of the stream (e.g. the file name for a file stream).

## Friends And Related Function Documentation

## ◆ operator<<

std::ostream& operator<< | ( | std::ostream & | os,
|  | StreamHandler const & | stream_handler
| ) |  |

friend

Overload for the insertion operator (operator<<) to have a formatted output of the StreamHandler.

## Member Data Documentation

## ◆ name_to_counter_map_

std::map<String, Size> name_to_counter_map_

protected

Maps all registered stream names to the number of times it was registered. If the counter goes to zero, the stream will be closed and removed.

## ◆ name_to_stream_map_

std::map<String, std::ostream *> name_to_stream_map_

protected

Maps all registered stream names to the corresponding std::ostream.

## ◆ name_to_type_map_

std::map<String, StreamType> name_to_type_map_

protected

Maps all registered stream names to the corresponding StreamHandler::StreamType.