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 17 #pragma once 18 19 #include <iostream> 20 #include <string> 21 #include <vector> 22 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. TextTableRow()32 TextTableRow() {} 33 34 // A row of cells. TextTableRow(std::vector<std::string> && v)35 explicit TextTableRow(std::vector<std::string>&& v) : mFields(std::move(v)) {} 36 37 // A single comment string. TextTableRow(std::string && s)38 explicit TextTableRow(std::string&& s) : mLine(std::move(s)) {} TextTableRow(const std::string & s)39 explicit TextTableRow(const std::string& s) : mLine(s) {} 40 41 // Whether this row is an actual row of cells. isRow()42 bool isRow() const { return !fields().empty(); } 43 44 // Get all cells. fields()45 const std::vector<std::string>& fields() const { return mFields; } 46 47 // Get the single comment string. line()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. add()60 void add() { mTable.emplace_back(); } add(std::vector<std::string> && v)61 void add(std::vector<std::string>&& v) { 62 computeWidth(v); 63 mTable.emplace_back(std::move(v)); 64 } add(const std::string & s)65 void add(const std::string& s) { mTable.emplace_back(s); } add(std::string && s)66 void add(std::string&& s) { mTable.emplace_back(std::move(s)); } 67 68 void addAll(TextTable&& other); 69 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 82