BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslfmt_format_string.h
Go to the documentation of this file.
1/// @file bslfmt_format_string.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslfmt_format_string.h -*-C++-*-
8
9#ifndef INCLUDED_BSLFMT_FORMAT_STRING
10#define INCLUDED_BSLFMT_FORMAT_STRING
11
12#include <bsls_ident.h>
13BSLS_IDENT("$Id: $")
14
15/// @defgroup bslfmt_format_string bslfmt_format_string
16/// @brief Provide a string_view wrapper for formatting library usage
17/// @addtogroup bsl
18/// @{
19/// @addtogroup bslfmt
20/// @{
21/// @addtogroup bslfmt_format_string
22/// @{
23///
24/// <h1> Outline </h1>
25/// * <a href="#bslfmt_format_string-purpose"> Purpose</a>
26/// * <a href="#bslfmt_format_string-classes"> Classes </a>
27/// * <a href="#bslfmt_format_string-canonical-header"> Canonical Header </a>
28/// * <a href="#bslfmt_format_string-description"> Description </a>
29/// * <a href="#bslfmt_format_string-usage"> Usage </a>
30/// * <a href="#bslfmt_format_string-example-own-format-function"> Example: Own format function </a>
31///
32/// # Purpose {#bslfmt_format_string-purpose}
33/// Provide a string_view wrapper for formatting library usage
34///
35/// # Classes {#bslfmt_format_string-classes}
36///
37/// - bslfmt::basic_format_string: formatting library string_view wrapper
38///
39/// # Canonical Header {#bslfmt_format_string-canonical-header}
40/// bsl_format.h
41///
42/// # Description {#bslfmt_format_string-description}
43/// This component provides an implementation of the C++20 Standard
44/// Library's @ref basic_format_string , providing wrapper around string_view for
45/// format specification strings.
46///
47/// Using this rather than string_view directly serves three main purposes:
48///
49/// * We ensure that the interface to `format` and `vformat` are consistent with
50/// the standard library, which takes a @ref format_string rather than a
51/// @ref string_view .
52/// * Under C++20 we can enforce that the source string used for construction is
53/// a compile-time constant.
54/// * Under C++17 and earlier, by limiting construction to a `const t_CHAR*` we
55/// can limit the number of scenarios which would fail to compile under C++20
56/// but succeed under earlier versions.
57/// * It is possible to perform compile-time checking of the format string when
58/// compiled under C++20, in the same way as `std::format_string` (not yet
59/// implemented).
60///
61/// Suppression of the argument deduction that would normally result in a
62/// compile-time error requires that we use intermediate template aliases for
63/// @ref format_string and @ref wformat_string . As this is not possible under C++03,
64/// usage of this type necessarily differs under C++03. This is acceptable as
65/// this type is typically only used internally within the `bslfmt::format`
66/// family of functions.
67///
68/// This header is not intended to be included directly. Please include
69/// `<bsl_format.h>` to be able to use `bsl::basic_format_string`.
70///
71/// ## Usage {#bslfmt_format_string-usage}
72///
73///
74/// In this section we show the intended use of this component.
75///
76/// ### Example: Own format function {#bslfmt_format_string-example-own-format-function}
77///
78///
79///
80/// This usage example reflects how `bslfmt::format` typically uses this type.
81///
82/// Suppose we have a function that takes a format string and requires that the
83/// string be constant evaluated under C++20:
84///
85/// @code
86/// #if defined(BSLS_COMPILERFEATURES_SUPPORT_ALIAS_TEMPLATES) &&
87/// defined(BSLS_COMPILERFEATURES_SUPPORT_VARIADIC_TEMPLATES)
88/// template <class t_ARG>
89/// void myFormatLikeFunction(bslfmt::format_string<t_ARG> fmtstr,
90/// const t_ARG&)
91/// {
92/// assert(fmtstr.get() == "{:}");
93/// }
94/// #else
95/// template <class t_ARG>
96/// void myFormatLikeFunction(bslfmt::format_string fmtstr,
97/// const t_ARG&)
98/// {
99/// assert(fmtstr.get() == "{:}");
100/// }
101/// #endif
102/// @endcode
103///
104/// We can then invoke our function:
105///
106/// @code
107/// int value = 5;
108/// myFormatLikeFunction("{:}", value);
109/// @endcode
110///
111/// @}
112/** @} */
113/** @} */
114
115/** @addtogroup bsl
116 * @{
117 */
118/** @addtogroup bslfmt
119 * @{
120 */
121/** @addtogroup bslfmt_format_string
122 * @{
123 */
124
125#include <bslscm_version.h>
126
128#include <bsls_libraryfeatures.h>
129#include <bsls_keyword.h>
130
131#include <bslstl_string.h>
132#include <bslstl_stringview.h>
133
134#include <bslmf_enableif.h>
135#include <bslmf_typeidentity.h>
136
137#include <stdexcept>
138
139
140namespace bslfmt {
141
142// FORWARD DECLARATIONS
143
144template <class t_CHAR>
145struct Format_String_TestUpdater;
146
147
148 // =========================
149 // class basic_format_string
150 // =========================
151
152#if defined(BSLS_LIBRARYFEATURES_HAS_CPP20_BASELINE_LIBRARY)
153
154/// Type that wraps a @ref basic_string_view for use by formatting function. The
155/// constructor performs additional compile-time checks of the format string
156/// provided (not yet implemented).
157///
158/// See @ref bslfmt_format_string
159template <class t_CHAR, class... t_ARGS>
161 private:
162 // DATA
163 bsl::basic_string_view<t_CHAR> d_formatString; // the wrapped string
164
165 // FRIENDS
166 template <class t_INNER_CHAR>
168
169 public:
170 // CREATORS
171
172 /// Create an instance of this type from the specified `str`. This
173 /// function only takes part in overload resolution if `str` is convertible
174 /// to a @ref basic_string_view . Construction is ill-formed if `str` is not a
175 /// compile-time constant.
176 template <class t_STR,
177 class = typename bsl::enable_if<bsl::is_convertible<
178 const t_STR&,
179 bsl::basic_string_view<t_CHAR> >::value>::type>
180 consteval basic_format_string(const t_STR& str);
181
182 // ACCESSORS
183
184 /// Return the wrapped @ref basic_string_view contained by this instance.
186};
187
188#elif defined(BSLS_COMPILERFEATURES_SUPPORT_ALIAS_TEMPLATES) && \
189 defined(BSLS_COMPILERFEATURES_SUPPORT_VARIADIC_TEMPLATES)
190
191template <class t_CHAR, class... t_ARGS>
193 private:
194 // DATA
195 bsl::basic_string_view<t_CHAR> d_formatString; // the wrapped string
196
197 // FRIENDS
198 template <class t_INNER_CHAR>
199 friend struct Format_String_TestUpdater;
200
201 public:
202 // CREATORS
203
204 /// Create an instance of this type from the specified `str`.
206
207 // ACCESSORS
208
209 /// Return the wrapped @ref basic_string_view contained by this instance.
211};
212
213#else // C++03
214
215template <class t_CHAR>
216class basic_format_string {
217 private:
218 // DATA
219 bsl::basic_string_view<t_CHAR> d_formatString; // the wrapped string
220
221 // FRIENDS
222 template <class t_INNER_CHAR>
223 friend struct Format_String_TestUpdater;
224
225 public:
226 // CREATORS
227
228 /// Create an instance of this type from the specified `str`.
229 BSLS_KEYWORD_CONSTEXPR_CPP14 basic_format_string(const t_CHAR *str);
230
231 // ACCESSORS
232
233 /// Return the wrapped @ref basic_string_view contained by this instance.
235};
236
237#endif
238
239// ALIAS TEMPLATES AND TYPEDEFS
240
241#if defined(BSLS_COMPILERFEATURES_SUPPORT_ALIAS_TEMPLATES) && \
242 defined(BSLS_COMPILERFEATURES_SUPPORT_VARIADIC_TEMPLATES)
243
244template <class... t_ARGS>
245using format_string =
246 basic_format_string<char, bsl::type_identity_t<t_ARGS>...>;
247
248template <class... t_ARGS>
249using wformat_string =
250 basic_format_string<wchar_t, bsl::type_identity_t<t_ARGS>...>;
251
252#else // C++03
253
254// Template aliases are not supported in C++03
257
258#endif // Support for Alias and Variadic templates
259
260 // ===============================
261 // class Format_String_TestUpdater
262 // ===============================
263
264/// This is a component-private type which enables test drivers to work around
265/// the compile-time restrictions in @ref basic_format_string construction.
266template <class t_CHAR>
268{
269 /// Update the string contained in the specified `out` to the specified
270 /// `value`.
271 template <class t_FORMATSTRING>
272 static void update(t_FORMATSTRING *out, const t_CHAR *value);
273
274 /// Update the string contained in the specified `out` to the specified
275 /// `value`.
276 template <class t_FORMATSTRING>
277 static void update(t_FORMATSTRING *out,
279};
280
281// ============================================================================
282// INLINE DEFINITIONS
283// ============================================================================
284
285 // -------------------------
286 // class basic_format_string
287 // -------------------------
288
289#if defined(BSLS_LIBRARYFEATURES_HAS_CPP20_BASELINE_LIBRARY)
290
291// CREATORS
292template <class t_CHAR, class... t_ARGS>
293template <class t_STR, class>
295 const t_STR& str)
296: d_formatString(str)
297{
298}
299
300// ACCESSORS
301template <class t_CHAR, class... t_ARGS>
302inline
305{
306 return d_formatString;
307}
308
309#elif defined(BSLS_COMPILERFEATURES_SUPPORT_ALIAS_TEMPLATES) && \
310 defined(BSLS_COMPILERFEATURES_SUPPORT_VARIADIC_TEMPLATES)
311
312template <class t_CHAR, class... t_ARGS>
315{
316 d_formatString = str;
317}
318
319template <class t_CHAR, class... t_ARGS>
320inline
323{
324 return d_formatString;
325}
326
327#else // C++03
328
329template <class t_CHAR>
332{
333 d_formatString = str;
334}
335
336template <class t_CHAR>
337inline
340{
341 return d_formatString;
342}
343
344#endif
345
346 // -------------------------------
347 // class Format_String_TestUpdater
348 // -------------------------------
349
350template <class t_CHAR>
351template <class t_FORMATSTRING>
353 const t_CHAR *value)
354{
355 out->d_formatString = value;
356}
357
358template <class t_CHAR>
359template <class t_FORMATSTRING>
361 t_FORMATSTRING *out,
363{
364 out->d_formatString = value;
365}
366
367} // close package namespace
368
369
370#endif // INCLUDED_BSLFMT_FORMAT_STRING
371
372// ----------------------------------------------------------------------------
373// Copyright 2023 Bloomberg Finance L.P.
374//
375// Licensed under the Apache License, Version 2.0 (the "License");
376// you may not use this file except in compliance with the License.
377// You may obtain a copy of the License at
378//
379// http://www.apache.org/licenses/LICENSE-2.0
380//
381// Unless required by applicable law or agreed to in writing, software
382// distributed under the License is distributed on an "AS IS" BASIS,
383// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
384// See the License for the specific language governing permissions and
385// limitations under the License.
386// ----------------------------- END-OF-FILE ----------------------------------
387
388/** @} */
389/** @} */
390/** @} */
Definition bslstl_stringview.h:471
Definition bslfmt_format_string.h:160
friend struct Format_String_TestUpdater
Definition bslfmt_format_string.h:167
consteval basic_format_string(const t_STR &str)
Definition bslfmt_format_string.h:294
BSLS_KEYWORD_CONSTEXPR bsl::basic_string_view< t_CHAR > get() const
Return the wrapped basic_string_view contained by this instance.
Definition bslfmt_format_string.h:304
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
#define BSLS_KEYWORD_CONSTEXPR_CPP14
Definition bsls_keyword.h:631
#define BSLS_KEYWORD_CONSTEXPR
Definition bsls_keyword.h:624
Definition bslfmt_enablestreamedformatter.h:130
basic_format_string< wchar_t > wformat_string
Definition bslfmt_format_string.h:256
basic_format_string< char > format_string
Definition bslfmt_format_string.h:255
Definition bslmf_enableif.h:530
Definition bslmf_isconvertible.h:875
Definition bslfmt_format_string.h:268
static void update(t_FORMATSTRING *out, const t_CHAR *value)
Definition bslfmt_format_string.h:352