1 // Copyright 2023 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef BASE_STRINGS_TO_STRING_H_
6 #define BASE_STRINGS_TO_STRING_H_
7
8 #include <concepts>
9 #include <ios>
10 #include <memory>
11 #include <sstream>
12 #include <string>
13 #include <tuple>
14 #include <type_traits>
15 #include <utility>
16
17 #include "base/template_util.h"
18 #include "base/types/supports_ostream_operator.h"
19
20 namespace base {
21
22 template <typename... Ts>
23 std::string ToString(const Ts&... values);
24
25 namespace internal {
26
27 template <typename T>
requires(const T & t)28 concept SupportsToString = requires(const T& t) { t.ToString(); };
29
30 // I/O manipulators are function pointers, but should be sent directly to the
31 // `ostream` instead of being cast to `const void*` like other function
32 // pointers.
33 template <typename T>
34 constexpr bool IsIomanip = false;
35 template <typename T>
36 requires(std::derived_from<T, std::ios_base>)
37 constexpr bool IsIomanip<T&(T&)> = true;
38
39 // Function pointers implicitly convert to `bool`, so use this to avoid printing
40 // function pointers as 1 or 0.
41 template <typename T>
42 concept WillBeIncorrectlyStreamedAsBool =
43 std::is_function_v<std::remove_pointer_t<T>> &&
44 !IsIomanip<std::remove_pointer_t<T>>;
45
46 // Fallback case when there is no better representation.
47 template <typename T>
48 struct ToStringHelper {
StringifyToStringHelper49 static void Stringify(const T& v, std::ostringstream& ss) {
50 ss << "[" << sizeof(v) << "-byte object at 0x" << std::addressof(v) << "]";
51 }
52 };
53
54 // Most streamables.
55 template <typename T>
56 requires(SupportsOstreamOperator<const T&> &&
57 !WillBeIncorrectlyStreamedAsBool<T>)
58 struct ToStringHelper<T> {
59 static void Stringify(const T& v, std::ostringstream& ss) { ss << v; }
60 };
61
62 // Functions and function pointers.
63 template <typename T>
64 requires(SupportsOstreamOperator<const T&> &&
65 WillBeIncorrectlyStreamedAsBool<T>)
66 struct ToStringHelper<T> {
67 static void Stringify(const T& v, std::ostringstream& ss) {
68 ToStringHelper<const void*>::Stringify(reinterpret_cast<const void*>(v),
69 ss);
70 }
71 };
72
73 // Non-streamables that have a `ToString` member.
74 template <typename T>
75 requires(!SupportsOstreamOperator<const T&> && SupportsToString<const T&>)
76 struct ToStringHelper<T> {
77 static void Stringify(const T& v, std::ostringstream& ss) {
78 // .ToString() may not return a std::string, e.g. blink::WTF::String.
79 ToStringHelper<decltype(v.ToString())>::Stringify(v.ToString(), ss);
80 }
81 };
82
83 // Non-streamable enums (i.e. scoped enums where no `operator<<` overload was
84 // declared).
85 template <typename T>
86 requires(!SupportsOstreamOperator<const T&> && std::is_enum_v<T>)
87 struct ToStringHelper<T> {
88 static void Stringify(const T& v, std::ostringstream& ss) {
89 using UT = typename std::underlying_type_t<T>;
90 ToStringHelper<UT>::Stringify(static_cast<UT>(v), ss);
91 }
92 };
93
94 // Tuples. Will recursively apply `ToString()` to each value in the tuple.
95 template <typename... T>
96 struct ToStringHelper<std::tuple<T...>> {
97 template <size_t... I>
98 static void StringifyHelper(const std::tuple<T...>& values,
99 std::index_sequence<I...>,
100 std::ostringstream& ss) {
101 ss << "<";
102 (..., (ss << (I == 0 ? "" : ", "), ss << ToString(std::get<I>(values))));
103 ss << ">";
104 }
105
106 static void Stringify(const std::tuple<T...>& v, std::ostringstream& ss) {
107 StringifyHelper(v, std::make_index_sequence<sizeof...(T)>(), ss);
108 }
109 };
110
111 } // namespace internal
112
113 // Converts any type to a string, preferring defined operator<<() or ToString()
114 // methods if they exist. If multiple `values` are given, returns the
115 // concatenation of the result of applying `ToString` to each value.
116 template <typename... Ts>
117 std::string ToString(const Ts&... values) {
118 std::ostringstream ss;
119 (...,
120 internal::ToStringHelper<std::remove_cvref_t<decltype(values)>>::Stringify(
121 values, ss));
122 return ss.str();
123 }
124
125 } // namespace base
126
127 #endif // BASE_STRINGS_TO_STRING_H_
128