BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdlb_optionalprinter.h
Go to the documentation of this file.
1/// @file bdlb_optionalprinter.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlb_optionalprinter.h -*-C++-*-
8#ifndef INCLUDED_BDLB_OPTIONALPRINTER
9#define INCLUDED_BDLB_OPTIONALPRINTER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlb_optionalprinter bdlb_optionalprinter
15/// @brief Provide a suite of helper classes for printing `bsl::optional`.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlb
19/// @{
20/// @addtogroup bdlb_optionalprinter
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlb_optionalprinter-purpose"> Purpose</a>
25/// * <a href="#bdlb_optionalprinter-classes"> Classes </a>
26/// * <a href="#bdlb_optionalprinter-description"> Description </a>
27/// * <a href="#bdlb_optionalprinter-usage"> Usage </a>
28/// * <a href="#bdlb_optionalprinter-example-1-printing-bsl-optional-to-a-stream"> Example 1: Printing bsl::optional to a Stream </a>
29///
30/// # Purpose {#bdlb_optionalprinter-purpose}
31/// Provide a suite of helper classes for printing `bsl::optional`.
32///
33/// # Classes {#bdlb_optionalprinter-classes}
34///
35/// - bdlb::OptionalPrinter: utility for printing `bsl::optional`
36/// - bdlb::OptionalPrinterUtil: factory for constructing `bdlb::OptionalPrinter`
37///
38/// # Description {#bdlb_optionalprinter-description}
39/// This component provides utility classes `bdlb::OptionalPrinter`
40/// and `bdlb::OptionalPrinterUtil` for printing `bsl::optional`.
41///
42/// The class `bdlb::OptionalPrinter` can also be used with `bsl::format` (it
43/// defines the `bslfmt::EnableStreamedFormatter` trait). For information about
44/// the supported format options see
45/// [the `bslfmt` package documentation](@ref bslfmt-streaming-based-formatting) .
46///
47/// ## Usage {#bdlb_optionalprinter-usage}
48///
49///
50/// This section illustrates intended use of this component.
51///
52/// ### Example 1: Printing bsl::optional to a Stream {#bdlb_optionalprinter-example-1-printing-bsl-optional-to-a-stream}
53///
54///
55/// In this example, we demonstrate how to use `bdlb::OptionalPrinterUtil` to
56/// print `bsl::optional` to a stream:
57/// @code
58/// bsl::optional<int> value(42);
59/// bsl::cout << bdlb::OptionalPrinterUtil::makePrinter(value);
60/// @endcode
61/// @}
62/** @} */
63/** @} */
64
65/** @addtogroup bdl
66 * @{
67 */
68/** @addtogroup bdlb
69 * @{
70 */
71/** @addtogroup bdlb_optionalprinter
72 * @{
73 */
74
75#include <bdlscm_version.h>
76
77#include <bdlb_printmethods.h>
78
81#include <bsls_assert.h>
82
83#include <bsl_optional.h>
84#include <bsl_ostream.h>
85
86
87namespace bdlb {
88
89 // ======================
90 // struct OptionalPrinter
91 // ======================
92
93/// Utility for printing `bsl::optional` to standard output streams. This
94/// class has `operator<<` defined for it, so it can be used, for example,
95/// in `ball` logs.
96///
97/// See @ref bdlb_optionalprinter
98template <class TYPE>
100
101 // DATA
102 const bsl::optional<TYPE>* d_data_p;
103
104 public:
105 // TRAITS
108
109 // CREATORS
110
111 /// Create `OptionalPrinter` with the specified `data`.
112 explicit OptionalPrinter(const bsl::optional<TYPE> *data);
113
114 // ACCESSORS
115
116 /// Format this object to the specified output `stream` at the (absolute
117 /// value of) the optionally specified indentation `level` and return a
118 /// reference to `stream`. If `level` is specified, optionally specify
119 /// `spacesPerLevel`, the number of spaces per indentation level for
120 /// this and all of its nested objects. If `level` is negative,
121 /// suppress indentation of the first line. If `spacesPerLevel` is
122 /// negative, format the entire output on one line, suppressing all but
123 /// the initial indentation (as governed by `level`). If `stream` is
124 /// not valid on entry, this operation has no effect.
125 bsl::ostream& print(bsl::ostream& stream,
126 int level = 0,
127 int spacesPerLevel = 4) const;
128};
129
130// FREE OPERATORS
131
132/// Write the value of the specified `printer` object to the specified
133/// output `stream` in a single-line format, and return a reference to
134/// `stream`. If `stream` is not valid on entry, this operation has no effect.
135///
136/// \note Note that this human-readable format is not fully specified,
137/// can change without notice, and is logically equivalent to:
138/// @code
139/// print(stream, 0, -1);
140/// @endcode
141template <class TYPE>
142bsl::ostream&
143operator<<(bsl::ostream& stream, const OptionalPrinter<TYPE>& printer);
144
145 // ==========================
146 // struct OptionalPrinterUtil
147 // ==========================
148
149/// This utility `struct` provides a namespace for a function that creates a
150/// `bdlb::OptionalPrinter` with its template argument deduced from a given
151/// instance of `bsl::optional`.
152///
153/// See @ref bdlb_optionalprinter
155 public:
156 // CLASS METHODS
157
158 /// Return an `OptionalPrinter` that prints the specified `data`.
159 template <class TYPE>
161};
162
163// ============================================================================
164// INLINE DEFINITIONS
165// ============================================================================
166
167 // ----------------------
168 // struct OptionalPrinter
169 // ----------------------
170
171// CREATORS
172template <class TYPE>
174: d_data_p(data)
175{
176 BSLS_ASSERT(data);
177}
178
179// ACCESSORS
180template <class TYPE>
181bsl::ostream& OptionalPrinter<TYPE>::print(bsl::ostream& stream,
182 int level,
183 int spacesPerLevel) const
184{
185 if (!d_data_p->has_value()) {
186 return bdlb::PrintMethods::print(stream,
187 "NULL",
188 level,
189 spacesPerLevel); // RETURN
190 }
191
192 return bdlb::PrintMethods::print(stream,
193 *(*d_data_p),
194 level,
195 spacesPerLevel);
196}
197
198 // --------------------------
199 // struct OptionalPrinterUtil
200 // --------------------------
201
202// CLASS METHODS
203template <class TYPE>
209
210} // close package namespace
211
212// FREE OPERATORS
213template <class TYPE>
214bsl::ostream& bdlb::operator<<(bsl::ostream& stream,
215 const bdlb::OptionalPrinter<TYPE>& object)
216{
217 return object.print(stream, 0, -1);
218}
219
220
221
222#endif // INCLUDED_BDLB_OPTIONALPRINTER
223
224// ----------------------------------------------------------------------------
225// Copyright 2021 Bloomberg Finance L.P.
226//
227// Licensed under the Apache License, Version 2.0 (the "License");
228// you may not use this file except in compliance with the License.
229// You may obtain a copy of the License at
230//
231// http://www.apache.org/licenses/LICENSE-2.0
232//
233// Unless required by applicable law or agreed to in writing, software
234// distributed under the License is distributed on an "AS IS" BASIS,
235// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
236// See the License for the specific language governing permissions and
237// limitations under the License.
238// ----------------------------- END-OF-FILE ----------------------------------
239
240/** @} */
241/** @} */
242/** @} */
Definition bdlb_optionalprinter.h:99
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
Definition bdlb_optionalprinter.h:181
OptionalPrinter(const bsl::optional< TYPE > *data)
Create OptionalPrinter with the specified data.
Definition bdlb_optionalprinter.h:173
BSLMF_NESTED_TRAIT_DECLARATION(OptionalPrinter, bslfmt::EnableStreamedFormatter)
Definition bslstl_optional.h:2043
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
bsl::ostream & print(bsl::ostream &stream, const TYPE &object, int level=0, int spacesPerLevel=4)
Definition bdlb_printmethods.h:725
Definition bdlb_algorithmworkaroundutil.h:74
bsl::ostream & operator<<(bsl::ostream &stream, const BigEndianInt16 &integer)
Definition bdlb_optionalprinter.h:154
static OptionalPrinter< TYPE > makePrinter(const bsl::optional< TYPE > &data)
Return an OptionalPrinter that prints the specified data.
Definition bdlb_optionalprinter.h:205
Definition bslfmt_enablestreamedformatter.h:141