6 #ifndef DISMEC_SRC_UTILS_CONVERSION_H
7 #define DISMEC_SRC_UTILS_CONVERSION_H
15 static_assert(std::is_integral_v<T>,
"Can only convert between integral types");
18 if constexpr(std::is_signed_v<T>) {
19 if (value < std::numeric_limits<long>::max()) {
20 return static_cast<long>(value);
23 if (value <
static_cast<unsigned long>(std::numeric_limits<long>::max())) {
24 return static_cast<long>(value);
27 THROW_EXCEPTION(std::range_error,
"Value {} cannot be represented as long.", value);
33 return static_cast<std::ptrdiff_t
>(
sizeof(T));
38 constexpr std::ptrdiff_t
ssizeof = calc_ssizeof<T>();
42 constexpr
auto ssize(
const C& c) -> std::common_type_t<std::ptrdiff_t, std::make_signed_t<decltype(c.size())>> {
43 using R = std::common_type_t<std::ptrdiff_t, std::make_signed_t<decltype(c.size())>>;
44 return static_cast<R
>(c.size());
Main namespace in which all types, classes, and functions are defined.
constexpr auto ssize(const C &c) -> std::common_type_t< std::ptrdiff_t, std::make_signed_t< decltype(c.size())>>
signed size free function. Taken from https://en.cppreference.com/w/cpp/iterator/size
constexpr long to_long(T value)
Convert the given value to long, throwing an error if the conversion is not possible.
constexpr std::ptrdiff_t ssizeof
Signed size of type T
constexpr std::ptrdiff_t calc_ssizeof()
Gets the sizeof of a type as a signed integer.
#define THROW_EXCEPTION(exception_type,...)