diff --git a/base/indented_stream.h b/base/indented_stream.h new file mode 100644 index 0000000..b47bca2 --- /dev/null +++ b/base/indented_stream.h @@ -0,0 +1,48 @@ +#ifndef BASE_INDENTED_STREAM_H +#define BASE_INDENTED_STREAM_H + +#include + +//---------------------------------------------------------------- + +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 + 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 diff --git a/caching/xml_format.cc b/caching/xml_format.cc index b03e997..cb03018 100644 --- a/caching/xml_format.cc +++ b/caching/xml_format.cc @@ -1,4 +1,5 @@ #include "base/base64.h" +#include "base/indented_stream.h" #include "caching/xml_format.h" #include @@ -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_ << "" << endl; - inc(); + out_.inc(); } virtual void end_superblock() { - dec(); - indent(); + out_.dec(); + out_.indent(); out_ << "" << endl; } virtual void begin_mappings() { - indent(); + out_.indent(); out_ << "" << endl; - inc(); + out_.inc(); } virtual void end_mappings() { - dec(); - indent(); + out_.dec(); + out_.indent(); out_ << "" << endl; } virtual void mapping(block_address cblock, block_address oblock, bool dirty) { - indent(); + out_.indent(); out_ << "" << endl; - inc(); + out_.inc(); } virtual void end_hints() { - dec(); - indent(); + out_.dec(); + out_.indent(); out_ << "" << endl; } @@ -81,7 +81,7 @@ namespace { vector const &data) { using namespace base; - indent(); + out_.indent(); out_ << "" << endl; - inc(); + out_.inc(); } virtual void end_discards() { - dec(); - indent(); + out_.dec(); + out_.indent(); out_ << "" << endl; } virtual void discard(block_address dblock_b, block_address dblock_e) { - indent(); + out_.indent(); out_ << "" << 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_; }; //-------------------------------- diff --git a/era/xml_format.cc b/era/xml_format.cc index 924a93b..0878410 100644 --- a/era/xml_format.cc +++ b/era/xml_format.cc @@ -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_ << "" << endl; - inc(); + << " current_era=\"" << current_era << "\">"; + out_ << endl; + out_.inc(); } void end_superblock() { - dec(); - indent(); + out_.dec(); + out_.indent(); out_ << "" << endl; } void begin_writeset(uint32_t era, uint32_t nr_bits) { - indent(); + out_.indent(); out_ << "" << endl; - inc(); + out_.inc(); } void writeset_bit(uint32_t bit, bool value) { - indent(); + out_.indent(); // FIXME: collect all the bits, then uuencode out_ << "" << endl; } void end_writeset() { - dec(); - indent(); + out_.dec(); + out_.indent(); out_ << "" << endl; } void begin_era_array() { - indent(); + out_.indent(); out_ << "" << endl; - inc(); + out_.inc(); } void era(pd::block_address block, uint32_t era) { - indent(); + out_.indent(); out_ << "" << endl; } void end_era_array() { - dec(); - indent(); + out_.dec(); + out_.indent(); out_ << "" << 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_; }; } diff --git a/thin-provisioning/xml_format.cc b/thin-provisioning/xml_format.cc index 55007ca..2767b9a 100644 --- a/thin-provisioning/xml_format.cc +++ b/thin-provisioning/xml_format.cc @@ -17,6 +17,7 @@ // . #include "xml_format.h" +#include "base/indented_stream.h" #include #include @@ -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 metadata_snap) { - indent(); + out_.indent(); out_ << "" - << endl; - inc(); + out_ << "\">" << endl; + out_.inc(); } void end_superblock() { - dec(); - indent(); + out_.dec(); + out_.indent(); out_ << "" << endl; } @@ -77,40 +76,40 @@ namespace { uint64_t trans_id, uint64_t creation_time, uint64_t snap_time) { - indent(); + out_.indent(); out_ << "" << endl; - inc(); + out_.inc(); } void end_device() { - dec(); - indent(); + out_.dec(); + out_.indent(); out_ << "" << endl; } void begin_named_mapping(string const &name) { - indent(); + out_.indent(); out_ << "" << endl; - inc(); + out_.inc(); } void end_named_mapping() { - dec(); - indent(); + out_.dec(); + out_.indent(); out_ << "" << endl; } void identifier(string const &name) { - indent(); + out_.indent(); out_ << "" << endl; } void range_map(uint64_t origin_begin, uint64_t data_begin, uint32_t time, uint64_t len) { - indent(); + out_.indent(); out_ << "