BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslfmt_streamedformatter.h
Go to the documentation of this file.
1/// @file bslfmt_streamedformatter.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslfmt_streamedformatter.h -*-C++-*-
8
9#ifndef INCLUDED_BSLFMT_STREAMEDFORMATTER
10#define INCLUDED_BSLFMT_STREAMEDFORMATTER
11
12#include <bsls_ident.h>
13BSLS_IDENT("$Id: $")
14
15/// @defgroup bslfmt_streamedformatter bslfmt_streamedformatter
16/// @brief Provide a formatter that uses the `ostream` insert `operator<<`.
17/// @addtogroup bsl
18/// @{
19/// @addtogroup bslfmt
20/// @{
21/// @addtogroup bslfmt_streamedformatter
22/// @{
23///
24/// <h1> Outline </h1>
25/// * <a href="#bslfmt_streamedformatter-purpose"> Purpose</a>
26/// * <a href="#bslfmt_streamedformatter-classes"> Classes </a>
27/// * <a href="#bslfmt_streamedformatter-description"> Description </a>
28/// * <a href="#bslfmt_streamedformatter-usage"> Usage </a>
29/// * <a href="#bslfmt_streamedformatter-enable-formatting-of-a-streamable-object"> Enable Formatting of a Streamable Object </a>
30///
31/// # Purpose {#bslfmt_streamedformatter-purpose}
32/// Provide a formatter that uses the `ostream` insert `operator<<`.
33///
34/// # Classes {#bslfmt_streamedformatter-classes}
35///
36/// - bslfmt::StreamedFormatter: formatter that uses `ostream` `operator<<`
37///
38///@SEE ALSO: bslfmt_streamed, bslfmt_enablestreamedformatter
39///
40/// # Description {#bslfmt_streamedformatter-description}
41/// This component provides a base formatter template that may be
42/// used to enable `bsl::format`ing of values that have an `ostream` insert
43/// `operator<<` implemented. The formatting template is also compatible with
44/// `std::format` on platforms where it is provided. Note that this component
45/// is not recommended to be used directly but mainly via the @ref bslfmt_streamed
46/// component that provides a formattable-wrapper, or by defining the
47/// @ref bslfmt_enablestreamedformatter nested trait in the class or template that
48/// should have a permanent streamed formatter.
49///
50/// The use case for this component is *very* narrow, so please read
51/// [the package documentation](@ref bslfmt-streaming-based-formatting) before using
52/// it and possibly locking yourself into forever supporting an inadequate
53/// format string specification.
54///
55/// ## Usage {#bslfmt_streamedformatter-usage}
56///
57///
58/// In this section we show the intended use of this component.
59///
60/// ### Enable Formatting of a Streamable Object {#bslfmt_streamedformatter-enable-formatting-of-a-streamable-object}
61///
62///
63/// Suppose we want to format an object that already supports streaming into an
64/// `ostream` using the insert `operator<<`.
65///
66/// First, we define the type with a streaming operator, but without a formatter
67/// specialization:
68/// @code
69/// class StreamableType {};
70///
71/// std::ostream& operator<<(std::ostream& os, const StreamableType&)
72/// {
73/// return os << "The printout";
74/// }
75/// @endcode
76/// Then, we enable formatting of this type using `bslfmt::StreamedFormatter` as
77/// its implementation:
78/// @code
79/// namespace bsl {
80/// template <>
81/// struct formatter<StreamableType, char> :
82/// BloombergLP::bslfmt::StreamedFormatter<StreamableType> {
83/// };
84/// } // close namespace bsl
85/// @endcode
86/// Next, we create an instance of this type and use `bsl::format` to format it:
87/// @code
88/// const StreamableType obj;
89/// bsl::string s = bsl::format("{}", obj);
90/// @endcode
91/// Finally, we verify the output is correct:
92/// @code
93/// assert(s == "The printout");
94/// @endcode
95/// @}
96/** @} */
97/** @} */
98
99/** @addtogroup bsl
100 * @{
101 */
102/** @addtogroup bslfmt
103 * @{
104 */
105/** @addtogroup bslfmt_streamedformatter
106 * @{
107 */
108
109#include <bslscm_version.h>
110
112
113#include <bslstl_ostringstream.h>
114#include <bslstl_string.h>
115
116#include <bsls_keyword.h>
117
118
119namespace bslfmt {
120
121 // ========================
122 // struct StreamedFormatter
123 // ========================
124
125/// This class provides the implementations of `parse` and `format` for
126/// formatters that format a type using its streaming operator.
127///
128/// See @ref bslfmt_streamedformatter
129template <class t_STREAMABLE>
131 private:
132 // DATA
134
135 public:
136 // MANIPULATORS
137
138 /// Parse and validate the specification string stored in the iterator
139 /// accessed via the `begin()` method of the parseContext passed via the
140 /// specified `parseContext` parameter. Where nested parameters are
141 /// encountered in the specification string then the @ref next_arg_id and
142 /// @ref check_arg_id are called on `fc` as specified in the C++ Standard.
143 /// Return an end iterator of the parsed range. Throw an exception of type
144 /// `bsl::format_error` in the event of failure.
145 template <class t_PARSE_CONTEXT>
146 BSLS_KEYWORD_CONSTEXPR_CPP20 typename t_PARSE_CONTEXT::iterator parse(
147 t_PARSE_CONTEXT& parseContext);
148
149 // ACCESSORS
150
151 /// Format the specified `value` according to the specification stored as a
152 /// result of a previous call to the `parse` method, and write the result
153 /// to the iterator accessed by calling the `out()` method on the specified
154 /// `formatContext` parameter. Return an end iterator of the output range.
155 /// Throw an exception of type `bsl::format_error` in the event of failure.
156 template <class t_FORMAT_CONTEXT>
157 typename t_FORMAT_CONTEXT::iterator format(
158 const t_STREAMABLE& value,
159 t_FORMAT_CONTEXT& formatContext) const;
160};
161
162// ============================================================================
163// INLINE DEFINITIONS
164// ============================================================================
165
166 // ------------------------
167 // struct StreamedFormatter
168 // ------------------------
169
170// MANIPULATORS
171template <class t_STREAMABLE>
172template <class t_PARSE_CONTEXT>
173BSLS_KEYWORD_CONSTEXPR_CPP20 typename t_PARSE_CONTEXT::iterator
174StreamedFormatter<t_STREAMABLE>::parse(t_PARSE_CONTEXT& parseContext)
175{
176 return d_stringFormatter.parse(parseContext);
177}
178
179// ACCESSORS
180template <class t_STREAMABLE>
181template <class t_FORMAT_CONTEXT>
182typename t_FORMAT_CONTEXT::iterator StreamedFormatter<t_STREAMABLE>::format(
183 const t_STREAMABLE& value,
184 t_FORMAT_CONTEXT& formatContext) const
185{
187 os << value;
188
189 return d_stringFormatter.format(os.view(), formatContext);
190}
191
192} // close package namespace
193
194
195#endif // INCLUDED_BSLFMT_STREAMEDFORMATTER
196
197// ----------------------------------------------------------------------------
198// Copyright 2025 Bloomberg Finance L.P.
199//
200// Licensed under the Apache License, Version 2.0 (the "License");
201// you may not use this file except in compliance with the License.
202// You may obtain a copy of the License at
203//
204// http://www.apache.org/licenses/LICENSE-2.0
205//
206// Unless required by applicable law or agreed to in writing, software
207// distributed under the License is distributed on an "AS IS" BASIS,
208// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
209// See the License for the specific language governing permissions and
210// limitations under the License.
211// ----------------------------- END-OF-FILE ----------------------------------
212
213/** @} */
214/** @} */
215/** @} */
#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
basic_ostringstream< char, char_traits< char >, allocator< char > > ostringstream
Definition bslstl_iosfwd.h:97
Definition bslfmt_enablestreamedformatter.h:130
Definition bslfmt_formatterbase.h:426
Definition bslfmt_streamedformatter.h:130
BSLS_KEYWORD_CONSTEXPR_CPP20 t_PARSE_CONTEXT::iterator parse(t_PARSE_CONTEXT &parseContext)
Definition bslfmt_streamedformatter.h:174
t_FORMAT_CONTEXT::iterator format(const t_STREAMABLE &value, t_FORMAT_CONTEXT &formatContext) const
Definition bslfmt_streamedformatter.h:182