BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdlb_guidformatter.h
Go to the documentation of this file.
1/// @file bdlb_guidformatter.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlb_guidformatter.h -*-C++-*-
8#ifndef INCLUDED_BDLB_GUIDFORMATTER
9#define INCLUDED_BDLB_GUIDFORMATTER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlb_guidformatter bdlb_guidformatter
15/// @brief Provide `bsl::formatter` specialization for `bdlb::Guid`.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlb
19/// @{
20/// @addtogroup bdlb_guidformatter
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlb_guidformatter-purpose"> Purpose</a>
25/// * <a href="#bdlb_guidformatter-classes"> Classes </a>
26/// * <a href="#bdlb_guidformatter-description"> Description </a>
27/// * <a href="#bdlb_guidformatter-format-specification"> Format Specification </a>
28/// * <a href="#bdlb_guidformatter-usage"> Usage </a>
29/// * <a href="#bdlb_guidformatter-example-1-formatting-a-bdlb-guid"> Example 1: Formatting a bdlb::Guid </a>
30///
31/// # Purpose {#bdlb_guidformatter-purpose}
32/// Provide `bsl::formatter` specialization for `bdlb::Guid`.
33///
34/// # Classes {#bdlb_guidformatter-classes}
35///
36/// - bsl::formatter<bdlb::Guid, t_CHAR>: specialization to format `bdlb::Guid`
37///
38/// @see bdlb_guid, bslfmt_format
39///
40/// # Description {#bdlb_guidformatter-description}
41/// This component provides a partial specialization of the
42/// `bsl::formatter` class template for `bdlb::Guid`, enabling `bdlb::Guid`
43/// values to be used with `bsl::format` and its variants.
44///
45/// ## Format Specification {#bdlb_guidformatter-format-specification}
46///
47///
48/// The supported format specification is a subset of the standard `std::format`
49/// grammar:
50/// @code
51/// format-spec ::= [ [fill] align ] [ width ]
52/// fill ::= any character other than '{' or '}'
53/// align ::= '<' | '>' | '^'
54/// width ::= non-negative-integer | '{' [ arg-id ] '}'
55/// @endcode
56/// No sign, alternate-form (`#`), zero-pad (`0`), precision, or type
57/// specifiers are supported. The default alignment is left, matching the
58/// convention for string-like types. The formatted representation is always
59/// the canonical 36-character lowercase hexadecimal form
60/// `xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx`.
61///
62/// ## Usage {#bdlb_guidformatter-usage}
63///
64///
65/// This section illustrates intended use of this component.
66///
67/// ### Example 1: Formatting a bdlb::Guid {#bdlb_guidformatter-example-1-formatting-a-bdlb-guid}
68///
69///
70/// Suppose we have a `bdlb::Guid` object and want to render it as a string:
71/// @code
72/// const unsigned char raw[bdlb::Guid::k_GUID_NUM_BYTES] = {
73/// 0x5c, 0x9d, 0x4e, 0x53, 0x0d, 0xf1, 0x11, 0xe4,
74/// 0x91, 0x91, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66 };
75/// bdlb::Guid guid(raw);
76///
77/// bsl::string s = bsl::format("{}", guid);
78/// assert(s == "5c9d4e53-0df1-11e4-9191-0800200c9a66");
79/// @endcode
80/// Width, alignment, and fill are supported as they would be for a string:
81/// @code
82/// bsl::string padded = bsl::format("{:*^40}", guid);
83/// assert(padded == "**5c9d4e53-0df1-11e4-9191-0800200c9a66**");
84/// @endcode
85/// @}
86/** @} */
87/** @} */
88
89/** @addtogroup bdl
90 * @{
91 */
92/** @addtogroup bdlb
93 * @{
94 */
95/** @addtogroup bdlb_guidformatter
96 * @{
97 */
98
99#include <bdlscm_version.h>
100
101#include <bdlb_guid.h>
102
103#include <bslfmt_format.h>
106#include <bslfmt_padutil.h>
107
108#include <bslmf_assert.h>
109#include <bslmf_issame.h>
110
111#include <bsls_keyword.h>
112#include <bsls_libraryfeatures.h>
113
114#include <bsl_iterator.h>
115#include <bsl_string_view.h>
116
117#include <cstddef> // for `std::ptrdiff_t`
118
119
120namespace bdlb {
121
122 // ==============================
123 // class Guid_BslFmtFormatterImpl
124 // ==============================
125
126/// This component-private class template provides the implementation of the
127/// `bsl::formatter` specialization for `bdlb::Guid` values. The specified
128/// `t_CHAR` template parameter determines both the format-string and the output character type.
129///
130/// \pre The behavior is undefined unless `t_CHAR` is
131/// `char` or `wchar_t`.
132///
133/// See @ref bdlb_guidformatter
134template <class t_CHAR>
136 private:
137 // PRIVATE TYPES
138
139 /// A type alias for the format-specification parser.
141
142 // DATA
143 Specification d_spec; // parsed specification
144
145 public:
146 // MANIPULATORS
147
148 /// Parse the format specification contained in the specified
149 /// `parseContext` and return an iterator, pointing to the beginning of the
150 /// unparsed section of the format string. Throw a `bsl::format_error`
151 /// exception if the specification does not conform to the grammar
152 /// documented at the top of this component.
153 template <class t_PARSE_CONTEXT>
154 BSLS_KEYWORD_CONSTEXPR_CPP20 typename t_PARSE_CONTEXT::iterator parse(
155 t_PARSE_CONTEXT& parseContext);
156
157 // ACCESSORS
158
159 /// Create a string representation of the specified `value` in accordance
160 /// with the format specification previously parsed by `parse` (see grammar
161 /// documented at the top of this component) and the specified
162 /// `formatContext`, and copy it to the output iterator held by
163 /// `formatContext`. Return the updated output iterator.
164 ///
165 /// \pre The behavior is undefined unless `t_FORMAT_CONTEXT::char_type` is `t_CHAR`.
166 template <class t_FORMAT_CONTEXT>
167 typename t_FORMAT_CONTEXT::iterator format(
168 const Guid& value,
169 t_FORMAT_CONTEXT& formatContext) const;
170};
171
172} // close package namespace
173
174
175
176// FORMATTER SPECIALIZATIONS
177namespace bsl {
178
179/// This template partial specialization defines `bsl::formatter` for
180/// `bdlb::Guid` values for both `char` and `wchar_t` character types.
181template <class t_CHAR>
182struct formatter<BloombergLP::bdlb::Guid, t_CHAR>
183: BloombergLP::bdlb::Guid_BslFmtFormatterImpl<t_CHAR> {
184};
185
186} // close namespace bsl
187
188#ifdef BSLS_LIBRARYFEATURES_HAS_CPP23_RANGE_FORMAT
189// Opt `bdlb::Guid` out of `std::formatter`'s range specialization; without
190// this, C++23 standard libraries would consider both this component's
191// `bsl::formatter<Guid, ...>` (via `bsl`'s `std::formatter<t_ARG, t_CHAR>`
192// bridge) and `std::formatter<Range, CharT>` as viable partial
193// specializations, because `bdlb::Guid` satisfies `std::ranges::input_range`.
194template <>
195inline constexpr std::range_format
196 std::format_kind<BloombergLP::bdlb::Guid> = std::range_format::disabled;
197#endif // BSLS_LIBRARYFEATURES_HAS_CPP23_RANGE_FORMAT
198
199
200// ============================================================================
201// INLINE DEFINITIONS
202// ============================================================================
203
204
205namespace bdlb {
206
207 // ------------------------------
208 // class Guid_BslFmtFormatterImpl
209 // ------------------------------
210
211// MANIPULATORS
212template <class t_CHAR>
213template <class t_PARSE_CONTEXT>
214BSLS_KEYWORD_CONSTEXPR_CPP20 typename t_PARSE_CONTEXT::iterator
215Guid_BslFmtFormatterImpl<t_CHAR>::parse(t_PARSE_CONTEXT& parseContext)
216{
217 const typename Specification::Sections sections =
218 static_cast<typename Specification::Sections>(
219 Specification::e_SECTIONS_FILL_ALIGN
220 | Specification::e_SECTIONS_WIDTH);
221
222 d_spec.parse(&parseContext, sections);
223
224 return parseContext.begin();
225}
226
227// ACCESSORS
228template <class t_CHAR>
229template <class t_FORMAT_CONTEXT>
230typename t_FORMAT_CONTEXT::iterator
232 t_FORMAT_CONTEXT& formatContext) const
233{
235 typename t_FORMAT_CONTEXT::char_type>::value));
236
237 typedef bslfmt::PadUtil<t_CHAR> PadUtil;
238 typedef bslfmt::FormatterCharUtil<t_CHAR> CharUtil;
239
240 // Postprocess a *copy* of the parsed spec so that this formatter object
241 // may be reused across multiple `format` invocations with different
242 // format contexts (each carrying different nested-argument values for
243 // width).
244
245 Specification finalSpec(d_spec);
246 finalSpec.postprocess(formatContext);
247
249 finalSpec.filler(),
250 finalSpec.numFillerCharacters());
251
252 std::ptrdiff_t leftPad = 0;
253 std::ptrdiff_t rightPad = 0;
254 PadUtil::computePadding(&leftPad,
255 &rightPad,
256 finalSpec.postprocessedWidth(),
258 finalSpec.alignment(),
259 Specification::e_ALIGN_LEFT);
260
261 // Render the guid as ASCII. `Guid::format` always writes exactly
262 // `k_GUID_NUM_CHARS` characters and never a trailing null.
263
264 char buffer[Guid::k_GUID_NUM_CHARS];
265 value.format(buffer);
266
267 typename t_FORMAT_CONTEXT::iterator outIt = formatContext.out();
268
269 outIt = PadUtil::pad(outIt, leftPad, filler);
270 outIt = CharUtil::outputFromChar(bsl::begin(buffer),
271 bsl::end(buffer),
272 outIt);
273 outIt = PadUtil::pad(outIt, rightPad, filler);
274
275 return outIt;
276}
277
278} // close package namespace
279
280
281#endif // INCLUDED_BDLB_GUIDFORMATTER
282
283// ----------------------------------------------------------------------------
284// Copyright 2026 Bloomberg Finance L.P.
285//
286// Licensed under the Apache License, Version 2.0 (the "License");
287// you may not use this file except in compliance with the License.
288// You may obtain a copy of the License at
289//
290// http://www.apache.org/licenses/LICENSE-2.0
291//
292// Unless required by applicable law or agreed to in writing, software
293// distributed under the License is distributed on an "AS IS" BASIS,
294// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
295// See the License for the specific language governing permissions and
296// limitations under the License.
297// ----------------------------- END-OF-FILE ----------------------------------
298
299/** @} */
300/** @} */
301/** @} */
Definition bdlb_guidformatter.h:135
t_FORMAT_CONTEXT::iterator format(const Guid &value, t_FORMAT_CONTEXT &formatContext) const
Definition bdlb_guidformatter.h:231
BSLS_KEYWORD_CONSTEXPR_CPP20 t_PARSE_CONTEXT::iterator parse(t_PARSE_CONTEXT &parseContext)
Definition bdlb_guidformatter.h:215
Definition bdlb_guid.h:201
void format(bsl::span< char, k_GUID_NUM_CHARS > buffer) const
@ k_GUID_NUM_CHARS
Definition bdlb_guid.h:207
Definition bslstl_stringview.h:471
Definition bslfmt_formatspecificationparser.h:151
void postprocess(const t_FORMAT_CONTEXT &context)
Definition bslfmt_formatspecificationparser.h:994
BSLS_KEYWORD_CONSTEXPR_CPP20 const t_CHAR * filler() const
Definition bslfmt_formatspecificationparser.h:1018
BSLS_KEYWORD_CONSTEXPR_CPP20 int numFillerCharacters() const
Definition bslfmt_formatspecificationparser.h:1030
BSLS_KEYWORD_CONSTEXPR_CPP20 const FormatterSpecificationNumericValue postprocessedWidth() const
Definition bslfmt_formatspecificationparser.h:1108
BSLS_KEYWORD_CONSTEXPR_CPP20 Alignment alignment() const
Definition bslfmt_formatspecificationparser.h:1058
#define BSLMF_ASSERT(expr)
Definition bslmf_assert.h:231
#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
Definition bdlb_algorithmworkaroundutil.h:74
Definition bdlat_valuetypefunctions.h:939
T::iterator begin(T &container)
Definition bslstl_iterator.h:1593
T::iterator end(T &container)
Definition bslstl_iterator.h:1621
Definition bslfmt_formatterbase.h:426
Definition bslmf_issame.h:146
Sections
Definition bslfmt_formatspecificationparser.h:128
Definition bslfmt_formattercharutil.h:138
Definition bslfmt_padutil.h:151