BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslfmt_formatterbool.h
Go to the documentation of this file.
1/// @file bslfmt_formatterbool.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslfmt_formatterbool.h -*-C++-*-
8
9#ifndef INCLUDED_BSLFMT_FORMATTERBOOL
10#define INCLUDED_BSLFMT_FORMATTERBOOL
11
12#include <bsls_ident.h>
13BSLS_IDENT("$Id: $")
14
15/// @defgroup bslfmt_formatterbool bslfmt_formatterbool
16/// @brief Provide a formatter customization for bool type
17/// @addtogroup bsl
18/// @{
19/// @addtogroup bslfmt
20/// @{
21/// @addtogroup bslfmt_formatterbool
22/// @{
23///
24/// <h1> Outline </h1>
25/// * <a href="#bslfmt_formatterbool-purpose"> Purpose</a>
26/// * <a href="#bslfmt_formatterbool-classes"> Classes </a>
27/// * <a href="#bslfmt_formatterbool-canonical-header"> Canonical Header </a>
28/// * <a href="#bslfmt_formatterbool-description"> Description </a>
29/// * <a href="#bslfmt_formatterbool-usage"> Usage </a>
30/// * <a href="#bslfmt_formatterbool-example-formatting-a-bool"> Example: Formatting a bool </a>
31///
32/// # Purpose {#bslfmt_formatterbool-purpose}
33/// Provide a formatter customization for bool type
34///
35/// # Classes {#bslfmt_formatterbool-classes}
36///
37/// - bsl::formatter<bool, t_CHAR>: formatter specialization for `bool`
38///
39/// # Canonical Header {#bslfmt_formatterbool-canonical-header}
40/// bsl_format.h
41///
42/// # Description {#bslfmt_formatterbool-description}
43/// This component provides partial specializations of
44/// `bsl::formatter` catering for bool type.
45///
46/// This header is not intended to be included directly. Please include
47/// `<bsl_format.h>` to be able to use the `bsl::formatter` for `bool` type.
48///
49/// ## Usage {#bslfmt_formatterbool-usage}
50///
51///
52/// In this section we show the intended use of this component.
53///
54/// ### Example: Formatting a bool {#bslfmt_formatterbool-example-formatting-a-bool}
55///
56///
57/// We do not expect most users of `bsl::format` to interact with this type
58/// directly and instead use `bsl::format` or `bsl::vformat`, so this example is
59/// necessarily unrealistic.
60///
61/// Suppose we want to test this formatter's ability to format a boolean with
62/// defined alignment and padding.
63///
64/// @code
65/// bslfmt::MockParseContext<char> mpc("*<6s", 1);
66///
67/// bsl::formatter<bool, char> f;
68/// mpc.advance_to(f.parse(mpc));
69///
70/// bool value = false;
71///
72/// bslfmt::MockFormatContext<char> mfc(value, 0, 0);
73///
74/// mfc.advance_to(bsl::as_const(f).format(value, mfc));
75///
76/// assert("false*" == mfc.finalString());
77/// @endcode
78///
79/// @}
80/** @} */
81/** @} */
82
83/** @addtogroup bsl
84 * @{
85 */
86/** @addtogroup bslfmt
87 * @{
88 */
89/** @addtogroup bslfmt_formatterbool
90 * @{
91 */
92
93#include <bslscm_version.h>
94
99
101
102
103namespace bslfmt {
104
105 // ========================
106 // struct FormatterBool_Imp
107 // ========================
108
109/// This type implements the formatter logic specific to the boolean type.
110///
111/// See @ref bslfmt_formatterbool
112template <class t_CHAR>
113struct FormatterBool_Imp : public FormatterIntegralBase<bool, t_CHAR> {
114 public:
115 // ACCESORS
116
117 /// Create string representation of the specified `value`, customized in
118 /// accordance with the requested format and the specified `formatContext`,
119 /// and copy it to the output that the output iterator of the
120 /// `formatContext` points to.
121 template <class t_FORMAT_CONTEXT>
122 typename t_FORMAT_CONTEXT::iterator format(
123 bool value,
124 t_FORMAT_CONTEXT& formatContext) const;
125};
126
127} // close package namespace
128
129
130namespace bsl {
131// FORMATTER SPECIALIZATIONS
132
133 // ==============================
134 // struct formatter<bool, t_CHAR>
135 // ==============================
136
137/// Partial specialization of the `bsl::formatter` template for the type
138/// `bool`.
139template <class t_CHAR>
140struct formatter<bool, t_CHAR>
141: BloombergLP::bslfmt::FormatterBool_Imp<t_CHAR> {
142};
143
144}
145
146// ============================================================================
147// INLINE DEFINITIONS
148// ============================================================================
149
150
151namespace bslfmt {
152
153 // ------------------------
154 // struct FormatterBool_Imp
155 // ------------------------
156
157template <class t_CHAR>
158template <class t_FORMAT_CONTEXT>
159inline
160typename t_FORMAT_CONTEXT::iterator FormatterBool_Imp<t_CHAR>::format(
161 bool value,
162 t_FORMAT_CONTEXT& formatContext) const
163{
164 typedef StandardFormatSpecification<t_CHAR> Specification;
165 typedef bslalg::NumericFormatterUtil NFUtil;
166 typedef FormatterCharUtil<t_CHAR> CharUtil;
167
168 const Specification& parsedSpec = this->specification();
169
170 const int maxPrefixSize = 4;
171 char prefixBuf[maxPrefixSize];
172 char *prefixBegin = prefixBuf;
173 char *prefixEnd = prefixBuf;
174
175 // We want to make sure that we have enough space to accommodate any
176 // representation of the `value`. In case of `unsigned char`
177 // it should be large enough for binary representation, in case of string
178 // we should be able to fit word `false`, which is 5 characters long.
179
180 const int maxValueSize =
181 NFUtil::ToCharsMaxLength<unsigned char, 2>::k_VALUE > 5
182 ? NFUtil::ToCharsMaxLength<unsigned char, 2>::k_VALUE
183 : 5;
184 t_CHAR valueBuf[maxValueSize];
185 t_CHAR *valueBegin = valueBuf;
186 t_CHAR *valueEnd = valueBuf;
187
188 if (Specification::e_BOOLEAN_STRING == parsedSpec.formatType()) {
189 // String representation.
190
191 const char *stringRepresentation = value ? "true" : "false";
192 const int stringRepresentationLength = value ? 4 : 5;
193 valueEnd = CharUtil::outputFromChar(
194 stringRepresentation,
195 stringRepresentation + stringRepresentationLength,
196 valueBegin);
197 }
198 else {
199 unsigned char intRepresentation = static_cast<unsigned char>(value);
200 prefixEnd = this->formatPrefix(prefixBuf,
201 maxPrefixSize,
202 intRepresentation);
203 valueEnd = this->formatValue(valueBuf,
204 maxValueSize,
205 intRepresentation);
206 }
207 return this->outputValue(prefixBegin,
208 prefixEnd,
209 valueBegin,
210 valueEnd,
211 formatContext);
212}
213
214} // close package namespace
215
216
217#endif // INCLUDED_BSLFMT_FORMATTERBOOL
218
219// ----------------------------------------------------------------------------
220// Copyright 2023 Bloomberg Finance L.P.
221//
222// Licensed under the Apache License, Version 2.0 (the "License");
223// you may not use this file except in compliance with the License.
224// You may obtain a copy of the License at
225//
226// http://www.apache.org/licenses/LICENSE-2.0
227//
228// Unless required by applicable law or agreed to in writing, software
229// distributed under the License is distributed on an "AS IS" BASIS,
230// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
231// See the License for the specific language governing permissions and
232// limitations under the License.
233// ----------------------------- END-OF-FILE ----------------------------------
234
235/** @} */
236/** @} */
237/** @} */
Definition bslfmt_standardformatspecification.h:78
#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 bslfmt_formatterbase.h:426
Definition bslalg_numericformatterutil.h:400
Definition bslfmt_formatterbool.h:113
t_FORMAT_CONTEXT::iterator format(bool value, t_FORMAT_CONTEXT &formatContext) const
Definition bslfmt_formatterbool.h:160
Definition bslfmt_formattercharutil.h:138
Definition bslfmt_formatterintegralbase.h:171