~ubuntu-branches/ubuntu/wily/openms/wily

« back to all changes in this revision

Viewing changes to include/OpenMS/CONCEPT/StreamHandler.h

  • Committer: Package Import Robot
  • Author(s): Filippo Rusconi
  • Date: 2013-12-20 11:30:16 UTC
  • mfrom: (5.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20131220113016-wre5g9bteeheq6he
Tags: 1.11.1-3
* remove version number from libbost development package names;
* ensure that AUTHORS is correctly shipped in all packages.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// -*- mode: C++; tab-width: 2; -*-
2
 
// vi: set ts=2:
3
 
//
4
 
// --------------------------------------------------------------------------
5
 
//                   OpenMS Mass Spectrometry Framework 
6
 
// --------------------------------------------------------------------------
7
 
//  Copyright (C) 2003-2011 -- Oliver Kohlbacher, Knut Reinert
8
 
//
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.
13
 
//
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.
18
 
//
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.
 
6
//
 
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.
22
29
//
23
30
// --------------------------------------------------------------------------
24
31
// $Maintainer: Stephan Aiche$
35
42
using std::ostream;
36
43
using std::map;
37
44
 
38
 
namespace OpenMS 
 
45
namespace OpenMS
39
46
{
40
47
  /**
41
48
   * @brief Provides a central class to register globally used output streams. Currently supported streams are
64
71
   */
65
72
  class OPENMS_DLLAPI StreamHandler
66
73
  {
67
 
  public:
 
74
public:
68
75
    /**
69
76
     * @brief Defines the type of the stream that should be handled
70
77
     */
71
 
    enum StreamType {
 
78
    enum StreamType
 
79
    {
72
80
      FILE,
73
81
      STRING
74
82
    };
75
 
    
76
 
                /// Default constructor
 
83
 
 
84
    /// Default constructor
77
85
    StreamHandler();
78
86
 
79
 
                /// destructor
80
 
                virtual ~StreamHandler();
 
87
    /// destructor
 
88
    virtual ~StreamHandler();
81
89
 
82
90
    /**
83
91
     * @brief Creates a stream of type @p type and with name @p stream_name
84
 
     *      
 
92
     *
85
93
     * If the stream is already registered the reference counter is increased.
86
 
     * 
 
94
     *
87
95
     * @note The stream name must be unique. You cannot register the same stream name with two different types.
88
96
     *
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).
91
 
     * 
 
99
     *
92
100
     * @return An integer indicating if the operation was completed succesfully (@p value != 1 means a failure occured).
93
101
     */
94
102
    Int registerStream(StreamType const type, const String & stream_name);
95
 
    
 
103
 
96
104
    /**
97
 
     * @brief Deregisters a stream of type @p type and with name @p stream_name from the handler. 
98
 
     * 
 
105
     * @brief Deregisters a stream of type @p type and with name @p stream_name from the handler.
 
106
     *
99
107
     * It also decreases the reference counter for the named stream. If the counter
100
108
     * reaches 0. The stream will be closed.
101
 
     * 
 
109
     *
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).
104
 
     * 
 
112
     *
105
113
     */
106
 
    void unregisterStream(StreamType const type, const String& stream_name);
107
 
    
 
114
    void unregisterStream(StreamType const type, const String & stream_name);
 
115
 
108
116
    /**
109
117
     * @brief Returns a reference to the stream of type @p type and with name @p stream_name.
110
 
     * 
 
118
     *
111
119
     * If the stream was not registered before an ElementNotFoundException will be thrown.
112
 
     * 
 
120
     *
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).
115
 
     * 
 
123
     *
116
124
     * @throw ElementNotFoundException
117
125
     *
118
126
     * @return A reference to the requested stream.
119
127
     */
120
 
    ostream& getStream(StreamType const type, const String& stream_name);
121
 
    
122
 
    
 
128
    ostream & getStream(StreamType const type, const String & stream_name);
 
129
 
 
130
 
123
131
    /**
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
125
133
     * registered.
126
134
     *
127
135
     * @param type  Type of the stream (e.g. FILE)
129
137
     *
130
138
     * @return bool indication if the stream is known.
131
139
     */
132
 
    bool hasStream(const StreamType type, const String& stream_name);
133
 
    
134
 
  protected:
135
 
    
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);
 
141
 
 
142
protected:
 
143
 
 
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.
139
147
 
140
148
    /**
141
149
     * @brief Creates a stream with the given type and the given name.
145
153
     *
146
154
     * @return A pointer to the created stream.
147
155
     */
148
 
    ostream* createStream_(const StreamType type, const String & stream_name);
 
156
    ostream * createStream_(const StreamType type, const String & stream_name);
149
157
 
150
 
  private:
 
158
private:
151
159
    // copy constructor and assignment operator are hidden to avoid
152
160
    // creating multiple pointers to a single filestream instance
153
161
 
154
162
    /// copy constructor
155
 
    StreamHandler(const StreamHandler& source);
 
163
    StreamHandler(const StreamHandler & source);
156
164
 
157
165
    /// assignment operator
158
 
    virtual StreamHandler& operator = (const StreamHandler& source);
 
166
    virtual StreamHandler & operator=(const StreamHandler & source);
159
167
 
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);
161
169
  };
162
 
  
 
170
 
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);
165
173
 
166
174
  /// Global StreamHandler instance.
167
175
  OPENMS_DLLAPI extern StreamHandler STREAM_HANDLER;