1 | #ifndef DATE_ITERATOR_HPP___ |
2 | #define DATE_ITERATOR_HPP___ |
3 | |
4 | /* Copyright (c) 2002,2003 CrystalClear Software, Inc. |
5 | * Use, modification and distribution is subject to the |
6 | * Boost Software License, Version 1.0. (See accompanying |
7 | * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) |
8 | * Author: Jeff Garland, Bart Garst |
9 | * $Date$ |
10 | */ |
11 | |
12 | #include <iterator> |
13 | |
14 | namespace boost { |
15 | namespace date_time { |
16 | //! An iterator over dates with varying resolution (day, week, month, year, etc) |
17 | enum date_resolutions {day, week, months, year, decade, century, NumDateResolutions}; |
18 | |
19 | //! Base date iterator type |
20 | /*! This class provides the skeleton for the creation of iterators. |
21 | * New and interesting interators can be created by plugging in a new |
22 | * function that derives the next value from the current state. |
23 | * generation of various types of -based information. |
24 | * |
25 | * <b>Template Parameters</b> |
26 | * |
27 | * <b>date_type</b> |
28 | * |
29 | * The date_type is a concrete date_type. The date_type must |
30 | * define a duration_type and a calendar_type. |
31 | */ |
32 | template<class date_type> |
33 | class date_itr_base { |
34 | // works, but benefit unclear at the moment |
35 | // class date_itr_base : public std::iterator<std::input_iterator_tag, |
36 | // date_type, void, void, void>{ |
37 | public: |
38 | typedef typename date_type::duration_type duration_type; |
39 | typedef date_type value_type; |
40 | typedef std::input_iterator_tag iterator_category; |
41 | |
42 | date_itr_base(date_type d) : current_(d) {} |
43 | virtual ~date_itr_base() {} |
44 | date_itr_base& operator++() |
45 | { |
46 | current_ = current_ + get_offset(current: current_); |
47 | return *this; |
48 | } |
49 | date_itr_base& operator--() |
50 | { |
51 | current_ = current_ + get_neg_offset(current: current_); |
52 | return *this; |
53 | } |
54 | virtual duration_type get_offset(const date_type& current) const=0; |
55 | virtual duration_type get_neg_offset(const date_type& current) const=0; |
56 | date_type operator*() {return current_;} |
57 | date_type* operator->() {return ¤t_;} |
58 | bool operator< (const date_type& d) {return current_ < d;} |
59 | bool operator<= (const date_type& d) {return current_ <= d;} |
60 | bool operator> (const date_type& d) {return current_ > d;} |
61 | bool operator>= (const date_type& d) {return current_ >= d;} |
62 | bool operator== (const date_type& d) {return current_ == d;} |
63 | bool operator!= (const date_type& d) {return current_ != d;} |
64 | private: |
65 | date_type current_; |
66 | }; |
67 | |
68 | //! Overrides the base date iterator providing hook for functors |
69 | /* |
70 | * <b>offset_functor</b> |
71 | * |
72 | * The offset functor must define a get_offset function that takes the |
73 | * current point in time and calculates and offset. |
74 | * |
75 | */ |
76 | template<class offset_functor, class date_type> |
77 | class date_itr : public date_itr_base<date_type> { |
78 | public: |
79 | typedef typename date_type::duration_type duration_type; |
80 | date_itr(date_type d, int factor=1) : |
81 | date_itr_base<date_type>(d), |
82 | of_(factor) |
83 | {} |
84 | private: |
85 | virtual duration_type get_offset(const date_type& current) const |
86 | { |
87 | return of_.get_offset(current); |
88 | } |
89 | virtual duration_type get_neg_offset(const date_type& current) const |
90 | { |
91 | return of_.get_neg_offset(current); |
92 | } |
93 | offset_functor of_; |
94 | }; |
95 | |
96 | |
97 | |
98 | } } //namespace date_time |
99 | |
100 | |
101 | #endif |
102 | |