BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslfmt_formatterpointer.h
Go to the documentation of this file.
1/// @file bslfmt_formatterpointer.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslfmt_formatterpointer.h -*-C++-*-
8
9#ifndef INCLUDED_BSLFMT_FORMATTERPOINTER
10#define INCLUDED_BSLFMT_FORMATTERPOINTER
11
12#include <bsls_ident.h>
13BSLS_IDENT("$Id: $")
14
15/// @defgroup bslfmt_formatterpointer bslfmt_formatterpointer
16/// @brief Provide a formatter customization for pointer types
17/// @addtogroup bsl
18/// @{
19/// @addtogroup bslfmt
20/// @{
21/// @addtogroup bslfmt_formatterpointer
22/// @{
23///
24/// <h1> Outline </h1>
25/// * <a href="#bslfmt_formatterpointer-purpose"> Purpose</a>
26/// * <a href="#bslfmt_formatterpointer-classes"> Classes </a>
27/// * <a href="#bslfmt_formatterpointer-canonical-header"> Canonical Header </a>
28/// * <a href="#bslfmt_formatterpointer-description"> Description </a>
29/// * <a href="#bslfmt_formatterpointer-usage"> Usage </a>
30/// * <a href="#bslfmt_formatterpointer-example-formatting-a-pointer"> Example: Formatting a pointer </a>
31///
32/// # Purpose {#bslfmt_formatterpointer-purpose}
33/// Provide a formatter customization for pointer types
34///
35/// # Classes {#bslfmt_formatterpointer-classes}
36///
37/// - bsl::formatter<void *, t_CHAR>: formatter specialization for `void *`
38/// - bsl::formatter<const void *, t_CHAR>: specialization for `const void *`
39/// - bsl::formatter<bsl::nullptr_t, t_CHAR>: specialization for `bsl::nullptr_t`
40///
41/// # Canonical Header {#bslfmt_formatterpointer-canonical-header}
42/// bsl_format.h
43///
44/// # Description {#bslfmt_formatterpointer-description}
45/// This component provides partial specializations of
46/// `bsl::formatter` catering for pointer types.
47///
48/// This header is not intended to be included directly. Please include
49/// `<bsl_format.h>` to be able to use specializations of the `bsl::formatter`
50/// for pointer types.
51///
52/// ## Usage {#bslfmt_formatterpointer-usage}
53///
54///
55/// In this section we show the intended use of this component.
56///
57/// ### Example: Formatting a pointer {#bslfmt_formatterpointer-example-formatting-a-pointer}
58///
59///
60/// We do not expect most users of `bsl::format` to interact with this type
61/// directly and instead use `bsl::format` or `bsl::vformat`, so this example is
62/// necessarily unrealistic.
63///
64/// Suppose we want to test pointer formatter's ability to format a pointer with
65/// defined alignment and padding.
66///
67/// @code
68/// bslfmt::MockParseContext<char> mpc("*<6p", 1);
69///
70/// bsl::formatter<const void *, char> f;
71/// mpc.advance_to(f.parse(mpc));
72///
73/// const void *value = 0;
74///
75/// bslfmt::MockFormatContext<char> mfc(value, 0, 0);
76///
77/// mfc.advance_to(bsl::as_const(f).format(value, mfc));
78///
79/// assert("0x0***" == mfc.finalString());
80/// @endcode
81/// @}
82/** @} */
83/** @} */
84
85/** @addtogroup bsl
86 * @{
87 */
88/** @addtogroup bslfmt
89 * @{
90 */
91/** @addtogroup bslfmt_formatterpointer
92 * @{
93 */
94
95#include <bslscm_version.h>
96
97#include <bslfmt_formaterror.h>
101
103
104#include <bslmf_assert.h>
105
106#include <bsls_exceptionutil.h>
107#include <bsls_nullptr.h>
108#include <bsls_types.h>
109
110#include <bslstl_iterator.h>
111
112#ifndef BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
113# include <locale>
114#endif // BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
115
116
117namespace bslfmt {
118
119 // ===========================
120 // struct FormatterPointer_Imp
121 // ===========================
122
123/// This struct is a specialization of the `bsl::formatter` template for the
124/// pointer types.
125///
126/// See @ref bslfmt_formatterpointer
127template <class t_VALUE, class t_CHAR>
128struct FormatterPointer_Imp : public FormatterIntegralBase<t_VALUE, t_CHAR> {
129 public:
130 // ACCESSORS
131
132 /// Create string representation of the specified `ptrValue`, customized in
133 /// accordance with the requested format and the specified `formatContext`,
134 /// and copy it to the output that the output iterator of the
135 /// `formatContext` points to.
136 template <class t_FORMAT_CONTEXT>
137 typename t_FORMAT_CONTEXT::iterator format(
138 t_VALUE ptrValue,
139 t_FORMAT_CONTEXT& formatContext) const;
140};
141
142
143 // ===============================
144 // struct FormatterPointer_Nullptr
145 // ===============================
146
147/// This struct is a specialization of the `bsl::formatter` template for the
148/// `bsl::nullptr_t`.
149template <class t_CHAR>
151: public FormatterIntegralBase<bsl::nullptr_t, t_CHAR> {
152 public:
153 // ACCESSORS
154
155 /// Create string representation of the `bsl::nullptr_t` object, customized
156 /// in accordance with the requested format and the specified
157 /// `formatContext`, and copy it to the output that the output iterator of
158 /// the `formatContext` points to.
159 template <class t_FORMAT_CONTEXT>
160 typename t_FORMAT_CONTEXT::iterator format(
162 t_FORMAT_CONTEXT& formatContext) const;
163};
164} // close package namespace
165
166
167namespace bsl {
168// FORMATTER SPECIALIZATIONS
169
170/// Partial specialization of the `bsl::formatter` template for the type
171/// `void *`.
172template <class t_CHAR>
173struct formatter<void *, t_CHAR>
174: BloombergLP::bslfmt::FormatterPointer_Imp<void *, t_CHAR> {
175};
176
177/// Partial specialization of the `bsl::formatter` template for the type
178/// `const void *`.
179template <class t_CHAR>
180struct formatter<const void *, t_CHAR>
181: BloombergLP::bslfmt::FormatterPointer_Imp<const void *, t_CHAR> {
182};
183
184/// Partial specialization of the `bsl::formatter` template for the type
185/// `bsl::nullptr_t`.
186template <class t_CHAR>
187struct formatter<bsl::nullptr_t, t_CHAR>
188: BloombergLP::bslfmt::FormatterPointer_Nullptr<t_CHAR> {
189};
190
191} // close namespace bsl
192
193// ============================================================================
194// INLINE DEFINITIONS
195// ============================================================================
196
197
198namespace bslfmt {
199
200 // -----------------------
201 // struct FormatterPointer
202 // -----------------------
203
204template <class t_VALUE, class t_CHAR>
205template <class t_FORMAT_CONTEXT>
206inline
207typename t_FORMAT_CONTEXT::iterator
209 t_VALUE ptrValue,
210 t_FORMAT_CONTEXT& formatContext) const
211{
212 typedef BloombergLP::bslalg::NumericFormatterUtil NFUtil;
213 typedef StandardFormatSpecification<t_CHAR> Specification;
214 typedef bsls::Types::UintPtr UintPtr;
215
216 const Specification& parsedSpec = this->specification();
217
218 // prefix
219
220 const int maxPrefixSize = 2;
221 char prefixBuf[maxPrefixSize];
222 char *prefixBegin = prefixBuf;
223 char *prefixEnd = prefixBegin + maxPrefixSize;
224
225 prefixBuf[0] = '0';
226
227 switch (parsedSpec.formatType()) {
228 case Specification::e_POINTER_HEX: {
229 prefixBuf[1] = 'x';
230 } break;
231 case Specification::e_POINTER_HEX_UC: {
232 prefixBuf[1] = 'X';
233 } break;
234 default: {
235 BSLS_THROW(bsl::format_error("Invalid pointer format type"));
236 }
237 }
238
239 // value
240
241 // `formatValue` is universal function used for formatting integer values,
242 // therefore it requires storage sufficient to accommodate any
243 // representation of the value (even if for pointers hex format is used).
244 // Binary representation takes up the most space.
245
246 const int maxValueSize = NFUtil::ToCharsMaxLength<UintPtr, 2>::k_VALUE;
247 t_CHAR valueBuf[maxValueSize];
248 t_CHAR *valueBegin = valueBuf;
249 t_CHAR *valueEnd = valueBuf;
250
251 const void *temp = static_cast<const void *>(ptrValue);
252 UintPtr value = reinterpret_cast<UintPtr>(temp);
253 valueEnd = this->formatValue(valueBuf, maxValueSize, value);
254
255 // output the result
256
257 return this->outputValue(prefixBegin,
258 prefixEnd,
259 valueBegin,
260 valueEnd,
261 formatContext);
262}
263
264 // -------------------------------
265 // struct FormatterPointer_Nullptr
266 // -------------------------------
267
268template <class t_CHAR>
269template <class t_FORMAT_CONTEXT>
270inline
271typename t_FORMAT_CONTEXT::iterator FormatterPointer_Nullptr<t_CHAR>::format(
273 t_FORMAT_CONTEXT& formatContext) const
274{
275 typedef StandardFormatSpecification<t_CHAR> Specification;
276
277 const Specification& parsedSpec = this->specification();
278
279 // prefix
280
281 const int maxPrefixSize = 2;
282 char prefixBuf[maxPrefixSize];
283 char *prefixBegin = prefixBuf;
284 char *prefixEnd = prefixBegin + maxPrefixSize;
285
286 prefixBuf[0] = '0';
287
288 switch (parsedSpec.formatType()) {
289 case Specification::e_POINTER_HEX: {
290 prefixBuf[1] = 'x';
291 } break;
292 case Specification::e_POINTER_HEX_UC: {
293 prefixBuf[1] = 'X';
294 } break;
295 default: {
296 BSLS_THROW(bsl::format_error("Invalid nullptr format type"));
297 }
298 }
299
300 // value
301
302 t_CHAR value = '0';
303 t_CHAR *valueBegin = &value;
304 t_CHAR *valueEnd = valueBegin + 1;
305
306 // output the result
307
308 return this->outputValue(prefixBegin,
309 prefixEnd,
310 valueBegin,
311 valueEnd,
312 formatContext);
313 }
314
315} // close package namespace
316
317
318#endif // INCLUDED_BSLFMT_FORMATTERPOINTER
319
320// ----------------------------------------------------------------------------
321// Copyright 2023 Bloomberg Finance L.P.
322//
323// Licensed under the Apache License, Version 2.0 (the "License");
324// you may not use this file except in compliance with the License.
325// You may obtain a copy of the License at
326//
327// http://www.apache.org/licenses/LICENSE-2.0
328//
329// Unless required by applicable law or agreed to in writing, software
330// distributed under the License is distributed on an "AS IS" BASIS,
331// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
332// See the License for the specific language governing permissions and
333// limitations under the License.
334// ----------------------------- END-OF-FILE ----------------------------------
335
336/** @} */
337/** @} */
338/** @} */
Definition bslfmt_standardformatspecification.h:78
#define BSLS_THROW(X)
Definition bsls_exceptionutil.h:374
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlat_valuetypefunctions.h:939
BloombergLP::bsls::Nullptr_Impl::Type nullptr_t
Definition bsls_nullptr.h:283
Definition bslfmt_enablestreamedformatter.h:130
Definition bslfmt_formatterbase.h:426
Definition bslfmt_formatterintegralbase.h:171
Definition bslfmt_formatterpointer.h:128
t_FORMAT_CONTEXT::iterator format(t_VALUE ptrValue, t_FORMAT_CONTEXT &formatContext) const
Definition bslfmt_formatterpointer.h:208
Definition bslfmt_formatterpointer.h:151
t_FORMAT_CONTEXT::iterator format(bsl::nullptr_t, t_FORMAT_CONTEXT &formatContext) const
Definition bslfmt_formatterpointer.h:271
std::size_t UintPtr
Definition bsls_types.h:128