1 #ifndef ABSL_STRINGS_INTERNAL_STR_FORMAT_ARG_H_ 2 #define ABSL_STRINGS_INTERNAL_STR_FORMAT_ARG_H_ 13 #include <type_traits> 26 class FormatCountCapture;
31 template <
typename T,
typename =
void>
36 T,
void_t<decltype(AbslFormatConvert(
37 std::declval<const T&>(), std::declval<ConversionSpec>(),
38 std::declval<FormatSink*>()))>> : std::true_type {};
51 decltype(reinterpret_cast<uintptr_t>(std::declval<T*>())) = 0>
53 :
value(ptr ? reinterpret_cast<uintptr_t>(ptr) : 0) {}
68 template <
class AbslCord,
69 typename std::enable_if<
71 class AbslCordReader = ::CordReader>
78 size_t space_remaining = 0;
80 int width = conv.
width();
81 if (width >= 0) space_remaining = width;
83 size_t to_write = value.size();
87 to_write = (std::min)(to_write, static_cast<size_t>(precision));
89 space_remaining =
Excess(to_write, space_remaining);
91 if (space_remaining > 0 && !is_left) sink->
Append(space_remaining,
' ');
94 for (AbslCordReader reader(value);
95 to_write > 0 && reader.ReadFragment(&piece); to_write -= piece.
size()) {
100 if (space_remaining > 0 && is_left) sink->
Append(space_remaining,
' ');
157 template <
typename T>
163 template <
typename T>
167 std::ostringstream oss;
169 if (!oss)
return {
false};
176 template <
class T =
int>
184 *v2.p_ =
static_cast<int>(sink->
size());
189 template <
class T =
int>
199 template <
typename Arg>
203 return arg.dispatcher_(arg.data_, {}, out);
206 template <
typename Arg>
209 return arg.dispatcher_(arg.data_, conv, out);
212 template <
typename Arg>
214 return arg.dispatcher_;
221 enum { kInlinedSpace = 8 };
233 template <
typename T>
235 : std::integral_constant<bool, (sizeof(T) <= kInlinedSpace) &&
236 (std::is_integral<T>::value ||
237 std::is_floating_point<T>::value ||
238 std::is_pointer<T>::value ||
239 std::is_same<VoidPtr, T>::value)> {};
241 enum StoragePolicy { ByPointer, ByVolatilePointer, ByValue };
242 template <typename T>
243 struct storage_policy
244 : std::integral_constant<StoragePolicy,
245 (std::is_volatile<T>::value
247 : (store_by_value<T>::value ? ByValue
258 template <typename T, typename = void>
260 static constexpr bool kHasUserDefined =
261 str_format_internal::HasUserDefinedConvert<T>::value;
262 using type = typename std::conditional<
263 !kHasUserDefined && std::is_convertible<T, const char*>::value,
265 typename std::conditional<!kHasUserDefined &&
266 std::is_convertible<T, VoidPtr>::value,
267 VoidPtr, const T&>::type>::type;
269 template <typename T>
271 typename std::enable_if<
272 !str_format_internal::HasUserDefinedConvert<T>::value &&
273 std::is_enum<T>::value>::type> {
274 using type = typename std::underlying_type<T>::type;
278 template <typename T>
279 explicit FormatArgImpl(const T& value) {
280 using D = typename DecayType<T>::type;
282 std::is_same<D, const T&>::value || storage_policy<D>::value == ByValue,
283 "Decayed types must be stored by value");
284 Init(static_cast<D>(value));
288 friend struct str_format_internal::FormatArgImplFriend;
289 template <typename T, StoragePolicy = storage_policy<T>::value>
292 template <typename T>
293 struct Manager<T, ByPointer> {
294 static Data SetValue(const T& value) {
296 data.ptr = std::addressof(value);
300 static const T& Value(Data arg) { return *static_cast<const T*>(arg.ptr); }
303 template <typename T>
304 struct Manager<T, ByVolatilePointer> {
305 static Data SetValue(const T& value) {
307 data.volatile_ptr = &value;
311 static const T& Value(Data arg) {
312 return *static_cast<const T*>(arg.volatile_ptr);
316 template <typename T>
317 struct Manager<T, ByValue> {
318 static Data SetValue(const T& value) {
320 memcpy(data.buf, &value, sizeof(value));
324 static T Value(Data arg) {
326 memcpy(&value, arg.buf, sizeof(T));
331 template <typename T>
332 void Init(const T& value) {
333 data_ = Manager<T>::SetValue(value);
334 dispatcher_ = &Dispatch<T>;
337 template <typename T>
338 static int ToIntVal(const T& val) {
339 using CommonType = typename std::conditional<std::is_signed<T>::value,
340 int64_t, uint64_t>::type;
341 if (static_cast<CommonType>(val) >
342 static_cast<CommonType>((std::numeric_limits<int>::max)())) {
343 return (std::numeric_limits<int>::max)();
345 static_cast<CommonType>(val) <
346 static_cast<CommonType
>((std::numeric_limits<int>::min)())) {
347 return (std::numeric_limits<int>::min)();
349 return static_cast<int>(val);
352 template <
typename T>
359 template <
typename T>
362 *out = ToIntVal(
static_cast<typename std::underlying_type<T>::type
>(
367 template <
typename T>
368 static bool ToInt(
Data,
int*, std::false_type, std::false_type) {
372 template <
typename T>
376 return ToInt<T>(
arg,
static_cast<int*
>(
out), std::is_integral<T>(),
389 #define ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(T, E) \ 390 E template bool FormatArgImpl::Dispatch<T>(Data, ConversionSpec, void*) 392 #define ABSL_INTERNAL_FORMAT_DISPATCH_OVERLOADS_EXPAND_(...) \ 393 ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(str_format_internal::VoidPtr, \ 395 ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(bool, __VA_ARGS__); \ 396 ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(char, __VA_ARGS__); \ 397 ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(signed char, __VA_ARGS__); \ 398 ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(unsigned char, __VA_ARGS__); \ 399 ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(short, __VA_ARGS__); \ 400 ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(unsigned short, \ 402 ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(int, __VA_ARGS__); \ 403 ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(unsigned int, __VA_ARGS__); \ 404 ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(long, __VA_ARGS__); \ 405 ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(unsigned long, \ 407 ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(long long, \ 409 ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(unsigned long long, \ 411 ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(uint128, __VA_ARGS__); \ 412 ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(float, __VA_ARGS__); \ 413 ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(double, __VA_ARGS__); \ 414 ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(long double, __VA_ARGS__); \ 415 ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(const char*, __VA_ARGS__); \ 416 ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(std::string, __VA_ARGS__); \ 417 ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(string_view, __VA_ARGS__) 424 #endif // ABSL_STRINGS_INTERNAL_STR_FORMAT_ARG_H_
#define ABSL_PREDICT_FALSE(x)
void remove_suffix(size_type n)
constexpr size_type size() const noexcept
typename std::enable_if< B, T >::type enable_if_t
typename type_traits_internal::VoidTImpl< Ts... >::type void_t