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