#ifndef BOOST_NUMERIC_SAFE_INTEGER_LITERAL_HPP #define BOOST_NUMERIC_SAFE_INTEGER_LITERAL_HPP // Copyright (c) 2012 Robert Ramey // // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) #include // for intmax_t/uintmax_t #include #include // conditional, enable_if #include #include "utility.hpp" #include "safe_integer.hpp" #include "checked_integer.hpp" namespace boost { namespace safe_numerics { template class safe_literal_impl; template struct is_safe > : public std::true_type {}; template struct get_promotion_policy > { using type = P; }; template struct get_exception_policy > { using type = E; }; template struct base_type > { using type = T; }; template constexpr T base_value( const safe_literal_impl & ) { return N; } template inline std::basic_ostream & operator<<( std::basic_ostream & os, const safe_literal_impl & ){ return os << ( (std::is_same::value || std::is_same::value ) ? static_cast(N) : N ); } template class safe_literal_impl { template< class CharT, class Traits > friend std::basic_ostream & operator<<( std::basic_ostream & os, const safe_literal_impl & ){ return os << ( (::std::is_same::value || ::std::is_same::value || ::std::is_same::value ) ? static_cast(N) : N ); }; public: //////////////////////////////////////////////////////////// // constructors // default constructor constexpr safe_literal_impl(){} ///////////////////////////////////////////////////////////////// // casting operators for intrinsic integers // convert to any type which is not safe. safe types need to be // excluded to prevent ambiguous function selection which // would otherwise occur template< class R, typename std::enable_if< ! boost::safe_numerics::is_safe::value, int >::type = 0 > constexpr operator R () const { // if static values don't overlap, the program can never function #if 1 constexpr const interval r_interval; static_assert( ! r_interval.excludes(N), "safe type cannot be constructed with this type" ); #endif return validate_detail< R, std::numeric_limits::min(), std::numeric_limits::max(), E >::return_value(*this); } // non mutating unary operators constexpr safe_literal_impl operator+() const { // unary plus return safe_literal_impl(); } // after much consideration, I've permitted the resulting value of a unary // - to change the type in accordance with the promotion policy. // The C++ standard does invoke integral promotions so it's changing the type as well. /* section 5.3.1 &8 of the C++ standard The operand of the unary - operator shall have arithmetic or unscoped enumeration type and the result is the negation of its operand. Integral promotion is performed on integral or enumeration operands. The negative of an unsigned quantity is computed by subtracting its value from 2n, where n is the number of bits in the promoted operand. The type of the result is the type of the promoted operand. */ template< typename Tx, Tx Nx, typename = std::enable_if_t > constexpr auto minus_helper() const { return safe_literal_impl(); } constexpr auto operator-() const { // unary minus return minus_helper(); } /* section 5.3.1 &10 of the C++ standard The operand of ~ shall have integral or unscoped enumeration type; the result is the ones’ complement of its operand. Integral promotions are performed. The type of the result is the type of the promoted operand. constexpr safe_literal_impl operator~() const { // invert bits return safe_literal_impl(); } */ template< typename Tx, Tx Nx, typename = std::enable_if_t > constexpr auto not_helper() const { return safe_literal_impl(); } constexpr auto operator~() const { // unary minus return not_helper(); } }; template< std::intmax_t N, class P = void, class E = void > using safe_signed_literal = safe_literal_impl< typename utility::signed_stored_type, N, P, E >; template< std::uintmax_t N, class P = void, class E = void > using safe_unsigned_literal = safe_literal_impl< typename utility::unsigned_stored_type, N, P, E >; template< class T, T N, class P = void, class E = void, typename std::enable_if< std::is_signed::value, int >::type = 0 > constexpr auto make_safe_literal_impl() { return boost::safe_numerics::safe_signed_literal(); } template< class T, T N, class P = void, class E = void, typename std::enable_if< ! std::is_signed::value, int >::type = 0 > constexpr auto inline make_safe_literal_impl() { return boost::safe_numerics::safe_unsigned_literal(); } } // safe_numerics } // boost #define make_safe_literal(n, P, E) \ boost::safe_numerics::make_safe_literal_impl() ///////////////////////////////////////////////////////////////// // numeric limits for safe_literal etc. #include namespace std { template< typename T, T N, class P, class E > class numeric_limits > : public std::numeric_limits { using SL = boost::safe_numerics::safe_literal_impl; public: constexpr static SL lowest() noexcept { return SL(); } constexpr static SL min() noexcept { return SL(); } constexpr static SL max() noexcept { return SL(); } }; } // std #endif // BOOST_NUMERIC_SAFE_INTEGER_LITERAL_HPP