Created
February 22, 2017 00:06
-
-
Save wawiesel/76c9797d265c47d70850feb31398e3e4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Table; | |
class Row { | |
size_t b_col; | |
const std::vector<std::string>* b_fmt; | |
Table* b_table; | |
std::vector<std::string> b_entries; | |
public: | |
Row(const std::vector<std::string>* fmt, Table* table) | |
: b_col(0) | |
, b_fmt(fmt) | |
, b_table(table) | |
{ | |
} | |
template<typename T> | |
Row& e( const T& x, size_t col = static_cast<size_t>(-1) ) | |
{ | |
if( col==static_cast<size_t>(-1) ) | |
{ | |
col = b_col; | |
} | |
b_entries.push_back( fmt::format(b_fmt->at(col),x) ); | |
++b_col; | |
return *this; | |
} | |
Table& n() | |
{ | |
return *b_table; | |
} | |
const std::vector<std::string>& entries() const | |
{ | |
return b_entries; | |
} | |
}; | |
class Table { | |
size_t b_row; | |
std::vector<std::string> b_fmts; | |
std::vector<Row> b_rows; | |
Table(){} | |
public: | |
Table(const std::vector<std::string>& fmts) : b_row(0), b_fmts(fmts) { | |
} | |
Row& r() | |
{ | |
b_rows.emplace_back( &b_fmts, this ); | |
return b_rows.back(); | |
} | |
const std::vector<Row>& rows() const | |
{ | |
return b_rows; | |
} | |
}; | |
int main( int, char* [] ) | |
{ | |
//This would be even better if we completely decoupled the table data | |
//from a format using std::variant or mapbox::variant or something similar | |
//so that we can store the actual data and then send it through a formatter | |
//for output. | |
Table t({"{:12.6f}","{:03d}","{}"}); | |
t.r().e(3.05).e(7).e("xyasdasdfas").n(); | |
t.r().e(4.05).e(8).e("uuuuuuuuuuuuuuuuu").n(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment