BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslfmt_enablestreamedformatter.h
Go to the documentation of this file.
1/// @file bslfmt_enablestreamedformatter.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslfmt_enablestreamedformatter.h -*-C++-*-
8
9#ifndef INCLUDED_BSLFMT_ENABLESTREAMEDFORMATTER
10#define INCLUDED_BSLFMT_ENABLESTREAMEDFORMATTER
11
12#include <bsls_ident.h>
13BSLS_IDENT("$Id: $")
14
15/// @defgroup bslfmt_enablestreamedformatter bslfmt_enablestreamedformatter
16/// @brief Provide a trait to enable stream based formatting of a type.
17/// @addtogroup bsl
18/// @{
19/// @addtogroup bslfmt
20/// @{
21/// @addtogroup bslfmt_enablestreamedformatter
22/// @{
23///
24/// <h1> Outline </h1>
25/// * <a href="#bslfmt_enablestreamedformatter-purpose"> Purpose</a>
26/// * <a href="#bslfmt_enablestreamedformatter-classes"> Classes </a>
27/// * <a href="#bslfmt_enablestreamedformatter-description"> Description </a>
28/// * <a href="#bslfmt_enablestreamedformatter-usage"> Usage </a>
29/// * <a href="#bslfmt_enablestreamedformatter-enabling-formatting-of-a-streamable-type"> Enabling Formatting of a Streamable Type </a>
30///
31/// # Purpose {#bslfmt_enablestreamedformatter-purpose}
32/// Provide a trait to enable stream based formatting of a type.
33///
34/// # Classes {#bslfmt_enablestreamedformatter-classes}
35///
36/// - bslfmt::EnableStreamedFormatter: trait to enable streamed `bsl::formatter`
37///
38/// # Description {#bslfmt_enablestreamedformatter-description}
39/// This component provides a trait, `EnableStreamedFormatter`,
40/// that a user can associate with a type, so that type will automatically use
41/// the streaming operator (`operator<<`) when the type is formatted with
42/// `bsl::format`. The `EnableStreamedFormatter` trait is typically associated
43/// with a type using `BSLMF_NESTED_TRAIT_DECLARATION` (see
44/// @ref bslmf_detectnestedtrait ), and indicates that `bslfmt::StreamedFormatter`
45/// (@ref bslfmt_streamedformatter ) should be used to format values of the type.
46///
47/// Be aware that enabling this trait for a type may preclude implementing a
48/// more type specific formatter in the future. Users of the type may come to
49/// rely on the "string-like" format specification, or the output format, in
50/// ways that prevent the type owner for implementing a specific formatter for
51/// the type in the future.
52///
53/// For more information read
54/// [the package documentation](@ref bslfmt-streaming-based-formatting) .
55///
56/// ## Usage {#bslfmt_enablestreamedformatter-usage}
57///
58///
59/// In this section we show the intended use of this component.
60///
61/// ### Enabling Formatting of a Streamable Type {#bslfmt_enablestreamedformatter-enabling-formatting-of-a-streamable-type}
62///
63///
64/// Suppose we own a simple type that supports `ostream` insert `operator<<` and
65/// want to quickly add `bsl::format`ing capability to it, based on streaming.
66///
67/// First, we introduce a type with a streaming operator but without a formatter
68/// that enables `bsl::format` use:
69/// @code
70/// class NonFormattableType {};
71///
72/// std::ostream& operator<<(std::ostream& os, const NonFormattableType&)
73/// {
74/// return os << "The printout";
75/// }
76///
77/// // The following would not compile:
78/// //
79/// // const NonFormattableType noFormatObj;
80/// // bsl::string s = bsl::format("{}", noFormatObj);
81/// @endcode
82/// Then, we enable formatting using the trait (notice the type name changed):
83/// @code
84/// class NowFormattableType {
85/// public:
86/// // TRAITS
87/// BSLMF_NESTED_TRAIT_DECLARATION(NowFormattableType,
88/// bslfmt::EnableStreamedFormatter);
89/// };
90///
91/// std::ostream& operator<<(std::ostream& os, const NowFormattableType&)
92/// {
93/// return os << "The printout";
94/// }
95/// @endcode
96/// Next, we create an instance of this type and use `bsl::format` to format it:
97/// @code
98/// const NowFormattableType obj;
99/// bsl::string s = bsl::format("{}", obj);
100/// @endcode
101/// Finally, we verify the output is correct:
102/// @code
103/// assert(s == "The printout");
104/// @endcode
105/// @}
106/** @} */
107/** @} */
108
109/** @addtogroup bsl
110 * @{
111 */
112/** @addtogroup bslfmt
113 * @{
114 */
115/** @addtogroup bslfmt_enablestreamedformatter
116 * @{
117 */
118
119#include <bslscm_version.h>
120
121#include <bslfmt_format.h>
123
125#include <bslmf_enableif.h>
126
127#include <bsls_keyword.h>
128
129
130namespace bslfmt {
131
132 // ==============================
133 // struct EnableStreamedFormatter
134 // ==============================
135
136/// A nested-trait-declaration compatible boolean trait indicating whether a
137/// type that is passed to `bsl::format` will be formatted using its `ostream`
138/// insertion operator, `operator<<`.
139template <class t_TYPE>
141: bslmf::DetectNestedTrait<t_TYPE, EnableStreamedFormatter>::type {
142};
143
144} // close package namespace
145
146
147namespace bsl {
148
149/// Standard (`bsl`) formatter for types that have their
150/// `bslfmt::EnableStreamedFormatter` set (to `true`). Delegates to
151/// `bslfmt::Streamed_Formatter`.
152template <class t_STREAMABLE>
154 t_STREAMABLE,
155 typename bsl::enable_if<
156 BloombergLP::bslfmt::EnableStreamedFormatter<t_STREAMABLE>::value,
157 char>::type> : BloombergLP::bslfmt::StreamedFormatter<t_STREAMABLE> {
158};
159
160} // close namespace bsl
161
162#endif // INCLUDED_BSLFMT_ENABLESTREAMEDFORMATTER
163
164// ----------------------------------------------------------------------------
165// Copyright 2025 Bloomberg Finance L.P.
166//
167// Licensed under the Apache License, Version 2.0 (the "License");
168// you may not use this file except in compliance with the License.
169// You may obtain a copy of the License at
170//
171// http://www.apache.org/licenses/LICENSE-2.0
172//
173// Unless required by applicable law or agreed to in writing, software
174// distributed under the License is distributed on an "AS IS" BASIS,
175// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
176// See the License for the specific language governing permissions and
177// limitations under the License.
178// ----------------------------- END-OF-FILE ----------------------------------
179
180/** @} */
181/** @} */
182/** @} */
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlat_valuetypefunctions.h:939
Definition bslfmt_enablestreamedformatter.h:130
Definition bslmf_enableif.h:530
Definition bslfmt_formatterbase.h:426
Definition bslfmt_enablestreamedformatter.h:141
Definition bslmf_detectnestedtrait.h:467