81 lines · c
1// -*- C++ -*-2//===----------------------------------------------------------------------===//3//4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.5// See https://llvm.org/LICENSE.txt for license information.6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCPP___FORMAT_FORMATTER_BOOL_H11#define _LIBCPP___FORMAT_FORMATTER_BOOL_H12 13#include <__algorithm/copy.h>14#include <__assert>15#include <__config>16#include <__format/concepts.h>17#include <__format/format_parse_context.h>18#include <__format/formatter.h>19#include <__format/formatter_integral.h>20#include <__format/parser_std_format_spec.h>21#include <__utility/unreachable.h>22 23#if _LIBCPP_HAS_LOCALIZATION24# include <__locale>25#endif26 27#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)28# pragma GCC system_header29#endif30 31_LIBCPP_BEGIN_NAMESPACE_STD32 33#if _LIBCPP_STD_VER >= 2034 35template <__fmt_char_type _CharT>36struct formatter<bool, _CharT> {37public:38 template <class _ParseContext>39 _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {40 typename _ParseContext::iterator __result = __parser_.__parse(__ctx, __format_spec::__fields_integral);41 __format_spec::__process_parsed_bool(__parser_, "a bool");42 return __result;43 }44 45 template <class _FormatContext>46 _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(bool __value, _FormatContext& __ctx) const {47 switch (__parser_.__type_) {48 case __format_spec::__type::__default:49 case __format_spec::__type::__string:50 return __formatter::__format_bool(__value, __ctx, __parser_.__get_parsed_std_specifications(__ctx));51 52 case __format_spec::__type::__binary_lower_case:53 case __format_spec::__type::__binary_upper_case:54 case __format_spec::__type::__octal:55 case __format_spec::__type::__decimal:56 case __format_spec::__type::__hexadecimal_lower_case:57 case __format_spec::__type::__hexadecimal_upper_case:58 // Promotes bool to an integral type. This reduces the number of59 // instantiations of __format_integer reducing code size.60 return __formatter::__format_integer(61 static_cast<unsigned>(__value), __ctx, __parser_.__get_parsed_std_specifications(__ctx));62 63 default:64 _LIBCPP_ASSERT_INTERNAL(false, "The parse function should have validated the type");65 __libcpp_unreachable();66 }67 }68 69 __format_spec::__parser<_CharT> __parser_;70};71 72# if _LIBCPP_STD_VER >= 2373template <>74inline constexpr bool enable_nonlocking_formatter_optimization<bool> = true;75# endif // _LIBCPP_STD_VER >= 2376#endif // _LIBCPP_STD_VER >= 2077 78_LIBCPP_END_NAMESPACE_STD79 80#endif // _LIBCPP___FORMAT_FORMATTER_BOOL_H81