BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlb_nulloutputiterator.h
Go to the documentation of this file.
1/// @file bdlb_nulloutputiterator.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlb_nulloutputiterator.h -*-C++-*-
8#ifndef INCLUDED_BDLB_NULLOUTPUTITERATOR
9#define INCLUDED_BDLB_NULLOUTPUTITERATOR
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlb_nulloutputiterator bdlb_nulloutputiterator
15/// @brief Provide an output iterator type that discards output.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlb
19/// @{
20/// @addtogroup bdlb_nulloutputiterator
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlb_nulloutputiterator-purpose"> Purpose</a>
25/// * <a href="#bdlb_nulloutputiterator-classes"> Classes </a>
26/// * <a href="#bdlb_nulloutputiterator-description"> Description </a>
27/// * <a href="#bdlb_nulloutputiterator-usage"> Usage </a>
28/// * <a href="#bdlb_nulloutputiterator-example-1-basic-use-of-bdlb-nulloutputiterator"> Example 1: Basic Use of bdlb::NullOutputIterator </a>
29///
30/// # Purpose {#bdlb_nulloutputiterator-purpose}
31/// Provide an output iterator type that discards output.
32///
33/// # Classes {#bdlb_nulloutputiterator-classes}
34///
35/// - bdlb::NullOutputIterator: output iterator template that discards the output
36/// - bdlb::NullOutputIterator::AssignmentProxy: proxy for assignment
37///
38/// @see bdlb_nullinputiterator
39///
40/// # Description {#bdlb_nulloutputiterator-description}
41/// This component provides a mechanism,
42/// `bdlb::NullOutputIterator`, that defines an output iterator with the
43/// following attributes:
44/// * Meets exactly the requirements for an output iterator according to the
45/// C++ Standard (C++98, Section 24.1.2 [lib.output.iterators]).
46/// * De-referencing an iterator and assigning to the returned value has no
47/// effect.
48/// * Incrementing an iterator has no effect.
49///
50/// This iterator type is typically used to call functions purely for their
51/// side-effects, discarding the normal output. It is also useful for testing
52/// whether a template function will compile when presented with a pure output
53/// iterator. This component also provides a template
54/// `bdlb::NullOutputIterator::AssignmentProxy`, that is used as the return type
55/// of `bdlb::NullOutputIterator::operator*`. The `AssignmentProxy` provides an
56/// `operator=` that does nothing, so that the result of the iterator's
57/// `operator*` can be assigned to even if the value type of the
58/// `bdlb::NullOutputIterator` does not provide a default constructor:
59/// @code
60/// class ValueType {
61/// // ... data members ...
62///
63/// public:
64/// ValueType(int value) { ... implementation elided ... }
65///
66/// // ... rest of class definition elided ...
67///
68/// };
69///
70/// ValueType v(42);
71/// bdlb::NullOutputIterator<ValueType> i;
72///
73/// // With a non-proxy return type for 'operator*' it would be difficult to
74/// // provide a value for the lefthand side of this expression:
75///
76/// *i = v;
77/// @endcode
78///
79/// ## Usage {#bdlb_nulloutputiterator-usage}
80///
81///
82/// This section illustrates intended use of this component.
83///
84/// ### Example 1: Basic Use of bdlb::NullOutputIterator {#bdlb_nulloutputiterator-example-1-basic-use-of-bdlb-nulloutputiterator}
85///
86///
87/// In the following example we use a `bdlb::NullOutputIterator` to enable us to
88/// call a function to capture its return code, while ignoring the output
89/// provided through an iterator.
90///
91/// First, we define a function `runningSum` that returns output both through an
92/// output iterator and through a return status code:
93/// @code
94/// template <class IN_ITER, class OUT_ITER>
95/// typename bsl::iterator_traits<OUT_ITER>::value_type
96/// runningSum(IN_ITER first, IN_ITER last, OUT_ITER output)
97/// {
98/// typename bsl::iterator_traits<OUT_ITER>::value_type total = 0;
99/// while (first != last) {
100/// total += *first++;
101/// *output++ = total;
102/// }
103/// return total;
104/// }
105/// @endcode
106/// Now, we define a function `average` that captures the total sum returned by
107/// `runningSum` and uses a `bdlb::NullOutputIterator` to facilitate calling the
108/// function, and ignoring the output it provides through its output iterator
109/// parameter:
110/// @code
111/// int average(int values[], int numValues)
112/// {
113/// // ... input validation elided ...
114/// return runningSum(values, values + numValues,
115/// bdlb::NullOutputIterator<int>()) / numValues;
116/// }
117/// @endcode
118/// Finally, we invoke function `average` on user array and validate result.
119/// @code
120/// void usageExample()
121/// {
122/// const int myArray[5] = { 3, 4, 5, 7, 11 };
123///
124/// int averageValue = average(myArray, 5);
125/// assert( averageValue == 6 );
126/// }
127/// @endcode
128/// @}
129/** @} */
130/** @} */
131
132/** @addtogroup bdl
133 * @{
134 */
135/** @addtogroup bdlb
136 * @{
137 */
138/** @addtogroup bdlb_nulloutputiterator
139 * @{
140 */
141
142#include <bdlscm_version.h>
143
144#include <bsls_libraryfeatures.h>
145#include <bsl_iterator.h>
146
147
148namespace bdlb {
149
150 // =======================================
151 // class NullOutputIteratorAssignmentProxy
152 // =======================================
153
154/// Provide an object that can appear on the left side of an assignment
155/// from `TYPE`. The operation AssignmentProxy() = TYPE() is valid and has
156/// no effect.
157///
158/// See @ref bdlb_nulloutputiterator
159template <class TYPE>
161 public:
162 // MANIPULATORS
163
164 /// Assign to this object the value of the specified `rhs`. The operator
165 /// has no effect.
166 void operator=(const TYPE& rhs);
167};
168
169 // ========================
170 // class NullOutputIterator
171 // ========================
172
173/// Provide an output iterator that ignores the output that is provided.
174/// De-referencing an iterator and assigning to the returned value has no
175/// effect.
176template <class TYPE>
178#if defined(BSLS_LIBRARYFEATURES_STDCPP_LIBCSTD)
179// Sun CC workaround: iterators must be derived from `std::iterator` to work
180// with the native std library algorithms. However, `std::iterator` is
181// deprecated in C++17, so do not rely on derivation unless required, to avoid
182// deprecation warnings on modern compilers.
183 : public bsl::iterator<bsl::output_iterator_tag, TYPE>
184#endif
185{
186
187 public:
188 // TYPES
189
190 /// `AssignmentProxy` is an alias for an object type returned by
191 /// de-referencing operator.
193
194 typedef bsl::output_iterator_tag iterator_category;
195 typedef TYPE value_type;
196 typedef void difference_type;
197 typedef void pointer;
198 typedef void reference;
199
200 public:
201 // CREATORS
202
203 /// Create a `NullOutputIterator` object.
205
206 /// Create a `NullOutputIterator` object having the value of the
207 /// specified `original`.
208 NullOutputIterator(const NullOutputIterator& original);
209
210 /// Destroy this object.
212
213 // MANIPULATORS
214
215 /// Assign to this object the value of the specified `rhs` iterator, and
216 /// return a reference providing modifiable access to this object.
218
219 /// Return an object that can appear on the left-hand side of an
220 /// assignment from `TYPE`. The assignment to the returned object has
221 /// no effect.
223
224 /// Set this object to point to the next writable element, and return a
225 /// reference providing modifiable access to this object. This
226 /// operation has no effect.
228
229 /// Set this object to point to the next writable element, and return a
230 /// reference providing modifiable access to this object. This
231 /// operation has no effect.
233};
234
235// ============================================================================
236// INLINE DEFINITIONS
237// ============================================================================
238
239 // ---------------------------------------
240 // class NullOutputIteratorAssignmentProxy
241 // ---------------------------------------
242
243// MANIPULATORS
244template <class TYPE>
245inline
246void
250 // ------------------------
251 // class NullOutputIterator
252 // ------------------------
253
254// CREATORS
255template <class TYPE>
256inline
260
261template <class TYPE>
262inline
266
267template <class TYPE>
268inline
272
273// MANIPULATORS
274template <class TYPE>
275inline
278{
279 return *this;
280}
281
282template <class TYPE>
283inline
289
290template <class TYPE>
291inline
296
297template <class TYPE>
298inline
303
304} // close package namespace
305
306
307#endif
308
309// ----------------------------------------------------------------------------
310// Copyright 2015 Bloomberg Finance L.P.
311//
312// Licensed under the Apache License, Version 2.0 (the "License");
313// you may not use this file except in compliance with the License.
314// You may obtain a copy of the License at
315//
316// http://www.apache.org/licenses/LICENSE-2.0
317//
318// Unless required by applicable law or agreed to in writing, software
319// distributed under the License is distributed on an "AS IS" BASIS,
320// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
321// See the License for the specific language governing permissions and
322// limitations under the License.
323// ----------------------------- END-OF-FILE ----------------------------------
324
325/** @} */
326/** @} */
327/** @} */
Definition bdlb_nulloutputiterator.h:160
void operator=(const TYPE &rhs)
Definition bdlb_nulloutputiterator.h:247
Definition bdlb_nulloutputiterator.h:185
TYPE value_type
Definition bdlb_nulloutputiterator.h:195
NullOutputIterator & operator=(const NullOutputIterator &rhs)
Definition bdlb_nulloutputiterator.h:277
void reference
Definition bdlb_nulloutputiterator.h:198
NullOutputIteratorAssignmentProxy< TYPE > AssignmentProxy
Definition bdlb_nulloutputiterator.h:192
~NullOutputIterator()
Destroy this object.
Definition bdlb_nulloutputiterator.h:269
AssignmentProxy operator*()
Definition bdlb_nulloutputiterator.h:285
bsl::output_iterator_tag iterator_category
Definition bdlb_nulloutputiterator.h:194
void pointer
Definition bdlb_nulloutputiterator.h:197
NullOutputIterator()
Create a NullOutputIterator object.
Definition bdlb_nulloutputiterator.h:257
void difference_type
Definition bdlb_nulloutputiterator.h:196
NullOutputIterator & operator++()
Definition bdlb_nulloutputiterator.h:292
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlb_algorithmworkaroundutil.h:74