BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlb_printadapter.h
Go to the documentation of this file.
1/// @file bdlb_printadapter.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlb_printadapter.h -*-C++-*-
8#ifndef INCLUDED_BDLB_PRINTADAPTER
9#define INCLUDED_BDLB_PRINTADAPTER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlb_printadapter bdlb_printadapter
15/// @brief Provide object for streaming objects using `bdlb::PrintMethods`.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlb
19/// @{
20/// @addtogroup bdlb_printadapter
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlb_printadapter-purpose"> Purpose</a>
25/// * <a href="#bdlb_printadapter-classes"> Classes </a>
26/// * <a href="#bdlb_printadapter-description"> Description </a>
27/// * <a href="#bdlb_printadapter-usage"> Usage </a>
28/// * <a href="#bdlb_printadapter-example-1-use-in-c-03"> Example 1: Use in C++03 </a>
29/// * <a href="#bdlb_printadapter-example-2-use-of-printadapterutil-makeadapter"> Example 2: Use of PrintAdapterUtil::makeAdapter </a>
30/// * <a href="#bdlb_printadapter-example-3-use-in-c-17-with-ctad"> Example 3: Use in C++17 With CTAD </a>
31///
32/// # Purpose {#bdlb_printadapter-purpose}
33/// Provide object for streaming objects using `bdlb::PrintMethods`.
34///
35/// # Classes {#bdlb_printadapter-classes}
36///
37/// - bdlb::PrintAdapter: class for streaming objects using `bdlb::PrintMethods`.
38/// - bdlb::PrintAdapterUtil: utility for constructing `bdlb::PrintAdapter`.
39///
40/// # Description {#bdlb_printadapter-description}
41/// This component provides a template `class` that will enable any
42/// object that can be streamed using `bdlb::PrintMethods` to be streamed via
43/// `<<`.
44///
45/// Through using this `class`, one can stream an object using `operator<<` and
46/// at the same time specify `level` and `spacesPerLevel` arguments to
47/// appropriately indent and format the output.
48///
49/// ## Usage {#bdlb_printadapter-usage}
50///
51///
52/// This section illustrates intended use of this component.
53///
54/// ### Example 1: Use in C++03 {#bdlb_printadapter-example-1-use-in-c-03}
55///
56///
57/// Suppose we have a few `bdlb::IdentSpan` objects that we want to print, only
58/// we want to stream them with `operator<<` and we want to specify non-default
59/// values of `level` and `spacesPerLevel` to their streaming.
60/// @code
61/// const bdlb::IndexSpan a(3, 7), b(241, 22), c(23, 17);
62/// @endcode
63/// First, we create a typedef of 'PrintAdapter<IdentSpan>' to use:
64/// @code
65/// typedef bdlb::PrintAdapter<bdlb::IndexSpan> PAIS;
66/// @endcode
67/// Then, we create a line of dashes:
68/// @code
69/// const char * const line = "------------------------------------"
70/// "------------------------------------\n";
71/// @endcode
72/// Now, we use the `typedef` to construct temporary `PrintAdapter` objects to
73/// stream our `IndexSpan` objects:
74/// @code
75/// cout << " a: " << line << PAIS(&a, 2, 2)
76/// << " b: " << line << PAIS(&b, 3, 2)
77/// << " c: " << line << PAIS(&c, 4, 2);
78/// @endcode
79/// Finally, we see the output:
80/// @code
81/// a: ------------------------------------------------------------------------
82/// [
83/// position = 3
84/// length = 7
85/// ]
86/// b: ------------------------------------------------------------------------
87/// [
88/// position = 241
89/// length = 22
90/// ]
91/// c: ------------------------------------------------------------------------
92/// [
93/// position = 23
94/// length = 17
95/// ]
96/// @endcode
97///
98/// ### Example 2: Use of PrintAdapterUtil::makeAdapter {#bdlb_printadapter-example-2-use-of-printadapterutil-makeadapter}
99///
100///
101/// Suppose we have a few `bdlb::IdentSpan` objects that we want to print, only
102/// we want to stream them with `operator<<` and we want to specify
103/// non-default values of `level` and `spacesPerLevel` to their streaming.
104/// @code
105/// const bdlb::IndexSpan a(3, 7), b(241, 22), c(23, 17);
106/// @endcode
107/// First, we make a typedef to the namespace `struct`:
108/// @code
109/// typedef bdlb::PrintAdapterUtil Util;
110/// @endcode
111/// Then, we create a line of dashes:
112/// @code
113/// const char * const line = "------------------------------------"
114/// "------------------------------------\n";
115/// @endcode
116/// Now, we call the static function `PrintAdapterUtil::makeAdapter` on the
117/// `IndexSpan` objects to stream which will return streamable `PrintAdapter`
118/// objects:
119/// @code
120/// cout << " a: " << line << Util::makeAdapter(a, 2, 2)
121/// << " b: " << line << Util::makeAdapter(b, 3, 2)
122/// << " c: " << line << Util::makeAdapter(c, 4, 2);
123/// @endcode
124/// Finally, we see the output:
125/// @code
126/// a: ------------------------------------------------------------------------
127/// [
128/// position = 3
129/// length = 7
130/// ]
131/// b: ------------------------------------------------------------------------
132/// [
133/// position = 241
134/// length = 22
135/// ]
136/// c: ------------------------------------------------------------------------
137/// [
138/// position = 23
139/// length = 17
140/// ]
141/// @endcode
142///
143/// ### Example 3: Use in C++17 With CTAD {#bdlb_printadapter-example-3-use-in-c-17-with-ctad}
144///
145///
146/// Suppose you have a few `bdlb::IdentSpan` objects that you want to print,
147/// only you want to stream them with `operator<<` and you want to specify
148/// non-default values of `level` and `spacesPerLevel` to their streaming.
149/// @code
150/// const bdlb::IndexSpan a(3, 7), b(241, 22), c(23, 17);
151/// @endcode
152/// First, we create a line of dashes:
153/// @code
154/// const char * const line = "------------------------------------"
155/// "------------------------------------\n";
156/// @endcode
157/// Now, you call the constructor `PrintAdapter` on the `IndexSpan` objects to
158/// stream and CTAD will create the right template specification:
159/// @code
160/// cout << " a: " << line << bdlb::PrintAdapter(&a, 2, 2)
161/// << " b: " << line << bdlb::PrintAdapter(&b, 3, 2)
162/// << " c: " << line << bdlb::PrintAdapter(&c, 4, 2);
163/// @endcode
164/// Finally, we see the output:
165/// @code
166/// a: ------------------------------------------------------------------------
167/// [
168/// position = 3
169/// length = 7
170/// ]
171/// b: ------------------------------------------------------------------------
172/// [
173/// position = 241
174/// length = 22
175/// ]
176/// c: ------------------------------------------------------------------------
177/// [
178/// position = 23
179/// length = 17
180/// ]
181/// @endcode
182/// @}
183/** @} */
184/** @} */
185
186/** @addtogroup bdl
187 * @{
188 */
189/** @addtogroup bdlb
190 * @{
191 */
192/** @addtogroup bdlb_printadapter
193 * @{
194 */
195
196#include <bdlscm_version.h>
197
198#include <bdlb_printmethods.h>
199
200#include <bsl_ostream.h>
201
202
203namespace bdlb {
204
205 // ==================
206 // class PrintAdapter
207 // ==================
208
209/// This `class` provides an object that may be streamed with `operator<<`
210/// to give full control of the indentation format of the streaming.
211///
212/// See @ref bdlb_printadapter
213template <class TYPE>
215
216 // DATA
217 const TYPE *d_object_p;
218 int d_level;
219 int d_spacesPerLevel;
220
221 public:
222 // CREATORS
223
224 // Create a print adapter object bound to the specified `object`, and
225 // with the optionally specified `level` and `spacesPerLevel`
226 // attributes, to be used when the `PrintAdapter` object is streamed.
227 // Unless `level` is specified, a value of 0 is used, unless
228 // `spacesPerLevel` is specified, a value of 4 is used.
229 explicit
230 PrintAdapter(const TYPE *object,
231 int level = 0,
232 int spacesPerLevel = 4);
233
234 PrintAdapter(const PrintAdapter&) = default;
235
236 // MANIPULATORS
237
239
240 // ACCESSORS
241
242 /// Return the `level` attribute.
243 int level() const;
244
245 /// Return a reference to the object referred to by this print adapter.
246 const TYPE& object() const;
247
248 /// Return the `spacesPerLevel` attribute.
249 int spacesPerLevel() const;
250};
251
252 // ======================
253 // class PrintAdapterUtil
254 // ======================
255
256// This `struct` provides a namespace for a function template that
257// facilitates the construction of a `PrintAdapter` without having to
258// explicitly specify template arguments in C++03. Note that in C++17 and
259// later, CTAD may be used instead of this `struct` to call the
260// `PrintAdapter` constructor without having to explicitly specify the
261// template argument.
263
264 // CLASS METHOD
265
266 /// Create and return a `PrintAdapter` object, passing the constructor
267 /// the specified `object`, `level`, and `spacesPerLevel`.
268 template <class TYPE>
269 static
270 PrintAdapter<TYPE> makeAdapter(const TYPE& object,
271 int level = 0,
272 int spacesPerLevel = 4);
273};
274
275// FREE OPERATORS
276
277/// Write the value of the specified `object` to the specified output
278/// `stream` and return a non-`const` reference to `stream`. If `stream` is
279/// not valid on entry, this operation has no effect. Note that this
280/// human-readable format is not fully specified and can change without
281/// notice.
282template <class TYPE>
283bsl::ostream& operator<<(bsl::ostream& stream,
284 const PrintAdapter<TYPE>& adapter);
285
286// ============================================================================
287// INLINE FUNCTION DEFINITIONS
288// ============================================================================
289
290 // ------------
291 // PrintAdapter
292 // ------------
293
294// CREATORS
295template <class TYPE>
296inline
298 int level,
299 int spacesPerLevel)
300: d_object_p(object)
301, d_level(level)
302, d_spacesPerLevel(spacesPerLevel)
303{}
304
305// ACCESSORS
306template <class TYPE>
307inline
309{
310 return d_level;
311}
312
313template <class TYPE>
314inline
315const TYPE& PrintAdapter<TYPE>::object() const
316{
317 return *d_object_p;
318}
319
320template <class TYPE>
321inline
323{
324 return d_spacesPerLevel;
325}
326
327// FREE OPERATORS
328template <class TYPE>
329inline
330bsl::ostream& operator<<(bsl::ostream& stream,
331 const PrintAdapter<TYPE>& adapter)
332 // Format this object to the specified output 'stream' at the (absolute
333 // value of) the optionally specified indentation 'level' and return a
334 // reference to 'stream'. If 'stream' is not valid on entry, this
335 // operation has no effect.
336{
337 return bdlb::PrintMethods::print(stream,
338 adapter.object(),
339 adapter.level(),
340 adapter.spacesPerLevel());
341}
342
343 // ----------------
344 // PrintAdapterUtil
345 // ----------------
346
347// CLASS METHOD
348template <class TYPE>
349inline
351 int level,
352 int spacesPerLevel)
353{
354 return PrintAdapter<TYPE>(&object, level, spacesPerLevel);
355}
356
357} // close package namespace
358
359
360#endif
361
362// ----------------------------------------------------------------------------
363// Copyright 2022 Bloomberg Finance L.P.
364//
365// Licensed under the Apache License, Version 2.0 (the "License");
366// you may not use this file except in compliance with the License.
367// You may obtain a copy of the License at
368//
369// http://www.apache.org/licenses/LICENSE-2.0
370//
371// Unless required by applicable law or agreed to in writing, software
372// distributed under the License is distributed on an "AS IS" BASIS,
373// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
374// See the License for the specific language governing permissions and
375// limitations under the License.
376// ----------------------------- END-OF-FILE ----------------------------------
377
378/** @} */
379/** @} */
380/** @} */
Definition bdlb_printadapter.h:214
int spacesPerLevel() const
Return the spacesPerLevel attribute.
Definition bdlb_printadapter.h:322
PrintAdapter & operator=(const PrintAdapter &)=default
const TYPE & object() const
Return a reference to the object referred to by this print adapter.
Definition bdlb_printadapter.h:315
int level() const
Return the level attribute.
Definition bdlb_printadapter.h:308
PrintAdapter(const PrintAdapter &)=default
PrintAdapter(const TYPE *object, int level=0, int spacesPerLevel=4)
Definition bdlb_printadapter.h:297
#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_printadapter.h:262
static PrintAdapter< TYPE > makeAdapter(const TYPE &object, int level=0, int spacesPerLevel=4)
Definition bdlb_printadapter.h:350