BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlt_calendarreverseiteratoradapter.h
Go to the documentation of this file.
1/// @file bdlt_calendarreverseiteratoradapter.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlt_calendarreverseiteratoradapter.h -*-C++-*-
8#ifndef INCLUDED_BDLT_CALENDARREVERSEITERATORADAPTER
9#define INCLUDED_BDLT_CALENDARREVERSEITERATORADAPTER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlt_calendarreverseiteratoradapter bdlt_calendarreverseiteratoradapter
15/// @brief Provide reverse iterator adapter for calendar iterators.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlt
19/// @{
20/// @addtogroup bdlt_calendarreverseiteratoradapter
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlt_calendarreverseiteratoradapter-purpose"> Purpose</a>
25/// * <a href="#bdlt_calendarreverseiteratoradapter-classes"> Classes </a>
26/// * <a href="#bdlt_calendarreverseiteratoradapter-description"> Description </a>
27/// * <a href="#bdlt_calendarreverseiteratoradapter-limitation"> Limitation </a>
28/// * <a href="#bdlt_calendarreverseiteratoradapter-usage"> Usage </a>
29/// * <a href="#bdlt_calendarreverseiteratoradapter-example-1-basic-use-of-bdlt-calendarreverseiteratoradapter"> Example 1: Basic Use of bdlt::CalendarReverseIteratorAdapter </a>
30///
31/// # Purpose {#bdlt_calendarreverseiteratoradapter-purpose}
32/// Provide reverse iterator adapter for calendar iterators.
33///
34/// # Classes {#bdlt_calendarreverseiteratoradapter-classes}
35///
36/// - bdlt::CalendarReverseIteratorAdapter: calendar reverse iterator adapter
37///
38/// @see bdlt_calendar, bdlt_packedcalendar
39///
40/// # Description {#bdlt_calendarreverseiteratoradapter-description}
41/// This component provides a template,
42/// `bdlt::CalendarReverseIteratorAdapter`, that can be used to adapt a calendar
43/// iterator to be a reverse iterator (see @ref bdlt_calendar and
44/// @ref bdlt_packedcalendar ). Calendar iterators cannot return a reference to an
45/// underlying element of the calendar and hence cannot be used with
46/// `bsl::reverse_iterator`. The reverse iterator adapter defined in this
47/// component provides a subset of the `bsl::reverse_iterator` interface that
48/// can be used with the calendar iterators defined in `bdlt`. Specifically,
49/// the types `value_type`, @ref difference_type , `pointer`, and `reference` are
50/// defined but @ref iterator_category is not defined (since this is not a
51/// fully-compliant iterator). Furthermore, the methods appropriate for
52/// random-access iterators are not included (e.g., `operator+=`).
53///
54/// ## Limitation {#bdlt_calendarreverseiteratoradapter-limitation}
55///
56///
57/// `bdlt::CalendarReverseIteratorAdapter` is *not* a fully-compliant
58/// implementation of `std::reverse_iterator` according to the C++ standard. It
59/// is an implementation of the minimum functionality needed to support the
60/// public iterators in the @ref bdlt_calendar and @ref bdlt_packedcalendar
61/// components. Within that limitation, it is a subset implementation of
62/// `bsl::reverse_iterator`. Specifically, @ref iterator_category is not defined
63/// for this adapter and the methods of a `bsl::reverse_iterator` relevant only
64/// to random-access compliant `ITERATOR` types are omitted.
65///
66/// ## Usage {#bdlt_calendarreverseiteratoradapter-usage}
67///
68///
69/// This section illustrates intended use of this component.
70///
71/// ### Example 1: Basic Use of bdlt::CalendarReverseIteratorAdapter {#bdlt_calendarreverseiteratoradapter-example-1-basic-use-of-bdlt-calendarreverseiteratoradapter}
72///
73///
74/// In this example, we will use the `bdlt::CalendarReverseIteratorAdapter` to
75/// traverse an iterable container type. Specifically, we will create an array
76/// of `struct` values, implement a bidirectional iterator class that is a
77/// forward iterator for this array, and then use
78/// `bdlt::CalendarReverseIteratorAdapter` to provide a reverse iterator that
79/// will be used to traverse the array.
80///
81/// First, we define a bidirectional iterator class:
82/// @code
83/// // This `class` basically behaves as a pointer to the (template
84/// // parameter) `TYPE` with 4 types defined to allow the use of
85/// // `bdlt::CalendarReverseIteratorAdapter` with this `class`. Note that
86/// // this `class` supports only a subset of the functionality that a
87/// // pointer would, but this subset covers all the functionality that a
88/// // `bdlt::CalendarReverseIteratorAdapter` needs.
89/// template <class TYPE>
90/// class Iterator {
91///
92/// // DATA
93/// TYPE *d_ptr; // pointer to the element referred to by this iterator
94///
95/// // FRIENDS
96/// template <class OTHER>
97/// friend bool operator==(const Iterator<OTHER>&, const Iterator<OTHER>&);
98///
99/// public:
100/// // PUBLIC TYPES
101/// typedef TYPE value_type;
102/// typedef int difference_type;
103/// typedef TYPE *pointer;
104/// typedef TYPE& reference;
105///
106/// // CREATORS
107///
108/// /// Create an `Iterator` object having the default value.
109/// Iterator()
110/// : d_ptr(0)
111/// {
112/// }
113///
114/// /// Create an `Iterator` object from the specified `value`.
115/// Iterator(TYPE *value) // IMPLICIT
116/// : d_ptr(value)
117/// {
118/// }
119///
120/// // Iterator(const Iterator&) = default;
121///
122/// // ~Iterator() = default;
123///
124/// // MANIPULATORS
125///
126/// // Iterator& operator=(const Iterator&) = default;
127///
128/// /// Increment to the next element in the iteration sequence, and
129/// /// return a reference providing modifiable access to this iterator.
130/// /// The behavior is undefined if, on entry, this iterator has the
131/// /// past-the-end value for an iterator over the underlying sequence.
132/// Iterator& operator++()
133/// {
134/// ++d_ptr;
135/// return *this;
136/// }
137///
138/// /// Decrement to the previous element in the iteration sequence, and
139/// /// return a reference providing modifiable access to this iterator.
140/// /// The behavior is undefined if, on entry, this iterator has the
141/// /// same value as an iterator at the start of the underlying
142/// /// sequence.
143/// Iterator& operator--()
144/// {
145/// --d_ptr;
146/// return *this;
147/// }
148///
149/// // ACCESSORS
150///
151/// /// Return a reference to the element referred to by this iterator.
152/// /// The behavior is undefined unless this iterator is within the
153/// /// bounds of the underlying sequence.
154/// reference operator*() const
155/// {
156/// return *d_ptr;
157/// }
158///
159/// /// Return a pointer to the element referred to by this iterator.
160/// /// The behavior is undefined unless this iterator is within the
161/// /// bounds of the underlying sequence.
162/// pointer operator->() const
163/// {
164/// return d_ptr;
165/// }
166///
167/// // Return an iterator referencing the location at the specified
168/// // `offset` from the element referenced by this iterator. The
169/// // behavior is undefined unless the resultant iterator is within
170/// // the bounds of the underlying sequence.
171/// Iterator operator+(bsl::ptrdiff_t offset) const
172/// {
173/// return Iterator(d_ptr + offset);
174/// }
175/// };
176///
177/// // FREE OPERATORS
178///
179/// /// Return `true` if the specified `lhs` iterator has the same value as
180/// /// the specified `rhs` iterator, and `false` otherwise. Two iterators
181/// /// have the same value if they refer to the same element, or both have
182/// /// the past-the-end value for am iterator over the underlying iteration
183/// /// sequence. The behavior is undefined unless `lhs` and `rhs` refer to
184/// /// the same underlying sequence.
185/// template <class TYPE>
186/// inline
187/// bool operator==(const Iterator<TYPE>& lhs, const Iterator<TYPE>& rhs)
188/// {
189/// return lhs.d_ptr == rhs.d_ptr;
190/// }
191///
192/// /// Return `true` if the specified `lhs` iterator does not have the same
193/// /// value as the specified `rhs` iterator, and `false` otherwise. Two
194/// /// iterators do not have the same value if (1) they do not refer to the
195/// /// same element and (2) both do not have the past-the-end value for an
196/// /// iterator over the underlying iteration sequence. The behavior is
197/// /// undefined unless `lhs` and `rhs` refer to the same underlying
198/// /// sequence.
199/// template <class TYPE>
200/// inline
201/// bool operator!=(const Iterator<TYPE>& lhs, const Iterator<TYPE>& rhs)
202/// {
203/// return !(lhs == rhs);
204/// }
205/// @endcode
206/// Then, we define `struct` `S`, the type that will be referred to by the
207/// `Iterator` type:
208/// @code
209/// struct S {
210/// char d_c;
211/// int d_i;
212/// };
213/// @endcode
214/// The `struct` `S` has two data members. By creating an array of distinct `S`
215/// values, the state of an iterator referring to an element of this array can
216/// be easily verified by inspecting these two members.
217///
218/// Next, we define four (distinct) `S` values:
219/// @code
220/// const S s0 = { 'A', 3 };
221/// const S s1 = { 'B', 5 };
222/// const S s2 = { 'C', 7 };
223/// const S s3 = { 'D', 9 };
224/// @endcode
225/// Then, we define `s`, an array of `S` values:
226/// @code
227/// S s[] = { s0, s1, s2, s3 };
228/// enum { NUM_S = sizeof s / sizeof *s };
229/// @endcode
230/// Next, we define an iterator, `sfBegin`, referring to the first element of
231/// `s` and an iterator, `sfEnd`, having the past-the-end value for an iterator
232/// over `s`:
233/// @code
234/// Iterator<S> sfBegin(s + 0), sfEnd(s + NUM_S);
235/// @endcode
236/// Then, for convenience we declare our reverse iterator type that will be used
237/// to traverse `s` in the reverse direction:
238/// @code
239/// typedef bdlt::CalendarReverseIteratorAdapter<Iterator<S> > Reverse;
240/// @endcode
241/// Next, we declare begin and end reverse iterators to our range of `S` values:
242/// @code
243/// const Reverse rBegin(sfEnd), rEnd(sfBegin);
244/// @endcode
245/// Now, we traverse our range in the reverse direction, from `rBegin` to
246/// `rEnd`, streaming out the contents of the `S` values as we go:
247/// @code
248/// bsl::ostringstream stream;
249/// for (Reverse it = rBegin; rEnd != it; ++it) {
250/// stream << (rBegin == it ? "" : ", ")
251/// << "{ "
252/// << it->d_c
253/// << ", "
254/// << it->d_i
255/// << " }";
256/// }
257/// stream << bsl::flush;
258/// @endcode
259/// Finally, we verify the contents of the range output:
260/// @code
261/// assert(stream.str() == "{ D, 9 }, { C, 7 }, { B, 5 }, { A, 3 }");
262/// @endcode
263/// @}
264/** @} */
265/** @} */
266
267/** @addtogroup bdl
268 * @{
269 */
270/** @addtogroup bdlt
271 * @{
272 */
273/** @addtogroup bdlt_calendarreverseiteratoradapter
274 * @{
275 */
276
277#include <bdlscm_version.h>
278
280
281
282namespace bdlt {
283
284 // ====================================
285 // class CalendarReverseIteratorAdapter
286 // ====================================
287
288/// This reverse iterator adapter provides a subset of the
289/// `bsl::reverse_iterator` interface that can be used with the calendar
290/// iterators defined in `bdlt`. Specifically, the types `value_type`,
291/// @ref difference_type , `pointer`, and `reference` are defined but
292/// @ref iterator_category is not defined (since this is not a fully-compliant
293/// iterator). Furthermore, the methods appropriate for random-access
294/// iterators are not included (e.g., `operator+=`).
295///
296/// See @ref bdlt_calendarreverseiteratoradapter
297template <class ITERATOR>
299
300 // DATA
301 ITERATOR d_forwardIter; // bidirectional iterator referring to current
302 // position
303
304 public:
305 // PUBLIC TYPES
306 typedef typename ITERATOR::value_type value_type;
307 typedef typename ITERATOR::difference_type difference_type;
308 typedef typename ITERATOR::pointer pointer;
309 typedef typename ITERATOR::reference reference;
310
311 // CREATORS
312
313 /// Create a reverse iterator having the default value. The
314 /// default-constructed reverse iterator does not have a singular value
315 /// unless an object of the type specified by the template parameter
316 /// `ITERATOR` has a singular value after default construction.
318
319 /// Create a reverse iterator referring to the element that precedes, in
320 /// the forward sequence (or that follows, in the backward sequence) the
321 /// element referred to by the specified `value`.
322 explicit
323 CalendarReverseIteratorAdapter(const ITERATOR& value);
324
325#ifdef BSLS_COMPILERFEATURES_SUPPORT_DEFAULTED_FUNCTIONS
326 /// Create a `CalendarReverseIteratorAdapter` object having the value of
327 /// the specified `original` object.
329 const CalendarReverseIteratorAdapter& original) = default;
330
331 /// Destroy this object.
333#endif
334
335 // MANIPULATORS
336
337 /// Assign the value of the specified `rhs` to this object, and return a
338 /// reference providing modifiable access to this object.
341
342 /// Set the value of this object to refer to the element that precedes, in
343 /// the forward sequence (or that follows, in the backward sequence) the
344 /// element referred to by the specified `rhs`, and return a reference
345 /// providing modifiable access to this object.
346 CalendarReverseIteratorAdapter& operator=(const ITERATOR& rhs);
347
348 /// Modify this reverse iterator to refer to the next element in the
349 /// reverse iteration sequence, and return a reference providing modifiable
350 /// access to this reverse iterator. The behavior is undefined unless, on
351 /// entry, this reverse iterator does not have the past-the-end value for a
352 /// reverse iterator over the underlying sequence.
354
355 /// Modify this reverse iterator to refer to the previous element in the
356 /// reverse iteration sequence, and return a reference providing modifiable
357 /// access to this reverse iterator. The behavior is undefined unless, on
358 /// entry, this reverse iterator does not have the same value as a reverse
359 /// iterator at the start of the underlying sequence.
361
362 // ACCESSORS
363
364 /// Return a reference to the element referred to by this reverse iterator.
365 /// The behavior is undefined unless this iterator is within the bounds of
366 /// the underlying sequence.
367 reference operator*() const;
368
369 /// Return a pointer to the element referred to by this reverse iterator.
370 /// The behavior is undefined unless this iterator is within the bounds of
371 /// the underlying sequence.
372 pointer operator->() const;
373
374 /// Return the forward iterator referring to the element in the forward
375 /// sequence after the element referred to by this reverse iterator.
376 ITERATOR forwardIterator() const;
377};
378
379// FREE OPERATORS
380
381/// Return `true` if the specified `lhs` reverse iterator has the same value
382/// as the specified `rhs` reverse iterator, and `false` otherwise. Two
383/// reverse iterators have the same value if they refer to the same element,
384/// or both have the past-the-end value for a reverse iterator over the
385/// underlying reverse iteration sequence. The behavior is undefined unless
386/// `lhs` and `rhs` refer to the same underlying sequence.
387template <class ITERATOR>
390
391/// Return `true` if the specified `lhs` reverse iterator does not have the
392/// same value as the specified `rhs` reverse iterator, and `false`
393/// otherwise. Two reverse iterators do not have the same value if (1) they
394/// do not refer to the same element and (2) both do not have the
395/// past-the-end value for a reverse iterator over the underlying reverse
396/// iteration sequence. The behavior is undefined unless `lhs` and `rhs`
397/// refer to the same underlying sequence.
398template <class ITERATOR>
401
402/// Modify the specified `iterator` to refer to the next element in the
403/// reverse iteration sequence, and return a reverse iterator having the
404/// pre-increment value of `iterator`. The behavior is undefined unless, on
405/// entry, `iterator` does not have the past-the-end value for a reverse
406/// iterator over the underlying sequence.
407template <class ITERATOR>
410
411/// Modify the specified `iterator` to refer to the previous element in the
412/// reverse iteration sequence, and return a reverse iterator having the
413/// pre-decrement value of `iterator`. The behavior is undefined unless, on
414/// entry, `iterator` does not have the same value as a reverse iterator to
415/// the start of the underlying sequence.
416template <class ITERATOR>
419
420// ============================================================================
421// INLINE DEFINITIONS
422// ============================================================================
423
424 // ------------------------------------
425 // class CalendarReverseIteratorAdapter
426 // ------------------------------------
427
428// CREATORS
429template <class ITERATOR>
430inline
435
436template <class ITERATOR>
437inline
439 const ITERATOR& value)
440: d_forwardIter(value)
441{
442}
443
444// MANIPULATORS
445template <class ITERATOR>
446inline
450{
451 d_forwardIter = rhs.d_forwardIter;
452 return *this;
453}
454
455template <class ITERATOR>
456inline
459{
460 d_forwardIter = rhs;
461 return *this;
462}
463
464template <class ITERATOR>
465inline
468{
469 --d_forwardIter;
470 return *this;
471}
472
473template <class ITERATOR>
474inline
477{
478 ++d_forwardIter;
479 return *this;
480}
481
482// ACCESSORS
483template <class ITERATOR>
484inline
487{
488 ITERATOR tmp = d_forwardIter;
489 return *--tmp;
490}
491
492template <class ITERATOR>
493inline
496{
497 ITERATOR tmp = d_forwardIter;
498 return &*--tmp;
499}
500
501template <class ITERATOR>
502inline
504{
505 return d_forwardIter;
506}
507
508} // close package namespace
509
510// FREE OPERATORS
511template <class ITERATOR>
512inline
513bool bdlt::operator==(const CalendarReverseIteratorAdapter<ITERATOR>& lhs,
514 const CalendarReverseIteratorAdapter<ITERATOR>& rhs)
515{
516 return lhs.forwardIterator() == rhs.forwardIterator();
517}
518
519template <class ITERATOR>
520inline
521bool bdlt::operator!=(const CalendarReverseIteratorAdapter<ITERATOR>& lhs,
522 const CalendarReverseIteratorAdapter<ITERATOR>& rhs)
523{
524 return lhs.forwardIterator() != rhs.forwardIterator();
525}
526
527template <class ITERATOR>
528inline
530 bdlt::operator++(CalendarReverseIteratorAdapter<ITERATOR>& iterator, int)
531{
532 CalendarReverseIteratorAdapter<ITERATOR> tmp = iterator;
533 ++iterator;
534 return tmp;
535}
536
537template <class ITERATOR>
538inline
540 bdlt::operator--(CalendarReverseIteratorAdapter<ITERATOR>& iterator, int)
541{
542 CalendarReverseIteratorAdapter<ITERATOR> tmp = iterator;
543 --iterator;
544 return tmp;
545}
546
547
548
549#endif
550
551// ----------------------------------------------------------------------------
552// Copyright 2015 Bloomberg Finance L.P.
553//
554// Licensed under the Apache License, Version 2.0 (the "License");
555// you may not use this file except in compliance with the License.
556// You may obtain a copy of the License at
557//
558// http://www.apache.org/licenses/LICENSE-2.0
559//
560// Unless required by applicable law or agreed to in writing, software
561// distributed under the License is distributed on an "AS IS" BASIS,
562// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
563// See the License for the specific language governing permissions and
564// limitations under the License.
565// ----------------------------- END-OF-FILE ----------------------------------
566
567/** @} */
568/** @} */
569/** @} */
Definition bdlt_calendarreverseiteratoradapter.h:298
ITERATOR::pointer pointer
Definition bdlt_calendarreverseiteratoradapter.h:308
ITERATOR forwardIterator() const
Definition bdlt_calendarreverseiteratoradapter.h:503
CalendarReverseIteratorAdapter & operator=(const CalendarReverseIteratorAdapter &rhs)
Definition bdlt_calendarreverseiteratoradapter.h:448
ITERATOR::value_type value_type
Definition bdlt_calendarreverseiteratoradapter.h:306
ITERATOR::reference reference
Definition bdlt_calendarreverseiteratoradapter.h:309
CalendarReverseIteratorAdapter & operator--()
Definition bdlt_calendarreverseiteratoradapter.h:476
pointer operator->() const
Definition bdlt_calendarreverseiteratoradapter.h:495
CalendarReverseIteratorAdapter()
Definition bdlt_calendarreverseiteratoradapter.h:431
ITERATOR::difference_type difference_type
Definition bdlt_calendarreverseiteratoradapter.h:307
reference operator*() const
Definition bdlt_calendarreverseiteratoradapter.h:486
CalendarReverseIteratorAdapter & operator++()
Definition bdlt_calendarreverseiteratoradapter.h:467
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bbldc_basicisma30360.h:112
Calendar_BusinessDayConstIter operator++(Calendar_BusinessDayConstIter &iterator, int)
Definition bdlt_calendar.h:2156
bool operator==(const Calendar &lhs, const Calendar &rhs)
bool operator!=(const Calendar &lhs, const Calendar &rhs)
Calendar_BusinessDayConstIter operator--(Calendar_BusinessDayConstIter &iterator, int)
Definition bdlt_calendar.h:2165