Skip to content

Commit

Permalink
Add static assertions to get_integral_result
Browse files Browse the repository at this point in the history
  • Loading branch information
pdimov committed Nov 16, 2024
1 parent 59fb676 commit 8b3d1a8
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions include/boost/hash2/get_integral_result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/hash2/digest.hpp>
#include <boost/hash2/detail/read.hpp>
#include <array>
#include <type_traits>
#include <cstddef>

Expand All @@ -20,29 +18,39 @@ template<class T, class R>
typename std::enable_if<std::is_integral<R>::value && (sizeof(R) >= sizeof(T)), T>::type
get_integral_result( R const & r )
{
static_assert( std::is_integral<T>::value, "T must be integral" );
static_assert( !std::is_same<typename std::remove_cv<T>::type, bool>::value, "T must not be bool" );

static_assert( std::is_unsigned<R>::value, "R must be unsigned" );

typedef typename std::make_unsigned<T>::type U;

return static_cast<T>( static_cast<U>( r ) );
}

template<class T, class R>
typename std::enable_if<std::is_integral<R>::value && sizeof(R) == 4 && sizeof(T) == 8, T>::type
get_integral_result( R const & r )
{
static_assert( std::is_integral<T>::value, "T must be integral" );
static_assert( !std::is_same<typename std::remove_cv<T>::type, bool>::value, "T must not be bool" );

static_assert( std::is_unsigned<R>::value, "R must be unsigned" );

typedef typename std::make_unsigned<T>::type U;

return static_cast<T>( ( static_cast<U>( r ) << 32 ) + r );
}

template<class T, std::size_t N>
T get_integral_result( std::array<unsigned char, N> const & r )
template<class T, class R>
typename std::enable_if< !std::is_integral<R>::value, T >::type
get_integral_result( R const & r )
{
static_assert( N >= 8, "Array result type is too short" );
return static_cast<T>( detail::read64le( r.data() ) );
}
static_assert( std::is_integral<T>::value, "T must be integral" );
static_assert( !std::is_same<typename std::remove_cv<T>::type, bool>::value, "T must not be bool" );

static_assert( R().size() >= 8, "Array-like result type is too short" );

template<class T, std::size_t N>
T get_integral_result( digest<N> const & r )
{
static_assert( N >= 8, "Digest result type is too short" );
return static_cast<T>( detail::read64le( r.data() ) );
}

Expand Down

0 comments on commit 8b3d1a8

Please sign in to comment.