bpp-core3  3.0.0
OutputStream.h
Go to the documentation of this file.
1 //
2 // File: OutputStream.h
3 // Authors:
4 // Julien Dutheil
5 // Created: 2010-01-25 17:41:00
6 //
7 
8 /*
9  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
10 
11  This software is a computer program whose purpose is to provide utilitary
12  classes. This file belongs to the Bio++ Project.
13 
14  This software is governed by the CeCILL license under French law and
15  abiding by the rules of distribution of free software. You can use,
16  modify and/ or redistribute the software under the terms of the CeCILL
17  license as circulated by CEA, CNRS and INRIA at the following URL
18  "http://www.cecill.info".
19 
20  As a counterpart to the access to the source code and rights to copy,
21  modify and redistribute granted by the license, users are provided only
22  with a limited warranty and the software's author, the holder of the
23  economic rights, and the successive licensors have only limited
24  liability.
25 
26  In this respect, the user's attention is drawn to the risks associated
27  with loading, using, modifying and/or developing or reproducing the
28  software by the user in light of its specific status of free software,
29  that may mean that it is complicated to manipulate, and that also
30  therefore means that it is reserved for developers and experienced
31  professionals having in-depth computer knowledge. Users are therefore
32  encouraged to load and test the software's suitability as regards their
33  requirements in conditions enabling the security of their systems and/or
34  data to be ensured and, more generally, to use and operate it in the
35  same conditions as regards security.
36 
37  The fact that you are presently reading this means that you have had
38  knowledge of the CeCILL license and that you accept its terms.
39 */
40 
41 #ifndef BPP_IO_OUTPUTSTREAM_H
42 #define BPP_IO_OUTPUTSTREAM_H
43 
44 
45 #include "../Clonable.h"
46 
47 // From the STL:
48 #include <string>
49 #include <iostream>
50 #include <fstream>
51 #include <sstream>
52 #include <memory>
53 #include <iomanip>
54 
55 namespace bpp
56 {
65 class OutputStream :
66  public virtual Clonable
67 {
68 public:
69  virtual OutputStream& operator<<(const std::string& message) = 0;
70  virtual OutputStream& operator<<(const char* message) = 0;
71  virtual OutputStream& operator<<(const char& message) = 0;
72  virtual OutputStream& operator<<(const int& message) = 0;
73  virtual OutputStream& operator<<(const unsigned int& message) = 0;
74  virtual OutputStream& operator<<(const long int& message) = 0;
75  virtual OutputStream& operator<<(const unsigned long int& message) = 0;
76  virtual OutputStream& operator<<(const double& message) = 0;
77  virtual OutputStream& operator<<(const long double& message) = 0;
78  virtual OutputStream& operator<<(const bool& message) = 0;
79  virtual OutputStream& endLine() = 0;
80  virtual OutputStream& flush() = 0;
81  virtual OutputStream& setPrecision(int digit) = 0;
82  virtual int getPrecision() const = 0;
83  virtual OutputStream& enableScientificNotation(bool yn) = 0;
84  virtual bool isScientificNotationEnabled() const = 0;
85 
91  OutputStream* clone() const = 0;
93 };
94 
95 
100  public virtual OutputStream
101 {
102 private:
105 
106 public:
108 
109 public:
111  {
112  precision_ = digit;
113  return *this;
114  }
115  int getPrecision() const { return precision_; }
116 
117  virtual OutputStream& enableScientificNotation(bool yn) { scienceNotation_ = yn; return *this; }
118  virtual bool isScientificNotationEnabled() const { return scienceNotation_; }
119 };
120 
121 
126  public AbstractOutputStream
127 {
128 public:
129  NullOutputStream& operator<<(const std::string& message) { return *this; }
130  NullOutputStream& operator<<(const char* message) { return *this; }
131  NullOutputStream& operator<<(const char& message) { return *this; }
132  NullOutputStream& operator<<(const int& message) { return *this; }
133  NullOutputStream& operator<<(const unsigned int& message) { return *this; }
134  NullOutputStream& operator<<(const long int& message) { return *this; }
135  NullOutputStream& operator<<(const unsigned long int& message) { return *this; }
136  NullOutputStream& operator<<(const double& message) { return *this; }
137  NullOutputStream& operator<<(const long double& message) { return *this; }
138  NullOutputStream& operator<<(const bool& message) { return *this; }
139  NullOutputStream& endLine() { return *this; }
140  NullOutputStream& flush() { return *this; }
141 
142  NullOutputStream* clone() const { return new NullOutputStream(*this); }
143 };
144 
145 
156  public AbstractOutputStream
157 {
158 private:
159  mutable std::unique_ptr<std::ostream> stream_;
160 
161 public:
162  StlOutputStream(std::ostream* stream) : stream_(stream) {}
163  StlOutputStream(const StlOutputStream& stlos) : stream_() { stream_ = std::move(stlos.stream_); }
165  {
166  stream_ = std::move(stlos.stream_);
167  return *this;
168  }
169 
170 public:
171  StlOutputStream& operator<<(const std::string& message) { if (stream_.get()) *stream_ << message; return *this; }
172  StlOutputStream& operator<<(const char* message) { if (stream_.get()) *stream_ << message; return *this; }
173  StlOutputStream& operator<<(const char& message) { if (stream_.get()) *stream_ << message; return *this; }
174  StlOutputStream& operator<<(const int& message) { if (stream_.get()) *stream_ << message; return *this; }
175  StlOutputStream& operator<<(const unsigned int& message) { if (stream_.get()) *stream_ << message; return *this; }
176  StlOutputStream& operator<<(const long int& message) { if (stream_.get()) *stream_ << message; return *this; }
177  StlOutputStream& operator<<(const unsigned long int& message) { if (stream_.get()) *stream_ << message; return *this; }
178  StlOutputStream& operator<<(const double& message)
179  {
180  if (stream_.get())
181  *stream_ << std::setprecision(getPrecision()) << (isScientificNotationEnabled() ? std::scientific : std::fixed) << message;
182  return *this;
183  }
184  StlOutputStream& operator<<(const long double& message)
185  {
186  if (stream_.get())
187  *stream_ << std::setprecision(getPrecision()) << (isScientificNotationEnabled() ? std::scientific : std::fixed) << message;
188  return *this;
189  }
190  StlOutputStream& operator<<(const bool& message) { if (stream_.get()) *stream_ << message; return *this; }
191  StlOutputStream& endLine() { if (stream_.get()) *stream_ << std::endl; return *this; }
192  StlOutputStream& flush() { if (stream_.get()) stream_->flush(); return *this; }
193 
194  StlOutputStream* clone() const { return new StlOutputStream(*this); }
195 };
196 
197 
205  public AbstractOutputStream
206 {
207 protected:
208  std::ostream* stream_;
209 
210 public:
211  StlOutputStreamWrapper(std::ostream* stream) : stream_(stream) {}
213  StlOutputStreamWrapper& operator=(const StlOutputStreamWrapper& stlos) { stream_ = stlos.stream_; return *this; }
214 
215 public:
216  StlOutputStreamWrapper& operator<<(const std::string& message) { if (stream_) *stream_ << message; return *this; }
217  StlOutputStreamWrapper& operator<<(const char* message) { if (stream_) *stream_ << message; return *this; }
218  StlOutputStreamWrapper& operator<<(const char& message) { if (stream_) *stream_ << message; return *this; }
219  StlOutputStreamWrapper& operator<<(const int& message) { if (stream_) *stream_ << message; return *this; }
220  StlOutputStreamWrapper& operator<<(const unsigned int& message) { if (stream_) *stream_ << message; return *this; }
221 
222  StlOutputStreamWrapper& operator<<(const long int& message) { if (stream_) *stream_ << message; return *this; }
223  StlOutputStreamWrapper& operator<<(const unsigned long int& message) { if (stream_) *stream_ << message; return *this; }
224  StlOutputStreamWrapper& operator<<(const double& message)
225  {
226  if (stream_)
227  *stream_ << std::setprecision(getPrecision()) << (isScientificNotationEnabled() ? std::scientific : std::fixed) << message;
228  return *this;
229  }
230  StlOutputStreamWrapper& operator<<(const long double& message)
231  {
232  if (stream_)
233  *stream_ << std::setprecision(getPrecision()) << (isScientificNotationEnabled() ? std::scientific : std::fixed) << message;
234  return *this;
235  }
236  StlOutputStreamWrapper& operator<<(const bool& message) { if (stream_) *stream_ << message; return *this; }
237  StlOutputStreamWrapper& endLine() { if (stream_) *stream_ << std::endl; return *this; }
238  StlOutputStreamWrapper& flush() { if (stream_) stream_->flush(); return *this; }
239 
240  StlOutputStreamWrapper* clone() const { return new StlOutputStreamWrapper(*this); }
241 };
242 
248 class StdOut :
249  public AbstractOutputStream
250 {
251 public:
252  OutputStream& operator<<(const std::string& message) { std::cout << message; return *this; }
253  OutputStream& operator<<(const char* message) { std::cout << message; return *this; }
254  OutputStream& operator<<(const char& message) { std::cout << message; return *this; }
255  OutputStream& operator<<(const int& message) { std::cout << message; return *this; }
256  OutputStream& operator<<(const unsigned int& message) { std::cout << message; return *this; }
257  OutputStream& operator<<(const long int& message) { std::cout << message; return *this; }
258  OutputStream& operator<<(const unsigned long int& message) { std::cout << message; return *this; }
259  OutputStream& operator<<(const double& message)
260  {
261  std::cout << std::setprecision(getPrecision()) << (isScientificNotationEnabled() ? std::scientific : std::fixed) << message;
262  return *this;
263  }
264  OutputStream& operator<<(const long double& message)
265  {
266  std::cout << std::setprecision(getPrecision()) << (isScientificNotationEnabled() ? std::scientific : std::fixed) << message;
267  return *this;
268  }
269  OutputStream& operator<<(const bool& message) { std::cout << message; return *this; }
270  OutputStream& endLine() { std::cout << std::endl; return *this; }
271  OutputStream& flush() { std::cout.flush(); return *this; }
272 
273  StdOut* clone() const { return new StdOut(*this); }
274 };
275 
276 
282 class StdErr :
283  public AbstractOutputStream
284 {
285 public:
286  OutputStream& operator<<(const std::string& message) { std::cerr << message; return *this; }
287  OutputStream& operator<<(const char* message) { std::cerr << message; return *this; }
288  OutputStream& operator<<(const char& message) { std::cerr << std::setprecision(getPrecision()) << message; return *this; }
289  OutputStream& operator<<(const int& message) { std::cerr << std::setprecision(getPrecision()) << message; return *this; }
290  OutputStream& operator<<(const unsigned int& message) { std::cerr << message; return *this; }
291  OutputStream& operator<<(const long int& message) { std::cerr << std::setprecision(getPrecision()) << message; return *this; }
292  OutputStream& operator<<(const unsigned long int& message) { std::cerr << message; return *this; }
293  OutputStream& operator<<(const double& message)
294  {
295  std::cerr << std::setprecision(getPrecision()) << (isScientificNotationEnabled() ? std::scientific : std::fixed) << message;
296  return *this;
297  }
298  OutputStream& operator<<(const long double& message)
299  {
300  std::cerr << std::setprecision(getPrecision()) << (isScientificNotationEnabled() ? std::scientific : std::fixed) << message;
301  return *this;
302  }
303  OutputStream& operator<<(const bool& message) { std::cerr << message; return *this; }
304  OutputStream& endLine() { std::cerr << std::endl; return *this; }
305  OutputStream& flush() { std::cerr.flush(); return *this; }
306 
307  StdErr* clone() const { return new StdErr(*this); }
308 };
309 
315 class StdStr :
317 {
318 public:
319  StdStr() : StlOutputStreamWrapper(new std::ostringstream()){}
320 
321  std::string str() const { return dynamic_cast<const std::ostringstream*>(stream_)->str();}
322 
323  ~StdStr() { delete stream_;}
324 };
325 } // end of namespace bpp;
326 #endif // BPP_IO_OUTPUTSTREAM_H
Helper implementation of the OutputStream interface.
Definition: OutputStream.h:101
virtual OutputStream & enableScientificNotation(bool yn)
Definition: OutputStream.h:117
OutputStream & setPrecision(int digit)
Definition: OutputStream.h:110
virtual bool isScientificNotationEnabled() const
Definition: OutputStream.h:118
The Clonable interface (allow an object to be cloned).
Definition: Clonable.h:103
Null output stream (swich off output).
Definition: OutputStream.h:127
NullOutputStream & operator<<(const int &message)
Definition: OutputStream.h:132
NullOutputStream & operator<<(const std::string &message)
Definition: OutputStream.h:129
NullOutputStream & endLine()
Definition: OutputStream.h:139
NullOutputStream & operator<<(const unsigned int &message)
Definition: OutputStream.h:133
NullOutputStream & flush()
Definition: OutputStream.h:140
NullOutputStream & operator<<(const long double &message)
Definition: OutputStream.h:137
NullOutputStream & operator<<(const char &message)
Definition: OutputStream.h:131
NullOutputStream & operator<<(const char *message)
Definition: OutputStream.h:130
NullOutputStream * clone() const
Create a copy of this object and send a pointer to it.
Definition: OutputStream.h:142
NullOutputStream & operator<<(const unsigned long int &message)
Definition: OutputStream.h:135
NullOutputStream & operator<<(const bool &message)
Definition: OutputStream.h:138
NullOutputStream & operator<<(const double &message)
Definition: OutputStream.h:136
NullOutputStream & operator<<(const long int &message)
Definition: OutputStream.h:134
OutputStream interface.
Definition: OutputStream.h:67
virtual bool isScientificNotationEnabled() const =0
virtual OutputStream & endLine()=0
virtual OutputStream & enableScientificNotation(bool yn)=0
virtual OutputStream & operator<<(const char *message)=0
virtual OutputStream & operator<<(const bool &message)=0
OutputStream * clone() const =0
Create a copy of this object and send a pointer to it.
virtual OutputStream & operator<<(const long int &message)=0
virtual OutputStream & operator<<(const char &message)=0
virtual OutputStream & operator<<(const long double &message)=0
virtual OutputStream & operator<<(const std::string &message)=0
virtual OutputStream & operator<<(const int &message)=0
virtual OutputStream & operator<<(const double &message)=0
virtual OutputStream & operator<<(const unsigned long int &message)=0
virtual OutputStream & flush()=0
virtual int getPrecision() const =0
virtual OutputStream & setPrecision(int digit)=0
virtual OutputStream & operator<<(const unsigned int &message)=0
Standard error stream.
Definition: OutputStream.h:284
OutputStream & operator<<(const char &message)
Definition: OutputStream.h:288
OutputStream & flush()
Definition: OutputStream.h:305
OutputStream & operator<<(const bool &message)
Definition: OutputStream.h:303
OutputStream & endLine()
Definition: OutputStream.h:304
OutputStream & operator<<(const long int &message)
Definition: OutputStream.h:291
OutputStream & operator<<(const long double &message)
Definition: OutputStream.h:298
OutputStream & operator<<(const int &message)
Definition: OutputStream.h:289
OutputStream & operator<<(const double &message)
Definition: OutputStream.h:293
OutputStream & operator<<(const char *message)
Definition: OutputStream.h:287
OutputStream & operator<<(const unsigned int &message)
Definition: OutputStream.h:290
OutputStream & operator<<(const std::string &message)
Definition: OutputStream.h:286
OutputStream & operator<<(const unsigned long int &message)
Definition: OutputStream.h:292
StdErr * clone() const
Create a copy of this object and send a pointer to it.
Definition: OutputStream.h:307
Standard output stream.
Definition: OutputStream.h:250
OutputStream & operator<<(const long int &message)
Definition: OutputStream.h:257
OutputStream & operator<<(const char *message)
Definition: OutputStream.h:253
OutputStream & operator<<(const std::string &message)
Definition: OutputStream.h:252
OutputStream & operator<<(const double &message)
Definition: OutputStream.h:259
OutputStream & operator<<(const long double &message)
Definition: OutputStream.h:264
StdOut * clone() const
Create a copy of this object and send a pointer to it.
Definition: OutputStream.h:273
OutputStream & operator<<(const char &message)
Definition: OutputStream.h:254
OutputStream & operator<<(const unsigned long int &message)
Definition: OutputStream.h:258
OutputStream & flush()
Definition: OutputStream.h:271
OutputStream & endLine()
Definition: OutputStream.h:270
OutputStream & operator<<(const bool &message)
Definition: OutputStream.h:269
OutputStream & operator<<(const int &message)
Definition: OutputStream.h:255
OutputStream & operator<<(const unsigned int &message)
Definition: OutputStream.h:256
String output stream.
Definition: OutputStream.h:317
std::string str() const
Definition: OutputStream.h:321
STL wrapper for output stream.
Definition: OutputStream.h:206
StlOutputStreamWrapper & operator<<(const double &message)
Definition: OutputStream.h:224
StlOutputStreamWrapper * clone() const
Create a copy of this object and send a pointer to it.
Definition: OutputStream.h:240
StlOutputStreamWrapper & operator<<(const unsigned int &message)
Definition: OutputStream.h:220
StlOutputStreamWrapper(std::ostream *stream)
Definition: OutputStream.h:211
StlOutputStreamWrapper & operator=(const StlOutputStreamWrapper &stlos)
Definition: OutputStream.h:213
StlOutputStreamWrapper & operator<<(const char &message)
Definition: OutputStream.h:218
StlOutputStreamWrapper & flush()
Definition: OutputStream.h:238
StlOutputStreamWrapper(const StlOutputStreamWrapper &stlos)
Definition: OutputStream.h:212
StlOutputStreamWrapper & operator<<(const bool &message)
Definition: OutputStream.h:236
StlOutputStreamWrapper & operator<<(const int &message)
Definition: OutputStream.h:219
StlOutputStreamWrapper & operator<<(const unsigned long int &message)
Definition: OutputStream.h:223
StlOutputStreamWrapper & operator<<(const long int &message)
Definition: OutputStream.h:222
StlOutputStreamWrapper & operator<<(const std::string &message)
Definition: OutputStream.h:216
StlOutputStreamWrapper & operator<<(const char *message)
Definition: OutputStream.h:217
StlOutputStreamWrapper & operator<<(const long double &message)
Definition: OutputStream.h:230
StlOutputStreamWrapper & endLine()
Definition: OutputStream.h:237
STL output stream.
Definition: OutputStream.h:157
StlOutputStream & operator<<(const double &message)
Definition: OutputStream.h:178
StlOutputStream(std::ostream *stream)
Definition: OutputStream.h:162
StlOutputStream & operator<<(const long double &message)
Definition: OutputStream.h:184
StlOutputStream * clone() const
Create a copy of this object and send a pointer to it.
Definition: OutputStream.h:194
StlOutputStream & operator=(const StlOutputStream &stlos)
Definition: OutputStream.h:164
std::unique_ptr< std::ostream > stream_
Definition: OutputStream.h:159
StlOutputStream & operator<<(const char *message)
Definition: OutputStream.h:172
StlOutputStream & operator<<(const bool &message)
Definition: OutputStream.h:190
StlOutputStream & flush()
Definition: OutputStream.h:192
StlOutputStream & operator<<(const char &message)
Definition: OutputStream.h:173
StlOutputStream & endLine()
Definition: OutputStream.h:191
StlOutputStream & operator<<(const int &message)
Definition: OutputStream.h:174
StlOutputStream(const StlOutputStream &stlos)
Definition: OutputStream.h:163
StlOutputStream & operator<<(const long int &message)
Definition: OutputStream.h:176
StlOutputStream & operator<<(const std::string &message)
Definition: OutputStream.h:171
StlOutputStream & operator<<(const unsigned int &message)
Definition: OutputStream.h:175
StlOutputStream & operator<<(const unsigned long int &message)
Definition: OutputStream.h:177