safe_base.hpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. #ifndef BOOST_NUMERIC_SAFE_BASE_HPP
  2. #define BOOST_NUMERIC_SAFE_BASE_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 <limits>
  9. #include <type_traits> // is_integral, enable_if, conditional is_convertible
  10. #include <boost/config.hpp> // BOOST_CLANG
  11. #include "concept/exception_policy.hpp"
  12. #include "concept/promotion_policy.hpp"
  13. #include "safe_common.hpp"
  14. #include "exception_policies.hpp"
  15. #include "boost/concept/assert.hpp"
  16. namespace boost {
  17. namespace safe_numerics {
  18. /////////////////////////////////////////////////////////////////
  19. // forward declarations to support friend function declarations
  20. // in safe_base
  21. template<
  22. class Stored,
  23. Stored Min,
  24. Stored Max,
  25. class P, // promotion polic
  26. class E // exception policy
  27. >
  28. class safe_base;
  29. template<
  30. class T,
  31. T Min,
  32. T Max,
  33. class P,
  34. class E
  35. >
  36. struct is_safe<safe_base<T, Min, Max, P, E> > : public std::true_type
  37. {};
  38. template<
  39. class T,
  40. T Min,
  41. T Max,
  42. class P,
  43. class E
  44. >
  45. struct get_promotion_policy<safe_base<T, Min, Max, P, E> > {
  46. using type = P;
  47. };
  48. template<
  49. class T,
  50. T Min,
  51. T Max,
  52. class P,
  53. class E
  54. >
  55. struct get_exception_policy<safe_base<T, Min, Max, P, E> > {
  56. using type = E;
  57. };
  58. template<
  59. class T,
  60. T Min,
  61. T Max,
  62. class P,
  63. class E
  64. >
  65. struct base_type<safe_base<T, Min, Max, P, E> > {
  66. using type = T;
  67. };
  68. template<
  69. class T,
  70. T Min,
  71. T Max,
  72. class P,
  73. class E
  74. >
  75. constexpr T base_value(
  76. const safe_base<T, Min, Max, P, E> & st
  77. ) {
  78. return static_cast<T>(st);
  79. }
  80. template<
  81. typename T,
  82. T N,
  83. class P, // promotion policy
  84. class E // exception policy
  85. >
  86. class safe_literal_impl;
  87. // works for both GCC and clang
  88. #if BOOST_CLANG==1
  89. #pragma GCC diagnostic push
  90. #pragma GCC diagnostic ignored "-Wmismatched-tags"
  91. #endif
  92. /////////////////////////////////////////////////////////////////
  93. // Main implementation
  94. // note in the current version of the library, it is a requirement than type
  95. // type "Stored" be a scalar type. Problem is when violates this rule, the error message (on clang)
  96. // is "A non-type template parameter cannot have type ... " where ... is some non-scalar type
  97. // The example which triggered this investigation is safe<safe<int>> . It was difficult to make
  98. // the trip from the error message to the source of the problem, hence we're including this
  99. // comment here.
  100. template<
  101. class Stored,
  102. Stored Min,
  103. Stored Max,
  104. class P, // promotion polic
  105. class E // exception policy
  106. >
  107. class safe_base {
  108. private:
  109. BOOST_CONCEPT_ASSERT((PromotionPolicy<P>));
  110. BOOST_CONCEPT_ASSERT((ExceptionPolicy<E>));
  111. Stored m_t;
  112. template<class T>
  113. constexpr Stored validated_cast(const T & t) const;
  114. // stream support
  115. template<class CharT, class Traits>
  116. void output(std::basic_ostream<CharT, Traits> & os) const;
  117. // note usage of friend declaration to mark function as
  118. // a global function rather than a member function. If
  119. // this is not done, the compiler will confuse this with
  120. // a member operator overload on the << operator. Weird
  121. // I know. But it's documented here
  122. // http://en.cppreference.com/w/cpp/language/friend
  123. // under the heading "Template friend operators"
  124. template<class CharT, class Traits>
  125. friend std::basic_ostream<CharT, Traits> &
  126. operator<<(
  127. std::basic_ostream<CharT, Traits> & os,
  128. const safe_base & t
  129. ){
  130. t.output(os);
  131. return os;
  132. }
  133. template<class CharT, class Traits>
  134. void input(std::basic_istream<CharT, Traits> & is);
  135. // see above
  136. template<class CharT, class Traits>
  137. friend inline std::basic_istream<CharT, Traits> &
  138. operator>>(
  139. std::basic_istream<CharT, Traits> & is,
  140. safe_base & t
  141. ){
  142. t.input(is);
  143. return is;
  144. }
  145. public:
  146. ////////////////////////////////////////////////////////////
  147. // constructors
  148. constexpr safe_base();
  149. struct skip_validation{};
  150. constexpr explicit safe_base(const Stored & rhs, skip_validation);
  151. // construct an instance of a safe type from an instance of a convertible underlying type.
  152. template<
  153. class T,
  154. typename std::enable_if<
  155. std::is_convertible<T, Stored>::value,
  156. bool
  157. >::type = 0
  158. >
  159. constexpr /*explicit*/ safe_base(const T & t);
  160. // construct an instance of a safe type from a literal value
  161. template<typename T, T N, class Px, class Ex>
  162. constexpr /*explicit*/ safe_base(const safe_literal_impl<T, N, Px, Ex> & t);
  163. // note: Rule of Five. Supply all or none of the following
  164. // a) user-defined destructor
  165. ~safe_base() = default;
  166. // b) copy-constructor
  167. constexpr safe_base(const safe_base &) = default;
  168. // c) copy-assignment
  169. constexpr safe_base & operator=(const safe_base &) = default;
  170. // d) move constructor
  171. constexpr safe_base(safe_base &&) = default;
  172. // e) move assignment operator
  173. constexpr safe_base & operator=(safe_base &&) = default;
  174. /////////////////////////////////////////////////////////////////
  175. // casting operators for intrinsic integers
  176. // convert to any type which is not safe. safe types need to be
  177. // excluded to prevent ambiguous function selection which
  178. // would otherwise occur. validity of safe types is checked in
  179. // the constructor of safe types
  180. template<
  181. class R,
  182. typename std::enable_if<
  183. ! boost::safe_numerics::is_safe<R>::value,
  184. int
  185. >::type = 0
  186. >
  187. constexpr /*explicit*/ operator R () const;
  188. /////////////////////////////////////////////////////////////////
  189. // modification binary operators
  190. template<class T>
  191. constexpr safe_base &
  192. operator=(const T & rhs){
  193. m_t = validated_cast(rhs);
  194. return *this;
  195. }
  196. // mutating unary operators
  197. constexpr safe_base & operator++(){ // pre increment
  198. return *this = *this + 1;
  199. }
  200. constexpr safe_base & operator--(){ // pre decrement
  201. return *this = *this - 1;
  202. }
  203. constexpr safe_base operator++(int){ // post increment
  204. safe_base old_t = *this;
  205. ++(*this);
  206. return old_t;
  207. }
  208. constexpr safe_base operator--(int){ // post decrement
  209. safe_base old_t = *this;
  210. --(*this);
  211. return old_t;
  212. }
  213. // non mutating unary operators
  214. constexpr auto operator+() const { // unary plus
  215. return *this;
  216. }
  217. // after much consideration, I've permited the resulting value of a unary
  218. // - to change the type. The C++ standard does invoke integral promotions
  219. // so it's changing the type as well.
  220. /* section 5.3.1 &8 of the C++ standard
  221. The operand of the unary - operator shall have arithmetic or unscoped
  222. enumeration type and the result is the negation of its operand. Integral
  223. promotion is performed on integral or enumeration operands. The negative
  224. of an unsigned quantity is computed by subtracting its value from 2n,
  225. where n is the number of bits in the promoted operand. The type of the
  226. result is the type of the promoted operand.
  227. */
  228. constexpr auto operator-() const { // unary minus
  229. // if this is a unsigned type and the promotion policy is native
  230. // the result will be unsigned. But then the operation will fail
  231. // according to the requirements of arithmetic correctness.
  232. // if this is an unsigned type and the promotion policy is automatic.
  233. // the result will be signed.
  234. return 0 - *this;
  235. }
  236. /* section 5.3.1 &10 of the C++ standard
  237. The operand of ~ shall have integral or unscoped enumeration type;
  238. the result is the ones’ complement of its operand. Integral promotions
  239. are performed. The type of the result is the type of the promoted operand.
  240. */
  241. constexpr auto operator~() const { // complement
  242. return ~Stored(0u) ^ *this;
  243. }
  244. };
  245. } // safe_numerics
  246. } // boost
  247. /////////////////////////////////////////////////////////////////
  248. // numeric limits for safe<int> etc.
  249. #include <limits>
  250. namespace std {
  251. template<
  252. class T,
  253. T Min,
  254. T Max,
  255. class P,
  256. class E
  257. >
  258. class numeric_limits<boost::safe_numerics::safe_base<T, Min, Max, P, E> >
  259. : public std::numeric_limits<T>
  260. {
  261. using SB = boost::safe_numerics::safe_base<T, Min, Max, P, E>;
  262. public:
  263. constexpr static SB lowest() noexcept {
  264. return SB(Min, typename SB::skip_validation());
  265. }
  266. constexpr static SB min() noexcept {
  267. return SB(Min, typename SB::skip_validation());
  268. }
  269. constexpr static SB max() noexcept {
  270. return SB(Max, typename SB::skip_validation());
  271. }
  272. };
  273. } // std
  274. #if BOOST_CLANG==1
  275. #pragma GCC diagnostic pop
  276. #endif
  277. #endif // BOOST_NUMERIC_SAFE_BASE_HPP