blob: 0e3fbb1982244eb31955b82f3545260710c3f84a [file] [log] [blame]
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -08001/*
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 Vishniakoub581f7f2022-12-07 20:23:06 +000019#include <bitset>
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -080020#include <map>
Prabir Pradhanc13ff082022-09-08 22:03:30 +000021#include <optional>
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -080022#include <set>
23#include <string>
Harry Cuttsea73eaa2023-01-16 17:55:46 +000024#include <vector>
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -080025
26namespace android {
27
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000028template <size_t N>
29std::string bitsetToString(const std::bitset<N>& bitset) {
Siarhei Vishniakou0836a302023-05-03 13:54:30 -070030 if (bitset.none()) {
31 return "<none>";
32 }
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000033 return bitset.to_string();
34}
35
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -080036template <typename T>
Prabir Pradhan07525ef2022-10-03 21:51:26 +000037inline std::string constToString(const T& v) {
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -080038 return std::to_string(v);
39}
40
Harry Cuttsea73eaa2023-01-16 17:55:46 +000041template <>
42inline std::string constToString(const bool& value) {
43 return value ? "true" : "false";
44}
45
46template <>
47inline std::string constToString(const std::vector<bool>::reference& value) {
48 return value ? "true" : "false";
49}
50
Prabir Pradhan07525ef2022-10-03 21:51:26 +000051inline std::string constToString(const std::string& s) {
52 return s;
53}
54
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -080055/**
Prabir Pradhanc13ff082022-09-08 22:03:30 +000056 * Convert an optional type to string.
57 */
58template <typename T>
Prabir Pradhan07525ef2022-10-03 21:51:26 +000059inline std::string toString(const std::optional<T>& optional,
60 std::string (*toString)(const T&) = constToString) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +000061 return optional ? toString(*optional) : "<not set>";
62}
63
64/**
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -080065 * Convert a set of integral types to string.
66 */
67template <typename T>
68std::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 */
80template <typename K, typename V>
81std::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
Harry Cuttsea73eaa2023-01-16 17:55:46 +000093/**
Prabir Pradhan852db892023-04-06 22:16:44 +000094 * Convert map keys to string. The keys of the map should be integral type.
95 */
96template <typename K, typename V>
97std::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/**
Harry Cuttsea73eaa2023-01-16 17:55:46 +0000108 * Convert a vector to a string. The values of the vector should be of a type supported by
109 * constToString.
110 */
111template <typename T>
112std::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
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -0800120const char* toString(bool value);
121
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700122/**
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 */
129std::string addLinePrefix(std::string str, const std::string& prefix);
130
Harry Cuttsea73eaa2023-01-16 17:55:46 +0000131} // namespace android