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-exception 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef _LIBCPP___FORMAT_FORMAT_PARSE_CONTEXT_H 11 #define _LIBCPP___FORMAT_FORMAT_PARSE_CONTEXT_H 12 13 #include <__config> 14 #include <__format/format_error.h> 15 #include <string_view> 16 17 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 18 #pragma GCC system_header 19 #endif 20 21 _LIBCPP_PUSH_MACROS 22 #include <__undef_macros> 23 24 _LIBCPP_BEGIN_NAMESPACE_STD 25 26 #if _LIBCPP_STD_VER > 17 27 28 // TODO FMT Remove this once we require compilers with proper C++20 support. 29 // If the compiler has no concepts support, the format header will be disabled. 30 // Without concepts support enable_if needs to be used and that too much effort 31 // to support compilers with partial C++20 support. 32 #if !defined(_LIBCPP_HAS_NO_CONCEPTS) 33 34 template <class _CharT> 35 class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT basic_format_parse_context { 36 public: 37 using char_type = _CharT; 38 using const_iterator = typename basic_string_view<_CharT>::const_iterator; 39 using iterator = const_iterator; 40 41 _LIBCPP_HIDE_FROM_ABI 42 constexpr explicit basic_format_parse_context(basic_string_view<_CharT> __fmt, 43 size_t __num_args = 0) noexcept 44 : __begin_(__fmt.begin()), 45 __end_(__fmt.end()), 46 __indexing_(__unknown), 47 __next_arg_id_(0), 48 __num_args_(__num_args) {} 49 50 basic_format_parse_context(const basic_format_parse_context&) = delete; 51 basic_format_parse_context& 52 operator=(const basic_format_parse_context&) = delete; 53 54 _LIBCPP_HIDE_FROM_ABI constexpr const_iterator begin() const noexcept { 55 return __begin_; 56 } 57 _LIBCPP_HIDE_FROM_ABI constexpr const_iterator end() const noexcept { 58 return __end_; 59 } 60 _LIBCPP_HIDE_FROM_ABI constexpr void advance_to(const_iterator __it) { 61 __begin_ = __it; 62 } 63 64 _LIBCPP_HIDE_FROM_ABI constexpr size_t next_arg_id() { 65 if (__indexing_ == __manual) 66 __throw_format_error("Using automatic argument numbering in manual " 67 "argument numbering mode"); 68 69 if (__indexing_ == __unknown) 70 __indexing_ = __automatic; 71 return __next_arg_id_++; 72 } 73 _LIBCPP_HIDE_FROM_ABI constexpr void check_arg_id(size_t __id) { 74 if (__indexing_ == __automatic) 75 __throw_format_error("Using manual argument numbering in automatic " 76 "argument numbering mode"); 77 78 if (__indexing_ == __unknown) 79 __indexing_ = __manual; 80 81 // Throws an exception to make the expression a non core constant 82 // expression as required by: 83 // [format.parse.ctx]/11 84 // Remarks: Call expressions where id >= num_args_ are not core constant 85 // expressions ([expr.const]). 86 // Note: the Throws clause [format.parse.ctx]/10 doesn't specify the 87 // behavior when id >= num_args_. 88 if (is_constant_evaluated() && __id >= __num_args_) 89 __throw_format_error("Argument index outside the valid range"); 90 } 91 92 private: 93 iterator __begin_; 94 iterator __end_; 95 enum _Indexing { __unknown, __manual, __automatic }; 96 _Indexing __indexing_; 97 size_t __next_arg_id_; 98 size_t __num_args_; 99 }; 100 101 using format_parse_context = basic_format_parse_context<char>; 102 using wformat_parse_context = basic_format_parse_context<wchar_t>; 103 104 #endif // !defined(_LIBCPP_HAS_NO_CONCEPTS) 105 106 #endif //_LIBCPP_STD_VER > 17 107 108 _LIBCPP_END_NAMESPACE_STD 109 110 _LIBCPP_POP_MACROS 111 112 #endif // _LIBCPP___FORMAT_FORMAT_PARSE_CONTEXT_H 113