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