1
// -*- mode: C++; tab-width: 2; -*-
4
// --------------------------------------------------------------------------
5
// OpenMS Mass Spectrometry Framework
6
// --------------------------------------------------------------------------
7
// Copyright (C) 2003-2011 -- Oliver Kohlbacher, Knut Reinert
9
// This library is free software; you can redistribute it and/or
10
// modify it under the terms of the GNU Lesser General Public
11
// License as published by the Free Software Foundation; either
12
// version 2.1 of the License, or (at your option) any later version.
14
// This library is distributed in the hope that it will be useful,
15
// but WITHOUT ANY WARRANTY; without even the implied warranty of
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
// Lesser General Public License for more details.
19
// You should have received a copy of the GNU Lesser General Public
20
// License along with this library; if not, write to the Free Software
21
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1
// --------------------------------------------------------------------------
2
// OpenMS -- Open-Source Mass Spectrometry
3
// --------------------------------------------------------------------------
4
// Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5
// ETH Zurich, and Freie Universitaet Berlin 2002-2013.
7
// This software is released under a three-clause BSD license:
8
// * Redistributions of source code must retain the above copyright
9
// notice, this list of conditions and the following disclaimer.
10
// * Redistributions in binary form must reproduce the above copyright
11
// notice, this list of conditions and the following disclaimer in the
12
// documentation and/or other materials provided with the distribution.
13
// * Neither the name of any author or any participating institution
14
// may be used to endorse or promote products derived from this software
15
// without specific prior written permission.
16
// For a full list of authors, refer to the file AUTHORS.
17
// --------------------------------------------------------------------------
18
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
// ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22
// INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
30
// --------------------------------------------------------------------------
24
31
// $Maintainer: Stephan Aiche$
65
72
class OPENMS_DLLAPI StreamHandler
69
76
* @brief Defines the type of the stream that should be handled
76
/// Default constructor
84
/// Default constructor
80
virtual ~StreamHandler();
88
virtual ~StreamHandler();
83
91
* @brief Creates a stream of type @p type and with name @p stream_name
85
93
* If the stream is already registered the reference counter is increased.
87
95
* @note The stream name must be unique. You cannot register the same stream name with two different types.
89
97
* @param type Type of the stream (e.g. FILE)
90
98
* @param stream_name Name of the stream (e.g. the file name for a file stream).
92
100
* @return An integer indicating if the operation was completed succesfully (@p value != 1 means a failure occured).
94
102
Int registerStream(StreamType const type, const String & stream_name);
97
* @brief Deregisters a stream of type @p type and with name @p stream_name from the handler.
105
* @brief Deregisters a stream of type @p type and with name @p stream_name from the handler.
99
107
* It also decreases the reference counter for the named stream. If the counter
100
108
* reaches 0. The stream will be closed.
102
110
* @param type Type of the stream (e.g. FILE)
103
111
* @param stream_name Name of the stream (e.g. the file name for a file stream).
106
void unregisterStream(StreamType const type, const String& stream_name);
114
void unregisterStream(StreamType const type, const String & stream_name);
109
117
* @brief Returns a reference to the stream of type @p type and with name @p stream_name.
111
119
* If the stream was not registered before an ElementNotFoundException will be thrown.
113
121
* @param type Type of the stream (e.g. FILE)
114
122
* @param stream_name Name of the stream (e.g. the file name for a file stream).
116
124
* @throw ElementNotFoundException
118
126
* @return A reference to the requested stream.
120
ostream& getStream(StreamType const type, const String& stream_name);
128
ostream & getStream(StreamType const type, const String & stream_name);
124
* @brief Returns true if the stream @p stream_name with type @p type is
132
* @brief Returns true if the stream @p stream_name with type @p type is
127
135
* @param type Type of the stream (e.g. FILE)
130
138
* @return bool indication if the stream is known.
132
bool hasStream(const StreamType type, const String& stream_name);
136
map< String, ostream* > name_to_stream_map_; ///< Maps all registered stream names to the corresponding std::ostream.
137
map< String, StreamType> name_to_type_map_; ///< Maps all registered stream names to the corresponding StreamHandler::StreamType
138
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.
140
bool hasStream(const StreamType type, const String & stream_name);
144
map<String, ostream *> name_to_stream_map_; ///< Maps all registered stream names to the corresponding std::ostream.
145
map<String, StreamType> name_to_type_map_; ///< Maps all registered stream names to the corresponding StreamHandler::StreamType
146
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.
141
149
* @brief Creates a stream with the given type and the given name.
146
154
* @return A pointer to the created stream.
148
ostream* createStream_(const StreamType type, const String & stream_name);
156
ostream * createStream_(const StreamType type, const String & stream_name);
151
159
// copy constructor and assignment operator are hidden to avoid
152
160
// creating multiple pointers to a single filestream instance
154
162
/// copy constructor
155
StreamHandler(const StreamHandler& source);
163
StreamHandler(const StreamHandler & source);
157
165
/// assignment operator
158
virtual StreamHandler& operator = (const StreamHandler& source);
166
virtual StreamHandler & operator=(const StreamHandler & source);
160
friend OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, StreamHandler const & stream_handler);
168
friend OPENMS_DLLAPI std::ostream & operator<<(std::ostream & os, StreamHandler const & stream_handler);
163
171
/// Overload for the \a insertion \a operator (operator<<) to have a formated output of the StreamHandler
164
OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, StreamHandler const & stream_handler);
172
OPENMS_DLLAPI std::ostream & operator<<(std::ostream & os, StreamHandler const & stream_handler);
166
174
/// Global StreamHandler instance.
167
175
OPENMS_DLLAPI extern StreamHandler STREAM_HANDLER;