BDE 4.39.x 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 access to this reverse iterator.
350 ///
351 /// \pre The behavior is undefined unless, on
352 /// entry, this reverse iterator does not have the past-the-end value for a
353 /// reverse iterator over the underlying sequence.
355
356 /// Modify this reverse iterator to refer to the previous element in the
357 /// reverse iteration sequence, and return a reference providing modifiable access to this reverse iterator.
358 ///
359 /// \pre The behavior is undefined unless, on
360 /// entry, this reverse iterator does not have the same value as a reverse
361 /// iterator at the start of the underlying sequence.
363
364 // ACCESSORS
365
366 /// Return a reference to the element referred to by this reverse iterator.
367 ///
368 /// \pre The behavior is undefined unless this iterator is within the bounds of
369 /// the underlying sequence.
370 reference operator*() const;
371
372 /// Return a pointer to the element referred to by this reverse iterator.
373 ///
374 /// \pre The behavior is undefined unless this iterator is within the bounds of
375 /// the underlying sequence.
376 pointer operator->() const;
377
378 /// Return the forward iterator referring to the element in the forward
379 /// sequence after the element referred to by this reverse iterator.
380 ITERATOR forwardIterator() const;
381};
382
383// FREE OPERATORS
384
385/// Return `true` if the specified `lhs` reverse iterator has the same value
386/// as the specified `rhs` reverse iterator, and `false` otherwise. Two
387/// reverse iterators have the same value if they refer to the same element,
388/// or both have the past-the-end value for a reverse iterator over the underlying reverse iteration sequence.
389///
390/// \pre The behavior is undefined unless
391/// `lhs` and `rhs` refer to the same underlying sequence.
392template <class ITERATOR>
395
396/// Return `true` if the specified `lhs` reverse iterator does not have the
397/// same value as the specified `rhs` reverse iterator, and `false`
398/// otherwise. Two reverse iterators do not have the same value if (1) they
399/// do not refer to the same element and (2) both do not have the
400/// past-the-end value for a reverse iterator over the underlying reverse iteration sequence.
401///
402/// \pre The behavior is undefined unless `lhs` and `rhs`
403/// refer to the same underlying sequence.
404template <class ITERATOR>
407
408/// Modify the specified `iterator` to refer to the next element in the
409/// reverse iteration sequence, and return a reverse iterator having the pre-increment value of `iterator`.
410///
411/// \pre The behavior is undefined unless, on
412/// entry, `iterator` does not have the past-the-end value for a reverse
413/// iterator over the underlying sequence.
414template <class ITERATOR>
417
418/// Modify the specified `iterator` to refer to the previous element in the
419/// reverse iteration sequence, and return a reverse iterator having the pre-decrement value of `iterator`.
420///
421/// \pre The behavior is undefined unless, on
422/// entry, `iterator` does not have the same value as a reverse iterator to
423/// the start of the underlying sequence.
424template <class ITERATOR>
427
428// ============================================================================
429// INLINE DEFINITIONS
430// ============================================================================
431
432 // ------------------------------------
433 // class CalendarReverseIteratorAdapter
434 // ------------------------------------
435
436// CREATORS
437template <class ITERATOR>
438inline
443
444template <class ITERATOR>
445inline
447 const ITERATOR& value)
448: d_forwardIter(value)
449{
450}
451
452// MANIPULATORS
453template <class ITERATOR>
454inline
458{
459 d_forwardIter = rhs.d_forwardIter;
460 return *this;
461}
462
463template <class ITERATOR>
464inline
467{
468 d_forwardIter = rhs;
469 return *this;
470}
471
472template <class ITERATOR>
473inline
476{
477 --d_forwardIter;
478 return *this;
479}
480
481template <class ITERATOR>
482inline
485{
486 ++d_forwardIter;
487 return *this;
488}
489
490// ACCESSORS
491template <class ITERATOR>
492inline
495{
496 ITERATOR tmp = d_forwardIter;
497 return *--tmp;
498}
499
500template <class ITERATOR>
501inline
504{
505 ITERATOR tmp = d_forwardIter;
506 return &*--tmp;
507}
508
509template <class ITERATOR>
510inline
512{
513 return d_forwardIter;
514}
515
516} // close package namespace
517
518// FREE OPERATORS
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
529bool bdlt::operator!=(const CalendarReverseIteratorAdapter<ITERATOR>& lhs,
530 const CalendarReverseIteratorAdapter<ITERATOR>& rhs)
531{
532 return lhs.forwardIterator() != rhs.forwardIterator();
533}
534
535template <class ITERATOR>
536inline
538 bdlt::operator++(CalendarReverseIteratorAdapter<ITERATOR>& iterator, int)
539{
540 CalendarReverseIteratorAdapter<ITERATOR> tmp = iterator;
541 ++iterator;
542 return tmp;
543}
544
545template <class ITERATOR>
546inline
548 bdlt::operator--(CalendarReverseIteratorAdapter<ITERATOR>& iterator, int)
549{
550 CalendarReverseIteratorAdapter<ITERATOR> tmp = iterator;
551 --iterator;
552 return tmp;
553}
554
555
556
557#endif
558
559// ----------------------------------------------------------------------------
560// Copyright 2015 Bloomberg Finance L.P.
561//
562// Licensed under the Apache License, Version 2.0 (the "License");
563// you may not use this file except in compliance with the License.
564// You may obtain a copy of the License at
565//
566// http://www.apache.org/licenses/LICENSE-2.0
567//
568// Unless required by applicable law or agreed to in writing, software
569// distributed under the License is distributed on an "AS IS" BASIS,
570// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
571// See the License for the specific language governing permissions and
572// limitations under the License.
573// ----------------------------- END-OF-FILE ----------------------------------
574
575/** @} */
576/** @} */
577/** @} */
Definition bdlt_calendarreverseiteratoradapter.h:298
ITERATOR::pointer pointer
Definition bdlt_calendarreverseiteratoradapter.h:308
ITERATOR forwardIterator() const
Definition bdlt_calendarreverseiteratoradapter.h:511
CalendarReverseIteratorAdapter & operator=(const CalendarReverseIteratorAdapter &rhs)
Definition bdlt_calendarreverseiteratoradapter.h:456
ITERATOR::value_type value_type
Definition bdlt_calendarreverseiteratoradapter.h:306
ITERATOR::reference reference
Definition bdlt_calendarreverseiteratoradapter.h:309
CalendarReverseIteratorAdapter & operator--()
Definition bdlt_calendarreverseiteratoradapter.h:484
pointer operator->() const
Definition bdlt_calendarreverseiteratoradapter.h:503
CalendarReverseIteratorAdapter()
Definition bdlt_calendarreverseiteratoradapter.h:439
ITERATOR::difference_type difference_type
Definition bdlt_calendarreverseiteratoradapter.h:307
reference operator*() const
Definition bdlt_calendarreverseiteratoradapter.h:494
CalendarReverseIteratorAdapter & operator++()
Definition bdlt_calendarreverseiteratoradapter.h:475
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bbldc_basicisma30360.h:112
Calendar_BusinessDayConstIter operator++(Calendar_BusinessDayConstIter &iterator, int)
Definition bdlt_calendar.h:2215
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:2224
ALLOCATOR const STRING_VIEW_LIKE_TYPE & rhs
Definition bslstl_string.h:3918
ALLOCATOR & lhs
Definition bslstl_string.h:3917