123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509 |
- #ifndef _DATE_TIME_INT_ADAPTER_HPP__
- #define _DATE_TIME_INT_ADAPTER_HPP__
- #include "boost/config.hpp"
- #include "boost/limits.hpp"
- #include "boost/date_time/special_defs.hpp"
- #include "boost/date_time/locale_config.hpp"
- #ifndef BOOST_DATE_TIME_NO_LOCALE
- # include <ostream>
- #endif
- namespace boost {
- namespace date_time {
- template<typename int_type_>
- class int_adapter {
- public:
- typedef int_type_ int_type;
- int_adapter(int_type v) :
- value_(v)
- {}
- static bool has_infinity()
- {
- return true;
- }
- static const int_adapter pos_infinity()
- {
- return (::std::numeric_limits<int_type>::max)();
- }
- static const int_adapter neg_infinity()
- {
- return (::std::numeric_limits<int_type>::min)();
- }
- static const int_adapter not_a_number()
- {
- return (::std::numeric_limits<int_type>::max)()-1;
- }
- static int_adapter max BOOST_PREVENT_MACRO_SUBSTITUTION ()
- {
- return (::std::numeric_limits<int_type>::max)()-2;
- }
- static int_adapter min BOOST_PREVENT_MACRO_SUBSTITUTION ()
- {
- return (::std::numeric_limits<int_type>::min)()+1;
- }
- static int_adapter from_special(special_values sv)
- {
- switch (sv) {
- case not_a_date_time: return not_a_number();
- case neg_infin: return neg_infinity();
- case pos_infin: return pos_infinity();
- case max_date_time: return (max)();
- case min_date_time: return (min)();
- default: return not_a_number();
- }
- }
- static bool is_inf(int_type v)
- {
- return (v == neg_infinity().as_number() ||
- v == pos_infinity().as_number());
- }
- static bool is_neg_inf(int_type v)
- {
- return (v == neg_infinity().as_number());
- }
- static bool is_pos_inf(int_type v)
- {
- return (v == pos_infinity().as_number());
- }
- static bool is_not_a_number(int_type v)
- {
- return (v == not_a_number().as_number());
- }
-
- static special_values to_special(int_type v)
- {
- if (is_not_a_number(v)) return not_a_date_time;
- if (is_neg_inf(v)) return neg_infin;
- if (is_pos_inf(v)) return pos_infin;
- return not_special;
- }
-
- static int_type maxcount()
- {
- return (::std::numeric_limits<int_type>::max)()-3;
- }
- bool is_infinity() const
- {
- return (value_ == neg_infinity().as_number() ||
- value_ == pos_infinity().as_number());
- }
- bool is_pos_infinity()const
- {
- return(value_ == pos_infinity().as_number());
- }
- bool is_neg_infinity()const
- {
- return(value_ == neg_infinity().as_number());
- }
- bool is_nan() const
- {
- return (value_ == not_a_number().as_number());
- }
- bool is_special() const
- {
- return(is_infinity() || is_nan());
- }
- bool operator==(const int_adapter& rhs) const
- {
- return (compare(rhs) == 0);
- }
- bool operator==(const int& rhs) const
- {
-
- bool is_signed = std::numeric_limits<int_type>::is_signed;
- if(!is_signed)
- {
- if(is_neg_inf(value_) && rhs == 0)
- {
- return false;
- }
- }
- return (compare(rhs) == 0);
- }
- bool operator!=(const int_adapter& rhs) const
- {
- return (compare(rhs) != 0);
- }
- bool operator!=(const int& rhs) const
- {
-
- bool is_signed = std::numeric_limits<int_type>::is_signed;
- if(!is_signed)
- {
- if(is_neg_inf(value_) && rhs == 0)
- {
- return true;
- }
- }
- return (compare(rhs) != 0);
- }
- bool operator<(const int_adapter& rhs) const
- {
- return (compare(rhs) == -1);
- }
- bool operator<(const int& rhs) const
- {
-
- bool is_signed = std::numeric_limits<int_type>::is_signed;
- if(!is_signed)
- {
- if(is_neg_inf(value_) && rhs == 0)
- {
- return true;
- }
- }
- return (compare(rhs) == -1);
- }
- bool operator>(const int_adapter& rhs) const
- {
- return (compare(rhs) == 1);
- }
- int_type as_number() const
- {
- return value_;
- }
-
- special_values as_special() const
- {
- return int_adapter::to_special(value_);
- }
-
-
- template<class rhs_type>
- inline
- int_adapter operator+(const int_adapter<rhs_type>& rhs) const
- {
- if(is_special() || rhs.is_special())
- {
- if (is_nan() || rhs.is_nan())
- {
- return int_adapter::not_a_number();
- }
- if((is_pos_inf(value_) && rhs.is_neg_inf(rhs.as_number())) ||
- (is_neg_inf(value_) && rhs.is_pos_inf(rhs.as_number())) )
- {
- return int_adapter::not_a_number();
- }
- if (is_infinity())
- {
- return *this;
- }
- if (rhs.is_pos_inf(rhs.as_number()))
- {
- return int_adapter::pos_infinity();
- }
- if (rhs.is_neg_inf(rhs.as_number()))
- {
- return int_adapter::neg_infinity();
- }
- }
- return int_adapter<int_type>(value_ + rhs.as_number());
- }
- int_adapter operator+(const int_type rhs) const
- {
- if(is_special())
- {
- if (is_nan())
- {
- return int_adapter<int_type>(not_a_number());
- }
- if (is_infinity())
- {
- return *this;
- }
- }
- return int_adapter<int_type>(value_ + rhs);
- }
-
-
- template<class rhs_type>
- inline
- int_adapter operator-(const int_adapter<rhs_type>& rhs)const
- {
- if(is_special() || rhs.is_special())
- {
- if (is_nan() || rhs.is_nan())
- {
- return int_adapter::not_a_number();
- }
- if((is_pos_inf(value_) && rhs.is_pos_inf(rhs.as_number())) ||
- (is_neg_inf(value_) && rhs.is_neg_inf(rhs.as_number())) )
- {
- return int_adapter::not_a_number();
- }
- if (is_infinity())
- {
- return *this;
- }
- if (rhs.is_pos_inf(rhs.as_number()))
- {
- return int_adapter::neg_infinity();
- }
- if (rhs.is_neg_inf(rhs.as_number()))
- {
- return int_adapter::pos_infinity();
- }
- }
- return int_adapter<int_type>(value_ - rhs.as_number());
- }
- int_adapter operator-(const int_type rhs) const
- {
- if(is_special())
- {
- if (is_nan())
- {
- return int_adapter<int_type>(not_a_number());
- }
- if (is_infinity())
- {
- return *this;
- }
- }
- return int_adapter<int_type>(value_ - rhs);
- }
-
- int_adapter operator*(const int_adapter& rhs)const
- {
- if(this->is_special() || rhs.is_special())
- {
- return mult_div_specials(rhs);
- }
- return int_adapter<int_type>(value_ * rhs.value_);
- }
-
- int_adapter operator*(const int rhs) const
- {
- if(is_special())
- {
- return mult_div_specials(rhs);
- }
- return int_adapter<int_type>(value_ * rhs);
- }
-
- int_adapter operator/(const int_adapter& rhs)const
- {
- if(this->is_special() || rhs.is_special())
- {
- if(is_infinity() && rhs.is_infinity())
- {
- return int_adapter<int_type>(not_a_number());
- }
- if(rhs != 0)
- {
- return mult_div_specials(rhs);
- }
- else {
- return int_adapter<int_type>(value_ / rhs.value_);
- }
- }
- return int_adapter<int_type>(value_ / rhs.value_);
- }
-
- int_adapter operator/(const int rhs) const
- {
- if(is_special() && rhs != 0)
- {
- return mult_div_specials(rhs);
- }
- return int_adapter<int_type>(value_ / rhs);
- }
-
- int_adapter operator%(const int_adapter& rhs)const
- {
- if(this->is_special() || rhs.is_special())
- {
- if(is_infinity() && rhs.is_infinity())
- {
- return int_adapter<int_type>(not_a_number());
- }
- if(rhs != 0)
- {
- return mult_div_specials(rhs);
- }
- else {
- return int_adapter<int_type>(value_ % rhs.value_);
- }
- }
- return int_adapter<int_type>(value_ % rhs.value_);
- }
-
- int_adapter operator%(const int rhs) const
- {
- if(is_special() && rhs != 0)
- {
- return mult_div_specials(rhs);
- }
- return int_adapter<int_type>(value_ % rhs);
- }
- private:
- int_type value_;
-
-
- int compare(const int_adapter& rhs)const
- {
- if(this->is_special() || rhs.is_special())
- {
- if(this->is_nan() || rhs.is_nan()) {
- if(this->is_nan() && rhs.is_nan()) {
- return 0;
- }
- else {
- return 2;
- }
- }
- if((is_neg_inf(value_) && !is_neg_inf(rhs.value_)) ||
- (is_pos_inf(rhs.value_) && !is_pos_inf(value_)) )
- {
- return -1;
- }
- if((is_pos_inf(value_) && !is_pos_inf(rhs.value_)) ||
- (is_neg_inf(rhs.value_) && !is_neg_inf(value_)) ) {
- return 1;
- }
- }
- if(value_ < rhs.value_) return -1;
- if(value_ > rhs.value_) return 1;
-
- return 0;
- }
-
-
- int_adapter mult_div_specials(const int_adapter& rhs)const
- {
- int min_value;
-
- bool is_signed = std::numeric_limits<int_type>::is_signed;
- if(is_signed) {
- min_value = 0;
- }
- else {
- min_value = 1;
- }
- if(this->is_nan() || rhs.is_nan()) {
- return int_adapter<int_type>(not_a_number());
- }
- if((*this > 0 && rhs > 0) || (*this < min_value && rhs < min_value)) {
- return int_adapter<int_type>(pos_infinity());
- }
- if((*this > 0 && rhs < min_value) || (*this < min_value && rhs > 0)) {
- return int_adapter<int_type>(neg_infinity());
- }
-
- return int_adapter<int_type>(not_a_number());
- }
-
-
- int_adapter mult_div_specials(const int& rhs) const
- {
- int min_value;
-
- bool is_signed = std::numeric_limits<int_type>::is_signed;
- if(is_signed) {
- min_value = 0;
- }
- else {
- min_value = 1;
- }
- if(this->is_nan()) {
- return int_adapter<int_type>(not_a_number());
- }
- if((*this > 0 && rhs > 0) || (*this < min_value && rhs < 0)) {
- return int_adapter<int_type>(pos_infinity());
- }
- if((*this > 0 && rhs < 0) || (*this < min_value && rhs > 0)) {
- return int_adapter<int_type>(neg_infinity());
- }
-
- return int_adapter<int_type>(not_a_number());
- }
-
- };
- #ifndef BOOST_DATE_TIME_NO_LOCALE
-
-
- template<class charT, class traits, typename int_type>
- inline
- std::basic_ostream<charT, traits>&
- operator<<(std::basic_ostream<charT, traits>& os, const int_adapter<int_type>& ia)
- {
- if(ia.is_special()) {
-
- switch(ia.as_special())
- {
- case not_a_date_time:
- os << "not-a-number";
- break;
- case pos_infin:
- os << "+infinity";
- break;
- case neg_infin:
- os << "-infinity";
- break;
- default:
- os << "";
- }
- }
- else {
- os << ia.as_number();
- }
- return os;
- }
- #endif
- } }
- #endif
|