blob: 3470be4dce72bf9a758f01afd9d228b7f167936d [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>
Siarhei Vishniakou366fb5b2023-12-06 11:23:41 -080023#include <sstream>
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -080024#include <string>
Harry Cuttsea73eaa2023-01-16 17:55:46 +000025#include <vector>
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -080026
27namespace android {
28
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000029template <size_t N>
30std::string bitsetToString(const std::bitset<N>& bitset) {
Siarhei Vishniakou0836a302023-05-03 13:54:30 -070031 if (bitset.none()) {
32 return "<none>";
33 }
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000034 return bitset.to_string();
35}
36
Siarhei Vishniakou366fb5b2023-12-06 11:23:41 -080037template <class T>
38std::string streamableToString(const T& streamable) {
39 std::stringstream out;
40 out << streamable;
41 return out.str();
42}
43
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -080044template <typename T>
Prabir Pradhan07525ef2022-10-03 21:51:26 +000045inline std::string constToString(const T& v) {
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -080046 return std::to_string(v);
47}
48
Harry Cuttsea73eaa2023-01-16 17:55:46 +000049template <>
50inline std::string constToString(const bool& value) {
51 return value ? "true" : "false";
52}
53
54template <>
55inline std::string constToString(const std::vector<bool>::reference& value) {
56 return value ? "true" : "false";
57}
58
Prabir Pradhan07525ef2022-10-03 21:51:26 +000059inline std::string constToString(const std::string& s) {
60 return s;
61}
62
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -080063/**
Prabir Pradhanc13ff082022-09-08 22:03:30 +000064 * Convert an optional type to string.
65 */
66template <typename T>
Prabir Pradhan07525ef2022-10-03 21:51:26 +000067inline std::string toString(const std::optional<T>& optional,
68 std::string (*toString)(const T&) = constToString) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +000069 return optional ? toString(*optional) : "<not set>";
70}
71
72/**
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -080073 * Convert a set of integral types to string.
74 */
75template <typename T>
76std::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 Cutts8c7cb592023-08-23 17:20:13 +000086 * Convert a map or multimap to string. Both keys and values of the map should be integral type.
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -080087 */
Harry Cutts8c7cb592023-08-23 17:20:13 +000088template <typename T>
89std::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 Vishniakoua6a660f2022-03-04 15:12:16 -080092 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 Cuttsea73eaa2023-01-16 17:55:46 +0000102/**
Prabir Pradhan852db892023-04-06 22:16:44 +0000103 * Convert map keys to string. The keys of the map should be integral type.
104 */
105template <typename K, typename V>
106std::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 Cutts8c7cb592023-08-23 17:20:13 +0000116/** Convert a vector to a string. */
Harry Cuttsea73eaa2023-01-16 17:55:46 +0000117template <typename T>
Harry Cutts8c7cb592023-08-23 17:20:13 +0000118std::string dumpVector(const std::vector<T>& values,
119 std::string (*valueToString)(const T&) = constToString) {
Siarhei Vishniakou6742ee92023-12-15 14:24:59 -0800120 std::string out;
121 for (const auto& value : values) {
122 out += out.empty() ? "[" : ", ";
123 out += valueToString(value);
Harry Cuttsea73eaa2023-01-16 17:55:46 +0000124 }
Siarhei Vishniakou6742ee92023-12-15 14:24:59 -0800125 return out.empty() ? "[]" : (out + "]");
Harry Cuttsea73eaa2023-01-16 17:55:46 +0000126}
127
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -0800128const char* toString(bool value);
129
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700130/**
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 */
137std::string addLinePrefix(std::string str, const std::string& prefix);
138
Harry Cuttsea73eaa2023-01-16 17:55:46 +0000139} // namespace android