| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516 | #ifndef _DATE_TIME_INT_ADAPTER_HPP__#define _DATE_TIME_INT_ADAPTER_HPP__/* Copyright (c) 2002,2003 CrystalClear Software, Inc. * Use, modification and distribution is subject to the  * Boost Software License, Version 1.0. (See accompanying * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) * Author: Jeff Garland, Bart Garst * $Date$ */#include "boost/config.hpp"#include "boost/limits.hpp" //work around compilers without limits#include "boost/date_time/special_defs.hpp"#include "boost/date_time/locale_config.hpp"#ifndef BOOST_DATE_TIME_NO_LOCALE#  include <ostream>#endif#if defined(BOOST_MSVC)#pragma warning(push)// conditional expression is constant#pragma warning(disable: 4127)#endifnamespace boost {namespace date_time {//! Adapter to create integer types with +-infinity, and not a value/*! This class is used internally in counted date/time representations. *  It adds the floating point like features of infinities and *  not a number. It also provides mathmatical operations with *  consideration to special values following these rules: *@code *  +infinity  -  infinity  == Not A Number (NAN) *   infinity  *  non-zero  == infinity *   infinity  *  zero      == NAN *  +infinity  * -integer   == -infinity *   infinity  /  infinity  == NAN *   infinity  *  infinity  == infinity  *@endcode  */template<typename int_type_>class int_adapter {public:  typedef int_type_ int_type;  BOOST_CXX14_CONSTEXPR int_adapter(int_type v) :    value_(v)  {}  static BOOST_CONSTEXPR bool has_infinity()  {    return  true;  }  static BOOST_CONSTEXPR int_adapter  pos_infinity()  {    return (::std::numeric_limits<int_type>::max)();  }  static BOOST_CONSTEXPR int_adapter  neg_infinity()  {    return (::std::numeric_limits<int_type>::min)();  }  static BOOST_CONSTEXPR int_adapter  not_a_number()  {    return (::std::numeric_limits<int_type>::max)()-1;  }  static BOOST_CONSTEXPR int_adapter max BOOST_PREVENT_MACRO_SUBSTITUTION ()  {    return (::std::numeric_limits<int_type>::max)()-2;  }  static BOOST_CONSTEXPR int_adapter min BOOST_PREVENT_MACRO_SUBSTITUTION ()  {    return (::std::numeric_limits<int_type>::min)()+1;  }  static BOOST_CXX14_CONSTEXPR 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 BOOST_CONSTEXPR bool is_inf(int_type v)  {    return (v == neg_infinity().as_number() ||            v == pos_infinity().as_number());  }  static BOOST_CXX14_CONSTEXPR bool is_neg_inf(int_type v)  {    return (v == neg_infinity().as_number());  }  static BOOST_CXX14_CONSTEXPR bool is_pos_inf(int_type v)  {    return (v == pos_infinity().as_number());  }  static BOOST_CXX14_CONSTEXPR bool is_not_a_number(int_type v)  {    return (v == not_a_number().as_number());  }  //! Returns either special value type or is_not_special  static BOOST_CXX14_CONSTEXPR 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;  }  //-3 leaves room for representations of infinity and not a date  static BOOST_CONSTEXPR int_type maxcount()  {    return (::std::numeric_limits<int_type>::max)()-3;  }  BOOST_CONSTEXPR bool is_infinity() const  {    return (value_ == neg_infinity().as_number() ||            value_ == pos_infinity().as_number());  }  BOOST_CONSTEXPR bool is_pos_infinity()const  {    return(value_ == pos_infinity().as_number());  }  BOOST_CONSTEXPR bool is_neg_infinity()const  {    return(value_ == neg_infinity().as_number());  }  BOOST_CONSTEXPR bool is_nan() const  {    return (value_ == not_a_number().as_number());  }  BOOST_CONSTEXPR bool is_special() const  {    return(is_infinity() || is_nan());   }  BOOST_CONSTEXPR bool operator==(const int_adapter& rhs) const  {    return (compare(rhs) == 0);  }  BOOST_CXX14_CONSTEXPR bool operator==(const int& rhs) const  {    if(!std::numeric_limits<int_type>::is_signed)    {      if(is_neg_inf(value_) && rhs == 0)      {        return false;      }    }    return (compare(rhs) == 0);  }  BOOST_CONSTEXPR bool operator!=(const int_adapter& rhs) const  {    return (compare(rhs) != 0);  }  BOOST_CXX14_CONSTEXPR bool operator!=(const int& rhs) const  {    if(!std::numeric_limits<int_type>::is_signed)    {      if(is_neg_inf(value_) && rhs == 0)      {        return true;      }    }    return (compare(rhs) != 0);  }  BOOST_CONSTEXPR bool operator<(const int_adapter& rhs) const  {    return (compare(rhs) == -1);  }  BOOST_CXX14_CONSTEXPR bool operator<(const int& rhs) const  {    // quiets compiler warnings    if(!std::numeric_limits<int_type>::is_signed)    {      if(is_neg_inf(value_) && rhs == 0)      {        return true;      }    }    return (compare(rhs) == -1);  }  BOOST_CONSTEXPR bool operator>(const int_adapter& rhs) const  {    return (compare(rhs) == 1);  }  BOOST_CONSTEXPR int_type as_number() const  {    return value_;  }  //! Returns either special value type or is_not_special  BOOST_CONSTEXPR special_values as_special() const  {    return int_adapter::to_special(value_);  }  //creates nasty ambiguities//   operator int_type() const//   {//     return value_;//   }  /*! Operator allows for adding dissimilar int_adapter types.   * The return type will match that of the the calling object's type */  template<class rhs_type>  BOOST_CXX14_CONSTEXPR  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_ + static_cast<int_type>(rhs.as_number()));  }  BOOST_CXX14_CONSTEXPR  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);  }    /*! Operator allows for subtracting dissimilar int_adapter types.   * The return type will match that of the the calling object's type */  template<class rhs_type>  BOOST_CXX14_CONSTEXPR  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_ - static_cast<int_type>(rhs.as_number()));  }  BOOST_CXX14_CONSTEXPR  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);  }  // should templatize this to be consistant with op +-  BOOST_CXX14_CONSTEXPR  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_);  }  /*! Provided for cases when automatic conversion from    * 'int' to 'int_adapter' causes incorrect results. */  BOOST_CXX14_CONSTEXPR  int_adapter operator*(const int rhs) const  {    if(is_special())    {      return mult_div_specials(rhs);    }    return int_adapter<int_type>(value_ * rhs);  }  // should templatize this to be consistant with op +-  BOOST_CXX14_CONSTEXPR  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 { // let divide by zero blow itself up        return int_adapter<int_type>(value_ / rhs.value_); //NOLINT      }    }    return int_adapter<int_type>(value_ / rhs.value_);  }  /*! Provided for cases when automatic conversion from    * 'int' to 'int_adapter' causes incorrect results. */  BOOST_CXX14_CONSTEXPR  int_adapter operator/(const int rhs) const  {    if(is_special() && rhs != 0)    {      return mult_div_specials(rhs);    }    // let divide by zero blow itself up like int    return int_adapter<int_type>(value_ / rhs); //NOLINT  }  // should templatize this to be consistant with op +-  BOOST_CXX14_CONSTEXPR  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 { // let divide by zero blow itself up        return int_adapter<int_type>(value_ % rhs.value_); //NOLINT      }    }    return int_adapter<int_type>(value_ % rhs.value_);  }  /*! Provided for cases when automatic conversion from    * 'int' to 'int_adapter' causes incorrect results. */  BOOST_CXX14_CONSTEXPR  int_adapter operator%(const int rhs) const  {    if(is_special() && rhs != 0)    {      return mult_div_specials(rhs);    }    // let divide by zero blow itself up    return int_adapter<int_type>(value_ % rhs); //NOLINT  }private:  int_type value_;    //! returns -1, 0, 1, or 2 if 'this' is <, ==, >, or 'nan comparison' rhs  BOOST_CXX14_CONSTEXPR  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; // equal        }        else {          return 2; // nan        }      }      if((is_neg_inf(value_) && !is_neg_inf(rhs.value_)) ||         (is_pos_inf(rhs.value_) && !is_pos_inf(value_)) )        {          return -1; // less than        }      if((is_pos_inf(value_) && !is_pos_inf(rhs.value_)) ||         (is_neg_inf(rhs.value_) && !is_neg_inf(value_)) ) {        return 1; // greater than      }    }    if(value_ < rhs.value_) return -1;    if(value_ > rhs.value_) return 1;    // implied-> if(value_ == rhs.value_)     return 0;  }  /* When multiplying and dividing with at least 1 special value   * very simmilar rules apply. In those cases where the rules   * are different, they are handled in the respective operator    * function. */  //! Assumes at least 'this' or 'rhs' is a special value  BOOST_CXX14_CONSTEXPR  int_adapter mult_div_specials(const int_adapter& rhs) const  {    if(this->is_nan() || rhs.is_nan()) {      return int_adapter<int_type>(not_a_number());    }    BOOST_CONSTEXPR_OR_CONST int min_value = std::numeric_limits<int_type>::is_signed ? 0 : 1;     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());    }    //implied -> if(this->value_ == 0 || rhs.value_ == 0)    return int_adapter<int_type>(not_a_number());  }  /* Overloaded function necessary because of special   * situation where int_adapter is instantiated with    * 'unsigned' and func is called with negative int.   * It would produce incorrect results since 'unsigned'   * wraps around when initialized with a negative value */  //! Assumes 'this' is a special value  BOOST_CXX14_CONSTEXPR  int_adapter mult_div_specials(const int& rhs) const  {    if(this->is_nan()) {      return int_adapter<int_type>(not_a_number());    }    BOOST_CONSTEXPR_OR_CONST int min_value = std::numeric_limits<int_type>::is_signed ? 0 : 1;     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());    }    //implied -> if(this->value_ == 0 || rhs.value_ == 0)    return int_adapter<int_type>(not_a_number());  }};#ifndef BOOST_DATE_TIME_NO_LOCALE  /*! Expected output is either a numeric representation    * or a special values representation.<BR>    * Ex. "12", "+infinity", "not-a-number", etc. */  //template<class charT = char, class traits = std::traits<charT>, typename int_type>  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 copied from date_names_put.hpp      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} } //namespace date_time#if defined(BOOST_MSVC)#pragma warning(pop)#endif#endif
 |