OpenMS
SVOutStream.h
Go to the documentation of this file.
1 // Copyright (c) 2002-present, The OpenMS Team -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Hendrik Weisser $
6 // $Authors: Hendrik Weisser $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
12 
13 #include <ostream>
14 #include <fstream> // std::ofstream
15 #include <sstream>
16 #include <boost/math/special_functions/fpclassify.hpp> // because isfinite not supported on Mac
17 
18 namespace OpenMS
19 {
21  enum Newline {nl};
22 
30  class OPENMS_DLLAPI SVOutStream :
31  public std::ostream
32  {
33 public:
34 
43  SVOutStream(const String& file_out,
44  const String& sep = "\t",
45  const String& replacement = "_",
47 
56  SVOutStream(std::ostream& out,
57  const String& sep = "\t",
58  const String& replacement = "_",
60 
67  ~SVOutStream() override;
68 
74  SVOutStream& operator<<(String str); // use call-by-value here
75 
76 
82  SVOutStream& operator<<(const std::string& str);
83 
84 
90  SVOutStream& operator<<(const char* c_str);
91 
92 
98  SVOutStream& operator<<(const char c);
99 
101  SVOutStream& operator<<(std::ostream& (*fp)(std::ostream&));
102 
109 
112  template<typename T>
113  typename std::enable_if<std::is_arithmetic<typename std::remove_reference<T>::type>::value, SVOutStream&>::type operator<<(const T& value)
114  {
115  if (!newline_) static_cast<std::ostream&>(*this) << sep_;
116  else newline_ = false;
117  static_cast<std::ostream&>(*this) << String(value);
118  return *this;
119  };
120 
122  template<typename T>
123  typename std::enable_if<!std::is_arithmetic<typename std::remove_reference<T>::type>::value, SVOutStream&>::type operator<<(const T& value)
124  {
125  if (!newline_)
126  static_cast<std::ostream &>(*this) << sep_;
127  else
128  newline_ = false;
129  static_cast<std::ostream &>(*this) << value;
130  return *this;
131  };
132 
134  SVOutStream& write(const String& str); // write unmodified string
135 
136 
142  bool modifyStrings(bool modify);
143 
144 
146  template <typename NumericT>
147  SVOutStream& writeValueOrNan(NumericT thing)
148  {
149  if ((boost::math::isfinite)(thing)) return operator<<(thing);
150 
151  bool old = modifyStrings(false);
152  if ((boost::math::isnan)(thing))
153  {
154  operator<<(nan_);
155  }
156  else if (thing < 0)
157  {
158  operator<<("-" + inf_);
159  }
160  else
161  {
162  operator<<(inf_);
163  }
164  modifyStrings(old);
165  return *this;
166  }
167 
168 protected:
170  std::ofstream* ofs_;
171 
174 
177 
180 
183 
186 
189 
191  bool newline_;
192 
194  std::stringstream ss_;
195  };
196 
197 }
198 
Stream class for writing to comma/tab/...-separated values files.
Definition: SVOutStream.h:32
String::QuotingMethod quoting_
String quoting method.
Definition: SVOutStream.h:185
bool newline_
Are we at the beginning of a line? (Otherwise, insert separator before next item.)
Definition: SVOutStream.h:191
SVOutStream & operator<<(const char *c_str)
Stream output operator for char*.
SVOutStream & operator<<(std::ostream &(*fp)(std::ostream &))
Stream output operator for manipulators (used to catch std::endl)
std::enable_if<!std::is_arithmetic< typename std::remove_reference< T >::type >::value, SVOutStream & >::type operator<<(const T &value)
Generic stream output operator (for non-character-based types)
Definition: SVOutStream.h:123
std::stringstream ss_
Stream for testing if a manipulator is "std::endl".
Definition: SVOutStream.h:194
SVOutStream & operator<<(enum Newline)
Stream output operator for custom newline (nl) without flushing.
String sep_
Separator string.
Definition: SVOutStream.h:173
SVOutStream & operator<<(String str)
Stream output operator for String.
String inf_
String to use for Inf values.
Definition: SVOutStream.h:182
bool modifyStrings(bool modify)
Switch modification of strings (quoting/replacing of separators) on/off.
SVOutStream & operator<<(const char c)
Stream output operator for char.
SVOutStream(const String &file_out, const String &sep="\t", const String &replacement="_", String::QuotingMethod quoting=String::DOUBLE)
Constructor.
String replacement_
Replacement for separator.
Definition: SVOutStream.h:176
std::enable_if< std::is_arithmetic< typename std::remove_reference< T >::type >::value, SVOutStream & >::type operator<<(const T &value)
Definition: SVOutStream.h:113
SVOutStream & writeValueOrNan(NumericT thing)
Write a numeric value or "nan"/"inf"/"-inf", if applicable (would not be needed for Linux)
Definition: SVOutStream.h:147
SVOutStream & operator<<(const std::string &str)
Stream output operator for std::string.
~SVOutStream() override
Destructor.
String nan_
String to use for NaN values.
Definition: SVOutStream.h:179
SVOutStream(std::ostream &out, const String &sep="\t", const String &replacement="_", String::QuotingMethod quoting=String::DOUBLE)
Constructor.
std::ofstream * ofs_
internal file stream when C'tor is called with a filename
Definition: SVOutStream.h:170
bool modify_strings_
On/off switch for modification of strings.
Definition: SVOutStream.h:188
SVOutStream & write(const String &str)
Unformatted output (no quoting: useful for comments, but use only on a line of its own!...
A more convenient string class.
Definition: String.h:34
QuotingMethod
How to handle embedded quotes when quoting strings.
Definition: String.h:55
@ DOUBLE
Definition: String.h:55
const double c
Definition: Constants.h:188
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
std::ostream & operator<<(std::ostream &os, const AccurateMassSearchResult &amsr)
Newline
custom newline indicator
Definition: SVOutStream.h:21
@ nl
Definition: SVOutStream.h:21