BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslfmt_format_args.h
Go to the documentation of this file.
1/// @file bslfmt_format_args.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslfmt_format_args.h -*-C++-*-
8
9#ifndef INCLUDED_BSLFMT_FORMAT_ARGS
10#define INCLUDED_BSLFMT_FORMAT_ARGS
11
12#include <bsls_ident.h>
13BSLS_IDENT("$Id: $")
14
15/// @defgroup bslfmt_format_args bslfmt_format_args
16/// @brief Provide a container of arguments for use by bsl::format
17/// @addtogroup bsl
18/// @{
19/// @addtogroup bslfmt
20/// @{
21/// @addtogroup bslfmt_format_args
22/// @{
23///
24/// <h1> Outline </h1>
25/// * <a href="#bslfmt_format_args-purpose"> Purpose</a>
26/// * <a href="#bslfmt_format_args-classes"> Classes </a>
27/// * <a href="#bslfmt_format_args-canonical-header"> Canonical Header </a>
28/// * <a href="#bslfmt_format_args-description"> Description </a>
29/// * <a href="#bslfmt_format_args-usage"> Usage </a>
30/// * <a href="#bslfmt_format_args-example-1-non-default-construction-and-value-verification"> Example: 1 Non-default construction and value verification </a>
31///
32/// # Purpose {#bslfmt_format_args-purpose}
33/// Provide a container of arguments for use by bsl::format
34///
35/// # Classes {#bslfmt_format_args-classes}
36///
37/// - bslfmt::basic_format_args: standard-compliant argument store
38///
39/// # Canonical Header {#bslfmt_format_args-canonical-header}
40/// bsl_format.h
41///
42/// # Description {#bslfmt_format_args-description}
43/// This component provides an implementation of the C++20 Standard
44/// Library's `std::basic_format_args`, providing access to an array of
45/// @ref basic_format_arg types. It also provides implementations of the standard
46/// library free functions @ref make_format_args and @ref make_wformat_args .
47///
48/// As also specified by the standard, the provided free functions return an
49/// exposition-only type that holds *references to* the arguments passed in. A
50/// @ref basic_format_args constructed from an instance of this type holds a
51/// reference to it. This means it is the users responsibility to ensure that
52/// the lifetime of the returned type does not end before the lifetime of the
53/// constructed @ref basic_format_args type. This means that, for example, the
54/// following code results in Undefined Behavior, in both the standard library
55/// and the `bslfmt` versions:
56/// @code
57/// int value = 5;
58/// format_args args = make_format_args(value);
59/// // args now holds a reference to a temporary whose lifetime has ended.
60/// do_something_with(args);
61/// @endcode
62///
63/// This header is not intended to be included directly. Please include
64/// `<bsl_format.h>` to be able to use `bsl::basic_format_args`.
65///
66/// ## Usage {#bslfmt_format_args-usage}
67///
68///
69/// In this section we show the intended use of this component.
70///
71/// ### Example: 1 Non-default construction and value verification {#bslfmt_format_args-example-1-non-default-construction-and-value-verification}
72///
73///
74/// We do not expect most users of `bsl::format` to interact with this type
75/// directly and instead use `bsl::format` or `bsl::vformat`. In addition,
76/// there are only a very limited number of public methods so this example is
77/// necessarily unrealistic.
78///
79/// Suppose we want to construct a int-containing @ref basic_format_args and verify
80/// that it contains that int. Note the use of a function to workaround the
81/// lifetime issues specified above. `Format_ArgsStore` passed to the
82/// @ref basic_format_args constructor must outlive the constructed object.
83/// `bslfmt::make_format_args` returns temporary `Format_ArgsStore` object so to
84/// avoid its destruction we have to do all the useful work within the
85/// execution of one function.
86/// @code
87/// struct UsageExampleVisitor {
88///
89/// void operator()(bsl::monostate) const
90/// {
91/// assert(false); // contains no value
92/// }
93///
94/// template <class t_TYPE>
95/// typename bsl::enable_if<bsl::is_integral<t_TYPE>::value>::type
96/// operator()(t_TYPE x) const
97/// {
98/// assert(static_cast<t_TYPE>(99) == x);
99/// }
100///
101/// template <class t_TYPE>
102/// typename bsl::enable_if<!bsl::is_integral<t_TYPE>::value>::type
103/// operator()(t_TYPE) const
104/// {
105/// assert(false); // contains non-integral value
106/// }
107/// };
108///
109/// struct UsageExampleChecker {
110/// static void checkValue(bslfmt::format_args args)
111/// {
112/// UsageExampleVisitor visitor;
113/// visit_format_arg(visitor, args.get(0));
114/// asssert(args.get(1));
115/// }
116/// };
117///
118/// int value = 99;
119/// UsageExampleChecker::checkValue(bslfmt::make_format_args(value));
120/// @endcode
121/// @}
122/** @} */
123/** @} */
124
125/** @addtogroup bsl
126 * @{
127 */
128/** @addtogroup bslfmt
129 * @{
130 */
131/** @addtogroup bslfmt_format_args
132 * @{
133 */
134
135#include <bslscm_version.h>
136
137#include <bslfmt_format_arg.h>
138#include <bslfmt_formaterror.h>
139
141
142#include <bslmf_conditional.h>
143#include <bslmf_enableif.h>
145#include <bslmf_isintegral.h>
146#include <bslmf_issame.h>
147#include <bslmf_movableref.h>
148
150#include <bsls_exceptionutil.h>
151#include <bsls_libraryfeatures.h>
152#include <bsls_unspecifiedbool.h>
153#include <bsls_util.h>
154
155#include <bslstl_array.h>
156#include <bslstl_iterator.h>
157#include <bslstl_monostate.h>
158#include <bslstl_utility.h>
159#include <bslstl_variant.h>
160#include <bslstl_vector.h>
161
162#if BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES
163// clang-format off
164// Include version that can be compiled with C++03
165// Generated on Thu Jan 23 14:54:23 2025
166// Command line: sim_cpp11_features.pl bslfmt_format_args.h
167
168# define COMPILING_BSLFMT_FORMAT_ARGS_H
170# undef COMPILING_BSLFMT_FORMAT_ARGS_H
171
172// clang-format on
173#else
174
175
176namespace bslfmt {
177
178// FORWARD DECLARATIONS
179
180template <class t_OUT, class t_CHAR>
181class basic_format_context;
182
183template <class t_CONTEXT>
184class basic_format_args;
185
186template <class t_VALUE>
187class Format_ContextOutputIteratorRef;
188
189// TYPEDEFS
190
191typedef basic_format_context<Format_ContextOutputIteratorRef<char>, char>
193
194typedef basic_format_context<Format_ContextOutputIteratorRef<wchar_t>, wchar_t>
196
198
200
201 // ======================
202 // class Format_ArgsStore
203 // ======================
204
205#if !BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES
206/// This component-private type holds an array of @ref basic_format_arg objects
207/// and is intended to be implicitly convertible to @ref basic_format_args .
208///
209/// \note Note that this type has reference semantics and users must ensure that this type
210/// does not outlive the arguments used in its construction.
211///
212/// See @ref bslfmt_format_args
213template <class t_CONTEXT, class... t_ARGS>
215
216 private:
217 // DATA
218 bsl::array<basic_format_arg<t_CONTEXT>, sizeof...(t_ARGS)> d_args;
219 // Contained array of arguments.
220
221 // FRIENDS
222 template <class t_INNER_CONTEXT>
223 friend class basic_format_args;
224
225 friend class Format_ArgsUtil;
226
227 // PRIVATE CREATORS
228
229 /// Create a `Format_ArgsStore` containing a copy of the specified
230 /// `args`.
231 explicit Format_ArgsStore(
232 const bsl::array<basic_format_arg<t_CONTEXT>, sizeof...(t_ARGS)>& args)
234
235 explicit Format_ArgsStore(
237 bsl::array<basic_format_arg<t_CONTEXT>, sizeof...(t_ARGS)> > args)
239};
240
241#endif
242
243// FREE FUNCTIONS
244
245#if !BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES
246/// From the specified @ref fmt_args return a type convertible to
247/// @ref format_arg_store that is suitable for use by the `char`-based
248/// `bslfmt::format` functions.
249template <class... t_ARGS>
251 t_ARGS&... fmt_args);
252
253/// From the specified @ref fmt_args return a type convertible to
254/// @ref format_arg_store that is suitable for use by the `wchar_t`-based
255/// `bslfmt::format` functions.
256template <class... t_ARGS>
258 t_ARGS&... fmt_args);
259#endif
260
261
262 // =======================
263 // class basic_format_args
264 // =======================
265
266/// A class intended to provide access to formatting arguments. This should
267/// not be constructed directly but indirectly using one of the `make_format_args` or the `make_wformat_args` functions.
268///
269/// \note Note that this
270/// type has reference semantics and users must ensure that this type does not
271/// outlive the arguments used in its construction.
272///
273/// See @ref bslfmt_format_args
274template <class t_CONTEXT>
276 private:
277 // DATA
278 size_t d_size; // number of arguments held
279
280 const basic_format_arg<t_CONTEXT> *d_data_p; // pointer to the first
281 // element of the contained
282 // array.
283
284 // PRIVATE ACCESSORS
285
286 /// Return the number of arguments contained within this object.
287 size_t size() const;
288
289 // FRIENDS
290 friend class Format_ArgsUtil;
291
292 public:
293 // CREATORS
294
295 /// Create a @ref basic_format_args object which contains no arguments.
297
298#if !BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES
299 /// Create a @ref basic_format_args object which contains the arguments held
300 /// by the specified `store`. Typically this constructor would be called
301 /// using the return value of @ref make_format_args or of @ref make_wformat_args .
302 template <class... t_ARGS>
304 BSLS_KEYWORD_NOEXCEPT; // IMPLICIT
305#endif
306
307 // ACCESSORS
308
309 /// Return the argument held at the position given by the specified `pos`.
310 /// If `pos >= size()` then a default-constructed @ref basic_format_arg is
311 /// returned.
313};
314
315 // ---------------------
316 // class Format_ArgsUtil
317 // ---------------------
318
319#if !BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES
320
321/// This class provides utility functions to enable manipulation of types
322/// declared by this component. It is solely for private use by other
323/// components of the `bslfmt` package and should not be used directly.
324///
325/// See @ref bslfmt_format_args
327 public:
328 // CLASS METHODS
329
330 /// Create a `Format_ArgsStore` object containing
331 /// @ref basic_format_arg objects constructed from the specified @ref fmt_args
332 /// values.
333 template <class t_CONTEXT, class... t_ARGS>
334 static Format_ArgsStore<t_CONTEXT, t_ARGS...> makeFormatArgs(
335 t_ARGS&... fmt_args);
336
337 /// Call `size()` on the specified `args` parameter and return the result.
338 /// This is to permit access to the private `size` accessor of
339 /// @ref basic_format_args without requiring long distance friendship.
340 template <class t_CONTEXT>
341 static size_t formatArgsSize(const basic_format_args<t_CONTEXT>& args);
342};
343
344#endif
345
346// ============================================================================
347// INLINE DEFINITIONS
348// ============================================================================
349
350 // -------------------------------------
351 // class Format_ArgsStore<t_OUT, T_CHAR>
352 // -------------------------------------
353
354#if !BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES
355
356// PRIVATE CREATORS
357template <class t_CONTEXT, class... t_ARGS>
358inline
360 const bsl::array<basic_format_arg<t_CONTEXT>, sizeof...(t_ARGS)>& args)
361 BSLS_KEYWORD_NOEXCEPT : d_args(args)
362{
363}
364
365template <class t_CONTEXT, class... t_ARGS>
366inline
367Format_ArgsStore<t_CONTEXT, t_ARGS...>::Format_ArgsStore(
369 bsl::array<basic_format_arg<t_CONTEXT>, sizeof...(t_ARGS)> > args)
371{
372}
373
374#endif
375
376 // ----------------------------------
377 // class basic_format_args<t_CONTEXT>
378 // ----------------------------------
379
380// CREATORS
381template <class t_CONTEXT>
382inline
387
388#if !BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES
389template <class t_CONTEXT>
390template <class... t_ARGS>
391inline
394 BSLS_KEYWORD_NOEXCEPT // IMPLICIT
395: d_size(sizeof...(t_ARGS))
396, d_data_p(store.d_args.data())
397{
398}
399#endif
400
401// ACCESSORS
402template <class t_CONTEXT>
403inline
405 size_t pos) const BSLS_KEYWORD_NOEXCEPT
406{
407 return pos < d_size ? d_data_p[pos] : basic_format_arg<t_CONTEXT>();
408}
409
410
411// PRIVATE ACCESSORS
412template <class t_CONTEXT>
413inline
415{
416 return d_size;
417}
418
419
420 // ---------------------
421 // class Format_ArgsUtil
422 // ---------------------
423
424// CLASS METHODS
425
426#if !BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES
427
428template <class t_CONTEXT, class... t_ARGS>
429inline
430Format_ArgsStore<t_CONTEXT, t_ARGS...>
432{
433 bsl::array<basic_format_arg<t_CONTEXT>, sizeof...(t_ARGS)> arg_array;
434 Format_ArgUtil::makeFormatArgArray<t_CONTEXT, t_ARGS...>(&arg_array,
435 fmt_args...);
436 return Format_ArgsStore<t_CONTEXT, t_ARGS...>(
437 bslmf::MovableRefUtil::move(arg_array));
438}
439
440#endif
441
442template <class t_CONTEXT>
443inline
446{
447 return args.size();
448}
449
450
451 // --------------
452 // FREE FUNCTIONS
453 // --------------
454
455// FREE FUNCTIONS
456
457#if !BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES
458
459template <class... t_ARGS>
460inline
462 t_ARGS&... fmt_args)
463{
464 return Format_ArgsUtil::makeFormatArgs<format_context>(fmt_args...);
465}
466
467template <class... t_ARGS>
468inline
470 t_ARGS&... fmt_args)
471{
472 return Format_ArgsUtil::makeFormatArgs<wformat_context>(fmt_args...);
473}
474
475#endif
476
477} // close package namespace
478
479
480#endif // End C++11 code
481
482#endif // INCLUDED_BSLFMT_FORMAT_ARGS
483
484// ----------------------------------------------------------------------------
485// Copyright 2023 Bloomberg Finance L.P.
486//
487// Licensed under the Apache License, Version 2.0 (the "License");
488// you may not use this file except in compliance with the License.
489// You may obtain a copy of the License at
490//
491// http://www.apache.org/licenses/LICENSE-2.0
492//
493// Unless required by applicable law or agreed to in writing, software
494// distributed under the License is distributed on an "AS IS" BASIS,
495// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
496// See the License for the specific language governing permissions and
497// limitations under the License.
498// ----------------------------- END-OF-FILE ----------------------------------
499
500/** @} */
501/** @} */
502/** @} */
static void makeFormatArgArray(bsl::array< basic_format_arg< t_CONTEXT >, sizeof...(t_FMTARGS)> *out, t_FMTARGS &... fmt_args)
Definition bslfmt_format_arg.h:875
Definition bslfmt_format_args.h:214
Definition bslfmt_format_args.h:326
static Format_ArgsStore< t_CONTEXT, t_ARGS... > makeFormatArgs(t_ARGS &... fmt_args)
Definition bslfmt_format_args.h:431
static size_t formatArgsSize(const basic_format_args< t_CONTEXT > &args)
Definition bslfmt_format_args.h:444
Definition bslfmt_format_arg.h:162
Definition bslfmt_format_args.h:275
basic_format_args() BSLS_KEYWORD_NOEXCEPT
Create a basic_format_args object which contains no arguments.
Definition bslfmt_format_args.h:383
basic_format_args(const Format_ArgsStore< t_CONTEXT, t_ARGS... > &store) BSLS_KEYWORD_NOEXCEPT
Definition bslfmt_format_args.h:392
basic_format_arg< t_CONTEXT > get(size_t pos) const BSLS_KEYWORD_NOEXCEPT
Definition bslfmt_format_args.h:404
Definition bslmf_movableref.h:752
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
#define BSLS_KEYWORD_NOEXCEPT
Definition bsls_keyword.h:674
Definition bslfmt_enablestreamedformatter.h:130
basic_format_context< Format_ContextOutputIteratorRef< char >, char > format_context
Definition bslfmt_format_arg.h:170
Format_ArgsStore< format_context, t_ARGS... > make_format_args(t_ARGS &... fmt_args)
Definition bslfmt_format_args.h:461
basic_format_context< Format_ContextOutputIteratorRef< wchar_t >, wchar_t > wformat_context
Definition bslfmt_format_arg.h:173
basic_format_args< format_context > format_args
Definition bslfmt_format_args.h:197
basic_format_args< wformat_context > wformat_args
Definition bslfmt_format_args.h:199
Format_ArgsStore< wformat_context, t_ARGS... > make_wformat_args(t_ARGS &... fmt_args)
Definition bslfmt_format_args.h:469
Definition bslstl_array.h:293
static MovableRef< t_TYPE > move(t_TYPE &reference) BSLS_KEYWORD_NOEXCEPT
Definition bslmf_movableref.h:1067