blob: ce8717009ec0da05bccd86d2e393748565fd787b [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
19#include <map>
Prabir Pradhanc13ff082022-09-08 22:03:30 +000020#include <optional>
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -080021#include <set>
22#include <string>
Harry Cuttsea73eaa2023-01-16 17:55:46 +000023#include <vector>
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -080024
25namespace android {
26
27template <typename T>
Prabir Pradhan07525ef2022-10-03 21:51:26 +000028inline std::string constToString(const T& v) {
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -080029 return std::to_string(v);
30}
31
Harry Cuttsea73eaa2023-01-16 17:55:46 +000032template <>
33inline std::string constToString(const bool& value) {
34 return value ? "true" : "false";
35}
36
37template <>
38inline std::string constToString(const std::vector<bool>::reference& value) {
39 return value ? "true" : "false";
40}
41
Prabir Pradhan07525ef2022-10-03 21:51:26 +000042inline std::string constToString(const std::string& s) {
43 return s;
44}
45
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -080046/**
Prabir Pradhanc13ff082022-09-08 22:03:30 +000047 * Convert an optional type to string.
48 */
49template <typename T>
Prabir Pradhan07525ef2022-10-03 21:51:26 +000050inline std::string toString(const std::optional<T>& optional,
51 std::string (*toString)(const T&) = constToString) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +000052 return optional ? toString(*optional) : "<not set>";
53}
54
55/**
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -080056 * Convert a set of integral types to string.
57 */
58template <typename T>
59std::string dumpSet(const std::set<T>& v, std::string (*toString)(const T&) = constToString) {
60 std::string out;
61 for (const T& entry : v) {
62 out += out.empty() ? "{" : ", ";
63 out += toString(entry);
64 }
65 return out.empty() ? "{}" : (out + "}");
66}
67
68/**
69 * Convert a map to string. Both keys and values of the map should be integral type.
70 */
71template <typename K, typename V>
72std::string dumpMap(const std::map<K, V>& map, std::string (*keyToString)(const K&) = constToString,
73 std::string (*valueToString)(const V&) = constToString) {
74 std::string out;
75 for (const auto& [k, v] : map) {
76 if (!out.empty()) {
77 out += "\n";
78 }
79 out += keyToString(k) + ":" + valueToString(v);
80 }
81 return out;
82}
83
Harry Cuttsea73eaa2023-01-16 17:55:46 +000084/**
85 * Convert a vector to a string. The values of the vector should be of a type supported by
86 * constToString.
87 */
88template <typename T>
89std::string dumpVector(std::vector<T> values) {
90 std::string dump = constToString(values[0]);
91 for (size_t i = 1; i < values.size(); i++) {
92 dump += ", " + constToString(values[i]);
93 }
94 return dump;
95}
96
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -080097const char* toString(bool value);
98
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -070099/**
100 * Add "prefix" to the beginning of each line in the provided string
101 * "str".
102 * The string 'str' is typically multi-line.
103 * The most common use case for this function is to add some padding
104 * when dumping state.
105 */
106std::string addLinePrefix(std::string str, const std::string& prefix);
107
Harry Cuttsea73eaa2023-01-16 17:55:46 +0000108} // namespace android