mlpack  2.0.1
prefixedoutstream.hpp
Go to the documentation of this file.
1 
15 #ifndef __MLPACK_CORE_UTIL_PREFIXEDOUTSTREAM_HPP
16 #define __MLPACK_CORE_UTIL_PREFIXEDOUTSTREAM_HPP
17 
18 #include <iostream>
19 #include <iomanip>
20 #include <string>
21 #include <streambuf>
22 #include <stdexcept>
23 
24 #include <boost/lexical_cast.hpp>
25 #include <boost/utility/enable_if.hpp>
26 #include <boost/type_traits.hpp>
27 
30 
31 namespace mlpack {
32 namespace util {
33 
60 {
61  public:
72  const char* prefix,
73  bool ignoreInput = false,
74  bool fatal = false) :
75  destination(destination),
77  prefix(prefix),
78  // We want the first call to operator<< to prefix the prefix so we set
79  // carriageReturned to true.
80  carriageReturned(true),
81  fatal(fatal)
82  { /* nothing to do */ }
83 
85  PrefixedOutStream& operator<<(bool val);
87  PrefixedOutStream& operator<<(short val);
89  PrefixedOutStream& operator<<(unsigned short val);
91  PrefixedOutStream& operator<<(int val);
93  PrefixedOutStream& operator<<(unsigned int val);
95  PrefixedOutStream& operator<<(long val);
97  PrefixedOutStream& operator<<(unsigned long val);
99  PrefixedOutStream& operator<<(float val);
101  PrefixedOutStream& operator<<(double val);
103  PrefixedOutStream& operator<<(long double val);
105  PrefixedOutStream& operator<<(void* val);
107  PrefixedOutStream& operator<<(const char* str);
109  PrefixedOutStream& operator<<(std::string& str);
111  PrefixedOutStream& operator<<(std::streambuf* sb);
113  PrefixedOutStream& operator<<(std::ostream& (*pf)(std::ostream&));
115  PrefixedOutStream& operator<<(std::ios& (*pf)(std::ios&));
117  PrefixedOutStream& operator<<(std::ios_base& (*pf)(std::ios_base&));
118 
120  template<typename T>
121  PrefixedOutStream& operator<<(const T& s);
122 
124  std::ostream& destination;
125 
128 
129  private:
137  template<typename T>
138  void BaseLogic(const T& val);
139 
143  inline void PrefixIfNeeded();
144 
146  std::string prefix;
147 
151 
154  bool fatal;
155 };
156 
157 } // namespace util
158 } // namespace mlpack
159 
160 // Template definitions.
161 #include "prefixedoutstream_impl.hpp"
162 
163 #endif
std::ostream & destination
The output stream that all data is to be sent too; example: std::cout.
Linear algebra utility functions, generally performed on matrices or vectors.
PrefixedOutStream & operator<<(bool val)
Write a bool to the stream.
bool fatal
If true, a std::runtime_error exception will be thrown when a CR is encountered.
std::string prefix
Contains the prefix we must prepend to each line.
PrefixedOutStream(std::ostream &destination, const char *prefix, bool ignoreInput=false, bool fatal=false)
Set up the PrefixedOutStream.
bool ignoreInput
Discards input, prints nothing if true.
bool carriageReturned
If true, the previous call to operator<< encountered a CR, and a prefix will be necessary.
void BaseLogic(const T &val)
Conducts the base logic required in all the operator << overloads.
void PrefixIfNeeded()
Output the prefix, but only if we need to and if we are allowed to.
Allows us to output to an ostream with a prefix at the beginning of each line, in the same way we wou...