1 // Copyright 2012 The Kyua Authors.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 /// \file utils/text/table.hpp
30 /// Table construction and formatting.
32 #if !defined(UTILS_TEXT_TABLE_HPP)
33 #define UTILS_TEXT_TABLE_HPP
35 #include "utils/text/table_fwd.hpp"
45 /// Representation of a table.
47 /// A table is nothing more than a matrix of rows by columns. The number of
48 /// columns is hardcoded at construction times, and the rows can be accumulated
51 /// The only value of this class is a simpler and more natural mechanism of the
52 /// construction of a table, with additional sanity checks. We could as well
53 /// just expose the internal data representation to our users.
55 /// Widths of the table columns so far.
56 widths_vector _column_widths;
58 /// Type defining the collection of rows in the table.
59 typedef std::vector< table_row > rows_vector;
61 /// The rows of the table.
63 /// This is actually the matrix representing the table. Every element of
64 /// this vector (which are vectors themselves) must have _ncolumns items.
68 table(const table_row::size_type);
70 widths_vector::size_type ncolumns(void) const;
71 widths_vector::value_type column_width(const widths_vector::size_type)
73 const widths_vector& column_widths(void) const;
75 void add_row(const table_row&);
77 bool empty(void) const;
79 /// Constant iterator on the rows of the table.
80 typedef rows_vector::const_iterator const_iterator;
82 const_iterator begin(void) const;
83 const_iterator end(void) const;
87 /// Settings to format a table.
89 /// This class implements a builder pattern to construct an object that contains
90 /// all the knowledge to format a table. Once all the settings have been set,
91 /// the format() method provides the algorithm to apply such formatting settings
92 /// to any input table.
93 class table_formatter {
94 /// Text to use as the separator between cells.
95 std::string _separator;
97 /// Colletion of widths of the columns of a table.
98 std::size_t _table_width;
100 /// Widths of the table columns.
102 /// Note that this only includes widths for the column widths explicitly
103 /// overriden by the caller. In other words, this vector can be shorter
104 /// than the table passed to the format() method, which is just fine. Any
105 /// non-specified column widths are assumed to be width_auto.
106 widths_vector _column_widths;
109 table_formatter(void);
111 static const std::size_t width_auto;
112 static const std::size_t width_refill;
113 table_formatter& set_column_width(const table_row::size_type,
115 table_formatter& set_separator(const char*);
116 table_formatter& set_table_width(const std::size_t);
118 std::vector< std::string > format(const table&) const;
125 #endif // !defined(UTILS_TEXT_TABLE_HPP)