int_adapter.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. namespace boost {
  18. namespace date_time {
  19. //! Adapter to create integer types with +-infinity, and not a value
  20. /*! This class is used internally in counted date/time representations.
  21. * It adds the floating point like features of infinities and
  22. * not a number. It also provides mathmatical operations with
  23. * consideration to special values following these rules:
  24. *@code
  25. * +infinity - infinity == Not A Number (NAN)
  26. * infinity * non-zero == infinity
  27. * infinity * zero == NAN
  28. * +infinity * -integer == -infinity
  29. * infinity / infinity == NAN
  30. * infinity * infinity == infinity
  31. *@endcode
  32. */
  33. template<typename int_type_>
  34. class int_adapter {
  35. public:
  36. typedef int_type_ int_type;
  37. int_adapter(int_type v) :
  38. value_(v)
  39. {}
  40. static bool has_infinity()
  41. {
  42. return true;
  43. }
  44. static const int_adapter pos_infinity()
  45. {
  46. return (::std::numeric_limits<int_type>::max)();
  47. }
  48. static const int_adapter neg_infinity()
  49. {
  50. return (::std::numeric_limits<int_type>::min)();
  51. }
  52. static const int_adapter not_a_number()
  53. {
  54. return (::std::numeric_limits<int_type>::max)()-1;
  55. }
  56. static int_adapter max BOOST_PREVENT_MACRO_SUBSTITUTION ()
  57. {
  58. return (::std::numeric_limits<int_type>::max)()-2;
  59. }
  60. static int_adapter min BOOST_PREVENT_MACRO_SUBSTITUTION ()
  61. {
  62. return (::std::numeric_limits<int_type>::min)()+1;
  63. }
  64. static int_adapter from_special(special_values sv)
  65. {
  66. switch (sv) {
  67. case not_a_date_time: return not_a_number();
  68. case neg_infin: return neg_infinity();
  69. case pos_infin: return pos_infinity();
  70. case max_date_time: return (max)();
  71. case min_date_time: return (min)();
  72. default: return not_a_number();
  73. }
  74. }
  75. static bool is_inf(int_type v)
  76. {
  77. return (v == neg_infinity().as_number() ||
  78. v == pos_infinity().as_number());
  79. }
  80. static bool is_neg_inf(int_type v)
  81. {
  82. return (v == neg_infinity().as_number());
  83. }
  84. static bool is_pos_inf(int_type v)
  85. {
  86. return (v == pos_infinity().as_number());
  87. }
  88. static bool is_not_a_number(int_type v)
  89. {
  90. return (v == not_a_number().as_number());
  91. }
  92. //! Returns either special value type or is_not_special
  93. static special_values to_special(int_type v)
  94. {
  95. if (is_not_a_number(v)) return not_a_date_time;
  96. if (is_neg_inf(v)) return neg_infin;
  97. if (is_pos_inf(v)) return pos_infin;
  98. return not_special;
  99. }
  100. //-3 leaves room for representations of infinity and not a date
  101. static int_type maxcount()
  102. {
  103. return (::std::numeric_limits<int_type>::max)()-3;
  104. }
  105. bool is_infinity() const
  106. {
  107. return (value_ == neg_infinity().as_number() ||
  108. value_ == pos_infinity().as_number());
  109. }
  110. bool is_pos_infinity()const
  111. {
  112. return(value_ == pos_infinity().as_number());
  113. }
  114. bool is_neg_infinity()const
  115. {
  116. return(value_ == neg_infinity().as_number());
  117. }
  118. bool is_nan() const
  119. {
  120. return (value_ == not_a_number().as_number());
  121. }
  122. bool is_special() const
  123. {
  124. return(is_infinity() || is_nan());
  125. }
  126. bool operator==(const int_adapter& rhs) const
  127. {
  128. return (compare(rhs) == 0);
  129. }
  130. bool operator==(const int& rhs) const
  131. {
  132. // quiets compiler warnings
  133. bool is_signed = std::numeric_limits<int_type>::is_signed;
  134. if(!is_signed)
  135. {
  136. if(is_neg_inf(value_) && rhs == 0)
  137. {
  138. return false;
  139. }
  140. }
  141. return (compare(rhs) == 0);
  142. }
  143. bool operator!=(const int_adapter& rhs) const
  144. {
  145. return (compare(rhs) != 0);
  146. }
  147. bool operator!=(const int& rhs) const
  148. {
  149. // quiets compiler warnings
  150. bool is_signed = std::numeric_limits<int_type>::is_signed;
  151. if(!is_signed)
  152. {
  153. if(is_neg_inf(value_) && rhs == 0)
  154. {
  155. return true;
  156. }
  157. }
  158. return (compare(rhs) != 0);
  159. }
  160. bool operator<(const int_adapter& rhs) const
  161. {
  162. return (compare(rhs) == -1);
  163. }
  164. bool operator<(const int& rhs) const
  165. {
  166. // quiets compiler warnings
  167. bool is_signed = std::numeric_limits<int_type>::is_signed;
  168. if(!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. int min_value;
  404. // quiets compiler warnings
  405. bool is_signed = std::numeric_limits<int_type>::is_signed;
  406. if(is_signed) {
  407. min_value = 0;
  408. }
  409. else {
  410. min_value = 1;// there is no zero with unsigned
  411. }
  412. if(this->is_nan() || rhs.is_nan()) {
  413. return int_adapter<int_type>(not_a_number());
  414. }
  415. if((*this > 0 && rhs > 0) || (*this < min_value && rhs < min_value)) {
  416. return int_adapter<int_type>(pos_infinity());
  417. }
  418. if((*this > 0 && rhs < min_value) || (*this < min_value && rhs > 0)) {
  419. return int_adapter<int_type>(neg_infinity());
  420. }
  421. //implied -> if(this->value_ == 0 || rhs.value_ == 0)
  422. return int_adapter<int_type>(not_a_number());
  423. }
  424. /* Overloaded function necessary because of special
  425. * situation where int_adapter is instantiated with
  426. * 'unsigned' and func is called with negative int.
  427. * It would produce incorrect results since 'unsigned'
  428. * wraps around when initialized with a negative value */
  429. //! Assumes 'this' is a special value
  430. int_adapter mult_div_specials(const int& rhs) const
  431. {
  432. int min_value;
  433. // quiets compiler warnings
  434. bool is_signed = std::numeric_limits<int_type>::is_signed;
  435. if(is_signed) {
  436. min_value = 0;
  437. }
  438. else {
  439. min_value = 1;// there is no zero with unsigned
  440. }
  441. if(this->is_nan()) {
  442. return int_adapter<int_type>(not_a_number());
  443. }
  444. if((*this > 0 && rhs > 0) || (*this < min_value && rhs < 0)) {
  445. return int_adapter<int_type>(pos_infinity());
  446. }
  447. if((*this > 0 && rhs < 0) || (*this < min_value && rhs > 0)) {
  448. return int_adapter<int_type>(neg_infinity());
  449. }
  450. //implied -> if(this->value_ == 0 || rhs.value_ == 0)
  451. return int_adapter<int_type>(not_a_number());
  452. }
  453. };
  454. #ifndef BOOST_DATE_TIME_NO_LOCALE
  455. /*! Expected output is either a numeric representation
  456. * or a special values representation.<BR>
  457. * Ex. "12", "+infinity", "not-a-number", etc. */
  458. //template<class charT = char, class traits = std::traits<charT>, typename int_type>
  459. template<class charT, class traits, typename int_type>
  460. inline
  461. std::basic_ostream<charT, traits>&
  462. operator<<(std::basic_ostream<charT, traits>& os, const int_adapter<int_type>& ia)
  463. {
  464. if(ia.is_special()) {
  465. // switch copied from date_names_put.hpp
  466. switch(ia.as_special())
  467. {
  468. case not_a_date_time:
  469. os << "not-a-number";
  470. break;
  471. case pos_infin:
  472. os << "+infinity";
  473. break;
  474. case neg_infin:
  475. os << "-infinity";
  476. break;
  477. default:
  478. os << "";
  479. }
  480. }
  481. else {
  482. os << ia.as_number();
  483. }
  484. return os;
  485. }
  486. #endif
  487. } } //namespace date_time
  488. #endif