int_adapter.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. int_adapter(int_type v) :
  43. value_(v)
  44. {}
  45. static bool has_infinity()
  46. {
  47. return true;
  48. }
  49. static const int_adapter pos_infinity()
  50. {
  51. return (::std::numeric_limits<int_type>::max)();
  52. }
  53. static const int_adapter neg_infinity()
  54. {
  55. return (::std::numeric_limits<int_type>::min)();
  56. }
  57. static const int_adapter not_a_number()
  58. {
  59. return (::std::numeric_limits<int_type>::max)()-1;
  60. }
  61. static int_adapter max BOOST_PREVENT_MACRO_SUBSTITUTION ()
  62. {
  63. return (::std::numeric_limits<int_type>::max)()-2;
  64. }
  65. static int_adapter min BOOST_PREVENT_MACRO_SUBSTITUTION ()
  66. {
  67. return (::std::numeric_limits<int_type>::min)()+1;
  68. }
  69. static 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 bool is_inf(int_type v)
  81. {
  82. return (v == neg_infinity().as_number() ||
  83. v == pos_infinity().as_number());
  84. }
  85. static bool is_neg_inf(int_type v)
  86. {
  87. return (v == neg_infinity().as_number());
  88. }
  89. static bool is_pos_inf(int_type v)
  90. {
  91. return (v == pos_infinity().as_number());
  92. }
  93. static 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 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 int_type maxcount()
  107. {
  108. return (::std::numeric_limits<int_type>::max)()-3;
  109. }
  110. bool is_infinity() const
  111. {
  112. return (value_ == neg_infinity().as_number() ||
  113. value_ == pos_infinity().as_number());
  114. }
  115. bool is_pos_infinity()const
  116. {
  117. return(value_ == pos_infinity().as_number());
  118. }
  119. bool is_neg_infinity()const
  120. {
  121. return(value_ == neg_infinity().as_number());
  122. }
  123. bool is_nan() const
  124. {
  125. return (value_ == not_a_number().as_number());
  126. }
  127. bool is_special() const
  128. {
  129. return(is_infinity() || is_nan());
  130. }
  131. bool operator==(const int_adapter& rhs) const
  132. {
  133. return (compare(rhs) == 0);
  134. }
  135. 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. bool operator!=(const int_adapter& rhs) const
  147. {
  148. return (compare(rhs) != 0);
  149. }
  150. 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. bool operator<(const int_adapter& rhs) const
  162. {
  163. return (compare(rhs) == -1);
  164. }
  165. 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. bool operator>(const int_adapter& rhs) const
  178. {
  179. return (compare(rhs) == 1);
  180. }
  181. int_type as_number() const
  182. {
  183. return value_;
  184. }
  185. //! Returns either special value type or is_not_special
  186. 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. inline
  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. int_adapter operator+(const int_type rhs) const
  228. {
  229. if(is_special())
  230. {
  231. if (is_nan())
  232. {
  233. return int_adapter<int_type>(not_a_number());
  234. }
  235. if (is_infinity())
  236. {
  237. return *this;
  238. }
  239. }
  240. return int_adapter<int_type>(value_ + rhs);
  241. }
  242. /*! Operator allows for subtracting dissimilar int_adapter types.
  243. * The return type will match that of the the calling object's type */
  244. template<class rhs_type>
  245. inline
  246. int_adapter operator-(const int_adapter<rhs_type>& rhs)const
  247. {
  248. if(is_special() || rhs.is_special())
  249. {
  250. if (is_nan() || rhs.is_nan())
  251. {
  252. return int_adapter::not_a_number();
  253. }
  254. if((is_pos_inf(value_) && rhs.is_pos_inf(rhs.as_number())) ||
  255. (is_neg_inf(value_) && rhs.is_neg_inf(rhs.as_number())) )
  256. {
  257. return int_adapter::not_a_number();
  258. }
  259. if (is_infinity())
  260. {
  261. return *this;
  262. }
  263. if (rhs.is_pos_inf(rhs.as_number()))
  264. {
  265. return int_adapter::neg_infinity();
  266. }
  267. if (rhs.is_neg_inf(rhs.as_number()))
  268. {
  269. return int_adapter::pos_infinity();
  270. }
  271. }
  272. return int_adapter<int_type>(value_ - static_cast<int_type>(rhs.as_number()));
  273. }
  274. int_adapter operator-(const int_type rhs) const
  275. {
  276. if(is_special())
  277. {
  278. if (is_nan())
  279. {
  280. return int_adapter<int_type>(not_a_number());
  281. }
  282. if (is_infinity())
  283. {
  284. return *this;
  285. }
  286. }
  287. return int_adapter<int_type>(value_ - rhs);
  288. }
  289. // should templatize this to be consistant with op +-
  290. int_adapter operator*(const int_adapter& rhs)const
  291. {
  292. if(this->is_special() || rhs.is_special())
  293. {
  294. return mult_div_specials(rhs);
  295. }
  296. return int_adapter<int_type>(value_ * rhs.value_);
  297. }
  298. /*! Provided for cases when automatic conversion from
  299. * 'int' to 'int_adapter' causes incorrect results. */
  300. int_adapter operator*(const int rhs) const
  301. {
  302. if(is_special())
  303. {
  304. return mult_div_specials(rhs);
  305. }
  306. return int_adapter<int_type>(value_ * rhs);
  307. }
  308. // should templatize this to be consistant with op +-
  309. int_adapter operator/(const int_adapter& rhs)const
  310. {
  311. if(this->is_special() || rhs.is_special())
  312. {
  313. if(is_infinity() && rhs.is_infinity())
  314. {
  315. return int_adapter<int_type>(not_a_number());
  316. }
  317. if(rhs != 0)
  318. {
  319. return mult_div_specials(rhs);
  320. }
  321. else { // let divide by zero blow itself up
  322. return int_adapter<int_type>(value_ / rhs.value_);
  323. }
  324. }
  325. return int_adapter<int_type>(value_ / rhs.value_);
  326. }
  327. /*! Provided for cases when automatic conversion from
  328. * 'int' to 'int_adapter' causes incorrect results. */
  329. int_adapter operator/(const int rhs) const
  330. {
  331. if(is_special() && rhs != 0)
  332. {
  333. return mult_div_specials(rhs);
  334. }
  335. return int_adapter<int_type>(value_ / rhs);
  336. }
  337. // should templatize this to be consistant with op +-
  338. int_adapter operator%(const int_adapter& rhs)const
  339. {
  340. if(this->is_special() || rhs.is_special())
  341. {
  342. if(is_infinity() && rhs.is_infinity())
  343. {
  344. return int_adapter<int_type>(not_a_number());
  345. }
  346. if(rhs != 0)
  347. {
  348. return mult_div_specials(rhs);
  349. }
  350. else { // let divide by zero blow itself up
  351. return int_adapter<int_type>(value_ % rhs.value_);
  352. }
  353. }
  354. return int_adapter<int_type>(value_ % rhs.value_);
  355. }
  356. /*! Provided for cases when automatic conversion from
  357. * 'int' to 'int_adapter' causes incorrect results. */
  358. int_adapter operator%(const int rhs) const
  359. {
  360. if(is_special() && rhs != 0)
  361. {
  362. return mult_div_specials(rhs);
  363. }
  364. return int_adapter<int_type>(value_ % rhs);
  365. }
  366. private:
  367. int_type value_;
  368. //! returns -1, 0, 1, or 2 if 'this' is <, ==, >, or 'nan comparison' rhs
  369. int compare(const int_adapter& rhs)const
  370. {
  371. if(this->is_special() || rhs.is_special())
  372. {
  373. if(this->is_nan() || rhs.is_nan()) {
  374. if(this->is_nan() && rhs.is_nan()) {
  375. return 0; // equal
  376. }
  377. else {
  378. return 2; // nan
  379. }
  380. }
  381. if((is_neg_inf(value_) && !is_neg_inf(rhs.value_)) ||
  382. (is_pos_inf(rhs.value_) && !is_pos_inf(value_)) )
  383. {
  384. return -1; // less than
  385. }
  386. if((is_pos_inf(value_) && !is_pos_inf(rhs.value_)) ||
  387. (is_neg_inf(rhs.value_) && !is_neg_inf(value_)) ) {
  388. return 1; // greater than
  389. }
  390. }
  391. if(value_ < rhs.value_) return -1;
  392. if(value_ > rhs.value_) return 1;
  393. // implied-> if(value_ == rhs.value_)
  394. return 0;
  395. }
  396. /* When multiplying and dividing with at least 1 special value
  397. * very simmilar rules apply. In those cases where the rules
  398. * are different, they are handled in the respective operator
  399. * function. */
  400. //! Assumes at least 'this' or 'rhs' is a special value
  401. int_adapter mult_div_specials(const int_adapter& rhs)const
  402. {
  403. if(this->is_nan() || rhs.is_nan()) {
  404. return int_adapter<int_type>(not_a_number());
  405. }
  406. BOOST_CONSTEXPR_OR_CONST int min_value = std::numeric_limits<int_type>::is_signed ? 0 : 1;
  407. if((*this > 0 && rhs > 0) || (*this < min_value && rhs < min_value)) {
  408. return int_adapter<int_type>(pos_infinity());
  409. }
  410. if((*this > 0 && rhs < min_value) || (*this < min_value && rhs > 0)) {
  411. return int_adapter<int_type>(neg_infinity());
  412. }
  413. //implied -> if(this->value_ == 0 || rhs.value_ == 0)
  414. return int_adapter<int_type>(not_a_number());
  415. }
  416. /* Overloaded function necessary because of special
  417. * situation where int_adapter is instantiated with
  418. * 'unsigned' and func is called with negative int.
  419. * It would produce incorrect results since 'unsigned'
  420. * wraps around when initialized with a negative value */
  421. //! Assumes 'this' is a special value
  422. int_adapter mult_div_specials(const int& rhs) const
  423. {
  424. if(this->is_nan()) {
  425. return int_adapter<int_type>(not_a_number());
  426. }
  427. BOOST_CONSTEXPR_OR_CONST int min_value = std::numeric_limits<int_type>::is_signed ? 0 : 1;
  428. if((*this > 0 && rhs > 0) || (*this < min_value && rhs < 0)) {
  429. return int_adapter<int_type>(pos_infinity());
  430. }
  431. if((*this > 0 && rhs < 0) || (*this < min_value && rhs > 0)) {
  432. return int_adapter<int_type>(neg_infinity());
  433. }
  434. //implied -> if(this->value_ == 0 || rhs.value_ == 0)
  435. return int_adapter<int_type>(not_a_number());
  436. }
  437. };
  438. #ifndef BOOST_DATE_TIME_NO_LOCALE
  439. /*! Expected output is either a numeric representation
  440. * or a special values representation.<BR>
  441. * Ex. "12", "+infinity", "not-a-number", etc. */
  442. //template<class charT = char, class traits = std::traits<charT>, typename int_type>
  443. template<class charT, class traits, typename int_type>
  444. inline
  445. std::basic_ostream<charT, traits>&
  446. operator<<(std::basic_ostream<charT, traits>& os, const int_adapter<int_type>& ia)
  447. {
  448. if(ia.is_special()) {
  449. // switch copied from date_names_put.hpp
  450. switch(ia.as_special())
  451. {
  452. case not_a_date_time:
  453. os << "not-a-number";
  454. break;
  455. case pos_infin:
  456. os << "+infinity";
  457. break;
  458. case neg_infin:
  459. os << "-infinity";
  460. break;
  461. default:
  462. os << "";
  463. }
  464. }
  465. else {
  466. os << ia.as_number();
  467. }
  468. return os;
  469. }
  470. #endif
  471. } } //namespace date_time
  472. #if defined(BOOST_MSVC)
  473. #pragma warning(pop)
  474. #endif
  475. #endif