BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslfmt_print_imp.h
Go to the documentation of this file.
1/// @file bslfmt_print_imp.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslfmt_print_imp.h -*-C++-*-
8#ifndef INCLUDED_BSLFMT_PRINT_IMP
9#define INCLUDED_BSLFMT_PRINT_IMP
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslfmt_print_imp bslfmt_print_imp
15/// @brief Provide a standard compliant `print(FILE)` implementation.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslfmt
19/// @{
20/// @addtogroup bslfmt_print_imp
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslfmt_print_imp-purpose"> Purpose</a>
25/// * <a href="#bslfmt_print_imp-classes"> Classes </a>
26/// * <a href="#bslfmt_print_imp-canonical-header"> Canonical Header </a>
27/// * <a href="#bslfmt_print_imp-description"> Description </a>
28///
29/// # Purpose {#bslfmt_print_imp-purpose}
30/// Provide a standard compliant `print(FILE)` implementation.
31///
32/// # Classes {#bslfmt_print_imp-classes}
33///
34///
35/// # Canonical Header {#bslfmt_print_imp-canonical-header}
36/// bsl_print.h
37///
38/// @see ISO C++ Standard, <print>
39///
40/// # Description {#bslfmt_print_imp-description}
41/// This component provides, in the `BloombergLP::bslfmt`
42/// namespace, functions exposed by the standard <print> header. These are
43/// available for C++03 and later.
44///
45/// There are 2 high-level sets of functions, that most users should use:
46///
47/// * `print`
48/// * `println` (incl. no-args version)
49///
50/// The only difference is that `println` always appends '\n' to the formatted
51/// output. The no-arguments version of `println` just writes '\n'.
52///
53/// Also there are 4 variants of low-level `vprint` function:
54///
55/// * @ref vprint_nonunicode
56/// * @ref vprint_nonunicode_buffered
57/// * @ref vprint_unicode
58/// * @ref vprint_unicode_buffered
59///
60/// The `_buffered` flavour of functions formats the result into a temporary
61/// string and then writes the result to the stream. The functions without the
62/// `_buffered` suffix permit potentially more efficient implementation that
63/// involves explicit stream locking and writing directly to it, but it can
64/// cause deadlocks when used inappropriately. The current implementation
65/// always uses an intermediate buffer for formatting, despite the presence of
66/// the `_buffered` suffix.
67///
68/// The `_unicode` set of functions is used by `print` and `println` if the
69/// ordinary literal encoding is UTF-8; otherwise the `_nonunicode` functions
70/// are used. Note that for most non-Windows target platforms, there is no
71/// implementation difference between the 2 sets.
72/// @}
73/** @} */
74/** @} */
75
76/** @addtogroup bsl
77 * @{
78 */
79/** @addtogroup bslfmt
80 * @{
81 */
82/** @addtogroup bslfmt_print_imp
83 * @{
84 */
85
86#include <bslscm_version.h>
87
88#include <bslfmt_format.h>
89
90#include <bslstl_string.h>
91#include <bslstl_stringview.h>
92
93#include <cstdio>
94
95#if BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES
96// clang-format off
97// Include version that can be compiled with C++03
98// Generated on Wed Nov 19 07:03:37 2025
99// Command line: sim_cpp11_features.pl bslfmt_print_imp.h
100
101# define COMPILING_BSLFMT_PRINT_IMP_H
102# include <bslfmt_print_imp_cpp03.h>
103# undef COMPILING_BSLFMT_PRINT_IMP_H
104
105// clang-format on
106#else
107
108#if defined(BSLS_COMPILERFEATURES_SUPPORT_ALIAS_TEMPLATES) && \
109 defined(BSLS_COMPILERFEATURES_SUPPORT_VARIADIC_TEMPLATES)
110# define BSLFMT_FORMAT_STRING_PARAMETER bsl::format_string<t_ARGS...>
111#else
112// We cannot define format_string<t_ARGS...> in a C++03 compliant manner, so
113// have to use non-template versions instead.
114# define BSLFMT_FORMAT_STRING_PARAMETER bsl::format_string
115#endif
116
117
118namespace bslfmt {
119
120 // --------------
121 // FREE FUNCTIONS
122 // --------------
123
124#if !BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES
125/// Format the specified `args` using `bsl::format_to` according to the
126/// specification given by the specified `fmt`, and write the result of this
127/// operation to `stdout`. In the event of formatting error throw the
128/// exception @ref format_error , in the event of I/O error throw the exception
129/// @ref system_error , on allocation failure throw @ref bad_alloc .
130template <class... t_ARGS>
131void print(BSLFMT_FORMAT_STRING_PARAMETER fmt, t_ARGS&&... args);
132
133/// Format the specified `args` using `bsl::format_to` according to the
134/// specification given by the specified `fmt`, and write the result of this
135/// operation to the specified `stream`. In the event of formatting error
136/// throw the exception @ref format_error , in the event of I/O error throw the
137/// exception @ref system_error , on allocation failure throw @ref bad_alloc .
138///
139/// \pre The behavior is undefined unless `stream` is a valid pointer to an output C
140/// stream.
141template <class... t_ARGS>
142void print(std::FILE *stream,
144 t_ARGS&&... args);
145
146/// Format the specified `args` using `bsl::format_to` according to the
147/// specification given by the specified `fmt`, append `\n`, and write the
148/// result of this operation to `stdout`. In the event of formatting error
149/// throw the exception @ref format_error , in the event of I/O error throw the
150/// exception @ref system_error , on allocation failure throw @ref bad_alloc .
151template <class... t_ARGS>
152void println(BSLFMT_FORMAT_STRING_PARAMETER fmt, t_ARGS&&... args);
153
154/// Format the specified `args` using `bsl::format_to` according to the
155/// specification given by the specified `fmt`, append `\n`, and write the
156/// result of this operation to the specified `stream`. In the event of
157/// formatting error throw the exception @ref format_error , in the event of I/O
158/// error throw the exception @ref system_error , on allocation failure throw `bad_alloc`.
159///
160/// \pre The behavior is undefined unless `stream` is a valid pointer
161/// to an output C stream.
162template <class... t_ARGS>
163void println(std::FILE *stream,
165 t_ARGS&&... args);
166#endif
167
168/// Write `\n` into `stdout`.
169void println();
170
171/// Write `\n` to the specified `stream`.
172/// \pre The behavior is undefined unless
173/// `stream` is a valid pointer to an output C stream.
174void println(std::FILE *stream);
175
176/// Format the specified `args` using `bsl::vformat_to` according to the
177/// specification given by the specified `fmt`, and write the result of this
178/// operation to `stdout`. In the event of formatting error throw the
179/// exception @ref format_error , in the event of I/O error throw the exception
180/// @ref system_error , on allocation failure throw @ref bad_alloc .
181void vprint_nonunicode(bsl::string_view fmt, bsl::format_args args);
182
183/// Format the specified `args` using `bsl::vformat_to` according to the
184/// specification given by the specified `fmt`, and write the result of this
185/// operation to the specified `stream`. In the event of formatting error
186/// throw the exception @ref format_error , in the event of I/O error throw the
187/// exception @ref system_error , on allocation failure throw @ref bad_alloc .
188///
189/// \pre The behavior is undefined unless `stream` is a valid pointer to an output C
190/// stream.
191void vprint_nonunicode(std::FILE *stream,
193 bsl::format_args args);
194
195/// Format the specified `args` using `bsl::vformat_to` according to the
196/// specification given by the specified `fmt` to a temporary buffer, and write
197/// the resulting buffer to the specified `stream`. In the event of formatting
198/// error throw the exception @ref format_error , in the event of I/O error throw
199/// the exception @ref system_error , on allocation failure throw @ref bad_alloc .
200///
201/// \pre The behavior is undefined unless `stream` is a valid pointer to an output C
202/// stream.
203void vprint_nonunicode_buffered(std::FILE *stream,
205 bsl::format_args args);
206
207/// Format the specified `args` using `bsl::vformat_to` according to the
208/// specification given by the specified `fmt`, and write the result of this
209/// operation to `stdout`. In the event of formatting error throw the
210/// exception @ref format_error , in the event of I/O error throw the exception
211/// @ref system_error , on allocation failure throw @ref bad_alloc .
212void vprint_unicode(bsl::string_view fmt, bsl::format_args args);
213
214/// Format the specified `args` using `bsl::vformat_to` according to the
215/// specification given by the specified `fmt`, and write the result of this
216/// operation to the specified `stream`. In the event of formatting error
217/// throw the exception @ref format_error , in the event of I/O error throw the
218/// exception @ref system_error , on allocation failure throw @ref bad_alloc .
219///
220/// \pre The behavior is undefined unless `stream` is a valid pointer to an output C
221/// stream.
222void vprint_unicode(std::FILE *stream,
224 bsl::format_args args);
225
226/// Format the specified `args` using `bsl::vformat_to` according to the
227/// specification given by the specified `fmt` to a temporary buffer, and write
228/// the resulting buffer to the specified `stream`. In the event of formatting
229/// error throw the exception @ref format_error , in the event of I/O error throw
230/// the exception @ref system_error , on allocation failure throw @ref bad_alloc .
231///
232/// \pre The behavior is undefined unless `stream` is a valid pointer to an output C
233/// stream.
234void vprint_unicode_buffered(std::FILE *stream,
236 bsl::format_args args);
237
238/// Throw @ref system_error with `errc::io_error`.
240
241// ============================================================================
242// INLINE DEFINITIONS
243// ============================================================================
244
245 // --------------
246 // FREE FUNCTIONS
247 // --------------
248
249#if !BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES
250template <class... t_ARGS>
251inline
252void print(std::FILE *stream,
254 t_ARGS&&... args)
255{
257 fmt.get(),
258 bsl::make_format_args(args...));
259}
260
261template <class... t_ARGS>
262inline
263void print(BSLFMT_FORMAT_STRING_PARAMETER fmt, t_ARGS&&... args)
264{
265 bslfmt::print(stdout, fmt, std::forward<t_ARGS>(args)...);
266}
267
268template <class... t_ARGS>
269inline
270void println(std::FILE *stream,
272 t_ARGS&&... args)
273{
274 bslfmt::print(stream,
275 "{}\n",
276 bsl::vformat(fmt.get(), bsl::make_format_args(args...)));
277}
278
279template <class... t_ARGS>
280inline
281void println(BSLFMT_FORMAT_STRING_PARAMETER fmt, t_ARGS&&... args)
282{
283 bslfmt::println(stdout, fmt, std::forward<t_ARGS>(args)...);
284}
285#endif
286
287inline
289{
290 bslfmt::println(stdout);
291}
292
293inline
294void vprint_nonunicode(bsl::string_view fmt, bsl::format_args args)
295{
296 return bslfmt::vprint_nonunicode(stdout, fmt, args);
297}
298
299inline
300void vprint_unicode(bsl::string_view fmt, bsl::format_args args)
301{
302 return bslfmt::vprint_unicode(stdout, fmt, args);
303}
304
305} // close package namespace
306
307
308#undef BSLFMT_FORMAT_STRING_PARAMETER
309
310#endif // End C++11 code
311
312#endif
313
314// ----------------------------------------------------------------------------
315// Copyright 2025 Bloomberg Finance L.P.
316//
317// Licensed under the Apache License, Version 2.0 (the "License");
318// you may not use this file except in compliance with the License.
319// You may obtain a copy of the License at
320//
321// http://www.apache.org/licenses/LICENSE-2.0
322//
323// Unless required by applicable law or agreed to in writing, software
324// distributed under the License is distributed on an "AS IS" BASIS,
325// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
326// See the License for the specific language governing permissions and
327// limitations under the License.
328// ----------------------------- END-OF-FILE ----------------------------------
329
330/** @} */
331/** @} */
332/** @} */
Definition bslstl_stringview.h:471
#define BSLFMT_FORMAT_STRING_PARAMETER
Definition bslfmt_format_imp.h:209
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bslfmt_enablestreamedformatter.h:130
void print(BSLFMT_FORMAT_STRING_PARAMETER fmt, t_ARGS &&... args)
Definition bslfmt_print_imp.h:263
void Print_Imp_ThrowIoError()
Throw system_error with errc::io_error.
void vprint_unicode_buffered(std::FILE *stream, bsl::string_view fmt, bsl::format_args args)
void vprint_unicode(bsl::string_view fmt, bsl::format_args args)
Definition bslfmt_print_imp.h:300
void println()
Write \n into stdout.
Definition bslfmt_print_imp.h:288
void vprint_nonunicode(bsl::string_view fmt, bsl::format_args args)
Definition bslfmt_print_imp.h:294
void vprint_nonunicode_buffered(std::FILE *stream, bsl::string_view fmt, bsl::format_args args)