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