blob: be41a08251d1b572df43ba466f4ea274240c5195 [file] [log] [blame]
Yifan Hong1bc1e9f2017-08-29 17:28:12 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Steven Moreland0b8e3872019-06-25 08:54:34 -070017#pragma once
Yifan Hong1bc1e9f2017-08-29 17:28:12 -070018
19#include <iostream>
20#include <string>
21#include <vector>
22
Yifan Hong1bc1e9f2017-08-29 17:28:12 -070023namespace android {
24namespace lshal {
25
26// An element in TextTable. This is either an actual row (an array of cells
27// in this row), or a string of explanatory text.
28// To see if this is an actual row, test fields().empty().
29class TextTableRow {
30public:
31 // An empty line.
32 TextTableRow() {}
33
34 // A row of cells.
Chih-Hung Hsieh45e31c72018-12-20 15:47:01 -080035 explicit TextTableRow(std::vector<std::string>&& v) : mFields(std::move(v)) {}
Yifan Hong1bc1e9f2017-08-29 17:28:12 -070036
37 // A single comment string.
Chih-Hung Hsieh45e31c72018-12-20 15:47:01 -080038 explicit TextTableRow(std::string&& s) : mLine(std::move(s)) {}
39 explicit TextTableRow(const std::string& s) : mLine(s) {}
Yifan Hong1bc1e9f2017-08-29 17:28:12 -070040
41 // Whether this row is an actual row of cells.
42 bool isRow() const { return !fields().empty(); }
43
44 // Get all cells.
45 const std::vector<std::string>& fields() const { return mFields; }
46
47 // Get the single comment string.
48 const std::string& line() const { return mLine; }
49
50private:
51 std::vector<std::string> mFields;
52 std::string mLine;
53};
54
55// A TextTable is a 2D array of strings.
56class TextTable {
57public:
58
59 // Add a TextTableRow.
60 void add() { mTable.emplace_back(); }
61 void add(std::vector<std::string>&& v) {
62 computeWidth(v);
63 mTable.emplace_back(std::move(v));
64 }
65 void add(const std::string& s) { mTable.emplace_back(s); }
66 void add(std::string&& s) { mTable.emplace_back(std::move(s)); }
67
Yifan Hongd4a77e82017-09-06 19:40:24 -070068 void addAll(TextTable&& other);
69
Yifan Hong1bc1e9f2017-08-29 17:28:12 -070070 // Prints the table to out, with column widths adjusted appropriately according
71 // to the content.
72 void dump(std::ostream& out) const;
73
74private:
75 void computeWidth(const std::vector<std::string>& v);
76 std::vector<size_t> mWidths;
77 std::vector<TextTableRow> mTable;
78};
79
80} // namespace lshal
81} // namespace android