BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslfmt_formatparsecontext.h
Go to the documentation of this file.
1/// @file bslfmt_formatparsecontext.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslfmt_formatparsecontext.h -*-C++-*-
8
9#ifndef INCLUDED_BSLFMT_FORMATPARSECONTEXT
10#define INCLUDED_BSLFMT_FORMATPARSECONTEXT
11
12#include <bsls_ident.h>
13BSLS_IDENT("$Id: $")
14
15/// @defgroup bslfmt_formatparsecontext bslfmt_formatparsecontext
16/// @brief Provides access to formatting parsing string and parsing state.
17/// @addtogroup bsl
18/// @{
19/// @addtogroup bslfmt
20/// @{
21/// @addtogroup bslfmt_formatparsecontext
22/// @{
23///
24/// <h1> Outline </h1>
25/// * <a href="#bslfmt_formatparsecontext-purpose"> Purpose</a>
26/// * <a href="#bslfmt_formatparsecontext-classes"> Classes </a>
27/// * <a href="#bslfmt_formatparsecontext-canonical-header"> Canonical Header </a>
28/// * <a href="#bslfmt_formatparsecontext-description"> Description </a>
29///
30/// # Purpose {#bslfmt_formatparsecontext-purpose}
31/// Provides access to formatting parsing string and parsing state.
32///
33/// # Classes {#bslfmt_formatparsecontext-classes}
34///
35/// - bslfmt::basic_format_parse_context: compliant format spec parse context
36///
37/// # Canonical Header {#bslfmt_formatparsecontext-canonical-header}
38/// bsl_format.h
39///
40/// # Description {#bslfmt_formatparsecontext-description}
41/// This component provides an implementation of the C++20 Standard
42/// Library's `std::basic_format_parse_context`, which provides access to
43/// formatting specification string, the parsing state, and the auto-indexing
44/// argument counter (where applicable).
45///
46/// This header is not intended to be included directly. Please include
47/// `<bsl_format.h>` to be able to use `bsl::basic_format_parse_context`.
48///
49/// @}
50/** @} */
51/** @} */
52
53/** @addtogroup bsl
54 * @{
55 */
56/** @addtogroup bslfmt
57 * @{
58 */
59/** @addtogroup bslfmt_formatparsecontext
60 * @{
61 */
62
63#include <bslscm_version.h>
64
65#include <bslfmt_formaterror.h>
66
67#include <bsls_exceptionutil.h>
68#include <bsls_keyword.h>
69
70#include <bslstl_stringview.h>
71
72
73
74namespace bslfmt {
75
76 // ================================
77 // class basic_format_parse_context
78 // ================================
79
80/// A format context for use by `formatter::parse()` partial specialization.
81/// It provides access to iterators for the start and end of the remaining
82/// (unparsed) format string, as well as argument counters.
83///
84/// See @ref bslfmt_formatparsecontext
85template <class t_CHAR>
87 public:
88 // TYPES
89 typedef
92
93 private:
94 // PRIVATE TYPES
95 enum IndexingMode { e_UNKNOWN, e_MANUAL, e_AUTOMATIC };
96
97 // DATA
98 iterator d_origBegin; // start of format string
99 iterator d_unparsed; // start of unparsed portion of format string
100 iterator d_end; // end of format string
101 IndexingMode d_indexing; // current indexing mode
102 size_t d_nextArgId; // next arg id for automatic indexing
103 size_t d_numArgs; // number of args
104
105 private:
106 // NOT IMPLEMENTED
111
112 public:
113 // TYPES
114 typedef t_CHAR char_type;
115
116 // CREATORS
117
118 /// Create an instance of this type with the specified `fmt` parse string
119 /// and number of arguments set to zero. The string is stored by reference,
120 /// so `fmt` must outlive this object.
123
124 /// Create an instance of this type with the specified `fmt` parse string
125 /// and number of arguments set to the specified `numArgs`. The string is
126 /// stored by reference, so `fmt` must outlive this object.
129 size_t numArgs) BSLS_KEYWORD_NOEXCEPT;
130
131 // MANIPULATORS
132
133 /// Update the held iterator to the unparsed portion of the format string
134 /// to be the specified `it`. Subsequent calls to `begin` will return this value.
135 ///
136 /// \pre The behavior is undefined unless `it` is a valid iterator to
137 /// the format string used in construction of this context.
139
140 /// Return the index of the next-unused argument. If the indexing mode is
141 /// unset, the indexing mode is set to `e_AUTOMATIC`. If the indexing mode
142 /// is `e_MANUAL`, caused by a previous call to @ref check_arg_id then an
143 /// exception of type @ref format_error is thrown. If there are no remaining
144 /// unused arguments then an exception of type @ref format_error is thrown.
146
147 /// Check that the specified `id` is a valid argument index. If the
148 /// indexing mode is unset, the indexing mode is set to `e_MANUAL`. If the
149 /// indexing mode is `e_AUTOMATIC`, caused by a previous call to
150 /// @ref next_arg_id then an exception of type @ref format_error is thrown. If
151 /// `id` is not less than the number of arguments then an exception of type
152 /// @ref format_error is thrown.
154
155 // ACCESSORS
156
157 /// Return an iterator to the start of the unparsed portion of the format
158 /// string.
161
162 /// Return an iterator to the start of the unparsed portion of the format
163 /// string.
166
167};
168
169// TYPEDEFS
172
173// ============================================================================
174// INLINE DEFINITIONS
175// ============================================================================
176
177 // ---------------------------------
178 // class basic_format_parse_context
179 // --------------------------------
180
181// CREATORS
182template <class t_CHAR>
183inline
186 bsl::basic_string_view<t_CHAR> fmt) BSLS_KEYWORD_NOEXCEPT
187: d_origBegin(fmt.begin())
188, d_unparsed(fmt.begin())
189, d_end(fmt.end())
190, d_indexing(e_UNKNOWN)
191, d_nextArgId(0)
192, d_numArgs(0)
193{
194}
195
196template <class t_CHAR>
197inline
201 size_t numArgs) BSLS_KEYWORD_NOEXCEPT
202: d_origBegin(fmt.begin())
203, d_unparsed(fmt.begin())
204, d_end(fmt.end())
205, d_indexing(e_UNKNOWN)
206, d_nextArgId(0)
207, d_numArgs(numArgs)
208{
209}
210
211// MANIPULATORS
212template <class t_CHAR>
213inline
216{
217 // This check is safe as, in our implementation, it is guaranteed that
218 // const_iterator is a random access iterator.
219 if (!((it >= d_origBegin) && (it <= d_end))) {
221 "Parse error: attempt to advance to an out-of-range iterator"));
222 }
223 d_unparsed = it;
224}
225
226template <class t_CHAR>
227inline
230{
231 if (d_indexing == e_MANUAL) {
232 BSLS_THROW(format_error("Mixing of automatic and manual indexing"));
233 }
234 if (d_nextArgId >= d_numArgs) {
235 BSLS_THROW(format_error("Number of conversion specifiers exceeds "
236 "number of arguments"));
237 }
238 if (d_indexing == e_UNKNOWN) {
239 d_indexing = e_AUTOMATIC;
240 }
241 return d_nextArgId++;
242}
243
244template <class t_CHAR>
245inline
248{
249 if (d_indexing == e_AUTOMATIC) {
250 BSLS_THROW(format_error("Mixing of automatic and manual indexing"));
251 }
252 if (id >= d_numArgs) {
253 BSLS_THROW(format_error("Invalid argument index"));
254 }
255 if (d_indexing == e_UNKNOWN) {
256 d_indexing = e_MANUAL;
257 }
258}
259
260// ACCESSORS
261template <class t_CHAR>
262inline
269
270template <class t_CHAR>
271inline
278
279} // close package namespace
280
281
282
283#endif // INCLUDED_BSLFMT_FORMATPARSECONTEXT
284
285// ----------------------------------------------------------------------------
286// Copyright 2023 Bloomberg Finance L.P.
287//
288// Licensed under the Apache License, Version 2.0 (the "License");
289// you may not use this file except in compliance with the License.
290// You may obtain a copy of the License at
291//
292// http://www.apache.org/licenses/LICENSE-2.0
293//
294// Unless required by applicable law or agreed to in writing, software
295// distributed under the License is distributed on an "AS IS" BASIS,
296// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
297// See the License for the specific language governing permissions and
298// limitations under the License.
299// ----------------------------- END-OF-FILE ----------------------------------
300
301/** @} */
302/** @} */
303/** @} */
Definition bslstl_stringview.h:471
const value_type * const_iterator
Definition bslstl_stringview.h:481
Definition bslfmt_formatparsecontext.h:86
BSLS_KEYWORD_CONSTEXPR_CPP20 const_iterator begin() const BSLS_KEYWORD_NOEXCEPT
Definition bslfmt_formatparsecontext.h:265
BSLS_KEYWORD_CONSTEXPR_CPP20 void advance_to(const_iterator it)
Definition bslfmt_formatparsecontext.h:215
const_iterator iterator
Definition bslfmt_formatparsecontext.h:91
BSLS_KEYWORD_CONSTEXPR_CPP20 const_iterator end() const BSLS_KEYWORD_NOEXCEPT
Definition bslfmt_formatparsecontext.h:274
BSLS_KEYWORD_CONSTEXPR_CPP20 void check_arg_id(size_t id)
Definition bslfmt_formatparsecontext.h:247
t_CHAR char_type
Definition bslfmt_formatparsecontext.h:114
bsl::basic_string_view< t_CHAR >::const_iterator const_iterator
Definition bslfmt_formatparsecontext.h:90
BSLS_KEYWORD_CONSTEXPR_CPP20 size_t next_arg_id()
Definition bslfmt_formatparsecontext.h:229
Definition bslfmt_formaterror.h:118
#define BSLS_THROW(X)
Definition bsls_exceptionutil.h:374
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
#define BSLS_KEYWORD_CONSTEXPR_CPP20
Definition bsls_keyword.h:645
#define BSLS_KEYWORD_DELETED
Definition bsls_keyword.h:651
#define BSLS_KEYWORD_NOEXCEPT
Definition bsls_keyword.h:674
Definition bdlat_valuetypefunctions.h:939
Definition bslfmt_enablestreamedformatter.h:130
basic_format_parse_context< char > format_parse_context
Definition bslfmt_formatparsecontext.h:170
basic_format_parse_context< wchar_t > wformat_parse_context
Definition bslfmt_formatparsecontext.h:171