BDE 4.14.0 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/// ## Usage {#bdlb_optionalprinter-usage}
43///
44///
45/// This section illustrates intended use of this component.
46///
47/// ### Example 1: Printing bsl::optional to a stream {#bdlb_optionalprinter-example-1-printing-bsl-optional-to-a-stream}
48///
49///
50/// In this example, we demonstrate how to use `bdlb::OptionalPrinterUtil` to
51/// print `bsl::optional` to a stream:
52/// @code
53/// bsl::optional<int> value(42);
54/// bsl::cout << bdlb::OptionalPrinterUtil::makePrinter(value);
55/// @endcode
56/// @}
57/** @} */
58/** @} */
59
60/** @addtogroup bdl
61 * @{
62 */
63/** @addtogroup bdlb
64 * @{
65 */
66/** @addtogroup bdlb_optionalprinter
67 * @{
68 */
69
70#include <bdlscm_version.h>
71
72#include <bdlb_printmethods.h>
73
74#include <bsls_assert.h>
75
76#include <bsl_optional.h>
77#include <bsl_ostream.h>
78
79
80namespace bdlb {
81
82 // ======================
83 // struct OptionalPrinter
84 // ======================
85
86/// Utility for printing `bsl::optional` to standard output streams. This
87/// class has `operator<<` defined for it, so it can be used, for example,
88/// in `ball` logs.
89///
90/// See @ref bdlb_optionalprinter
91template <class TYPE>
93
94 // DATA
95 const bsl::optional<TYPE>* d_data_p;
96
97 public:
98 // CREATORS
99
100 /// Create `OptionalPrinter` with the specified `data`.
101 explicit OptionalPrinter(const bsl::optional<TYPE> *data);
102
103 // ACCESSORS
104
105 /// Format this object to the specified output `stream` at the (absolute
106 /// value of) the optionally specified indentation `level` and return a
107 /// reference to `stream`. If `level` is specified, optionally specify
108 /// `spacesPerLevel`, the number of spaces per indentation level for
109 /// this and all of its nested objects. If `level` is negative,
110 /// suppress indentation of the first line. If `spacesPerLevel` is
111 /// negative, format the entire output on one line, suppressing all but
112 /// the initial indentation (as governed by `level`). If `stream` is
113 /// not valid on entry, this operation has no effect.
114 bsl::ostream& print(bsl::ostream& stream,
115 int level = 0,
116 int spacesPerLevel = 4) const;
117};
118
119// FREE OPERATORS
120
121/// Write the value of the specified `printer` object to the specified
122/// output `stream` in a single-line format, and return a reference to
123/// `stream`. If `stream` is not valid on entry, this operation has no
124/// effect. Note that this human-readable format is not fully specified,
125/// can change without notice, and is logically equivalent to:
126/// @code
127/// print(stream, 0, -1);
128/// @endcode
129template <class TYPE>
130bsl::ostream&
131operator<<(bsl::ostream& stream, const OptionalPrinter<TYPE>& printer);
132
133 // ==========================
134 // struct OptionalPrinterUtil
135 // ==========================
136
137/// This utility `struct` provides a namespace for a function that creates a
138/// `bdlb::OptionalPrinter` with its template argument deduced from a given
139/// instance of `bsl::optional`.
141 public:
142 // CLASS METHODS
143
144 /// Return an `OptionalPrinter` that prints the specified `data`.
145 template <class TYPE>
147};
148
149// ============================================================================
150// INLINE DEFINITIONS
151// ============================================================================
152
153 // ----------------------
154 // struct OptionalPrinter
155 // ----------------------
156
157// CREATORS
158template <class TYPE>
160: d_data_p(data)
161{
162 BSLS_ASSERT(data);
163}
164
165// ACCESSORS
166template <class TYPE>
167bsl::ostream& OptionalPrinter<TYPE>::print(bsl::ostream& stream,
168 int level,
169 int spacesPerLevel) const
170{
171 if (!d_data_p->has_value()) {
172 return bdlb::PrintMethods::print(stream,
173 "NULL",
174 level,
175 spacesPerLevel); // RETURN
176 }
177
178 return bdlb::PrintMethods::print(stream,
179 *(*d_data_p),
180 level,
181 spacesPerLevel);
182}
183
184 // --------------------------
185 // struct OptionalPrinterUtil
186 // --------------------------
187
188// CLASS METHODS
189template <class TYPE>
195
196} // close package namespace
197
198// FREE OPERATORS
199template <class TYPE>
200bsl::ostream& bdlb::operator<<(bsl::ostream& stream,
201 const bdlb::OptionalPrinter<TYPE>& object)
202{
203 return object.print(stream, 0, -1);
204}
205
206
207
208#endif // INCLUDED_BDLB_OPTIONALPRINTER
209
210// ----------------------------------------------------------------------------
211// Copyright 2021 Bloomberg Finance L.P.
212//
213// Licensed under the Apache License, Version 2.0 (the "License");
214// you may not use this file except in compliance with the License.
215// You may obtain a copy of the License at
216//
217// http://www.apache.org/licenses/LICENSE-2.0
218//
219// Unless required by applicable law or agreed to in writing, software
220// distributed under the License is distributed on an "AS IS" BASIS,
221// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
222// See the License for the specific language governing permissions and
223// limitations under the License.
224// ----------------------------- END-OF-FILE ----------------------------------
225
226/** @} */
227/** @} */
228/** @} */
Definition bdlb_optionalprinter.h:92
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
Definition bdlb_optionalprinter.h:167
OptionalPrinter(const bsl::optional< TYPE > *data)
Create OptionalPrinter with the specified data.
Definition bdlb_optionalprinter.h:159
Definition bslstl_optional.h:1861
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
bsl::ostream & print(bsl::ostream &stream, const TYPE &object, int level=0, int spacesPerLevel=4)
Definition bdlb_printmethods.h:719
Definition bdlb_algorithmworkaroundutil.h:74
bsl::ostream & operator<<(bsl::ostream &stream, const BigEndianInt16 &integer)
Definition bdlb_optionalprinter.h:140
static OptionalPrinter< TYPE > makePrinter(const bsl::optional< TYPE > &data)
Return an OptionalPrinter that prints the specified data.
Definition bdlb_optionalprinter.h:191