bpp-core3  3.0.0
OutputStream.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_IO_OUTPUTSTREAM_H
6 #define BPP_IO_OUTPUTSTREAM_H
7 
8 
9 #include "../Clonable.h"
10 
11 // From the STL:
12 #include <string>
13 #include <iostream>
14 #include <fstream>
15 #include <sstream>
16 #include <memory>
17 #include <iomanip>
18 
19 namespace bpp
20 {
29 class OutputStream :
30  public virtual Clonable
31 {
32 public:
33  virtual OutputStream& operator<<(const std::string& message) = 0;
34  virtual OutputStream& operator<<(const char* message) = 0;
35  virtual OutputStream& operator<<(const char& message) = 0;
36  virtual OutputStream& operator<<(const int& message) = 0;
37  virtual OutputStream& operator<<(const unsigned int& message) = 0;
38  virtual OutputStream& operator<<(const long int& message) = 0;
39  virtual OutputStream& operator<<(const unsigned long int& message) = 0;
40  virtual OutputStream& operator<<(const double& message) = 0;
41  virtual OutputStream& operator<<(const long double& message) = 0;
42  virtual OutputStream& operator<<(const bool& message) = 0;
43  virtual OutputStream& endLine() = 0;
44  virtual OutputStream& flush() = 0;
45  virtual OutputStream& setPrecision(int digit) = 0;
46  virtual int getPrecision() const = 0;
47  virtual OutputStream& enableScientificNotation(bool yn) = 0;
48  virtual bool isScientificNotationEnabled() const = 0;
49 
55  OutputStream* clone() const = 0;
57 };
58 
59 
64  public virtual OutputStream
65 {
66 private:
69 
70 public:
71  AbstractOutputStream() : precision_(6), scienceNotation_(false) {}
72 
73 public:
75  {
76  precision_ = digit;
77  return *this;
78  }
79  int getPrecision() const { return precision_; }
80 
81  virtual OutputStream& enableScientificNotation(bool yn) { scienceNotation_ = yn; return *this; }
82  virtual bool isScientificNotationEnabled() const { return scienceNotation_; }
83 };
84 
85 
91 {
92 public:
93  NullOutputStream& operator<<(const std::string& message) { return *this; }
94  NullOutputStream& operator<<(const char* message) { return *this; }
95  NullOutputStream& operator<<(const char& message) { return *this; }
96  NullOutputStream& operator<<(const int& message) { return *this; }
97  NullOutputStream& operator<<(const unsigned int& message) { return *this; }
98  NullOutputStream& operator<<(const long int& message) { return *this; }
99  NullOutputStream& operator<<(const unsigned long int& message) { return *this; }
100  NullOutputStream& operator<<(const double& message) { return *this; }
101  NullOutputStream& operator<<(const long double& message) { return *this; }
102  NullOutputStream& operator<<(const bool& message) { return *this; }
103  NullOutputStream& endLine() { return *this; }
104  NullOutputStream& flush() { return *this; }
105 
106  NullOutputStream* clone() const { return new NullOutputStream(*this); }
107 };
108 
109 
120  public AbstractOutputStream
121 {
122 private:
123  mutable std::unique_ptr<std::ostream> stream_;
124 
125 public:
126  StlOutputStream(std::unique_ptr<std::ostream> stream) : stream_(std::move(stream)) {}
127  StlOutputStream(const StlOutputStream& stlos) : stream_() { stream_ = std::move(stlos.stream_); }
129  {
130  stream_ = std::move(stlos.stream_);
131  return *this;
132  }
133 
134 public:
135  StlOutputStream& operator<<(const std::string& message) { if (stream_.get()) *stream_ << message; return *this; }
136  StlOutputStream& operator<<(const char* message) { if (stream_.get()) *stream_ << message; return *this; }
137  StlOutputStream& operator<<(const char& message) { if (stream_.get()) *stream_ << message; return *this; }
138  StlOutputStream& operator<<(const int& message) { if (stream_.get()) *stream_ << message; return *this; }
139  StlOutputStream& operator<<(const unsigned int& message) { if (stream_.get()) *stream_ << message; return *this; }
140  StlOutputStream& operator<<(const long int& message) { if (stream_.get()) *stream_ << message; return *this; }
141  StlOutputStream& operator<<(const unsigned long int& message) { if (stream_.get()) *stream_ << message; return *this; }
142  StlOutputStream& operator<<(const double& message)
143  {
144  if (stream_.get())
145  *stream_ << std::setprecision(getPrecision()) << (isScientificNotationEnabled() ? std::scientific : std::fixed) << message;
146  return *this;
147  }
148  StlOutputStream& operator<<(const long double& message)
149  {
150  if (stream_.get())
151  *stream_ << std::setprecision(getPrecision()) << (isScientificNotationEnabled() ? std::scientific : std::fixed) << message;
152  return *this;
153  }
154  StlOutputStream& operator<<(const bool& message) { if (stream_.get()) *stream_ << message; return *this; }
155  StlOutputStream& endLine() { if (stream_.get()) *stream_ << std::endl; return *this; }
156  StlOutputStream& flush() { if (stream_.get()) stream_->flush(); return *this; }
157 
158  StlOutputStream* clone() const { return new StlOutputStream(*this); }
159 };
160 
161 
169  public AbstractOutputStream
170 {
171 protected:
172  std::ostream* stream_;
173 
174 public:
175  StlOutputStreamWrapper(std::ostream* stream) : stream_(stream) {}
176  StlOutputStreamWrapper(const StlOutputStreamWrapper& stlos) : stream_(stlos.stream_) {}
177  StlOutputStreamWrapper& operator=(const StlOutputStreamWrapper& stlos) { stream_ = stlos.stream_; return *this; }
178 
179 public:
180  StlOutputStreamWrapper& operator<<(const std::string& message) { if (stream_) *stream_ << message; return *this; }
181  StlOutputStreamWrapper& operator<<(const char* message) { if (stream_) *stream_ << message; return *this; }
182  StlOutputStreamWrapper& operator<<(const char& message) { if (stream_) *stream_ << message; return *this; }
183  StlOutputStreamWrapper& operator<<(const int& message) { if (stream_) *stream_ << message; return *this; }
184  StlOutputStreamWrapper& operator<<(const unsigned int& message) { if (stream_) *stream_ << message; return *this; }
185 
186  StlOutputStreamWrapper& operator<<(const long int& message) { if (stream_) *stream_ << message; return *this; }
187  StlOutputStreamWrapper& operator<<(const unsigned long int& message) { if (stream_) *stream_ << message; return *this; }
188  StlOutputStreamWrapper& operator<<(const double& message)
189  {
190  if (stream_)
191  *stream_ << std::setprecision(getPrecision()) << (isScientificNotationEnabled() ? std::scientific : std::fixed) << message;
192  return *this;
193  }
194  StlOutputStreamWrapper& operator<<(const long double& message)
195  {
196  if (stream_)
197  *stream_ << std::setprecision(getPrecision()) << (isScientificNotationEnabled() ? std::scientific : std::fixed) << message;
198  return *this;
199  }
200  StlOutputStreamWrapper& operator<<(const bool& message) { if (stream_) *stream_ << message; return *this; }
201  StlOutputStreamWrapper& endLine() { if (stream_) *stream_ << std::endl; return *this; }
202  StlOutputStreamWrapper& flush() { if (stream_) stream_->flush(); return *this; }
203 
204  StlOutputStreamWrapper* clone() const { return new StlOutputStreamWrapper(*this); }
205 };
206 
212 class StdOut :
213  public AbstractOutputStream
214 {
215 public:
216  OutputStream& operator<<(const std::string& message) { std::cout << message; return *this; }
217  OutputStream& operator<<(const char* message) { std::cout << message; return *this; }
218  OutputStream& operator<<(const char& message) { std::cout << message; return *this; }
219  OutputStream& operator<<(const int& message) { std::cout << message; return *this; }
220  OutputStream& operator<<(const unsigned int& message) { std::cout << message; return *this; }
221  OutputStream& operator<<(const long int& message) { std::cout << message; return *this; }
222  OutputStream& operator<<(const unsigned long int& message) { std::cout << message; return *this; }
223  OutputStream& operator<<(const double& message)
224  {
225  std::cout << std::setprecision(getPrecision()) << (isScientificNotationEnabled() ? std::scientific : std::fixed) << message;
226  return *this;
227  }
228  OutputStream& operator<<(const long double& message)
229  {
230  std::cout << std::setprecision(getPrecision()) << (isScientificNotationEnabled() ? std::scientific : std::fixed) << message;
231  return *this;
232  }
233  OutputStream& operator<<(const bool& message) { std::cout << message; return *this; }
234  OutputStream& endLine() { std::cout << std::endl; return *this; }
235  OutputStream& flush() { std::cout.flush(); return *this; }
236 
237  StdOut* clone() const { return new StdOut(*this); }
238 };
239 
240 
246 class StdErr :
247  public AbstractOutputStream
248 {
249 public:
250  OutputStream& operator<<(const std::string& message) { std::cerr << message; return *this; }
251  OutputStream& operator<<(const char* message) { std::cerr << message; return *this; }
252  OutputStream& operator<<(const char& message) { std::cerr << std::setprecision(getPrecision()) << message; return *this; }
253  OutputStream& operator<<(const int& message) { std::cerr << std::setprecision(getPrecision()) << message; return *this; }
254  OutputStream& operator<<(const unsigned int& message) { std::cerr << message; return *this; }
255  OutputStream& operator<<(const long int& message) { std::cerr << std::setprecision(getPrecision()) << message; return *this; }
256  OutputStream& operator<<(const unsigned long int& message) { std::cerr << message; return *this; }
257  OutputStream& operator<<(const double& message)
258  {
259  std::cerr << std::setprecision(getPrecision()) << (isScientificNotationEnabled() ? std::scientific : std::fixed) << message;
260  return *this;
261  }
262  OutputStream& operator<<(const long double& message)
263  {
264  std::cerr << std::setprecision(getPrecision()) << (isScientificNotationEnabled() ? std::scientific : std::fixed) << message;
265  return *this;
266  }
267  OutputStream& operator<<(const bool& message) { std::cerr << message; return *this; }
268  OutputStream& endLine() { std::cerr << std::endl; return *this; }
269  OutputStream& flush() { std::cerr.flush(); return *this; }
270 
271  StdErr* clone() const { return new StdErr(*this); }
272 };
273 
279 class StdStr :
281 {
282 public:
283  StdStr() : StlOutputStreamWrapper(new std::ostringstream()){}
284 
285  std::string str() const { return dynamic_cast<const std::ostringstream*>(stream_)->str();}
286 
287  virtual ~StdStr() { delete stream_;}
288 };
289 } // end of namespace bpp;
290 #endif // BPP_IO_OUTPUTSTREAM_H
OutputStream & operator<<(const bool &message)
Definition: OutputStream.h:267
virtual bool isScientificNotationEnabled() const
Definition: OutputStream.h:82
StlOutputStream * clone() const
Create a copy of this object and send a pointer to it.
Definition: OutputStream.h:158
StlOutputStream & flush()
Definition: OutputStream.h:156
NullOutputStream & operator<<(const char *message)
Definition: OutputStream.h:94
OutputStream * clone() const =0
Create a copy of this object and send a pointer to it.
virtual OutputStream & enableScientificNotation(bool yn)
Definition: OutputStream.h:81
StlOutputStreamWrapper & operator<<(const std::string &message)
Definition: OutputStream.h:180
virtual OutputStream & operator<<(const std::string &message)=0
virtual int getPrecision() const =0
Helper implementation of the OutputStream interface.
Definition: OutputStream.h:63
OutputStream & operator<<(const unsigned int &message)
Definition: OutputStream.h:254
virtual OutputStream & flush()=0
StlOutputStream(std::unique_ptr< std::ostream > stream)
Definition: OutputStream.h:126
NullOutputStream & operator<<(const unsigned int &message)
Definition: OutputStream.h:97
NullOutputStream * clone() const
Create a copy of this object and send a pointer to it.
Definition: OutputStream.h:106
StlOutputStream & operator<<(const unsigned int &message)
Definition: OutputStream.h:139
StlOutputStream & operator<<(const char &message)
Definition: OutputStream.h:137
STL output stream.
Definition: OutputStream.h:119
std::string str() const
Definition: OutputStream.h:285
OutputStream & operator<<(const std::string &message)
Definition: OutputStream.h:250
OutputStream & operator<<(const long int &message)
Definition: OutputStream.h:255
std::unique_ptr< std::ostream > stream_
Definition: OutputStream.h:123
STL namespace.
OutputStream & operator<<(const unsigned long int &message)
Definition: OutputStream.h:256
NullOutputStream & endLine()
Definition: OutputStream.h:103
NullOutputStream & operator<<(const unsigned long int &message)
Definition: OutputStream.h:99
OutputStream & operator<<(const char *message)
Definition: OutputStream.h:217
OutputStream & endLine()
Definition: OutputStream.h:268
virtual OutputStream & enableScientificNotation(bool yn)=0
Standard output stream.
Definition: OutputStream.h:212
StlOutputStreamWrapper & operator<<(const int &message)
Definition: OutputStream.h:183
StlOutputStream & operator<<(const double &message)
Definition: OutputStream.h:142
StlOutputStream & operator<<(const int &message)
Definition: OutputStream.h:138
OutputStream & operator<<(const unsigned long int &message)
Definition: OutputStream.h:222
OutputStream & operator<<(const bool &message)
Definition: OutputStream.h:233
StlOutputStreamWrapper(std::ostream *stream)
Definition: OutputStream.h:175
virtual ~StdStr()
Definition: OutputStream.h:287
NullOutputStream & operator<<(const int &message)
Definition: OutputStream.h:96
virtual OutputStream & setPrecision(int digit)=0
OutputStream & operator<<(const long double &message)
Definition: OutputStream.h:228
virtual bool isScientificNotationEnabled() const =0
OutputStream & operator<<(const char &message)
Definition: OutputStream.h:252
String output stream.
Definition: OutputStream.h:279
StlOutputStreamWrapper & operator<<(const bool &message)
Definition: OutputStream.h:200
StlOutputStreamWrapper & operator=(const StlOutputStreamWrapper &stlos)
Definition: OutputStream.h:177
StlOutputStream & operator=(const StlOutputStream &stlos)
Definition: OutputStream.h:128
StlOutputStream & operator<<(const bool &message)
Definition: OutputStream.h:154
StlOutputStreamWrapper & operator<<(const unsigned long int &message)
Definition: OutputStream.h:187
OutputStream & setPrecision(int digit)
Definition: OutputStream.h:74
NullOutputStream & operator<<(const std::string &message)
Definition: OutputStream.h:93
StlOutputStreamWrapper & operator<<(const char *message)
Definition: OutputStream.h:181
NullOutputStream & operator<<(const long double &message)
Definition: OutputStream.h:101
Standard error stream.
Definition: OutputStream.h:246
StlOutputStreamWrapper & operator<<(const long int &message)
Definition: OutputStream.h:186
OutputStream & operator<<(const double &message)
Definition: OutputStream.h:223
StdErr * clone() const
Create a copy of this object and send a pointer to it.
Definition: OutputStream.h:271
StlOutputStream & operator<<(const unsigned long int &message)
Definition: OutputStream.h:141
OutputStream & operator<<(const std::string &message)
Definition: OutputStream.h:216
virtual OutputStream & endLine()=0
OutputStream interface.
Definition: OutputStream.h:29
StlOutputStreamWrapper & operator<<(const char &message)
Definition: OutputStream.h:182
OutputStream & flush()
Definition: OutputStream.h:235
StlOutputStreamWrapper & flush()
Definition: OutputStream.h:202
OutputStream & operator<<(const int &message)
Definition: OutputStream.h:219
STL wrapper for output stream.
Definition: OutputStream.h:168
The Clonable interface (allow an object to be cloned).
Definition: Clonable.h:63
StlOutputStream & endLine()
Definition: OutputStream.h:155
OutputStream & operator<<(const long double &message)
Definition: OutputStream.h:262
StlOutputStream & operator<<(const long double &message)
Definition: OutputStream.h:148
Null output stream (swich off output).
Definition: OutputStream.h:89
NullOutputStream & operator<<(const double &message)
Definition: OutputStream.h:100
NullOutputStream & flush()
Definition: OutputStream.h:104
StlOutputStreamWrapper & operator<<(const long double &message)
Definition: OutputStream.h:194
StlOutputStreamWrapper * clone() const
Create a copy of this object and send a pointer to it.
Definition: OutputStream.h:204
OutputStream & operator<<(const long int &message)
Definition: OutputStream.h:221
OutputStream & operator<<(const unsigned int &message)
Definition: OutputStream.h:220
StlOutputStream & operator<<(const char *message)
Definition: OutputStream.h:136
StlOutputStreamWrapper & operator<<(const unsigned int &message)
Definition: OutputStream.h:184
NullOutputStream & operator<<(const char &message)
Definition: OutputStream.h:95
OutputStream & operator<<(const char *message)
Definition: OutputStream.h:251
StlOutputStream & operator<<(const long int &message)
Definition: OutputStream.h:140
StlOutputStreamWrapper & endLine()
Definition: OutputStream.h:201
NullOutputStream & operator<<(const long int &message)
Definition: OutputStream.h:98
NullOutputStream & operator<<(const bool &message)
Definition: OutputStream.h:102
StlOutputStream(const StlOutputStream &stlos)
Definition: OutputStream.h:127
OutputStream & operator<<(const int &message)
Definition: OutputStream.h:253
OutputStream & operator<<(const double &message)
Definition: OutputStream.h:257
StlOutputStream & operator<<(const std::string &message)
Definition: OutputStream.h:135
OutputStream & operator<<(const char &message)
Definition: OutputStream.h:218
StlOutputStreamWrapper & operator<<(const double &message)
Definition: OutputStream.h:188
StdOut * clone() const
Create a copy of this object and send a pointer to it.
Definition: OutputStream.h:237
OutputStream & endLine()
Definition: OutputStream.h:234
OutputStream & flush()
Definition: OutputStream.h:269
StlOutputStreamWrapper(const StlOutputStreamWrapper &stlos)
Definition: OutputStream.h:176