safe_uint_truncate.hpp
Go to the documentation of this file.
/*
* restinio
*/
#pragma once
#include <stdexcept>
#include <type_traits>
#include <limits>
#include <cstddef>
#include <cstdint>
namespace restinio {
namespace utils {
namespace impl {
template<bool Is_Uint64_Longer>
struct safe_uint64_to_size_t {};
template<>
struct safe_uint64_to_size_t<true> {
static std::size_t
truncate(std::uint64_t v)
{
if( v > static_cast<std::uint64_t>(std::numeric_limits<std::size_t>::max()) )
throw std::runtime_error( "64-bit value can't be safely truncated "
"into std::size_t type" );
return static_cast<std::size_t>(v);
}
};
template<>
struct safe_uint64_to_size_t<false> {
static std::size_t
truncate(std::uint64_t v) { return static_cast<std::size_t>(v); }
};
inline std::size_t
uint64_to_size_t(std::uint64_t v)
{
return safe_uint64_to_size_t<(sizeof(std::uint64_t) > sizeof(std::size_t))>::truncate(v);
}
} /* namespace impl */
} /* namespace utils */
} /* namespace restinio */
restinio::utils::impl::uint64_to_size_t
std::size_t uint64_to_size_t(std::uint64_t v)
Helper function for truncating uint64 to std::size_t with exception if that truncation will lead to d...
Definition: safe_uint_truncate.hpp:59
restinio
Definition: asio_include.hpp:21
restinio::utils::impl::safe_uint64_to_size_t< false >::truncate
static std::size_t truncate(std::uint64_t v)
Definition: safe_uint_truncate.hpp:42
restinio::utils::impl::safe_uint64_to_size_t< true >::truncate
static std::size_t truncate(std::uint64_t v)
Definition: safe_uint_truncate.hpp:30
restinio::utils::impl::safe_uint64_to_size_t
Definition: safe_uint_truncate.hpp:25