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