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