bpp-core  2.1.0
 All Classes Namespaces Files Functions Variables Typedefs Friends
OutputStream.h
Go to the documentation of this file.
1 //
2 // File: BppStream.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 _BPPSTREAM_H_
41 #define _BPPSTREAM_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(unsigned int digit) = 0;
81  virtual unsigned 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:
108  unsigned int precision_;
110 
111 public:
113 
114 public:
115  OutputStream& setPrecision(unsigned int digit)
116  {
117  precision_ = digit;
118  return *this;
119  }
120  unsigned 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 //_BPPSTREAM_H_
354