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