Factor out base/indented_stream.h

This commit is contained in:
Joe Thornber 2014-01-31 13:43:39 +00:00
parent 8531a2befa
commit 344f4b1e08
4 changed files with 109 additions and 104 deletions

48
base/indented_stream.h Normal file
View File

@ -0,0 +1,48 @@
#ifndef BASE_INDENTED_STREAM_H
#define BASE_INDENTED_STREAM_H
#include <iostream>
//----------------------------------------------------------------
namespace {
class indented_stream {
public:
indented_stream(std::ostream &out)
: out_(out),
indent_(0) {
}
void indent() {
for (unsigned i = 0; i < indent_ * 2; i++)
out_ << ' ';
}
void inc() {
indent_++;
}
void dec() {
indent_--;
}
template <typename T>
indented_stream &operator <<(T const &t) {
out_ << t;
return *this;
}
indented_stream &operator <<(std::ostream &(*fp)(std::ostream &)) {
out_ << fp;
return *this;
}
private:
std::ostream &out_;
unsigned indent_;
};
}
//----------------------------------------------------------------
#endif

View File

@ -1,4 +1,5 @@
#include "base/base64.h"
#include "base/indented_stream.h"
#include "caching/xml_format.h"
#include <boost/lexical_cast.hpp>
@ -18,8 +19,7 @@ namespace {
class xml_emitter : public emitter {
public:
xml_emitter(ostream &out)
: out_(out),
indent_(0) {
: out_(out) {
}
void begin_superblock(std::string const &uuid,
@ -27,37 +27,37 @@ namespace {
block_address nr_cache_blocks,
std::string const &policy,
size_t hint_width) {
indent();
out_.indent();
out_ << "<superblock uuid=\"" << uuid << "\""
<< " block_size=\"" << block_size << "\""
<< " nr_cache_blocks=\"" << nr_cache_blocks << "\""
<< " policy=\"" << policy << "\""
<< " hint_width=\"" << hint_width << "\">" << endl;
inc();
out_.inc();
}
virtual void end_superblock() {
dec();
indent();
out_.dec();
out_.indent();
out_ << "</superblock>" << endl;
}
virtual void begin_mappings() {
indent();
out_.indent();
out_ << "<mappings>" << endl;
inc();
out_.inc();
}
virtual void end_mappings() {
dec();
indent();
out_.dec();
out_.indent();
out_ << "</mappings>" << endl;
}
virtual void mapping(block_address cblock,
block_address oblock,
bool dirty) {
indent();
out_.indent();
out_ << "<mapping"
<< " cache_block=\"" << cblock << "\""
<< " origin_block=\"" << oblock << "\""
@ -66,14 +66,14 @@ namespace {
}
virtual void begin_hints() {
indent();
out_.indent();
out_ << "<hints>" << endl;
inc();
out_.inc();
}
virtual void end_hints() {
dec();
indent();
out_.dec();
out_.indent();
out_ << "</hints>" << endl;
}
@ -81,7 +81,7 @@ namespace {
vector<unsigned char> const &data) {
using namespace base;
indent();
out_.indent();
out_ << "<hint"
<< " cache_block=\"" << cblock << "\""
<< " data=\"" << base64_encode(data) << "\""
@ -89,19 +89,19 @@ namespace {
}
virtual void begin_discards() {
indent();
out_.indent();
out_ << "<discards>" << endl;
inc();
out_.inc();
}
virtual void end_discards() {
dec();
indent();
out_.dec();
out_.indent();
out_ << "</discards>" << endl;
}
virtual void discard(block_address dblock_b, block_address dblock_e) {
indent();
out_.indent();
out_ << "<discard dbegin=\"" << dblock_b << "\""
<< " dend=\"" << dblock_e << "\"/>" << endl;
}
@ -111,22 +111,7 @@ namespace {
return v ? "true" : "false";
}
// FIXME: factor out a common class with the thin_provisioning emitter
void indent() {
for (unsigned i = 0; i < indent_ * 2; i++)
out_ << ' ';
}
void inc() {
indent_++;
}
void dec() {
indent_--;
}
ostream &out_;
unsigned indent_;
indented_stream out_;
};
//--------------------------------

View File

@ -1,5 +1,7 @@
#include "era/xml_format.h"
#include "base/indented_stream.h"
using namespace boost;
using namespace era;
using namespace persistent_data;
@ -11,82 +13,67 @@ namespace {
class xml_emitter : public emitter {
public:
xml_emitter(ostream &out)
: out_(out),
indent_(0) {
: out_(out) {
}
void begin_superblock(std::string const &uuid,
uint32_t block_size,
pd::block_address nr_blocks,
uint32_t current_era) {
indent();
out_.indent();
out_ << "<superblock uuid=\"" << uuid << "\""
<< " block_size=\"" << block_size << "\""
<< " nr_blocks=\"" << nr_blocks << "\""
<< " current_era=\"" << current_era << "\">" << endl;
inc();
<< " current_era=\"" << current_era << "\">";
out_ << endl;
out_.inc();
}
void end_superblock() {
dec();
indent();
out_.dec();
out_.indent();
out_ << "</superblock>" << endl;
}
void begin_writeset(uint32_t era, uint32_t nr_bits) {
indent();
out_.indent();
out_ << "<writeset era=\"" << era << "\""
<< " nr_bits=\"" << nr_bits << "\">" << endl;
inc();
out_.inc();
}
void writeset_bit(uint32_t bit, bool value) {
indent();
out_.indent();
// FIXME: collect all the bits, then uuencode
out_ << "<bit bit=\"" << bit << "\" value=\"" << value << "\">" << endl;
}
void end_writeset() {
dec();
indent();
out_.dec();
out_.indent();
out_ << "</writeset>" << endl;
}
void begin_era_array() {
indent();
out_.indent();
out_ << "<era_array>" << endl;
inc();
out_.inc();
}
void era(pd::block_address block, uint32_t era) {
indent();
out_.indent();
out_ << "<era block=\"" << block
<< "\" era=\"" << era << "\">" << endl;
}
void end_era_array() {
dec();
indent();
out_.dec();
out_.indent();
out_ << "</era_array>" << endl;
}
private:
// FIXME: factor out a common class with the thin_provisioning emitter
void indent() {
for (unsigned i = 0; i < indent_ * 2; i++)
out_ << ' ';
}
void inc() {
indent_++;
}
void dec() {
indent_--;
}
ostream &out_;
unsigned indent_;
indented_stream out_;
};
}

View File

@ -17,6 +17,7 @@
// <http://www.gnu.org/licenses/>.
#include "xml_format.h"
#include "base/indented_stream.h"
#include <boost/lexical_cast.hpp>
#include <boost/optional.hpp>
@ -41,8 +42,7 @@ namespace {
class xml_emitter : public emitter {
public:
xml_emitter(ostream &out)
: out_(out),
indent_(0) {
: out_(out) {
}
void begin_superblock(string const &uuid,
@ -51,7 +51,7 @@ namespace {
uint32_t data_block_size,
uint64_t nr_data_blocks,
boost::optional<uint64_t> metadata_snap) {
indent();
out_.indent();
out_ << "<superblock uuid=\"" << uuid << "\""
<< " time=\"" << time << "\""
<< " transaction=\"" << trans_id << "\""
@ -61,14 +61,13 @@ namespace {
if (metadata_snap)
out_ << "\" metadata_snap=\"" << *metadata_snap;
out_ << "\">"
<< endl;
inc();
out_ << "\">" << endl;
out_.inc();
}
void end_superblock() {
dec();
indent();
out_.dec();
out_.indent();
out_ << "</superblock>" << endl;
}
@ -77,40 +76,40 @@ namespace {
uint64_t trans_id,
uint64_t creation_time,
uint64_t snap_time) {
indent();
out_.indent();
out_ << "<device dev_id=\"" << dev_id << "\""
<< " mapped_blocks=\"" << mapped_blocks << "\""
<< " transaction=\"" << trans_id << "\""
<< " creation_time=\"" << creation_time << "\""
<< " snap_time=\"" << snap_time << "\">" << endl;
inc();
out_.inc();
}
void end_device() {
dec();
indent();
out_.dec();
out_.indent();
out_ << "</device>" << endl;
}
void begin_named_mapping(string const &name) {
indent();
out_.indent();
out_ << "<named_mapping>" << endl;
inc();
out_.inc();
}
void end_named_mapping() {
dec();
indent();
out_.dec();
out_.indent();
out_ << "</named_mapping>" << endl;
}
void identifier(string const &name) {
indent();
out_.indent();
out_ << "<identifier name=\"" << name << "\"/>" << endl;
}
void range_map(uint64_t origin_begin, uint64_t data_begin, uint32_t time, uint64_t len) {
indent();
out_.indent();
out_ << "<range_mapping origin_begin=\"" << origin_begin << "\""
<< " data_begin=\"" << data_begin << "\""
@ -120,7 +119,7 @@ namespace {
}
void single_map(uint64_t origin_block, uint64_t data_block, uint32_t time) {
indent();
out_.indent();
out_ << "<single_mapping origin_block=\"" << origin_block << "\""
<< " data_block=\"" << data_block << "\""
@ -129,21 +128,7 @@ namespace {
}
private:
void indent() {
for (unsigned i = 0; i < indent_ * 2; i++)
out_ << ' ';
}
void inc() {
indent_++;
}
void dec() {
indent_--;
}
ostream &out_;
unsigned indent_;
indented_stream out_;
};
//------------------------------------------------