Home | History | Annotate | Download | only in lshal
      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 #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 namespace android {
     25 namespace lshal {
     26 
     27 // An element in TextTable. This is either an actual row (an array of cells
     28 // in this row), or a string of explanatory text.
     29 // To see if this is an actual row, test fields().empty().
     30 class TextTableRow {
     31 public:
     32     // An empty line.
     33     TextTableRow() {}
     34 
     35     // A row of cells.
     36     TextTableRow(std::vector<std::string>&& v) : mFields(std::move(v)) {}
     37 
     38     // A single comment string.
     39     TextTableRow(std::string&& s) : mLine(std::move(s)) {}
     40     TextTableRow(const std::string& s) : mLine(s) {}
     41 
     42     // Whether this row is an actual row of cells.
     43     bool isRow() const { return !fields().empty(); }
     44 
     45     // Get all cells.
     46     const std::vector<std::string>& fields() const { return mFields; }
     47 
     48     // Get the single comment string.
     49     const std::string& line() const { return mLine; }
     50 
     51 private:
     52     std::vector<std::string> mFields;
     53     std::string mLine;
     54 };
     55 
     56 // A TextTable is a 2D array of strings.
     57 class TextTable {
     58 public:
     59 
     60     // Add a TextTableRow.
     61     void add() { mTable.emplace_back(); }
     62     void add(std::vector<std::string>&& v) {
     63         computeWidth(v);
     64         mTable.emplace_back(std::move(v));
     65     }
     66     void add(const std::string& s) { mTable.emplace_back(s); }
     67     void add(std::string&& s) { mTable.emplace_back(std::move(s)); }
     68 
     69     void addAll(TextTable&& other);
     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 
     75 private:
     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_
     85