Siarhei Vishniakou | a6a660f | 2022-03-04 15:12:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 <map> |
Prabir Pradhan | c13ff08 | 2022-09-08 22:03:30 +0000 | [diff] [blame] | 20 | #include <optional> |
Siarhei Vishniakou | a6a660f | 2022-03-04 15:12:16 -0800 | [diff] [blame] | 21 | #include <set> |
| 22 | #include <string> |
Harry Cutts | ea73eaa | 2023-01-16 17:55:46 +0000 | [diff] [blame^] | 23 | #include <vector> |
Siarhei Vishniakou | a6a660f | 2022-03-04 15:12:16 -0800 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | |
| 27 | template <typename T> |
Prabir Pradhan | 07525ef | 2022-10-03 21:51:26 +0000 | [diff] [blame] | 28 | inline std::string constToString(const T& v) { |
Siarhei Vishniakou | a6a660f | 2022-03-04 15:12:16 -0800 | [diff] [blame] | 29 | return std::to_string(v); |
| 30 | } |
| 31 | |
Harry Cutts | ea73eaa | 2023-01-16 17:55:46 +0000 | [diff] [blame^] | 32 | template <> |
| 33 | inline std::string constToString(const bool& value) { |
| 34 | return value ? "true" : "false"; |
| 35 | } |
| 36 | |
| 37 | template <> |
| 38 | inline std::string constToString(const std::vector<bool>::reference& value) { |
| 39 | return value ? "true" : "false"; |
| 40 | } |
| 41 | |
Prabir Pradhan | 07525ef | 2022-10-03 21:51:26 +0000 | [diff] [blame] | 42 | inline std::string constToString(const std::string& s) { |
| 43 | return s; |
| 44 | } |
| 45 | |
Siarhei Vishniakou | a6a660f | 2022-03-04 15:12:16 -0800 | [diff] [blame] | 46 | /** |
Prabir Pradhan | c13ff08 | 2022-09-08 22:03:30 +0000 | [diff] [blame] | 47 | * Convert an optional type to string. |
| 48 | */ |
| 49 | template <typename T> |
Prabir Pradhan | 07525ef | 2022-10-03 21:51:26 +0000 | [diff] [blame] | 50 | inline std::string toString(const std::optional<T>& optional, |
| 51 | std::string (*toString)(const T&) = constToString) { |
Prabir Pradhan | c13ff08 | 2022-09-08 22:03:30 +0000 | [diff] [blame] | 52 | return optional ? toString(*optional) : "<not set>"; |
| 53 | } |
| 54 | |
| 55 | /** |
Siarhei Vishniakou | a6a660f | 2022-03-04 15:12:16 -0800 | [diff] [blame] | 56 | * Convert a set of integral types to string. |
| 57 | */ |
| 58 | template <typename T> |
| 59 | std::string dumpSet(const std::set<T>& v, std::string (*toString)(const T&) = constToString) { |
| 60 | std::string out; |
| 61 | for (const T& entry : v) { |
| 62 | out += out.empty() ? "{" : ", "; |
| 63 | out += toString(entry); |
| 64 | } |
| 65 | return out.empty() ? "{}" : (out + "}"); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Convert a map to string. Both keys and values of the map should be integral type. |
| 70 | */ |
| 71 | template <typename K, typename V> |
| 72 | std::string dumpMap(const std::map<K, V>& map, std::string (*keyToString)(const K&) = constToString, |
| 73 | std::string (*valueToString)(const V&) = constToString) { |
| 74 | std::string out; |
| 75 | for (const auto& [k, v] : map) { |
| 76 | if (!out.empty()) { |
| 77 | out += "\n"; |
| 78 | } |
| 79 | out += keyToString(k) + ":" + valueToString(v); |
| 80 | } |
| 81 | return out; |
| 82 | } |
| 83 | |
Harry Cutts | ea73eaa | 2023-01-16 17:55:46 +0000 | [diff] [blame^] | 84 | /** |
| 85 | * Convert a vector to a string. The values of the vector should be of a type supported by |
| 86 | * constToString. |
| 87 | */ |
| 88 | template <typename T> |
| 89 | std::string dumpVector(std::vector<T> values) { |
| 90 | std::string dump = constToString(values[0]); |
| 91 | for (size_t i = 1; i < values.size(); i++) { |
| 92 | dump += ", " + constToString(values[i]); |
| 93 | } |
| 94 | return dump; |
| 95 | } |
| 96 | |
Siarhei Vishniakou | a6a660f | 2022-03-04 15:12:16 -0800 | [diff] [blame] | 97 | const char* toString(bool value); |
| 98 | |
Siarhei Vishniakou | 9f330c5 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 99 | /** |
| 100 | * Add "prefix" to the beginning of each line in the provided string |
| 101 | * "str". |
| 102 | * The string 'str' is typically multi-line. |
| 103 | * The most common use case for this function is to add some padding |
| 104 | * when dumping state. |
| 105 | */ |
| 106 | std::string addLinePrefix(std::string str, const std::string& prefix); |
| 107 | |
Harry Cutts | ea73eaa | 2023-01-16 17:55:46 +0000 | [diff] [blame^] | 108 | } // namespace android |