Put nested_output in it's own file

This commit is contained in:
Joe Thornber 2013-08-16 13:48:25 +01:00
parent 1e925d4cf1
commit 94bd3aef3b
2 changed files with 95 additions and 84 deletions

94
base/nested_output.h Normal file
View File

@ -0,0 +1,94 @@
#ifndef BASE_NESTED_OUTPUT_H
#define BASE_NESTED_OUTPUT_H
#include <iostream>
//----------------------------------------------------------------
namespace base {
class end_message {};
class nested_output {
public:
nested_output(std::ostream &out, unsigned step)
: out_(out),
step_(step),
beginning_of_line_(true),
enabled_(true),
indent_(0) {
}
template <typename T>
nested_output &operator <<(T const &t) {
if (beginning_of_line_) {
beginning_of_line_ = false;
indent();
}
if (enabled_)
out_ << t;
return *this;
}
nested_output &operator <<(end_message const &m) {
beginning_of_line_ = true;
if (enabled_)
out_ << std::endl;
return *this;
}
void inc_indent() {
indent_ += step_;
}
void dec_indent() {
indent_ -= step_;
}
struct nest {
nest(nested_output &out)
: out_(out) {
out_.inc_indent();
}
~nest() {
out_.dec_indent();
}
nested_output &out_;
};
nest push() {
return nest(*this);
}
void enable() {
enabled_ = true;
}
void disable() {
enabled_ = false;
}
private:
void indent() {
if (enabled_)
for (unsigned i = 0; i < indent_; i++)
out_ << ' ';
}
std::ostream &out_;
unsigned step_;
bool beginning_of_line_;
bool enabled_;
unsigned indent_;
};
}
//----------------------------------------------------------------
#endif

View File

@ -22,6 +22,7 @@
#include "version.h" #include "version.h"
#include "base/nested_output.h"
#include "persistent-data/space-maps/core.h" #include "persistent-data/space-maps/core.h"
#include "thin-provisioning/device_tree.h" #include "thin-provisioning/device_tree.h"
#include "thin-provisioning/file_utils.h" #include "thin-provisioning/file_utils.h"
@ -35,90 +36,6 @@ using namespace thin_provisioning;
namespace { namespace {
class end_message {};
class nested_output {
public:
nested_output(ostream &out, unsigned step)
: out_(out),
step_(step),
beginning_of_line_(true),
enabled_(true),
indent_(0) {
}
template <typename T>
nested_output &operator <<(T const &t) {
if (beginning_of_line_) {
beginning_of_line_ = false;
indent();
}
if (enabled_)
out_ << t;
return *this;
}
nested_output &operator <<(end_message const &m) {
beginning_of_line_ = true;
if (enabled_)
out_ << endl;
return *this;
}
void inc_indent() {
indent_ += step_;
}
void dec_indent() {
indent_ -= step_;
}
struct nest {
nest(nested_output &out)
: out_(out) {
out_.inc_indent();
}
~nest() {
out_.dec_indent();
}
nested_output &out_;
};
nest push() {
return nest(*this);
}
void enable() {
enabled_ = true;
}
void disable() {
enabled_ = false;
}
private:
void indent() {
if (enabled_)
for (unsigned i = 0; i < indent_; i++)
out_ << ' ';
}
ostream &out_;
unsigned step_;
bool beginning_of_line_;
bool enabled_;
unsigned indent_;
};
//--------------------------------
enum error_state { enum error_state {
NO_ERROR, NO_ERROR,
NON_FATAL, // eg, lost blocks NON_FATAL, // eg, lost blocks