blob: 0ca6fa30ce3f3b86082bb5abbf6fba27250c6563 [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) {
30 return bitset.to_string();
31}
32
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -080033template <typename T>
Prabir Pradhan07525ef2022-10-03 21:51:26 +000034inline std::string constToString(const T& v) {
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -080035 return std::to_string(v);
36}
37
Harry Cuttsea73eaa2023-01-16 17:55:46 +000038template <>
39inline std::string constToString(const bool& value) {
40 return value ? "true" : "false";
41}
42
43template <>
44inline std::string constToString(const std::vector<bool>::reference& value) {
45 return value ? "true" : "false";
46}
47
Prabir Pradhan07525ef2022-10-03 21:51:26 +000048inline std::string constToString(const std::string& s) {
49 return s;
50}
51
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -080052/**
Prabir Pradhanc13ff082022-09-08 22:03:30 +000053 * Convert an optional type to string.
54 */
55template <typename T>
Prabir Pradhan07525ef2022-10-03 21:51:26 +000056inline std::string toString(const std::optional<T>& optional,
57 std::string (*toString)(const T&) = constToString) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +000058 return optional ? toString(*optional) : "<not set>";
59}
60
61/**
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -080062 * Convert a set of integral types to string.
63 */
64template <typename T>
65std::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 */
77template <typename K, typename V>
78std::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 Cuttsea73eaa2023-01-16 17:55:46 +000090/**
Prabir Pradhan852db892023-04-06 22:16:44 +000091 * Convert map keys to string. The keys of the map should be integral type.
92 */
93template <typename K, typename V>
94std::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 Cuttsea73eaa2023-01-16 17:55:46 +0000105 * Convert a vector to a string. The values of the vector should be of a type supported by
106 * constToString.
107 */
108template <typename T>
109std::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 Vishniakoua6a660f2022-03-04 15:12:16 -0800117const char* toString(bool value);
118
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700119/**
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 */
126std::string addLinePrefix(std::string str, const std::string& prefix);
127
Harry Cuttsea73eaa2023-01-16 17:55:46 +0000128} // namespace android