123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- #ifndef _DATE_TIME_WRAPPING_INT_HPP__
- #define _DATE_TIME_WRAPPING_INT_HPP__
- namespace boost {
- namespace date_time {
- template<typename int_type_, int_type_ wrap_val>
- class wrapping_int {
- public:
- typedef int_type_ int_type;
-
- static int_type wrap_value() {return wrap_val;}
-
- wrapping_int(int_type v) : value_(v) {};
-
- int_type as_int() const {return value_;}
- operator int_type() const {return value_;}
-
-
- template< typename IntT >
- IntT add(IntT v)
- {
- int_type remainder = static_cast<int_type>(v % (wrap_val));
- IntT overflow = static_cast<IntT>(v / (wrap_val));
- value_ = static_cast<int_type>(value_ + remainder);
- return calculate_wrap(overflow);
- }
-
-
- template< typename IntT >
- IntT subtract(IntT v)
- {
- int_type remainder = static_cast<int_type>(v % (wrap_val));
- IntT underflow = static_cast<IntT>(-(v / (wrap_val)));
- value_ = static_cast<int_type>(value_ - remainder);
- return calculate_wrap(underflow) * -1;
- }
- private:
- int_type value_;
- template< typename IntT >
- IntT calculate_wrap(IntT wrap)
- {
- if ((value_) >= wrap_val)
- {
- ++wrap;
- value_ -= (wrap_val);
- }
- else if(value_ < 0)
- {
- --wrap;
- value_ += (wrap_val);
- }
- return wrap;
- }
-
- };
- template<typename int_type_, int_type_ wrap_min, int_type_ wrap_max>
- class wrapping_int2 {
- public:
- typedef int_type_ int_type;
- static int_type wrap_value() {return wrap_max;}
- static int_type min_value() {return wrap_min;}
-
- wrapping_int2(int_type v) : value_(v) {
- if(value_ < wrap_min)
- {
- value_ = wrap_min;
- }
- if(value_ > wrap_max)
- {
- value_ = wrap_max;
- }
- }
-
- int_type as_int() const {return value_;}
- operator int_type() const {return value_;}
-
-
- template< typename IntT >
- IntT add(IntT v)
- {
- int_type remainder = static_cast<int_type>(v % (wrap_max - wrap_min + 1));
- IntT overflow = static_cast<IntT>(v / (wrap_max - wrap_min + 1));
- value_ = static_cast<int_type>(value_ + remainder);
- return calculate_wrap(overflow);
- }
-
-
- template< typename IntT >
- IntT subtract(IntT v)
- {
- int_type remainder = static_cast<int_type>(v % (wrap_max - wrap_min + 1));
- IntT underflow = static_cast<IntT>(-(v / (wrap_max - wrap_min + 1)));
- value_ = static_cast<int_type>(value_ - remainder);
- return calculate_wrap(underflow);
- }
-
- private:
- int_type value_;
- template< typename IntT >
- IntT calculate_wrap(IntT wrap)
- {
- if ((value_) > wrap_max)
- {
- ++wrap;
- value_ -= (wrap_max - wrap_min + 1);
- }
- else if((value_) < wrap_min)
- {
- --wrap;
- value_ += (wrap_max - wrap_min + 1);
- }
- return wrap;
- }
- };
- } }
- #endif
|