safe_base.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. template<
  95. class Stored,
  96. Stored Min,
  97. Stored Max,
  98. class P, // promotion polic
  99. class E // exception policy
  100. >
  101. class safe_base {
  102. private:
  103. BOOST_CONCEPT_ASSERT((PromotionPolicy<P>));
  104. BOOST_CONCEPT_ASSERT((ExceptionPolicy<E>));
  105. Stored m_t;
  106. template<
  107. class StoredX,
  108. StoredX MinX,
  109. StoredX MaxX,
  110. class PX, // promotion polic
  111. class EX // exception policy
  112. >
  113. friend class safe_base;
  114. friend class std::numeric_limits<safe_base>;
  115. template<class T>
  116. constexpr Stored validated_cast(const T & t) const;
  117. // stream support
  118. template<class CharT, class Traits>
  119. void output(std::basic_ostream<CharT, Traits> & os) const;
  120. // note usage of friend declaration to mark function as
  121. // a global function rather than a member function. If
  122. // this is not done, the compiler will confuse this with
  123. // a member operator overload on the << operator. Weird
  124. // I know. But it's documented here
  125. // http://en.cppreference.com/w/cpp/language/friend
  126. // under the heading "Template friend operators"
  127. template<class CharT, class Traits>
  128. friend std::basic_ostream<CharT, Traits> &
  129. operator<<(
  130. std::basic_ostream<CharT, Traits> & os,
  131. const safe_base & t
  132. ){
  133. t.output(os);
  134. return os;
  135. }
  136. template<class CharT, class Traits>
  137. void input(std::basic_istream<CharT, Traits> & is);
  138. // see above
  139. template<class CharT, class Traits>
  140. friend inline std::basic_istream<CharT, Traits> &
  141. operator>>(
  142. std::basic_istream<CharT, Traits> & is,
  143. safe_base & t
  144. ){
  145. t.input(is);
  146. return is;
  147. }
  148. public:
  149. ////////////////////////////////////////////////////////////
  150. // constructors
  151. constexpr safe_base();
  152. struct skip_validation{};
  153. constexpr explicit safe_base(const Stored & rhs, skip_validation);
  154. // construct an instance of a safe type from an instance of a convertible underlying type.
  155. template<
  156. class T,
  157. typename std::enable_if<
  158. std::is_convertible<T, Stored>::value,
  159. bool
  160. >::type = 0
  161. >
  162. constexpr /*explicit*/ safe_base(const T & t);
  163. // construct an instance of a safe type from a literal value
  164. template<typename T, T N, class Px, class Ex>
  165. constexpr /*explicit*/ safe_base(const safe_literal_impl<T, N, Px, Ex> & t);
  166. // note: Rule of Five. Supply all or none of the following
  167. // a) user-defined destructor
  168. ~safe_base() = default;
  169. // b) copy-constructor
  170. constexpr safe_base(const safe_base &) = default;
  171. // c) copy-assignment
  172. constexpr safe_base & operator=(const safe_base &) = default;
  173. // d) move constructor
  174. constexpr safe_base(safe_base &&) = default;
  175. // e) move assignment operator
  176. constexpr safe_base & operator=(safe_base &&) = default;
  177. /////////////////////////////////////////////////////////////////
  178. // casting operators for intrinsic integers
  179. // convert to any type which is not safe. safe types need to be
  180. // excluded to prevent ambiguous function selection which
  181. // would otherwise occur. validity of safe types is checked in
  182. // the constructor of safe types
  183. template<
  184. class R,
  185. typename std::enable_if<
  186. ! boost::safe_numerics::is_safe<R>::value,
  187. int
  188. >::type = 0
  189. >
  190. constexpr /*explicit*/ operator R () const;
  191. /////////////////////////////////////////////////////////////////
  192. // modification binary operators
  193. template<class T>
  194. constexpr safe_base &
  195. operator=(const T & rhs){
  196. m_t = validated_cast(rhs);
  197. return *this;
  198. }
  199. // mutating unary operators
  200. safe_base & operator++(){ // pre increment
  201. return *this = *this + 1;
  202. }
  203. safe_base & operator--(){ // pre decrement
  204. return *this = *this - 1;
  205. }
  206. safe_base operator++(int){ // post increment
  207. safe_base old_t = *this;
  208. ++(*this);
  209. return old_t;
  210. }
  211. safe_base operator--(int){ // post decrement
  212. safe_base old_t = *this;
  213. --(*this);
  214. return old_t;
  215. }
  216. // non mutating unary operators
  217. constexpr auto operator+() const { // unary plus
  218. return *this;
  219. }
  220. // after much consideration, I've permited the resulting value of a unary
  221. // - to change the type. The C++ standard does invoke integral promotions
  222. // so it's changing the type as well.
  223. /* section 5.3.1 &8 of the C++ standard
  224. The operand of the unary - operator shall have arithmetic or unscoped
  225. enumeration type and the result is the negation of its operand. Integral
  226. promotion is performed on integral or enumeration operands. The negative
  227. of an unsigned quantity is computed by subtracting its value from 2n,
  228. where n is the number of bits in the promoted operand. The type of the
  229. result is the type of the promoted operand.
  230. */
  231. constexpr auto operator-() const { // unary minus
  232. // if this is a unsigned type and the promotion policy is native
  233. // the result will be unsigned. But then the operation will fail
  234. // according to the requirements of arithmetic correctness.
  235. // if this is an unsigned type and the promotion policy is automatic.
  236. // the result will be signed.
  237. return 0 - *this;
  238. }
  239. /* section 5.3.1 &10 of the C++ standard
  240. The operand of ~ shall have integral or unscoped enumeration type;
  241. the result is the ones’ complement of its operand. Integral promotions
  242. are performed. The type of the result is the type of the promoted operand.
  243. */
  244. constexpr auto operator~() const { // complement
  245. return ~Stored(0u) ^ *this;
  246. }
  247. };
  248. } // safe_numerics
  249. } // boost
  250. /////////////////////////////////////////////////////////////////
  251. // numeric limits for safe<int> etc.
  252. #include <limits>
  253. namespace std {
  254. template<
  255. class T,
  256. T Min,
  257. T Max,
  258. class P,
  259. class E
  260. >
  261. class numeric_limits<boost::safe_numerics::safe_base<T, Min, Max, P, E> >
  262. : public std::numeric_limits<T>
  263. {
  264. using SB = boost::safe_numerics::safe_base<T, Min, Max, P, E>;
  265. public:
  266. constexpr static SB lowest() noexcept {
  267. return SB(Min, typename SB::skip_validation());
  268. }
  269. constexpr static SB min() noexcept {
  270. return SB(Min, typename SB::skip_validation());
  271. }
  272. constexpr static SB max() noexcept {
  273. return SB(Max, typename SB::skip_validation());
  274. }
  275. };
  276. } // std
  277. #if BOOST_CLANG==1
  278. #pragma GCC diagnostic pop
  279. #endif
  280. #endif // BOOST_NUMERIC_SAFE_BASE_HPP