blob: 4636f15a2705f9d1fd3a699760a5be42fece074c [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
17#ifndef FRAMEWORK_NATIVE_CMDS_LSHAL_TEXT_TABLE_H_
18#define FRAMEWORK_NATIVE_CMDS_LSHAL_TEXT_TABLE_H_
19
20#include <iostream>
21#include <string>
22#include <vector>
23
24#include "TableEntry.h"
25
26namespace android {
27namespace lshal {
28
29// An element in TextTable. This is either an actual row (an array of cells
30// in this row), or a string of explanatory text.
31// To see if this is an actual row, test fields().empty().
32class TextTableRow {
33public:
34 // An empty line.
35 TextTableRow() {}
36
37 // A row of cells.
38 TextTableRow(std::vector<std::string>&& v) : mFields(std::move(v)) {}
39
40 // A single comment string.
41 TextTableRow(std::string&& s) : mLine(std::move(s)) {}
42 TextTableRow(const std::string& s) : mLine(s) {}
43
44 // Whether this row is an actual row of cells.
45 bool isRow() const { return !fields().empty(); }
46
47 // Get all cells.
48 const std::vector<std::string>& fields() const { return mFields; }
49
50 // Get the single comment string.
51 const std::string& line() const { return mLine; }
52
53private:
54 std::vector<std::string> mFields;
55 std::string mLine;
56};
57
58// A TextTable is a 2D array of strings.
59class TextTable {
60public:
61
62 // Add a TextTableRow.
63 void add() { mTable.emplace_back(); }
64 void add(std::vector<std::string>&& v) {
65 computeWidth(v);
66 mTable.emplace_back(std::move(v));
67 }
68 void add(const std::string& s) { mTable.emplace_back(s); }
69 void add(std::string&& s) { mTable.emplace_back(std::move(s)); }
70
71 // Prints the table to out, with column widths adjusted appropriately according
72 // to the content.
73 void dump(std::ostream& out) const;
74
75private:
76 void computeWidth(const std::vector<std::string>& v);
77 std::vector<size_t> mWidths;
78 std::vector<TextTableRow> mTable;
79};
80
81} // namespace lshal
82} // namespace android
83
84#endif // FRAMEWORK_NATIVE_CMDS_LSHAL_TEXT_TABLE_H_