blob: e24344b3f17e19612a0240d3aa3eb2e206430e82 [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>
23
24namespace android {
25
26template <typename T>
Prabir Pradhan07525ef2022-10-03 21:51:26 +000027inline std::string constToString(const T& v) {
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -080028 return std::to_string(v);
29}
30
Prabir Pradhan07525ef2022-10-03 21:51:26 +000031inline std::string constToString(const std::string& s) {
32 return s;
33}
34
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -080035/**
Prabir Pradhanc13ff082022-09-08 22:03:30 +000036 * Convert an optional type to string.
37 */
38template <typename T>
Prabir Pradhan07525ef2022-10-03 21:51:26 +000039inline std::string toString(const std::optional<T>& optional,
40 std::string (*toString)(const T&) = constToString) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +000041 return optional ? toString(*optional) : "<not set>";
42}
43
44/**
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -080045 * Convert a set of integral types to string.
46 */
47template <typename T>
48std::string dumpSet(const std::set<T>& v, std::string (*toString)(const T&) = constToString) {
49 std::string out;
50 for (const T& entry : v) {
51 out += out.empty() ? "{" : ", ";
52 out += toString(entry);
53 }
54 return out.empty() ? "{}" : (out + "}");
55}
56
57/**
58 * Convert a map to string. Both keys and values of the map should be integral type.
59 */
60template <typename K, typename V>
61std::string dumpMap(const std::map<K, V>& map, std::string (*keyToString)(const K&) = constToString,
62 std::string (*valueToString)(const V&) = constToString) {
63 std::string out;
64 for (const auto& [k, v] : map) {
65 if (!out.empty()) {
66 out += "\n";
67 }
68 out += keyToString(k) + ":" + valueToString(v);
69 }
70 return out;
71}
72
73const char* toString(bool value);
74
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -070075/**
76 * Add "prefix" to the beginning of each line in the provided string
77 * "str".
78 * The string 'str' is typically multi-line.
79 * The most common use case for this function is to add some padding
80 * when dumping state.
81 */
82std::string addLinePrefix(std::string str, const std::string& prefix);
83
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -080084} // namespace android