int_adapter.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. #ifndef _DATE_TIME_INT_ADAPTER_HPP__
  2. #define _DATE_TIME_INT_ADAPTER_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 "boost/config.hpp"
  11. #include "boost/limits.hpp" //work around compilers without limits
  12. #include "boost/date_time/special_defs.hpp"
  13. #include "boost/date_time/locale_config.hpp"
  14. #ifndef BOOST_DATE_TIME_NO_LOCALE
  15. # include <ostream>
  16. #endif
  17. #if defined(BOOST_MSVC)
  18. #pragma warning(push)
  19. // conditional expression is constant
  20. #pragma warning(disable: 4127)
  21. #endif
  22. namespace boost {
  23. namespace date_time {
  24. //! Adapter to create integer types with +-infinity, and not a value
  25. /*! This class is used internally in counted date/time representations.
  26. * It adds the floating point like features of infinities and
  27. * not a number. It also provides mathmatical operations with
  28. * consideration to special values following these rules:
  29. *@code
  30. * +infinity - infinity == Not A Number (NAN)
  31. * infinity * non-zero == infinity
  32. * infinity * zero == NAN
  33. * +infinity * -integer == -infinity
  34. * infinity / infinity == NAN
  35. * infinity * infinity == infinity
  36. *@endcode
  37. */
  38. template<typename int_type_>
  39. class int_adapter {
  40. public:
  41. typedef int_type_ int_type;
  42. BOOST_CXX14_CONSTEXPR int_adapter(int_type v) :
  43. value_(v)
  44. {}
  45. static BOOST_CONSTEXPR bool has_infinity()
  46. {
  47. return true;
  48. }
  49. static BOOST_CONSTEXPR int_adapter pos_infinity()
  50. {
  51. return (::std::numeric_limits<int_type>::max)();
  52. }
  53. static BOOST_CONSTEXPR int_adapter neg_infinity()
  54. {
  55. return (::std::numeric_limits<int_type>::min)();
  56. }
  57. static BOOST_CONSTEXPR int_adapter not_a_number()
  58. {
  59. return (::std::numeric_limits<int_type>::max)()-1;
  60. }
  61. static BOOST_CONSTEXPR int_adapter max BOOST_PREVENT_MACRO_SUBSTITUTION ()
  62. {
  63. return (::std::numeric_limits<int_type>::max)()-2;
  64. }
  65. static BOOST_CONSTEXPR int_adapter min BOOST_PREVENT_MACRO_SUBSTITUTION ()
  66. {
  67. return (::std::numeric_limits<int_type>::min)()+1;
  68. }
  69. static BOOST_CXX14_CONSTEXPR int_adapter from_special(special_values sv)
  70. {
  71. switch (sv) {
  72. case not_a_date_time: return not_a_number();
  73. case neg_infin: return neg_infinity();
  74. case pos_infin: return pos_infinity();
  75. case max_date_time: return (max)();
  76. case min_date_time: return (min)();
  77. default: return not_a_number();
  78. }
  79. }
  80. static BOOST_CONSTEXPR bool is_inf(int_type v)
  81. {
  82. return (v == neg_infinity().as_number() ||
  83. v == pos_infinity().as_number());
  84. }
  85. static BOOST_CXX14_CONSTEXPR bool is_neg_inf(int_type v)
  86. {
  87. return (v == neg_infinity().as_number());
  88. }
  89. static BOOST_CXX14_CONSTEXPR bool is_pos_inf(int_type v)
  90. {
  91. return (v == pos_infinity().as_number());
  92. }
  93. static BOOST_CXX14_CONSTEXPR bool is_not_a_number(int_type v)
  94. {
  95. return (v == not_a_number().as_number());
  96. }
  97. //! Returns either special value type or is_not_special
  98. static BOOST_CXX14_CONSTEXPR special_values to_special(int_type v)
  99. {
  100. if (is_not_a_number(v)) return not_a_date_time;
  101. if (is_neg_inf(v)) return neg_infin;
  102. if (is_pos_inf(v)) return pos_infin;
  103. return not_special;
  104. }
  105. //-3 leaves room for representations of infinity and not a date
  106. static BOOST_CONSTEXPR int_type maxcount()
  107. {
  108. return (::std::numeric_limits<int_type>::max)()-3;
  109. }
  110. BOOST_CONSTEXPR bool is_infinity() const
  111. {
  112. return (value_ == neg_infinity().as_number() ||
  113. value_ == pos_infinity().as_number());
  114. }
  115. BOOST_CONSTEXPR bool is_pos_infinity()const
  116. {
  117. return(value_ == pos_infinity().as_number());
  118. }
  119. BOOST_CONSTEXPR bool is_neg_infinity()const
  120. {
  121. return(value_ == neg_infinity().as_number());
  122. }
  123. BOOST_CONSTEXPR bool is_nan() const
  124. {
  125. return (value_ == not_a_number().as_number());
  126. }
  127. BOOST_CONSTEXPR bool is_special() const
  128. {
  129. return(is_infinity() || is_nan());
  130. }
  131. BOOST_CONSTEXPR bool operator==(const int_adapter& rhs) const
  132. {
  133. return (compare(rhs) == 0);
  134. }
  135. BOOST_CXX14_CONSTEXPR bool operator==(const int& rhs) const
  136. {
  137. if(!std::numeric_limits<int_type>::is_signed)
  138. {
  139. if(is_neg_inf(value_) && rhs == 0)
  140. {
  141. return false;
  142. }
  143. }
  144. return (compare(rhs) == 0);
  145. }
  146. BOOST_CONSTEXPR bool operator!=(const int_adapter& rhs) const
  147. {
  148. return (compare(rhs) != 0);
  149. }
  150. BOOST_CXX14_CONSTEXPR bool operator!=(const int& rhs) const
  151. {
  152. if(!std::numeric_limits<int_type>::is_signed)
  153. {
  154. if(is_neg_inf(value_) && rhs == 0)
  155. {
  156. return true;
  157. }
  158. }
  159. return (compare(rhs) != 0);
  160. }
  161. BOOST_CONSTEXPR bool operator<(const int_adapter& rhs) const
  162. {
  163. return (compare(rhs) == -1);
  164. }
  165. BOOST_CXX14_CONSTEXPR bool operator<(const int& rhs) const
  166. {
  167. // quiets compiler warnings
  168. if(!std::numeric_limits<int_type>::is_signed)
  169. {
  170. if(is_neg_inf(value_) && rhs == 0)
  171. {
  172. return true;
  173. }
  174. }
  175. return (compare(rhs) == -1);
  176. }
  177. BOOST_CONSTEXPR bool operator>(const int_adapter& rhs) const
  178. {
  179. return (compare(rhs) == 1);
  180. }
  181. BOOST_CONSTEXPR int_type as_number() const
  182. {
  183. return value_;
  184. }
  185. //! Returns either special value type or is_not_special
  186. BOOST_CONSTEXPR special_values as_special() const
  187. {
  188. return int_adapter::to_special(value_);
  189. }
  190. //creates nasty ambiguities
  191. // operator int_type() const
  192. // {
  193. // return value_;
  194. // }
  195. /*! Operator allows for adding dissimilar int_adapter types.
  196. * The return type will match that of the the calling object's type */
  197. template<class rhs_type>
  198. BOOST_CXX14_CONSTEXPR
  199. int_adapter operator+(const int_adapter<rhs_type>& rhs) const
  200. {
  201. if(is_special() || rhs.is_special())
  202. {
  203. if (is_nan() || rhs.is_nan())
  204. {
  205. return int_adapter::not_a_number();
  206. }
  207. if((is_pos_inf(value_) && rhs.is_neg_inf(rhs.as_number())) ||
  208. (is_neg_inf(value_) && rhs.is_pos_inf(rhs.as_number())) )
  209. {
  210. return int_adapter::not_a_number();
  211. }
  212. if (is_infinity())
  213. {
  214. return *this;
  215. }
  216. if (rhs.is_pos_inf(rhs.as_number()))
  217. {
  218. return int_adapter::pos_infinity();
  219. }
  220. if (rhs.is_neg_inf(rhs.as_number()))
  221. {
  222. return int_adapter::neg_infinity();
  223. }
  224. }
  225. return int_adapter<int_type>(value_ + static_cast<int_type>(rhs.as_number()));
  226. }
  227. BOOST_CXX14_CONSTEXPR
  228. int_adapter operator+(const int_type rhs) const
  229. {
  230. if(is_special())
  231. {
  232. if (is_nan())
  233. {
  234. return int_adapter<int_type>(not_a_number());
  235. }
  236. if (is_infinity())
  237. {
  238. return *this;
  239. }
  240. }
  241. return int_adapter<int_type>(value_ + rhs);
  242. }
  243. /*! Operator allows for subtracting dissimilar int_adapter types.
  244. * The return type will match that of the the calling object's type */
  245. template<class rhs_type>
  246. BOOST_CXX14_CONSTEXPR
  247. int_adapter operator-(const int_adapter<rhs_type>& rhs)const
  248. {
  249. if(is_special() || rhs.is_special())
  250. {
  251. if (is_nan() || rhs.is_nan())
  252. {
  253. return int_adapter::not_a_number();
  254. }
  255. if((is_pos_inf(value_) && rhs.is_pos_inf(rhs.as_number())) ||
  256. (is_neg_inf(value_) && rhs.is_neg_inf(rhs.as_number())) )
  257. {
  258. return int_adapter::not_a_number();
  259. }
  260. if (is_infinity())
  261. {
  262. return *this;
  263. }
  264. if (rhs.is_pos_inf(rhs.as_number()))
  265. {
  266. return int_adapter::neg_infinity();
  267. }
  268. if (rhs.is_neg_inf(rhs.as_number()))
  269. {
  270. return int_adapter::pos_infinity();
  271. }
  272. }
  273. return int_adapter<int_type>(value_ - static_cast<int_type>(rhs.as_number()));
  274. }
  275. BOOST_CXX14_CONSTEXPR
  276. int_adapter operator-(const int_type rhs) const
  277. {
  278. if(is_special())
  279. {
  280. if (is_nan())
  281. {
  282. return int_adapter<int_type>(not_a_number());
  283. }
  284. if (is_infinity())
  285. {
  286. return *this;
  287. }
  288. }
  289. return int_adapter<int_type>(value_ - rhs);
  290. }
  291. // should templatize this to be consistant with op +-
  292. BOOST_CXX14_CONSTEXPR
  293. int_adapter operator*(const int_adapter& rhs)const
  294. {
  295. if(this->is_special() || rhs.is_special())
  296. {
  297. return mult_div_specials(rhs);
  298. }
  299. return int_adapter<int_type>(value_ * rhs.value_);
  300. }
  301. /*! Provided for cases when automatic conversion from
  302. * 'int' to 'int_adapter' causes incorrect results. */
  303. BOOST_CXX14_CONSTEXPR
  304. int_adapter operator*(const int rhs) const
  305. {
  306. if(is_special())
  307. {
  308. return mult_div_specials(rhs);
  309. }
  310. return int_adapter<int_type>(value_ * rhs);
  311. }
  312. // should templatize this to be consistant with op +-
  313. BOOST_CXX14_CONSTEXPR
  314. int_adapter operator/(const int_adapter& rhs)const
  315. {
  316. if(this->is_special() || rhs.is_special())
  317. {
  318. if(is_infinity() && rhs.is_infinity())
  319. {
  320. return int_adapter<int_type>(not_a_number());
  321. }
  322. if(rhs != 0)
  323. {
  324. return mult_div_specials(rhs);
  325. }
  326. else { // let divide by zero blow itself up
  327. return int_adapter<int_type>(value_ / rhs.value_); //NOLINT
  328. }
  329. }
  330. return int_adapter<int_type>(value_ / rhs.value_);
  331. }
  332. /*! Provided for cases when automatic conversion from
  333. * 'int' to 'int_adapter' causes incorrect results. */
  334. BOOST_CXX14_CONSTEXPR
  335. int_adapter operator/(const int rhs) const
  336. {
  337. if(is_special() && rhs != 0)
  338. {
  339. return mult_div_specials(rhs);
  340. }
  341. // let divide by zero blow itself up like int
  342. return int_adapter<int_type>(value_ / rhs); //NOLINT
  343. }
  344. // should templatize this to be consistant with op +-
  345. BOOST_CXX14_CONSTEXPR
  346. int_adapter operator%(const int_adapter& rhs)const
  347. {
  348. if(this->is_special() || rhs.is_special())
  349. {
  350. if(is_infinity() && rhs.is_infinity())
  351. {
  352. return int_adapter<int_type>(not_a_number());
  353. }
  354. if(rhs != 0)
  355. {
  356. return mult_div_specials(rhs);
  357. }
  358. else { // let divide by zero blow itself up
  359. return int_adapter<int_type>(value_ % rhs.value_); //NOLINT
  360. }
  361. }
  362. return int_adapter<int_type>(value_ % rhs.value_);
  363. }
  364. /*! Provided for cases when automatic conversion from
  365. * 'int' to 'int_adapter' causes incorrect results. */
  366. BOOST_CXX14_CONSTEXPR
  367. int_adapter operator%(const int rhs) const
  368. {
  369. if(is_special() && rhs != 0)
  370. {
  371. return mult_div_specials(rhs);
  372. }
  373. // let divide by zero blow itself up
  374. return int_adapter<int_type>(value_ % rhs); //NOLINT
  375. }
  376. private:
  377. int_type value_;
  378. //! returns -1, 0, 1, or 2 if 'this' is <, ==, >, or 'nan comparison' rhs
  379. BOOST_CXX14_CONSTEXPR
  380. int compare( const int_adapter& rhs ) const
  381. {
  382. if(this->is_special() || rhs.is_special())
  383. {
  384. if(this->is_nan() || rhs.is_nan()) {
  385. if(this->is_nan() && rhs.is_nan()) {
  386. return 0; // equal
  387. }
  388. else {
  389. return 2; // nan
  390. }
  391. }
  392. if((is_neg_inf(value_) && !is_neg_inf(rhs.value_)) ||
  393. (is_pos_inf(rhs.value_) && !is_pos_inf(value_)) )
  394. {
  395. return -1; // less than
  396. }
  397. if((is_pos_inf(value_) && !is_pos_inf(rhs.value_)) ||
  398. (is_neg_inf(rhs.value_) && !is_neg_inf(value_)) ) {
  399. return 1; // greater than
  400. }
  401. }
  402. if(value_ < rhs.value_) return -1;
  403. if(value_ > rhs.value_) return 1;
  404. // implied-> if(value_ == rhs.value_)
  405. return 0;
  406. }
  407. /* When multiplying and dividing with at least 1 special value
  408. * very simmilar rules apply. In those cases where the rules
  409. * are different, they are handled in the respective operator
  410. * function. */
  411. //! Assumes at least 'this' or 'rhs' is a special value
  412. BOOST_CXX14_CONSTEXPR
  413. int_adapter mult_div_specials(const int_adapter& rhs) const
  414. {
  415. if(this->is_nan() || rhs.is_nan()) {
  416. return int_adapter<int_type>(not_a_number());
  417. }
  418. BOOST_CONSTEXPR_OR_CONST int min_value = std::numeric_limits<int_type>::is_signed ? 0 : 1;
  419. if((*this > 0 && rhs > 0) || (*this < min_value && rhs < min_value)) {
  420. return int_adapter<int_type>(pos_infinity());
  421. }
  422. if((*this > 0 && rhs < min_value) || (*this < min_value && rhs > 0)) {
  423. return int_adapter<int_type>(neg_infinity());
  424. }
  425. //implied -> if(this->value_ == 0 || rhs.value_ == 0)
  426. return int_adapter<int_type>(not_a_number());
  427. }
  428. /* Overloaded function necessary because of special
  429. * situation where int_adapter is instantiated with
  430. * 'unsigned' and func is called with negative int.
  431. * It would produce incorrect results since 'unsigned'
  432. * wraps around when initialized with a negative value */
  433. //! Assumes 'this' is a special value
  434. BOOST_CXX14_CONSTEXPR
  435. int_adapter mult_div_specials(const int& rhs) const
  436. {
  437. if(this->is_nan()) {
  438. return int_adapter<int_type>(not_a_number());
  439. }
  440. BOOST_CONSTEXPR_OR_CONST int min_value = std::numeric_limits<int_type>::is_signed ? 0 : 1;
  441. if((*this > 0 && rhs > 0) || (*this < min_value && rhs < 0)) {
  442. return int_adapter<int_type>(pos_infinity());
  443. }
  444. if((*this > 0 && rhs < 0) || (*this < min_value && rhs > 0)) {
  445. return int_adapter<int_type>(neg_infinity());
  446. }
  447. //implied -> if(this->value_ == 0 || rhs.value_ == 0)
  448. return int_adapter<int_type>(not_a_number());
  449. }
  450. };
  451. #ifndef BOOST_DATE_TIME_NO_LOCALE
  452. /*! Expected output is either a numeric representation
  453. * or a special values representation.<BR>
  454. * Ex. "12", "+infinity", "not-a-number", etc. */
  455. //template<class charT = char, class traits = std::traits<charT>, typename int_type>
  456. template<class charT, class traits, typename int_type>
  457. inline
  458. std::basic_ostream<charT, traits>&
  459. operator<<(std::basic_ostream<charT, traits>& os, const int_adapter<int_type>& ia)
  460. {
  461. if(ia.is_special()) {
  462. // switch copied from date_names_put.hpp
  463. switch(ia.as_special())
  464. {
  465. case not_a_date_time:
  466. os << "not-a-number";
  467. break;
  468. case pos_infin:
  469. os << "+infinity";
  470. break;
  471. case neg_infin:
  472. os << "-infinity";
  473. break;
  474. default:
  475. os << "";
  476. }
  477. }
  478. else {
  479. os << ia.as_number();
  480. }
  481. return os;
  482. }
  483. #endif
  484. } } //namespace date_time
  485. #if defined(BOOST_MSVC)
  486. #pragma warning(pop)
  487. #endif
  488. #endif