// Copyright 2012 The Kyua Authors.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728/// \file utils/text/table.hpp29/// Table construction and formatting.3031#if !defined(UTILS_TEXT_TABLE_HPP)32#define UTILS_TEXT_TABLE_HPP3334#include "utils/text/table_fwd.hpp"3536#include <cstddef>37#include <string>38#include <vector>3940namespace utils {41namespace text {424344/// Representation of a table.45///46/// A table is nothing more than a matrix of rows by columns. The number of47/// columns is hardcoded at construction times, and the rows can be accumulated48/// at a later stage.49///50/// The only value of this class is a simpler and more natural mechanism of the51/// construction of a table, with additional sanity checks. We could as well52/// just expose the internal data representation to our users.53class table {54/// Widths of the table columns so far.55widths_vector _column_widths;5657/// Type defining the collection of rows in the table.58typedef std::vector< table_row > rows_vector;5960/// The rows of the table.61///62/// This is actually the matrix representing the table. Every element of63/// this vector (which are vectors themselves) must have _ncolumns items.64rows_vector _rows;6566public:67table(const table_row::size_type);6869widths_vector::size_type ncolumns(void) const;70widths_vector::value_type column_width(const widths_vector::size_type)71const;72const widths_vector& column_widths(void) const;7374void add_row(const table_row&);7576bool empty(void) const;7778/// Constant iterator on the rows of the table.79typedef rows_vector::const_iterator const_iterator;8081const_iterator begin(void) const;82const_iterator end(void) const;83};848586/// Settings to format a table.87///88/// This class implements a builder pattern to construct an object that contains89/// all the knowledge to format a table. Once all the settings have been set,90/// the format() method provides the algorithm to apply such formatting settings91/// to any input table.92class table_formatter {93/// Text to use as the separator between cells.94std::string _separator;9596/// Colletion of widths of the columns of a table.97std::size_t _table_width;9899/// Widths of the table columns.100///101/// Note that this only includes widths for the column widths explicitly102/// overriden by the caller. In other words, this vector can be shorter103/// than the table passed to the format() method, which is just fine. Any104/// non-specified column widths are assumed to be width_auto.105widths_vector _column_widths;106107public:108table_formatter(void);109110static const std::size_t width_auto;111static const std::size_t width_refill;112table_formatter& set_column_width(const table_row::size_type,113const std::size_t);114table_formatter& set_separator(const char*);115table_formatter& set_table_width(const std::size_t);116117std::vector< std::string > format(const table&) const;118};119120121} // namespace text122} // namespace utils123124#endif // !defined(UTILS_TEXT_TABLE_HPP)125126127