p-ranav / tabulate

Table Maker for Modern C++
MIT License
1.94k stars 138 forks source link

add stream insertion for rows #112

Closed joe-barnier closed 8 months ago

joe-barnier commented 8 months ago

The existing Table::add_row member function is limited to accepting only strings and inner tables. In many cases, the data being added to a table is not string data, and forcing applications to always serialize that data before calling add_row may be needlessly cumbersome (at least it often feels that way when I am using the library).

This PR adds a RowStream class that takes advantage of std::ostringstream formatting to streamline application code.

RowStream is implicitly convertible to a Table::Row_t to minimize disruption to the existing API.

table.add_row(RowStream{} << 101 << "Donald" << "Patrick" << "Finance" << 59.6154);

RowStream is expected to provide all the standard formatting behavior of std::ostringstream (e.g. .imbue(...), .precision(...), << std::setprecision(...), etc.). Thus more complex formatting is supported.


Another possible option that I considered would be to add another add_row overload (or perhaps a differently named member function) that returns a similar RowStream object that adds the row data when it goes out of scope. This option would allow for the following application code:

table.add_row() << 101 << "Donald" << "Patrick" << "Finance" << 59.6154;

While it is more concise, I ruled against this option because I think it is more foreign to the existing API. However, if there is interest I would be willing to change the implementation accordingly.


I also considered a variadic template solution, but then formatting options would be significantly limited.


Thank you for your consideration.

joe-barnier commented 8 months ago

Hi @p-ranav, is there anything you're looking for from me to help push this forward? Or should I just be patient? :)

p-ranav commented 8 months ago

Really appreciate your contribution @joe-barnier :)

Thanks for the bump, I did lose track of this in my notifications. Merged now.