|
| 1 | +// Copyright 2025 The Abseil Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#ifndef ABSL_STRINGS_INTERNAL_GENERIC_PRINTER_H_ |
| 16 | +#define ABSL_STRINGS_INTERNAL_GENERIC_PRINTER_H_ |
| 17 | + |
| 18 | +#include "absl/strings/internal/generic_printer_internal.h" // IWYU pragma: export |
| 19 | + |
| 20 | +// Helpers for printing objects in generic code. |
| 21 | +// |
| 22 | +// These functions help with streaming in generic code. It may be desirable, for |
| 23 | +// example, to log values from generic functions; however, operator<< may not be |
| 24 | +// overloaded for all types. |
| 25 | +// |
| 26 | +// The helpers in this library use, in order of precedence: |
| 27 | +// |
| 28 | +// 1. std::string and std::string_view are quoted and escaped. (The specific |
| 29 | +// format is not guaranteed to be stable.) |
| 30 | +// 2. A defined AbslStringify() method. |
| 31 | +// 3. absl::log_internal::LogContainer, if the object is a STL container. |
| 32 | +// 4. For std::tuple, std::pair, and std::optional, recursively calls |
| 33 | +// GenericPrint for each element. |
| 34 | +// 5. Floating point values are printed with enough precision for round-trip. |
| 35 | +// 6. char values are quoted, with non-printable values escaped, and the |
| 36 | +// char's numeric value is included. |
| 37 | +// 7. A defined operator<< overload (which should be found by ADL). |
| 38 | +// 8. A defined DebugString() const method. |
| 39 | +// 9. A fallback value with basic information otherwise. |
| 40 | +// |
| 41 | +// Note that the fallback value means that if no formatting conversion is |
| 42 | +// defined, you will not see a compile-time error. This also means that |
| 43 | +// GenericPrint() can safely be used in generic template contexts, and can |
| 44 | +// format any types needed (even though the output will vary). |
| 45 | +// |
| 46 | +// Example usage: |
| 47 | +// |
| 48 | +// // All values after GenericPrint() are formatted: |
| 49 | +// LOG(INFO) << GenericPrint() |
| 50 | +// << int_var // <- printed normally |
| 51 | +// << float_var // <- sufficient precision for round-trip |
| 52 | +// << " unchanged literal text "; |
| 53 | +// |
| 54 | +// // Just one value is formatted: |
| 55 | +// LOG(INFO) << GenericPrint(string("this is quoted and escaped\t\n")) |
| 56 | +// << GenericPrint("but not this, "); |
| 57 | +// << string("and not this."); |
| 58 | +// |
| 59 | +// To make a type loggable with GenericPrint, prefer defining operator<< as a |
| 60 | +// friend function. For example: |
| 61 | +// |
| 62 | +// class TypeToLog { |
| 63 | +// public: |
| 64 | +// string ToString() const; // Many types already implement this. |
| 65 | +// // Define out-of-line if it is complex. |
| 66 | +// friend std::ostream& operator<<(std::ostream& os, const TypeToLog& v) { |
| 67 | +// return os << v.ToString(); // OK to define in-line instead, if simple. |
| 68 | +// } |
| 69 | +// }; |
| 70 | +// |
| 71 | +// (Defining operator<< as an inline friend free function allows it to be found |
| 72 | +// by Argument-Dependent Lookup, or ADL, which is the mechanism typically used |
| 73 | +// for operator overload resolution. An inline friend function is the tightest |
| 74 | +// scope possible for overloading the left-hand side of an operator.) |
| 75 | + |
| 76 | +#include <ostream> |
| 77 | +#include <utility> |
| 78 | + |
| 79 | +namespace absl { |
| 80 | +ABSL_NAMESPACE_BEGIN |
| 81 | +namespace strings_internal { |
| 82 | + |
| 83 | +// Helper for logging values in generic code. |
| 84 | +// |
| 85 | +// Example usage: |
| 86 | +// |
| 87 | +// template <typename T> |
| 88 | +// void GenericFunction() { |
| 89 | +// T v1, v2; |
| 90 | +// VLOG(1) << GenericPrint() << v1 << v2; // GenericPrint everything |
| 91 | +// VLOG(1) << GenericPrint(v1) << v2; // GenericPrint just v1 |
| 92 | +// } |
| 93 | +// |
| 94 | +inline constexpr internal_generic_printer::GenericPrintAdapterFactory |
| 95 | + GenericPrint{}; |
| 96 | + |
| 97 | +// Generic printer type: this class can be used, for example, as a template |
| 98 | +// argument to allow users to provide alternative printing strategies. |
| 99 | +// |
| 100 | +// For example, to allow callers to provide a custom strategy: |
| 101 | +// |
| 102 | +// template <typename T, typename PrinterT = GenericPrinter<T>> |
| 103 | +// void GenericFunction() { |
| 104 | +// T value; |
| 105 | +// VLOG(1) << PrinterT{value}; |
| 106 | +// } |
| 107 | +// |
| 108 | +template <typename T> |
| 109 | +using GenericPrinter = internal_generic_printer::GenericPrinter<T>; |
| 110 | + |
| 111 | +} // namespace strings_internal |
| 112 | +ABSL_NAMESPACE_END |
| 113 | +} // namespace absl |
| 114 | + |
| 115 | +#endif // ABSL_STRINGS_INTERNAL_GENERIC_PRINTER_H_ |
0 commit comments