safe_integer_literal.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #ifndef BOOST_NUMERIC_SAFE_INTEGER_LITERAL_HPP
  2. #define BOOST_NUMERIC_SAFE_INTEGER_LITERAL_HPP
  3. // Copyright (c) 2012 Robert Ramey
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. #include <cstdint> // for intmax_t/uintmax_t
  9. #include <iosfwd>
  10. #include <type_traits> // conditional, enable_if
  11. #include <boost/mp11/utility.hpp>
  12. #include "utility.hpp"
  13. #include "safe_integer.hpp"
  14. #include "checked_integer.hpp"
  15. namespace boost {
  16. namespace safe_numerics {
  17. template<typename T, T N, class P, class E>
  18. class safe_literal_impl;
  19. template<typename T, T N, class P, class E>
  20. struct is_safe<safe_literal_impl<T, N, P, E> > : public std::true_type
  21. {};
  22. template<typename T, T N, class P, class E>
  23. struct get_promotion_policy<safe_literal_impl<T, N, P, E> > {
  24. using type = P;
  25. };
  26. template<typename T, T N, class P, class E>
  27. struct get_exception_policy<safe_literal_impl<T, N, P, E> > {
  28. using type = E;
  29. };
  30. template<typename T, T N, class P, class E>
  31. struct base_type<safe_literal_impl<T, N, P, E> > {
  32. using type = T;
  33. };
  34. template<typename T, T N, class P, class E>
  35. constexpr T base_value(
  36. const safe_literal_impl<T, N, P, E> &
  37. ) {
  38. return N;
  39. }
  40. template<typename CharT, typename Traits, typename T, T N, class P, class E>
  41. inline std::basic_ostream<CharT, Traits> & operator<<(
  42. std::basic_ostream<CharT, Traits> & os,
  43. const safe_literal_impl<T, N, P, E> &
  44. ){
  45. return os << (
  46. (std::is_same<T, signed char>::value
  47. || std::is_same<T, unsigned char>::value
  48. ) ?
  49. static_cast<int>(N)
  50. :
  51. N
  52. );
  53. }
  54. template<typename T, T N, class P, class E>
  55. class safe_literal_impl {
  56. template<
  57. class CharT,
  58. class Traits
  59. >
  60. friend std::basic_ostream<CharT, Traits> & operator<<(
  61. std::basic_ostream<CharT, Traits> & os,
  62. const safe_literal_impl &
  63. ){
  64. return os << (
  65. (::std::is_same<T, signed char>::value
  66. || ::std::is_same<T, unsigned char>::value
  67. || ::std::is_same<T, wchar_t>::value
  68. ) ?
  69. static_cast<int>(N)
  70. :
  71. N
  72. );
  73. };
  74. public:
  75. ////////////////////////////////////////////////////////////
  76. // constructors
  77. // default constructor
  78. constexpr safe_literal_impl(){}
  79. /////////////////////////////////////////////////////////////////
  80. // casting operators for intrinsic integers
  81. // convert to any type which is not safe. safe types need to be
  82. // excluded to prevent ambiguous function selection which
  83. // would otherwise occur
  84. template<
  85. class R,
  86. typename std::enable_if<
  87. ! boost::safe_numerics::is_safe<R>::value,
  88. int
  89. >::type = 0
  90. >
  91. constexpr operator R () const {
  92. return N;
  93. }
  94. // non mutating unary operators
  95. constexpr safe_literal_impl<T, N, P, E> operator+() const { // unary plus
  96. return safe_literal_impl<T, N, P, E>();
  97. }
  98. // after much consideration, I've permitted the resulting value of a unary
  99. // - to change the type in accordance with the promotion policy.
  100. // The C++ standard does invoke integral promotions so it's changing the type as well.
  101. /* section 5.3.1 &8 of the C++ standard
  102. The operand of the unary - operator shall have arithmetic or unscoped
  103. enumeration type and the result is the negation of its operand. Integral
  104. promotion is performed on integral or enumeration operands. The negative
  105. of an unsigned quantity is computed by subtracting its value from 2n,
  106. where n is the number of bits in the promoted operand. The type of the
  107. result is the type of the promoted operand.
  108. */
  109. template<
  110. typename Tx, Tx Nx, typename = std::enable_if_t<! checked::minus(Nx).exception()>
  111. >
  112. constexpr auto minus_helper() const {
  113. return safe_literal_impl<Tx, -N, P, E>();
  114. }
  115. constexpr auto operator-() const { // unary minus
  116. return minus_helper<T, N>();
  117. }
  118. /* section 5.3.1 &10 of the C++ standard
  119. The operand of ~ shall have integral or unscoped enumeration type;
  120. the result is the ones’ complement of its operand. Integral promotions
  121. are performed. The type of the result is the type of the promoted operand.
  122. constexpr safe_literal_impl<T, checked::bitwise_not(N), P, E> operator~() const { // invert bits
  123. return safe_literal_impl<T, checked::bitwise_not(N), P, E>();
  124. }
  125. */
  126. template<
  127. typename Tx, Tx Nx, typename = std::enable_if_t<! checked::bitwise_not(Nx).exception()>
  128. >
  129. constexpr auto not_helper() const {
  130. return safe_literal_impl<Tx, ~N, P, E>();
  131. }
  132. constexpr auto operator~() const { // unary minus
  133. return not_helper<T, N>();
  134. }
  135. };
  136. template<
  137. std::intmax_t N,
  138. class P = void,
  139. class E = void
  140. >
  141. using safe_signed_literal = safe_literal_impl<
  142. typename utility::signed_stored_type<N, N>,
  143. N,
  144. P,
  145. E
  146. >;
  147. template<
  148. std::uintmax_t N,
  149. class P = void,
  150. class E = void
  151. >
  152. using safe_unsigned_literal = safe_literal_impl<
  153. typename utility::unsigned_stored_type<N, N>,
  154. N,
  155. P,
  156. E
  157. >;
  158. template<
  159. class T,
  160. T N,
  161. class P = void,
  162. class E = void,
  163. typename std::enable_if<
  164. std::is_signed<T>::value,
  165. int
  166. >::type = 0
  167. >
  168. constexpr auto make_safe_literal_impl() {
  169. return boost::safe_numerics::safe_signed_literal<N, P, E>();
  170. }
  171. template<
  172. class T,
  173. T N,
  174. class P = void,
  175. class E = void,
  176. typename std::enable_if<
  177. ! std::is_signed<T>::value,
  178. int
  179. >::type = 0
  180. >
  181. constexpr auto make_safe_literal_impl() {
  182. return boost::safe_numerics::safe_unsigned_literal<N, P, E>();
  183. }
  184. } // safe_numerics
  185. } // boost
  186. #define make_safe_literal(n, P, E) \
  187. boost::safe_numerics::make_safe_literal_impl<decltype(n), n, P, E>()
  188. /////////////////////////////////////////////////////////////////
  189. // numeric limits for safe_literal etc.
  190. #include <limits>
  191. namespace std {
  192. template<
  193. typename T,
  194. T N,
  195. class P,
  196. class E
  197. >
  198. class numeric_limits<boost::safe_numerics::safe_literal_impl<T, N, P, E> >
  199. : public std::numeric_limits<T>
  200. {
  201. using SL = boost::safe_numerics::safe_literal_impl<T, N, P, E>;
  202. public:
  203. constexpr static SL lowest() noexcept {
  204. return SL();
  205. }
  206. constexpr static SL min() noexcept {
  207. return SL();
  208. }
  209. constexpr static SL max() noexcept {
  210. return SL();
  211. }
  212. };
  213. } // std
  214. #endif // BOOST_NUMERIC_SAFE_INTEGER_LITERAL_HPP