Chris@16: #ifndef DATE_ITERATOR_HPP___ Chris@16: #define DATE_ITERATOR_HPP___ Chris@16: Chris@16: /* Copyright (c) 2002,2003 CrystalClear Software, Inc. Chris@16: * Use, modification and distribution is subject to the Chris@16: * Boost Software License, Version 1.0. (See accompanying Chris@16: * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) Chris@16: * Author: Jeff Garland, Bart Garst Chris@101: * $Date$ Chris@16: */ Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace date_time { Chris@16: //! An iterator over dates with varying resolution (day, week, month, year, etc) Chris@16: enum date_resolutions {day, week, months, year, decade, century, NumDateResolutions}; Chris@16: Chris@16: //! Base date iterator type Chris@16: /*! This class provides the skeleton for the creation of iterators. Chris@16: * New and interesting interators can be created by plugging in a new Chris@16: * function that derives the next value from the current state. Chris@16: * generation of various types of -based information. Chris@16: * Chris@16: * Template Parameters Chris@16: * Chris@16: * date_type Chris@16: * Chris@16: * The date_type is a concrete date_type. The date_type must Chris@16: * define a duration_type and a calendar_type. Chris@16: */ Chris@16: template Chris@16: class date_itr_base { Chris@16: // works, but benefit unclear at the moment Chris@16: // class date_itr_base : public std::iterator{ Chris@16: public: Chris@16: typedef typename date_type::duration_type duration_type; Chris@16: typedef date_type value_type; Chris@16: typedef std::input_iterator_tag iterator_category; Chris@16: Chris@16: date_itr_base(date_type d) : current_(d) {} Chris@16: virtual ~date_itr_base() {} Chris@16: date_itr_base& operator++() Chris@16: { Chris@16: current_ = current_ + get_offset(current_); Chris@16: return *this; Chris@16: } Chris@16: date_itr_base& operator--() Chris@16: { Chris@16: current_ = current_ + get_neg_offset(current_); Chris@16: return *this; Chris@16: } Chris@16: virtual duration_type get_offset(const date_type& current) const=0; Chris@16: virtual duration_type get_neg_offset(const date_type& current) const=0; Chris@16: date_type operator*() {return current_;} Chris@16: date_type* operator->() {return ¤t_;} Chris@16: bool operator< (const date_type& d) {return current_ < d;} Chris@16: bool operator<= (const date_type& d) {return current_ <= d;} Chris@16: bool operator> (const date_type& d) {return current_ > d;} Chris@16: bool operator>= (const date_type& d) {return current_ >= d;} Chris@16: bool operator== (const date_type& d) {return current_ == d;} Chris@16: bool operator!= (const date_type& d) {return current_ != d;} Chris@16: private: Chris@16: date_type current_; Chris@16: }; Chris@16: Chris@16: //! Overrides the base date iterator providing hook for functors Chris@16: /* Chris@16: * offset_functor Chris@16: * Chris@16: * The offset functor must define a get_offset function that takes the Chris@16: * current point in time and calculates and offset. Chris@16: * Chris@16: */ Chris@16: template Chris@16: class date_itr : public date_itr_base { Chris@16: public: Chris@16: typedef typename date_type::duration_type duration_type; Chris@16: date_itr(date_type d, int factor=1) : Chris@16: date_itr_base(d), Chris@16: of_(factor) Chris@16: {} Chris@16: private: Chris@16: virtual duration_type get_offset(const date_type& current) const Chris@16: { Chris@16: return of_.get_offset(current); Chris@16: } Chris@16: virtual duration_type get_neg_offset(const date_type& current) const Chris@16: { Chris@16: return of_.get_neg_offset(current); Chris@16: } Chris@16: offset_functor of_; Chris@16: }; Chris@16: Chris@16: Chris@16: Chris@16: } } //namespace date_time Chris@16: Chris@16: Chris@16: #endif