[thin_ls, base] Factor out grid_layout

This commit is contained in:
Joe Thornber 2016-01-20 06:58:59 +00:00
parent 5aaa26fe34
commit e15b11edb1
4 changed files with 125 additions and 71 deletions

View File

@ -31,6 +31,7 @@ SOURCE=\
base/endian_utils.cc \ base/endian_utils.cc \
base/error_state.cc \ base/error_state.cc \
base/error_string.cc \ base/error_string.cc \
base/grid_layout.cc \
base/progress_monitor.cc \ base/progress_monitor.cc \
base/xml_utils.cc \ base/xml_utils.cc \
block-cache/block_cache.cc \ block-cache/block_cache.cc \

82
base/grid_layout.cc Normal file
View File

@ -0,0 +1,82 @@
#include "base/grid_layout.h"
#include <stdexcept>
using namespace base;
using namespace std;
//----------------------------------------------------------------
grid_layout::grid_layout()
: nr_fields_(0)
{
new_row();
}
void
grid_layout::render(ostream &out) const
{
vector<unsigned> widths;
calc_field_widths(widths);
grid::const_iterator row;
for (row = grid_.begin(); row != grid_.end(); ++row) {
row::const_iterator col;
unsigned i;
for (col = row->begin(), i = 0; col != row->end(); ++col, ++i)
out << justify(widths[i], *col) << " ";
out << "\n";
}
}
void
grid_layout::new_row()
{
grid_.push_back(row());
}
grid_layout::row const &
grid_layout::current_row() const
{
return grid_.back();
}
grid_layout::row &
grid_layout::current_row()
{
return grid_.back();
}
void
grid_layout::push_field(string const &s)
{
current_row().push_back(s);
nr_fields_ = max<unsigned>(nr_fields_, current_row().size());
}
void
grid_layout::calc_field_widths(vector<unsigned> &widths) const
{
widths.resize(nr_fields_, 0);
grid::const_iterator row;
for (row = grid_.begin(); row != grid_.end(); ++row) {
row::const_iterator col;
unsigned i;
for (col = row->begin(), i = 0; col != row->end(); ++col, ++i)
widths[i] = max<unsigned>(widths[i], col->length());
}
}
string
grid_layout::justify(unsigned width, string const &txt) const
{
if (txt.length() > width)
throw runtime_error("string field too long, internal error");
string result(width - txt.length(), ' ');
result += txt;
return result;
}
//----------------------------------------------------------------

41
base/grid_layout.h Normal file
View File

@ -0,0 +1,41 @@
#ifndef BASE_GRID_LAYOUT_H
#define BASE_GRID_LAYOUT_H
#include <boost/lexical_cast.hpp>
#include <iosfwd>
#include <list>
#include <string>
#include <vector>
//----------------------------------------------------------------
namespace base {
class grid_layout {
public:
typedef std::list<std::string> row;
typedef std::list<row> grid;
grid_layout();
void render(std::ostream &out) const;
void new_row();
template <typename T>
void field(T const &t) {
push_field(boost::lexical_cast<std::string>(t));
}
private:
row &current_row();
row const &current_row() const;
void push_field(std::string const &s);
void calc_field_widths(std::vector<unsigned> &widths) const;
std::string justify(unsigned width, std::string const &txt) const;
grid grid_;
unsigned nr_fields_;
};
}
//----------------------------------------------------------------
#endif

View File

@ -22,6 +22,7 @@
#include <libgen.h> #include <libgen.h>
#include "base/disk_units.h" #include "base/disk_units.h"
#include "base/grid_layout.h"
#include "boost/lexical_cast.hpp" #include "boost/lexical_cast.hpp"
#include "boost/optional.hpp" #include "boost/optional.hpp"
#include "boost/range.hpp" #include "boost/range.hpp"
@ -43,77 +44,6 @@ using namespace thin_provisioning;
//---------------------------------------------------------------- //----------------------------------------------------------------
namespace { namespace {
// FIXME: move to own file
class grid_layout {
public:
typedef list<string> row;
typedef list<row> grid;
grid_layout()
: nr_fields_(0) {
new_row();
}
void render(ostream &out) {
vector<unsigned> widths;
calc_field_widths(widths);
grid::const_iterator row;
for (row = grid_.begin(); row != grid_.end(); ++row) {
row::const_iterator col;
unsigned i;
for (col = row->begin(), i = 0; col != row->end(); ++col, ++i)
out << justify(widths[i], *col) << " ";
out << "\n";
}
}
void new_row() {
grid_.push_back(row());
}
template <typename T>
void field(T const &t) {
push_field(lexical_cast<string>(t));
}
private:
row &current_row() {
return grid_.back();
}
void push_field(string const &s) {
current_row().push_back(s);
nr_fields_ = max<unsigned>(nr_fields_, current_row().size());
}
void calc_field_widths(vector<unsigned> &widths) const {
widths.resize(nr_fields_, 0);
grid::const_iterator row;
for (row = grid_.begin(); row != grid_.end(); ++row) {
row::const_iterator col;
unsigned i;
for (col = row->begin(), i = 0; col != row->end(); ++col, ++i)
widths[i] = max<unsigned>(widths[i], col->length());
}
}
string justify(unsigned width, string const &txt) const {
if (txt.length() > width)
throw runtime_error("string field too long, internal error");
string result(width - txt.length(), ' ');
result += txt;
return result;
}
grid grid_;
unsigned nr_fields_;
};
//------------------------------------------------
class mapping_set { class mapping_set {
public: public:
mapping_set(block_address nr_blocks) mapping_set(block_address nr_blocks)