BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslstl_iomanip.h
Go to the documentation of this file.
1/// @file bslstl_iomanip.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslstl_iomanip.h -*-C++-*-
8#ifndef INCLUDED_BSLSTL_IOMANIP
9#define INCLUDED_BSLSTL_IOMANIP
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslstl_iomanip bslstl_iomanip
15/// @brief Provide BSL implementations for standard <iomanip> features.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslstl
19/// @{
20/// @addtogroup bslstl_iomanip
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslstl_iomanip-purpose"> Purpose</a>
25/// * <a href="#bslstl_iomanip-classes"> Classes </a>
26/// * <a href="#bslstl_iomanip-description"> Description </a>
27/// * <a href="#bslstl_iomanip-usage"> Usage </a>
28/// * <a href="#bslstl_iomanip-example-1-basic-use-of-bsl-quoted"> Example 1: Basic Use of bsl::quoted </a>
29///
30/// # Purpose {#bslstl_iomanip-purpose}
31/// Provide BSL implementations for standard <iomanip> features.
32///
33/// # Classes {#bslstl_iomanip-classes}
34///
35///
36/// **Canonical header:** bsl_iomanip.h
37///
38/// @see bsl+bslhdrs
39///
40///
41/// # Description {#bslstl_iomanip-description}
42/// This component is for internal use only. Please include
43/// `<bsl_iomanip.h>` instead.
44///
45/// This component exists to provide BSL implementations for facilities in the
46/// standard <iomanip> header. While most of the facilities in `bsl_iomanip.h`
47/// are simply aliases to the platform standard library, some of them need BSL
48/// specific implementations. For example, this component provides
49/// implementations for `bsl::quoted` overloads that accept `bsl::basic_string`
50/// and proprietary `bsl::string_view` objects.
51///
52/// ## Usage {#bslstl_iomanip-usage}
53///
54///
55/// This section illustrates intended use of this component.
56///
57/// ### Example 1: Basic Use of bsl::quoted {#bslstl_iomanip-example-1-basic-use-of-bsl-quoted}
58///
59///
60/// Suppose we want to serialize some data into JSON.
61///
62/// First, we define a struct, `Employee`, to contain the data:
63/// @code
64/// struct Employee {
65/// bsl::string d_firstName;
66/// bsl::string d_lastName;
67/// int d_age;
68/// };
69/// @endcode
70/// Then, we create an `Employee` object and populate it with data:
71/// @code
72/// Employee john;
73/// john.d_firstName = "John";
74/// john.d_lastName = "Doe";
75/// john.d_age = 20;
76/// @endcode
77/// Now, we create an output stream and manually construct the JSON string
78/// using `bsl::quoted`:
79/// @code
80/// bsl::stringstream ss;
81/// ss << '{' << '\n';
82/// ss << bsl::quoted("firstName");
83/// ss << ':';
84/// ss << bsl::quoted(john.d_firstName);
85/// ss << ',' << '\n';
86/// ss << bsl::quoted("lastName");
87/// ss << ':';
88/// ss << bsl::quoted(john.d_lastName);
89/// ss << ',' << '\n';
90/// ss << bsl::quoted("age");
91/// ss << ':';
92/// ss << john.d_age;
93/// ss << '\n' << '}';
94/// @endcode
95/// Finally, we check out the JSON string:
96/// @code
97/// bsl::string expected = "{\n"
98/// "\"firstName\":\"John\",\n"
99/// "\"lastName\":\"Doe\",\n"
100/// "\"age\":20\n"
101/// "}";
102/// assert(expected == ss.str());
103/// @endcode
104/// The output should look like:
105/// @code
106/// {
107/// "firstName":"John",
108/// "lastName":"Doe",
109/// "age":20
110/// }
111/// @endcode
112/// @}
113/** @} */
114/** @} */
115
116/** @addtogroup bsl
117 * @{
118 */
119/** @addtogroup bslstl
120 * @{
121 */
122/** @addtogroup bslstl_iomanip
123 * @{
124 */
125
126#include <bslstl_string.h>
127#include <bslstl_stringview.h>
128
129#include <bsls_libraryfeatures.h>
130#include <bsls_platform.h>
131
132#include <iomanip>
133#include <istream>
134#include <ostream>
135
136namespace bsl {
137 // Import selected symbols into bsl namespace
138
139 using std::resetiosflags;
140 using std::setbase;
141 using std::setfill;
142 using std::setiosflags;
143 using std::setprecision;
144 using std::setw;
145
146#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
147 using std::get_money;
148 using std::put_money;
149#endif // BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
150
151#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_MISCELLANEOUS_UTILITIES
152 using std::get_time;
153 using std::put_time;
154#endif // BSLS_LIBRARYFEATURES_HAS_CPP11_MISCELLANEOUS_UTILITIES
155
156#ifndef BDE_OMIT_INTERNAL_DEPRECATED
157 // Export additional names, leaked to support transitive dependencies in
158 // higher level (non BDE) Bloomberg code.
159# if !defined(BSLS_PLATFORM_CMP_MSVC) && __cplusplus < 201703L
160 // As some of these names are removed from C++17, take a sledgehammer to
161 // crack this nut, and remove all non-standard exports.
162 using std::bad_exception;
163 using std::basic_ios;
164 using std::basic_iostream;
165 using std::basic_istream;
166 using std::basic_ostream;
167 using std::basic_streambuf;
168 using std::bidirectional_iterator_tag;
169 using std::ctype;
170 using std::ctype_base;
171 using std::ctype_byname;
172 using std::exception;
173 using std::forward_iterator_tag;
174 using std::input_iterator_tag;
175 using std::ios_base;
176 using std::istreambuf_iterator;
177 using std::iterator;
178 using std::locale;
179 using std::num_get;
180 using std::numpunct;
181 using std::numpunct_byname;
182 using std::ostreambuf_iterator;
183 using std::output_iterator_tag;
184 using std::random_access_iterator_tag;
185 using std::set_terminate;
186 using std::set_unexpected;
187 using std::swap;
188 using std::terminate;
189 using std::terminate_handler;
190 using std::uncaught_exception;
191 using std::unexpected;
192 using std::unexpected_handler;
193 using std::use_facet;
194# endif // MSVC, or C++2017
195#endif // BDE_OMIT_INTERNAL_DEPRECATED
196
197#if defined BSLS_LIBRARYFEATURES_HAS_CPP17_BASELINE_LIBRARY
198 using std::quoted;
199#endif // BSLS_LIBRARYFEATURES_HAS_CPP17_BASELINE_LIBRARY
200
201} // close namespace bsl
202
203
204namespace bslstl {
205#if defined BSLS_LIBRARYFEATURES_HAS_CPP17_BASELINE_LIBRARY
206// 'std::quoted' function does not support the 'bsl::basic_string' and
207// proprietary implementation of the 'bsl::basic_string_view' classes. To fix
208// this we need to add special overloads that return objects of the special
209// classes, that can be used in stream input/output operations and construct a
210// bridge between 'std' and 'bsl' 'string'/@ref string_view implementations.
211
212 // ===================================
213 // class IoManip_QuotedStringFormatter
214 // ===================================
215
216/// Private class: do not use outside of `bslstl_iomanip.h` header. This
217/// private class provides a temporary storage that can be extracted to/from
218/// a stream and provides data to the standard implementation of the
219/// `quoted` function. Note that this class does not contain the storage
220/// itself, but only points to an external object. Note that
221/// `QuotedStringViewFormatter` is designed so that its objects are returned
222/// as temporary objects (of opaque type) from 'bsl::quoted`, and then
223/// passed to a streaming operator; it serves as a temporary proxy for a
224/// string value.
225///
226/// See @ref bslstl_iomanip
227template <class t_CHAR_TYPE, class t_CHAR_TRAITS, class t_ALLOC>
228class IoManip_QuotedStringFormatter {
229
230 // DATA
231
232 // string for quoting (held, not owned)
234
235 // delimiter
236 t_CHAR_TYPE d_delim;
237
238 // escape character
239 t_CHAR_TYPE d_escape;
240
241 public:
242 // CREATORS
243
244 /// Create an object pointing to the specified `str` and having the
245 /// optionally specified `delim` and `escape` characters.
246 explicit IoManip_QuotedStringFormatter(
248 t_CHAR_TYPE delim =
249 t_CHAR_TYPE('"'),
250 t_CHAR_TYPE escape =
251 t_CHAR_TYPE('\\'));
252
253 // ACCESSORS
254
255 /// Return the delimiter character.
256 t_CHAR_TYPE delim() const;
257
258 /// Return the escape character.
259 t_CHAR_TYPE escape() const;
260
261 /// Return a reference providing modifiable access to the underlying
262 /// string. Note that this operation provides modifiable access to the
263 /// string because this type is designed so that its objects are created
264 /// as temporary objects that proxy an underlying string (see class
265 /// documentation).
267};
268
269 // =======================================
270 // class IoManip_QuotedStringViewFormatter
271 // =======================================
272
273/// Private class: do not use outside of `bslstl_iomanip.h` header. This
274/// private class provides a temporary storage that can be extracted to a
275/// stream and provides data to the standard implementation of the `quoted`
276/// function. Note that `QuotedStringViewFormatter` is designed to be
277/// returned as a temporary object (of opaque type) from 'bsl::quoted`, and
278/// passed to a streaming operator; it serves as a temporary proxy for a
279/// string value. Also note that this value is copied to `bsl::string` data
280/// member that is owned by the object of this class.
281///
282/// See @ref bslstl_iomanip
283template <class t_CHAR_TYPE, class t_CHAR_TRAITS>
284class IoManip_QuotedStringViewFormatter {
285
286 // DATA
288 // quoting
289
290 t_CHAR_TYPE d_delim; // delimiter
291
292 t_CHAR_TYPE d_escape; // escape
293 // character
294
295 public:
296 // CREATORS
297
298 /// Create an object having the value of the specified `strView` and the
299 /// optionally specified `delim` and `escape` characters.
300 explicit IoManip_QuotedStringViewFormatter(
302 t_CHAR_TYPE delim =
303 t_CHAR_TYPE('"'),
304 t_CHAR_TYPE escape =
305 t_CHAR_TYPE('\\'));
306
307 // ACCESSORS
308
309 /// Return a reference providing modifiable access to the character
310 /// buffer of the underlying string.
311 const t_CHAR_TYPE *data() const;
312
313 /// Return the delimiter character.
314 t_CHAR_TYPE delim() const;
315
316 /// Return the escape character.
317 t_CHAR_TYPE escape() const;
318};
319
320// FREE FUNCTIONS
321
322/// Read quoted string from the specified `input` stream into the underlying
323/// string of the specified `object`. Note that the `object` here is
324/// `const` because the `QuotedStringFormatter` is designed to be returned
325/// as a temporary object from 'bsl::quoted`, serving as a proxy for the
326/// underlying string.
327template <class t_CHAR_TYPE, class t_CHAR_TRAITS, class t_ALLOC>
328std::basic_istream<t_CHAR_TYPE, t_CHAR_TRAITS>& operator>>(
329 std::basic_istream<t_CHAR_TYPE, t_CHAR_TRAITS>& input,
330 const IoManip_QuotedStringFormatter<t_CHAR_TYPE,
331 t_CHAR_TRAITS,
332 t_ALLOC>& object);
333
334/// Write the value of the specified `object` to the specified `output`
335/// stream.
336template <class t_CHAR_TYPE, class t_CHAR_TRAITS, class t_ALLOC>
337std::basic_ostream<t_CHAR_TYPE, t_CHAR_TRAITS>& operator<<(
338 std::basic_ostream<t_CHAR_TYPE, t_CHAR_TRAITS>& output,
339 const IoManip_QuotedStringFormatter<t_CHAR_TYPE,
340 t_CHAR_TRAITS,
341 t_ALLOC>& object);
342
343/// Write the value of the specified `object` to the specified `output`
344/// stream.
345template <class t_CHAR_TYPE, class t_CHAR_TRAITS>
346std::basic_ostream<t_CHAR_TYPE, t_CHAR_TRAITS>& operator<<(
347 std::basic_ostream<t_CHAR_TYPE, t_CHAR_TRAITS>& output,
348 const IoManip_QuotedStringViewFormatter<t_CHAR_TYPE,
349 t_CHAR_TRAITS>& object);
350
351#endif // BSLS_LIBRARYFEATURES_HAS_CPP17_BASELINE_LIBRARY
352} // close package namespace
353
354
355namespace bsl {
356#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_BASELINE_LIBRARY
357/// Return an object, containing quoted version of the specified `value`
358/// obtained using the optionally specified `delim` and `escape` characters,
359/// and that can be inserted to output stream.
360/// Return an object, containing quoted version of the specified `value`
361/// obtained using the optionally specified `delim` and `escape` characters,
362/// and that can be inserted to output stream.
363template <class t_CHAR_TYPE, class t_CHAR_TRAITS, class t_ALLOC>
364decltype(auto)
366 t_CHAR_TYPE delim =
367 t_CHAR_TYPE('"'),
368 t_CHAR_TYPE escape =
369 t_CHAR_TYPE('\\'));
370
371/// Return an object, containing quoted version of the specified `value`
372/// obtained using the optionally specified `delim` and `escape` characters,
373/// and that can be inserted to output (or extracted from input) stream.
374/// Return an object, containing quoted version of the specified `value`
375/// obtained using the optionally specified `delim` and `escape` characters,
376/// and that can be inserted to output (or extracted from input) stream.
377template <class t_CHAR_TYPE, class t_CHAR_TRAITS, class t_ALLOC>
378BloombergLP::bslstl::IoManip_QuotedStringFormatter<t_CHAR_TYPE,
379 t_CHAR_TRAITS,
380 t_ALLOC>
382 t_CHAR_TYPE delim =
383 t_CHAR_TYPE('"'),
384 t_CHAR_TYPE escape =
385 t_CHAR_TYPE('\\'));
386
387#ifndef BSLS_LIBRARYFEATURES_HAS_CPP20_BASELINE_LIBRARY
388template <class t_CHAR_TYPE, class t_CHAR_TRAITS>
389BloombergLP::bslstl::IoManip_QuotedStringViewFormatter<t_CHAR_TYPE,
390 t_CHAR_TRAITS>
392 t_CHAR_TYPE delim =
393 t_CHAR_TYPE('"'),
394 t_CHAR_TYPE escape =
395 t_CHAR_TYPE('\\'));
396 // Return an object, containing quoted version of the specified 'value'
397 // obtained using the optionally specified 'delim' and 'escape' characters,
398 // and that can be inserted to output stream.
399#endif // BSLS_LIBRARYFEATURES_HAS_CPP20_BASELINE_LIBRARY
400#endif // BSLS_LIBRARYFEATURES_HAS_CPP17_BASELINE_LIBRARY
401} // close namespace bsl
402
403// ============================================================================
404// INLINE FUNCTION DEFINITIONS
405// ============================================================================
406
407#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_BASELINE_LIBRARY
408 // -----------------------------------
409 // class IoManip_QuotedStringFormatter
410 // -----------------------------------
411
412// CREATORS
413template <class t_CHAR_TYPE, class t_CHAR_TRAITS, class t_ALLOC>
414inline
415BloombergLP::bslstl::
416 IoManip_QuotedStringFormatter<t_CHAR_TYPE, t_CHAR_TRAITS, t_ALLOC>::
417 IoManip_QuotedStringFormatter(
419 t_CHAR_TYPE delim,
420 t_CHAR_TYPE escape)
421: d_str_p(str)
422, d_delim(delim)
423, d_escape(escape)
424{
425}
426
427// ACCESSORS
428template <class t_CHAR_TYPE, class t_CHAR_TRAITS, class t_ALLOC>
429inline
430t_CHAR_TYPE
431BloombergLP::bslstl::IoManip_QuotedStringFormatter<t_CHAR_TYPE,
432 t_CHAR_TRAITS,
433 t_ALLOC>::delim() const
434{
435 return d_delim;
436}
437
438template <class t_CHAR_TYPE, class t_CHAR_TRAITS, class t_ALLOC>
439inline
440t_CHAR_TYPE
441BloombergLP::bslstl::IoManip_QuotedStringFormatter<t_CHAR_TYPE,
442 t_CHAR_TRAITS,
443 t_ALLOC>::escape() const
444{
445 return d_escape;
446}
447
448template <class t_CHAR_TYPE, class t_CHAR_TRAITS, class t_ALLOC>
449inline
451BloombergLP::bslstl::IoManip_QuotedStringFormatter<t_CHAR_TYPE,
452 t_CHAR_TRAITS,
453 t_ALLOC>::str() const
454{
455 return d_str_p;
456}
457
458 // ---------------------------------------
459 // class IoManip_QuotedStringViewFormatter
460 // ---------------------------------------
461
462// CREATORS
463template <class t_CHAR_TYPE, class t_CHAR_TRAITS>
464inline
465BloombergLP::bslstl::IoManip_QuotedStringViewFormatter<t_CHAR_TYPE,
466 t_CHAR_TRAITS>::
467 IoManip_QuotedStringViewFormatter(
469 t_CHAR_TYPE delim,
470 t_CHAR_TYPE escape)
471: d_str(strView)
472, d_delim(delim)
473, d_escape(escape)
474{
475}
476
477// ACCESSORS
478template <class t_CHAR_TYPE, class t_CHAR_TRAITS>
479inline
480const t_CHAR_TYPE *
481BloombergLP::bslstl::IoManip_QuotedStringViewFormatter<t_CHAR_TYPE,
482 t_CHAR_TRAITS>::data()
483 const
484{
485 return d_str.c_str();
486}
487
488template <class t_CHAR_TYPE, class t_CHAR_TRAITS>
489inline
490t_CHAR_TYPE
491BloombergLP::bslstl::IoManip_QuotedStringViewFormatter<t_CHAR_TYPE,
492 t_CHAR_TRAITS>::delim()
493 const
494{
495 return d_delim;
496}
497
498template <class t_CHAR_TYPE, class t_CHAR_TRAITS>
499inline
500t_CHAR_TYPE
501BloombergLP::bslstl::IoManip_QuotedStringViewFormatter<t_CHAR_TYPE,
502 t_CHAR_TRAITS>::escape()
503 const
504{
505 return d_escape;
506}
507
508// FREE FUNCTIONS
509template <class t_CHAR_TYPE, class t_CHAR_TRAITS, class t_ALLOC>
510std::basic_istream<t_CHAR_TYPE, t_CHAR_TRAITS>&
511BloombergLP::bslstl::operator>>(
512 std::basic_istream<t_CHAR_TYPE, t_CHAR_TRAITS>& input,
513 const IoManip_QuotedStringFormatter<t_CHAR_TYPE,
514 t_CHAR_TRAITS,
515 t_ALLOC>& object)
516{
517 std::basic_string<t_CHAR_TYPE, t_CHAR_TRAITS> temp;
518 input >> std::quoted(temp, object.delim(), object.escape());
519 *object.str() = temp;
520 return input;
521}
522
523template <class t_CHAR_TYPE, class t_CHAR_TRAITS, class t_ALLOC>
524std::basic_ostream<t_CHAR_TYPE, t_CHAR_TRAITS>&
525BloombergLP::bslstl::operator<<(
526 std::basic_ostream<t_CHAR_TYPE, t_CHAR_TRAITS>& output,
527 const IoManip_QuotedStringFormatter<t_CHAR_TYPE,
528 t_CHAR_TRAITS,
529 t_ALLOC>& object)
530{
531 output << std::quoted(object.str()->c_str(),
532 object.delim(),
533 object.escape());
534 return output;
535}
536
537template <class t_CHAR_TYPE, class t_CHAR_TRAITS>
538std::basic_ostream<t_CHAR_TYPE, t_CHAR_TRAITS>&
539BloombergLP::bslstl::operator<<(
540 std::basic_ostream<t_CHAR_TYPE, t_CHAR_TRAITS>& output,
541 const IoManip_QuotedStringViewFormatter<t_CHAR_TYPE, t_CHAR_TRAITS>& object)
542{
543 output << std::quoted(object.data(), object.delim(), object.escape());
544 return output;
545}
546
547 // -----------------------------
548 // 'bsl::quoted' implementations
549 // -----------------------------
550
551template <class t_CHAR_TYPE, class t_CHAR_TRAITS, class t_ALLOC>
552decltype(auto)
553bsl::quoted(
555 t_CHAR_TYPE delim,
556 t_CHAR_TYPE escape)
557{
558 return bsl::quoted(value.c_str(), delim, escape);
559}
560
561template <class t_CHAR_TYPE, class t_CHAR_TRAITS, class t_ALLOC>
562BloombergLP::bslstl::IoManip_QuotedStringFormatter<t_CHAR_TYPE,
563 t_CHAR_TRAITS,
564 t_ALLOC>
566 t_CHAR_TYPE delim,
567 t_CHAR_TYPE escape)
568{
569 return BloombergLP::bslstl::IoManip_QuotedStringFormatter<t_CHAR_TYPE,
570 t_CHAR_TRAITS,
571 t_ALLOC>(&value,
572 delim,
573 escape);
574}
575
576#ifndef BSLS_LIBRARYFEATURES_HAS_CPP20_BASELINE_LIBRARY
577template <class t_CHAR_TYPE, class t_CHAR_TRAITS>
578BloombergLP::bslstl::IoManip_QuotedStringViewFormatter<t_CHAR_TYPE,
579 t_CHAR_TRAITS>
581 t_CHAR_TYPE delim,
582 t_CHAR_TYPE escape)
583 // Return an object, containing quoted version of the specified 'value'
584 // obtained using the optionally specified 'delim' and 'escape' characters,
585 // and that can be inserted to output stream.
586{
587 return BloombergLP::bslstl::IoManip_QuotedStringViewFormatter<
588 t_CHAR_TYPE,
589 t_CHAR_TRAITS>(value,
590 delim,
591 escape);
592}
593#endif // BSLS_LIBRARYFEATURES_HAS_CPP20_BASELINE_LIBRARY
594#endif // BSLS_LIBRARYFEATURES_HAS_CPP17_BASELINE_LIBRARY
595
596#endif
597
598// ----------------------------------------------------------------------------
599// Copyright 2022 Bloomberg Finance L.P.
600//
601// Licensed under the Apache License, Version 2.0 (the "License");
602// you may not use this file except in compliance with the License.
603// You may obtain a copy of the License at
604//
605// http://www.apache.org/licenses/LICENSE-2.0
606//
607// Unless required by applicable law or agreed to in writing, software
608// distributed under the License is distributed on an "AS IS" BASIS,
609// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
610// See the License for the specific language governing permissions and
611// limitations under the License.
612// ----------------------------- END-OF-FILE ----------------------------------
613
614/** @} */
615/** @} */
616/** @} */
Definition bslstl_stringview.h:441
Definition bslstl_string.h:1281
const CHAR_TYPE * c_str() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_string.h:6705
bsl::ostream & operator<<(bsl::ostream &stream, const bdlat_AttributeInfo &attributeInfo)
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
BitArray operator>>(const BitArray &array, bsl::size_t numBits)
Definition bdlb_printmethods.h:283
BSLS_KEYWORD_CONSTEXPR CONTAINER::value_type * data(CONTAINER &container)
Definition bslstl_iterator.h:1231
Definition bslstl_algorithm.h:82
void swap(TYPE &a, TYPE &b)