Repo created
This commit is contained in:
parent
4af19165ec
commit
68073add76
12458 changed files with 12350765 additions and 2 deletions
43
libs/base/checked_cast.hpp
Normal file
43
libs/base/checked_cast.hpp
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
#pragma once
|
||||
|
||||
#include "base/assert.hpp"
|
||||
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
|
||||
namespace base
|
||||
{
|
||||
template <typename ReturnType, typename ParameterType>
|
||||
ReturnType checked_cast(ParameterType v)
|
||||
{
|
||||
static_assert(std::is_integral_v<ParameterType>, "ParameterType should be integral");
|
||||
static_assert(std::is_integral_v<ReturnType>, "ReturnType should be integral");
|
||||
|
||||
auto const result = static_cast<ReturnType>(v);
|
||||
CHECK_EQUAL(static_cast<ParameterType>(result), v, ());
|
||||
CHECK((result > 0) == (v > 0), ("checked_cast failed, value =", v, ", result =", result));
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename ReturnType, typename ParameterType>
|
||||
ReturnType asserted_cast(ParameterType v)
|
||||
{
|
||||
static_assert(std::is_integral_v<ParameterType>, "ParameterType should be integral");
|
||||
static_assert(std::is_integral_v<ReturnType>, "ReturnType should be integral");
|
||||
|
||||
auto const result = static_cast<ReturnType>(v);
|
||||
ASSERT_EQUAL(static_cast<ParameterType>(result), v, ());
|
||||
ASSERT((result > 0) == (v > 0), ("asserted_cast failed, value =", v, ", result =", result));
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename ResultType, typename ParameterType>
|
||||
bool IsCastValid(ParameterType v)
|
||||
{
|
||||
static_assert(std::is_integral_v<ParameterType>, "ParameterType should be integral");
|
||||
static_assert(std::is_integral_v<ResultType>, "ReturnType should be integral");
|
||||
|
||||
auto const result = static_cast<ResultType>(v);
|
||||
return static_cast<ParameterType>(result) == v && ((result > 0) == (v > 0));
|
||||
}
|
||||
} // namespace base
|
||||
Loading…
Add table
Add a link
Reference in a new issue