bpp-core  2.2.0
OutputStream.h
Go to the documentation of this file.
1 //
2 // File: OutputStream.h
3 // Created by: Julien Dutheil
4 // Created on: Mon Jan 25 17:41 2010
5 //
6 
7 /*
8 Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
9 
10 This software is a computer program whose purpose is to provide utilitary
11 classes. This file belongs to the Bio++ Project.
12 
13 This software is governed by the CeCILL license under French law and
14 abiding by the rules of distribution of free software. You can use,
15 modify and/ or redistribute the software under the terms of the CeCILL
16 license as circulated by CEA, CNRS and INRIA at the following URL
17 "http://www.cecill.info".
18 
19 As a counterpart to the access to the source code and rights to copy,
20 modify and redistribute granted by the license, users are provided only
21 with a limited warranty and the software's author, the holder of the
22 economic rights, and the successive licensors have only limited
23 liability.
24 
25 In this respect, the user's attention is drawn to the risks associated
26 with loading, using, modifying and/or developing or reproducing the
27 software by the user in light of its specific status of free software,
28 that may mean that it is complicated to manipulate, and that also
29 therefore means that it is reserved for developers and experienced
30 professionals having in-depth computer knowledge. Users are therefore
31 encouraged to load and test the software's suitability as regards their
32 requirements in conditions enabling the security of their systems and/or
33 data to be ensured and, more generally, to use and operate it in the
34 same conditions as regards security.
35 
36 The fact that you are presently reading this means that you have had
37 knowledge of the CeCILL license and that you accept its terms.
38 */
39 
40 #ifndef _OUTPUTSTREAM_H_
41 #define _OUTPUTSTREAM_H_
42 
43 #include "../Clonable.h"
44 
45 //From the STL:
46 #include <string>
47 #include <iostream>
48 #include <fstream>
49 #include <sstream>
50 #include <memory>
51 #include <iomanip>
52 
53 namespace bpp
54 {
55 
64 class OutputStream :
65  public virtual Clonable
66 {
67 public:
68  virtual OutputStream& operator<<(const std::string& message) = 0;
69  virtual OutputStream& operator<<(const char* message) = 0;
70  virtual OutputStream& operator<<(const char& message) = 0;
71  virtual OutputStream& operator<<(const int& message) = 0;
72  virtual OutputStream& operator<<(const unsigned int& message) = 0;
73  virtual OutputStream& operator<<(const long int& message) = 0;
74  virtual OutputStream& operator<<(const unsigned long int& message) = 0;
75  virtual OutputStream& operator<<(const double& message) = 0;
76  virtual OutputStream& operator<<(const long double& message) = 0;
77  virtual OutputStream& operator<<(const bool& message) = 0;
78  virtual OutputStream& endLine() = 0;
79  virtual OutputStream& flush() = 0;
80  virtual OutputStream& setPrecision(int digit) = 0;
81  virtual int getPrecision() const = 0;
82  virtual OutputStream& enableScientificNotation(bool yn) = 0;
83  virtual bool isScientificNotationEnabled() const = 0;
84 
90 #ifndef NO_VIRTUAL_COV
91  OutputStream* clone() const = 0;
92 #endif
93 
95 };
96 
97 
98 
99 
100 
105  public virtual OutputStream
106 {
107 private:
110 
111 public:
113 
114 public:
116  {
117  precision_ = digit;
118  return *this;
119  }
120  int getPrecision() const { return precision_; }
121 
122  virtual OutputStream& enableScientificNotation(bool yn) { scienceNotation_ = yn; return *this; }
123  virtual bool isScientificNotationEnabled() const { return scienceNotation_; }
124 };
125 
126 
127 
128 
129 
134  public AbstractOutputStream
135 {
136 public:
137  NullOutputStream& operator<<(const std::string& message) { return *this; }
138  NullOutputStream& operator<<(const char* message) { return *this; }
139  NullOutputStream& operator<<(const char& message) { return *this; }
140  NullOutputStream& operator<<(const int& message) { return *this; }
141  NullOutputStream& operator<<(const unsigned int& message) { return *this; }
142  NullOutputStream& operator<<(const long int& message) { return *this; }
143  NullOutputStream& operator<<(const unsigned long int& message) { return *this; }
144  NullOutputStream& operator<<(const double& message) { return *this; }
145  NullOutputStream& operator<<(const long double& message) { return *this; }
146  NullOutputStream& operator<<(const bool& message) { return *this; }
147  NullOutputStream& endLine() { return *this; }
148  NullOutputStream& flush() { return *this; }
149 
150  NullOutputStream* clone() const { return new NullOutputStream(*this); }
151 
152 };
153 
154 
155 
156 
157 
158 
169  public AbstractOutputStream
170 {
171 private:
172  mutable std::auto_ptr<std::ostream> stream_;
173 
174 public:
175  StlOutputStream(std::ostream* stream): stream_(stream) {}
176  StlOutputStream(const StlOutputStream& stlos) : stream_(stlos.stream_) {}
178  {
179  stream_ = stlos.stream_;
180  return *this;
181  }
182 
183 public:
184  StlOutputStream& operator<<(const std::string& message) { if (stream_.get()) *stream_ << message; return *this; }
185  StlOutputStream& operator<<(const char* message) { if (stream_.get()) *stream_ << message; return *this; }
186  StlOutputStream& operator<<(const char& message) { if (stream_.get()) *stream_ << message; return *this; }
187  StlOutputStream& operator<<(const int& message) { if (stream_.get()) *stream_ << message; return *this; }
188  StlOutputStream& operator<<(const unsigned int& message) { if (stream_.get()) *stream_ << message; return *this; }
189  StlOutputStream& operator<<(const long int& message) { if (stream_.get()) *stream_ << message; return *this; }
190  StlOutputStream& operator<<(const unsigned long int& message) { if (stream_.get()) *stream_ << message; return *this; }
191  StlOutputStream& operator<<(const double& message)
192  {
193  if (stream_.get())
194  *stream_ << std::setprecision(getPrecision()) << (isScientificNotationEnabled() ? std::scientific : std::fixed) << message;
195  return *this;
196  }
197  StlOutputStream& operator<<(const long double& message)
198  {
199  if (stream_.get())
200  *stream_ << std::setprecision(getPrecision()) << (isScientificNotationEnabled() ? std::scientific : std::fixed) << message;
201  return *this;
202  }
203  StlOutputStream& operator<<(const bool& message) { if (stream_.get()) *stream_ << message; return *this; }
204  StlOutputStream& endLine() { if (stream_.get()) *stream_ << std::endl; return *this; }
205  StlOutputStream& flush() { if (stream_.get()) stream_->flush(); return *this; }
206 
207  StlOutputStream* clone() const { return new StlOutputStream(*this); }
208 
209 };
210 
211 
212 
213 
214 
222  public AbstractOutputStream
223 {
224 protected:
225  std::ostream* stream_;
226 
227 public:
228  StlOutputStreamWrapper(std::ostream* stream): stream_(stream) {}
230  StlOutputStreamWrapper& operator=(const StlOutputStreamWrapper& stlos) { stream_ = stlos.stream_; return *this; }
231 
232 public:
233  StlOutputStreamWrapper& operator<<(const std::string& message) { if (stream_) *stream_ << message; return *this; }
234  StlOutputStreamWrapper& operator<<(const char* message) { if (stream_) *stream_ << message; return *this; }
235  StlOutputStreamWrapper& operator<<(const char& message) { if (stream_) *stream_ << message; return *this; }
236  StlOutputStreamWrapper& operator<<(const int& message) { if (stream_) *stream_ << message; return *this; }
237  StlOutputStreamWrapper& operator<<(const unsigned int& message) { if (stream_) *stream_ << message; return *this; }
238 
239  StlOutputStreamWrapper& operator<<(const long int& message) { if (stream_) *stream_ << message; return *this; }
240  StlOutputStreamWrapper& operator<<(const unsigned long int& message) { if (stream_) *stream_ << message; return *this; }
241  StlOutputStreamWrapper& operator<<(const double& message)
242  {
243  if (stream_)
244  *stream_ << std::setprecision(getPrecision()) << (isScientificNotationEnabled() ? std::scientific : std::fixed) << message;
245  return *this;
246  }
247  StlOutputStreamWrapper& operator<<(const long double& message)
248  {
249  if (stream_)
250  *stream_ << std::setprecision(getPrecision()) << (isScientificNotationEnabled() ? std::scientific : std::fixed) << message;
251  return *this;
252  }
253  StlOutputStreamWrapper& operator<<(const bool& message) { if (stream_) *stream_ << message; return *this; }
254  StlOutputStreamWrapper& endLine() { if (stream_) *stream_ << std::endl; return *this; }
255  StlOutputStreamWrapper& flush() { if (stream_) stream_->flush(); return *this; }
256 
257  StlOutputStreamWrapper* clone() const { return new StlOutputStreamWrapper(*this); }
258 
259 };
260 
266 class StdOut :
267  public AbstractOutputStream
268 {
269 public:
270  OutputStream& operator<<(const std::string& message) { std::cout << message; return *this; }
271  OutputStream& operator<<(const char* message) { std::cout << message; return *this; }
272  OutputStream& operator<<(const char& message) { std::cout << message; return *this; }
273  OutputStream& operator<<(const int& message) { std::cout << message; return *this; }
274  OutputStream& operator<<(const unsigned int& message) { std::cout << message; return *this; }
275  OutputStream& operator<<(const long int& message) { std::cout << message; return *this; }
276  OutputStream& operator<<(const unsigned long int& message) { std::cout << message; return *this; }
277  OutputStream& operator<<(const double& message)
278  {
279  std::cout << std::setprecision(getPrecision()) << (isScientificNotationEnabled() ? std::scientific : std::fixed) << message;
280  return *this;
281  }
282  OutputStream& operator<<(const long double& message)
283  {
284  std::cout << std::setprecision(getPrecision()) << (isScientificNotationEnabled() ? std::scientific : std::fixed) << message;
285  return *this;
286  }
287  OutputStream& operator<<(const bool& message) { std::cout << message; return *this; }
288  OutputStream& endLine() { std::cout << std::endl; return *this; }
289  OutputStream& flush() { std::cout.flush(); return *this; }
290 
291  StdOut* clone() const { return new StdOut(*this); }
292 
293 };
294 
295 
296 
297 
298 
304 class StdErr :
305  public AbstractOutputStream
306 {
307 public:
308  OutputStream& operator<<(const std::string& message) { std::cerr << message; return *this; }
309  OutputStream& operator<<(const char* message) { std::cerr << message; return *this; }
310  OutputStream& operator<<(const char& message) { std::cerr << std::setprecision(getPrecision()) << message; return *this; }
311  OutputStream& operator<<(const int& message) { std::cerr << std::setprecision(getPrecision()) << message; return *this; }
312  OutputStream& operator<<(const unsigned int& message) { std::cerr << message; return *this; }
313  OutputStream& operator<<(const long int& message) { std::cerr << std::setprecision(getPrecision()) << message; return *this; }
314  OutputStream& operator<<(const unsigned long int& message) { std::cerr << message; return *this; }
315  OutputStream& operator<<(const double& message)
316  {
317  std::cerr << std::setprecision(getPrecision()) << (isScientificNotationEnabled() ? std::scientific : std::fixed) << message;
318  return *this;
319  }
320  OutputStream& operator<<(const long double& message)
321  {
322  std::cerr << std::setprecision(getPrecision()) << (isScientificNotationEnabled() ? std::scientific : std::fixed) << message;
323  return *this;
324  }
325  OutputStream& operator<<(const bool& message) { std::cerr << message; return *this; }
326  OutputStream& endLine() { std::cerr << std::endl; return *this; }
327  OutputStream& flush() { std::cerr.flush(); return *this; }
328 
329 #ifndef NO_VIRTUAL_COV
330  StdErr* clone() const { return new StdErr(*this); }
331 #endif
332 
333 };
334 
340 class StdStr :
342 {
343 public:
344  StdStr(): StlOutputStreamWrapper(new std::ostringstream()){}
345 
346  std::string str() const { return dynamic_cast<const std::ostringstream*>(stream_)->str();}
347 
348  ~StdStr() { delete stream_;}
349 };
350 
351 } // end of namespace bpp;
352 
353 #endif //_OUTPUTSTREAM_H_
354 
OutputStream & operator<<(const bool &message)
Definition: OutputStream.h:325
virtual bool isScientificNotationEnabled() const
Definition: OutputStream.h:123
StlOutputStream * clone() const
Create a copy of this object and send a pointer to it.
Definition: OutputStream.h:207
StlOutputStream & flush()
Definition: OutputStream.h:205
NullOutputStream & operator<<(const char *message)
Definition: OutputStream.h:138
OutputStream * clone() const =0
Create a copy of this object and send a pointer to it.
virtual OutputStream & enableScientificNotation(bool yn)
Definition: OutputStream.h:122
StlOutputStream(std::ostream *stream)
Definition: OutputStream.h:175
StlOutputStreamWrapper & operator<<(const std::string &message)
Definition: OutputStream.h:233
virtual OutputStream & operator<<(const std::string &message)=0
virtual int getPrecision() const =0
Helper implementation of the OutputStream interface.
Definition: OutputStream.h:104
OutputStream & operator<<(const unsigned int &message)
Definition: OutputStream.h:312
virtual OutputStream & flush()=0
NullOutputStream & operator<<(const unsigned int &message)
Definition: OutputStream.h:141
NullOutputStream * clone() const
Create a copy of this object and send a pointer to it.
Definition: OutputStream.h:150
StlOutputStream & operator<<(const unsigned int &message)
Definition: OutputStream.h:188
This class allows to perform a correspondence analysis.
StlOutputStream & operator<<(const char &message)
Definition: OutputStream.h:186
STL output stream.
Definition: OutputStream.h:168
std::string str() const
Definition: OutputStream.h:346
OutputStream & operator<<(const std::string &message)
Definition: OutputStream.h:308
OutputStream & operator<<(const long int &message)
Definition: OutputStream.h:313
STL namespace.
OutputStream & operator<<(const unsigned long int &message)
Definition: OutputStream.h:314
NullOutputStream & endLine()
Definition: OutputStream.h:147
NullOutputStream & operator<<(const unsigned long int &message)
Definition: OutputStream.h:143
OutputStream & operator<<(const char *message)
Definition: OutputStream.h:271
OutputStream & endLine()
Definition: OutputStream.h:326
std::auto_ptr< std::ostream > stream_
Definition: OutputStream.h:172
virtual OutputStream & enableScientificNotation(bool yn)=0
Standard output stream.
Definition: OutputStream.h:266
StlOutputStreamWrapper & operator<<(const int &message)
Definition: OutputStream.h:236
StlOutputStream & operator<<(const double &message)
Definition: OutputStream.h:191
StlOutputStream & operator<<(const int &message)
Definition: OutputStream.h:187
OutputStream & operator<<(const unsigned long int &message)
Definition: OutputStream.h:276
OutputStream & operator<<(const bool &message)
Definition: OutputStream.h:287
StlOutputStreamWrapper(std::ostream *stream)
Definition: OutputStream.h:228
NullOutputStream & operator<<(const int &message)
Definition: OutputStream.h:140
virtual OutputStream & setPrecision(int digit)=0
OutputStream & operator<<(const long double &message)
Definition: OutputStream.h:282
virtual bool isScientificNotationEnabled() const =0
OutputStream & operator<<(const char &message)
Definition: OutputStream.h:310
String output stream.
Definition: OutputStream.h:340
StlOutputStreamWrapper & operator<<(const bool &message)
Definition: OutputStream.h:253
StlOutputStreamWrapper & operator=(const StlOutputStreamWrapper &stlos)
Definition: OutputStream.h:230
StlOutputStream & operator=(const StlOutputStream &stlos)
Definition: OutputStream.h:177
StlOutputStream & operator<<(const bool &message)
Definition: OutputStream.h:203
StlOutputStreamWrapper & operator<<(const unsigned long int &message)
Definition: OutputStream.h:240
OutputStream & setPrecision(int digit)
Definition: OutputStream.h:115
NullOutputStream & operator<<(const std::string &message)
Definition: OutputStream.h:137
StlOutputStreamWrapper & operator<<(const char *message)
Definition: OutputStream.h:234
NullOutputStream & operator<<(const long double &message)
Definition: OutputStream.h:145
Standard error stream.
Definition: OutputStream.h:304
StlOutputStreamWrapper & operator<<(const long int &message)
Definition: OutputStream.h:239
OutputStream & operator<<(const double &message)
Definition: OutputStream.h:277
StdErr * clone() const
Create a copy of this object and send a pointer to it.
Definition: OutputStream.h:330
StlOutputStream & operator<<(const unsigned long int &message)
Definition: OutputStream.h:190
OutputStream & operator<<(const std::string &message)
Definition: OutputStream.h:270
virtual OutputStream & endLine()=0
OutputStream interface.
Definition: OutputStream.h:64
StlOutputStreamWrapper & operator<<(const char &message)
Definition: OutputStream.h:235
OutputStream & flush()
Definition: OutputStream.h:289
StlOutputStreamWrapper & flush()
Definition: OutputStream.h:255
OutputStream & operator<<(const int &message)
Definition: OutputStream.h:273
STL wrapper for output stream.
Definition: OutputStream.h:221
The Clonable interface (allow an object to be cloned).
Definition: Clonable.h:99
StlOutputStream & endLine()
Definition: OutputStream.h:204
OutputStream & operator<<(const long double &message)
Definition: OutputStream.h:320
StlOutputStream & operator<<(const long double &message)
Definition: OutputStream.h:197
Null output stream (swich off output).
Definition: OutputStream.h:133
NullOutputStream & operator<<(const double &message)
Definition: OutputStream.h:144
NullOutputStream & flush()
Definition: OutputStream.h:148
StlOutputStreamWrapper & operator<<(const long double &message)
Definition: OutputStream.h:247
StlOutputStreamWrapper * clone() const
Create a copy of this object and send a pointer to it.
Definition: OutputStream.h:257
OutputStream & operator<<(const long int &message)
Definition: OutputStream.h:275
OutputStream & operator<<(const unsigned int &message)
Definition: OutputStream.h:274
StlOutputStream & operator<<(const char *message)
Definition: OutputStream.h:185
StlOutputStreamWrapper & operator<<(const unsigned int &message)
Definition: OutputStream.h:237
NullOutputStream & operator<<(const char &message)
Definition: OutputStream.h:139
OutputStream & operator<<(const char *message)
Definition: OutputStream.h:309
StlOutputStream & operator<<(const long int &message)
Definition: OutputStream.h:189
StlOutputStreamWrapper & endLine()
Definition: OutputStream.h:254
NullOutputStream & operator<<(const long int &message)
Definition: OutputStream.h:142
NullOutputStream & operator<<(const bool &message)
Definition: OutputStream.h:146
StlOutputStream(const StlOutputStream &stlos)
Definition: OutputStream.h:176
OutputStream & operator<<(const int &message)
Definition: OutputStream.h:311
OutputStream & operator<<(const double &message)
Definition: OutputStream.h:315
StlOutputStream & operator<<(const std::string &message)
Definition: OutputStream.h:184
OutputStream & operator<<(const char &message)
Definition: OutputStream.h:272
StlOutputStreamWrapper & operator<<(const double &message)
Definition: OutputStream.h:241
StdOut * clone() const
Create a copy of this object and send a pointer to it.
Definition: OutputStream.h:291
OutputStream & endLine()
Definition: OutputStream.h:288
OutputStream & flush()
Definition: OutputStream.h:327
StlOutputStreamWrapper(const StlOutputStreamWrapper &stlos)
Definition: OutputStream.h:229