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