Yifan Hong | 1bc1e9f | 2017-08-29 17:28:12 -0700 | [diff] [blame] | 1 | /* |
| 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 Moreland | 0b8e387 | 2019-06-25 08:54:34 -0700 | [diff] [blame] | 17 | #pragma once |
Yifan Hong | 1bc1e9f | 2017-08-29 17:28:12 -0700 | [diff] [blame] | 18 | |
| 19 | #include <iostream> |
| 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
Yifan Hong | 1bc1e9f | 2017-08-29 17:28:12 -0700 | [diff] [blame] | 23 | namespace android { |
| 24 | namespace 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(). |
| 29 | class TextTableRow { |
| 30 | public: |
| 31 | // An empty line. |
| 32 | TextTableRow() {} |
| 33 | |
| 34 | // A row of cells. |
Chih-Hung Hsieh | 45e31c7 | 2018-12-20 15:47:01 -0800 | [diff] [blame] | 35 | explicit TextTableRow(std::vector<std::string>&& v) : mFields(std::move(v)) {} |
Yifan Hong | 1bc1e9f | 2017-08-29 17:28:12 -0700 | [diff] [blame] | 36 | |
| 37 | // A single comment string. |
Chih-Hung Hsieh | 45e31c7 | 2018-12-20 15:47:01 -0800 | [diff] [blame] | 38 | explicit TextTableRow(std::string&& s) : mLine(std::move(s)) {} |
| 39 | explicit TextTableRow(const std::string& s) : mLine(s) {} |
Yifan Hong | 1bc1e9f | 2017-08-29 17:28:12 -0700 | [diff] [blame] | 40 | |
| 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 | |
| 50 | private: |
| 51 | std::vector<std::string> mFields; |
| 52 | std::string mLine; |
| 53 | }; |
| 54 | |
| 55 | // A TextTable is a 2D array of strings. |
| 56 | class TextTable { |
| 57 | public: |
| 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 Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 68 | void addAll(TextTable&& other); |
| 69 | |
Yifan Hong | 1bc1e9f | 2017-08-29 17:28:12 -0700 | [diff] [blame] | 70 | // Prints the table to out, with column widths adjusted appropriately according |
| 71 | // to the content. |
| 72 | void dump(std::ostream& out) const; |
| 73 | |
| 74 | private: |
| 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 |