blob: 63c0e40e4e6628a917a23c57b922c04bae968205 [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/**
Harry Cutts8c7cb592023-08-23 17:20:13 +000078 * 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 -080079 */
Harry Cutts8c7cb592023-08-23 17:20:13 +000080template <typename T>
81std::string dumpMap(const T& map,
82 std::string (*keyToString)(const typename T::key_type&) = constToString,
83 std::string (*valueToString)(const typename T::mapped_type&) = constToString) {
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -080084 std::string out;
85 for (const auto& [k, v] : map) {
86 if (!out.empty()) {
87 out += "\n";
88 }
89 out += keyToString(k) + ":" + valueToString(v);
90 }
91 return out;
92}
93
Harry Cuttsea73eaa2023-01-16 17:55:46 +000094/**
Prabir Pradhan852db892023-04-06 22:16:44 +000095 * Convert map keys to string. The keys of the map should be integral type.
96 */
97template <typename K, typename V>
98std::string dumpMapKeys(const std::map<K, V>& map,
99 std::string (*keyToString)(const K&) = constToString) {
100 std::string out;
101 for (const auto& [k, _] : map) {
102 out += out.empty() ? "{" : ", ";
103 out += keyToString(k);
104 }
105 return out.empty() ? "{}" : (out + "}");
106}
107
Harry Cutts8c7cb592023-08-23 17:20:13 +0000108/** Convert a vector to a string. */
Harry Cuttsea73eaa2023-01-16 17:55:46 +0000109template <typename T>
Harry Cutts8c7cb592023-08-23 17:20:13 +0000110std::string dumpVector(const std::vector<T>& values,
111 std::string (*valueToString)(const T&) = constToString) {
112 std::string dump = valueToString(values[0]);
Harry Cuttsea73eaa2023-01-16 17:55:46 +0000113 for (size_t i = 1; i < values.size(); i++) {
Harry Cutts8c7cb592023-08-23 17:20:13 +0000114 dump += ", " + valueToString(values[i]);
Harry Cuttsea73eaa2023-01-16 17:55:46 +0000115 }
116 return dump;
117}
118
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -0800119const char* toString(bool value);
120
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700121/**
122 * Add "prefix" to the beginning of each line in the provided string
123 * "str".
124 * The string 'str' is typically multi-line.
125 * The most common use case for this function is to add some padding
126 * when dumping state.
127 */
128std::string addLinePrefix(std::string str, const std::string& prefix);
129
Harry Cuttsea73eaa2023-01-16 17:55:46 +0000130} // namespace android