mlpack  1.0.12
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 
23 #include <boost/lexical_cast.hpp>
24 #include <boost/utility/enable_if.hpp>
25 #include <boost/type_traits.hpp>
26 
29 
30 namespace mlpack {
31 namespace util {
32 
59 {
60  public:
68  const char* prefix,
69  bool ignoreInput = false,
70  bool fatal = false) :
71  destination(destination),
73  prefix(prefix),
74  // We want the first call to operator<< to prefix the prefix so we set
75  // carriageReturned to true.
76  carriageReturned(true),
77  fatal(fatal)
78  { /* nothing to do */ }
79 
81  PrefixedOutStream& operator<<(bool val);
83  PrefixedOutStream& operator<<(short val);
85  PrefixedOutStream& operator<<(unsigned short val);
87  PrefixedOutStream& operator<<(int val);
89  PrefixedOutStream& operator<<(unsigned int val);
91  PrefixedOutStream& operator<<(long val);
93  PrefixedOutStream& operator<<(unsigned long val);
95  PrefixedOutStream& operator<<(float val);
97  PrefixedOutStream& operator<<(double val);
99  PrefixedOutStream& operator<<(long double val);
101  PrefixedOutStream& operator<<(void* val);
103  PrefixedOutStream& operator<<(const char* str);
105  PrefixedOutStream& operator<<(std::string& str);
107  PrefixedOutStream& operator<<(std::streambuf* sb);
109  PrefixedOutStream& operator<<(std::ostream& (*pf)(std::ostream&));
111  PrefixedOutStream& operator<<(std::ios& (*pf)(std::ios&));
113  PrefixedOutStream& operator<<(std::ios_base& (*pf)(std::ios_base&));
114 
116  template<typename T>
117  PrefixedOutStream& operator<<(const T& s);
118 
120  std::ostream& destination;
121 
124 
125  private:
126  HAS_MEM_FUNC(ToString, HasToString)
127 
128 
129  template<typename T>
130  void CallBaseLogic(const T& s,
131  typename boost::disable_if<
132  boost::is_class<T>
133  >::type* = 0);
134 
136  template<typename T>
137  void CallBaseLogic(const T& s,
138  typename boost::enable_if<
139  boost::is_class<T>
140  >::type* = 0,
141  typename boost::disable_if<
142  HasToString<T, std::string(T::*)() const>
143  >::type* = 0);
144 
146  template<typename T>
147  void CallBaseLogic(const T& s,
148  typename boost::enable_if<
149  boost::is_class<T>
150  >::type* = 0,
151  typename boost::enable_if<
152  HasToString<T, std::string(T::*)() const>
153  >::type* = 0);
154 
162  template<typename T>
163  void BaseLogic(const T& val);
164 
168  inline void PrefixIfNeeded();
169 
171  std::string prefix;
172 
176 
179  bool fatal;
180 };
181 
182 }; // namespace util
183 }; // namespace mlpack
184 
185 // Template definitions.
186 #include "prefixedoutstream_impl.hpp"
187 
188 #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.
Definition: load.hpp:23
void CallBaseLogic(const T &s, typename boost::disable_if< boost::is_class< T > >::type *=0)
This handles forwarding all primitive types transparently.
PrefixedOutStream & operator<<(bool val)
Write a bool to the stream.
bool fatal
If true, the application will terminate with an error code 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.
#define HAS_MEM_FUNC(FUNC, NAME)
Allows us to output to an ostream with a prefix at the beginning of each line, in the same way we wou...