io: add make_indented

This commit is contained in:
Danny Robson 2014-06-29 22:49:26 +10:00
parent aa52f440bc
commit c76914b354
2 changed files with 50 additions and 1 deletions

20
io.hpp
View File

@ -59,6 +59,7 @@ namespace util {
}; };
//-------------------------------------------------------------------------
class indenter : public std::streambuf { class indenter : public std::streambuf {
protected: protected:
std::streambuf* m_dest; std::streambuf* m_dest;
@ -76,7 +77,19 @@ namespace util {
virtual ~indenter (); virtual ~indenter ();
}; };
//-------------------------------------------------------------------------
template <typename T>
struct indented {
indented (const T &_data);
const T &data;
};
template <typename T>
indented<T>
make_indented (const T &_data);
//-------------------------------------------------------------------------
class scoped_cwd { class scoped_cwd {
public: public:
scoped_cwd (); scoped_cwd ();
@ -124,4 +137,11 @@ namespace util {
}; };
} }
template <typename T>
std::ostream&
operator<< (std::ostream &os, const util::indented<T> &v);
#include "io.ipp"
#endif #endif

29
io.ipp Normal file
View File

@ -0,0 +1,29 @@
#ifdef __UTIL_IO_IPP
#error "multiple inclusions"
#else
#define __UTIL__IO_IPP
#endif
namespace util {
template <typename T>
indented<T>::indented (const T &_data):
data (_data)
{ ; }
template <typename T>
indented<T>
make_indented (const T &_data) {
return indented<T> (_data);
}
}
template <typename T>
std::ostream&
operator<< (std::ostream &os, const util::indented<T> &v) {
util::indenter raii (os);
os << v.data;
return os;
}