date_iterator.hpp 3.2 KB

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