blob: a20544b5fd33669d4e983b7eb87f10371d494839 [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>
24
25namespace android {
26
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000027template <size_t N>
28std::string bitsetToString(const std::bitset<N>& bitset) {
29 return bitset.to_string();
30}
31
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -080032template <typename T>
Prabir Pradhan07525ef2022-10-03 21:51:26 +000033inline std::string constToString(const T& v) {
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -080034 return std::to_string(v);
35}
36
Prabir Pradhan07525ef2022-10-03 21:51:26 +000037inline std::string constToString(const std::string& s) {
38 return s;
39}
40
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -080041/**
Prabir Pradhanc13ff082022-09-08 22:03:30 +000042 * Convert an optional type to string.
43 */
44template <typename T>
Prabir Pradhan07525ef2022-10-03 21:51:26 +000045inline std::string toString(const std::optional<T>& optional,
46 std::string (*toString)(const T&) = constToString) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +000047 return optional ? toString(*optional) : "<not set>";
48}
49
50/**
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -080051 * Convert a set of integral types to string.
52 */
53template <typename T>
54std::string dumpSet(const std::set<T>& v, std::string (*toString)(const T&) = constToString) {
55 std::string out;
56 for (const T& entry : v) {
57 out += out.empty() ? "{" : ", ";
58 out += toString(entry);
59 }
60 return out.empty() ? "{}" : (out + "}");
61}
62
63/**
64 * Convert a map to string. Both keys and values of the map should be integral type.
65 */
66template <typename K, typename V>
67std::string dumpMap(const std::map<K, V>& map, std::string (*keyToString)(const K&) = constToString,
68 std::string (*valueToString)(const V&) = constToString) {
69 std::string out;
70 for (const auto& [k, v] : map) {
71 if (!out.empty()) {
72 out += "\n";
73 }
74 out += keyToString(k) + ":" + valueToString(v);
75 }
76 return out;
77}
78
79const char* toString(bool value);
80
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -070081/**
82 * Add "prefix" to the beginning of each line in the provided string
83 * "str".
84 * The string 'str' is typically multi-line.
85 * The most common use case for this function is to add some padding
86 * when dumping state.
87 */
88std::string addLinePrefix(std::string str, const std::string& prefix);
89
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -080090} // namespace android