11#ifndef _TPF_SAFE_TYPE_HPP
12#define _TPF_SAFE_TYPE_HPP
19 #if _MSVC_LANG < 201703L
20 #error This libary requires C++17 Standard (Visual Studio 2017).
24 #if __cplusplus < 201703
25 #error This library requires C++17 Standard (GNU g++ version 8.0 or clang++ version 8.0 above)
54 std::numeric_limits<Type>::max();
62 std::numeric_limits<Type>::min();
73 template<
typename T,
typename = types::enable_if_
integral_t<T>>
76 template<
typename T,
typename = types::enable_if_
integral_t<T>>
79 template<
typename S,
typename T>
82 template<
typename S,
typename T,
87 template<
typename S,
typename T,
118 template<
typename Type>
126 for (; mask; mask >>= 1)
127 count += (
int)(mask & 1);
139 template<
typename Type>
145 int count = 0; std::string str;
147 for (
int pos = sizeof_bits<Type>; pos; mask <<= 1, --pos)
149 if (!str.empty() && (pos % 4 == 0))
152 str.push_back((mask & high_bit_mask<Type>) ?
'1' :
'0');
165 template<
typename Type>
171 if constexpr (types::is_char_v<Type>)
173 std::ostringstream os;
175 os << std::setfill(
'0') << std::setw(
sizeof(Type) * 2)
176 << std::uppercase << std::hex << (short)v;
191 ss.push_back(str[2]);
192 ss.push_back(str[3]);
200 std::ostringstream os;
202 os << std::setfill(
'0') << std::setw(
sizeof(Type) * 2)
203 << std::uppercase << std::hex <<v;
218 template<
typename Type>
222 std::ostringstream os;
224 if constexpr (types::is_char_v<Type>)
239 template<
typename Type>
243 std::ostringstream os;
245 if constexpr (types::is_char_v<Type>)
250 return (
int) os.str().size();
259 template<
typename Type>
263 int a =
field_width(std::numeric_limits<Type>::max());
264 int b =
field_width(std::numeric_limits<Type>::min());
266 return a > b ? a : b;
276 template<
typename Type>
280 std::ostringstream os;
282 int max_field = numeric_width<Type>();
284 if constexpr (types::is_char_v<Type>)
285 os << std::setw(max_field)<<(short)v;
287 os << std::setw(max_field)<<v;
299 template<
typename Type>
303 std::ostringstream os;
305 os <<
to_dec_width(v) <<
" (" << to_hex<Type>(v) <<
"): " << to_bits<Type>(v);
316 template<
typename Type>
320 std::ostringstream os;
322 constexpr auto minimum = std::numeric_limits<Type>::min();
323 constexpr auto maximum = std::numeric_limits<Type>::max();
326 <<
",\tByte size: " <<
sizeof(Type) <<
",\tBit count: " << sizeof_bits<Type>
337 template<
typename =
void>
361 template<
typename Type>
365 std::cout << numeric_type_info<Type>((Type)0) <<
"\n";
385 template<
typename Type>
395 if(limits_min<Type> - b <= a)
399 std::ostringstream os;
400 os <<
"overflow: a = " << a
406 else if(a > 0 && b > 0)
413 if( limits_max<Type> - b >= a)
417 std::ostringstream os;
418 os <<
"overflow: a = " << a
437 template<
typename Type>
453 template<
typename Type>
461 if(a <= limits_max<Type> - b)
465 std::ostringstream os;
466 os <<
"overflow: a = " << a
482 template<
typename Type>
494 std::ostringstream os;
495 os <<
"overflow: a = " << a
510 template<
typename Type>
516 if( (b == 0) || (rlt / b == a))
520 std::ostringstream os;
521 os <<
"overflow: a = " << a
536 template<
typename Type>
544 std::ostringstream os;
545 os <<
"division by zero - a = " << a
552 template<
typename Type>
559 std::ostringstream os;
560 os <<
"floating-point operation failed - a = "
561 << a <<
", b = " << b;
569 template<
typename Type>
576 std::ostringstream os;
577 os <<
"floating-point operation failed - a = "
578 << a <<
", b = " << b;
586 template<
typename Type>
593 std::ostringstream os;
594 os <<
"floating-point operation failed - a = "
595 << a <<
", b = " << b;
603 template<
typename Type>
610 std::ostringstream os;
612 os <<
"floating-point operation failed - a = "
613 << a <<
", b = " << b;
signed_integral_t< common_t< S, T > > signed_common_t
types::make_signed_integral_t< T > signed_integral_t
unsigned_integral_t< common_t< S, T > > unsigned_common_t
types::make_unsigned_integral_t< T > unsigned_integral_t
types::common_type_t< S, T > common_t
types::enable_if_signed_integral_t< Type > safe_add(Type a, Type b)
Safe addition of the signed integral types.
types::enable_if_integral_t< Type, int > numeric_width()
Return's maximum numeric field width.
constexpr Type limits_max
Upperbound number the Type can represent.
types::enable_if_integral_t< Type > safe_mul(Type a, Type b)
Safe multiplication of integral types.
types::enable_if_integral_t< Type, std::string > to_bits(Type bits)
Returns binary bit pattern.
types::enable_if_integral_t< Type, void > twos_complement(Type c)
Displays Twos complement.
types::enable_if_integral_t< Type, std::string > numeric_base(Type v)
Displays how integral types are represented.
types::enable_if_integral_t< Type > safe_div(Type a, Type b)
Safe division of integral types.
void integral_type_info()
Displays all integral types' representation.
types::enable_if_integral_t< Type, std::string > to_hex(Type v)
Returns hexidecimal representation.
constexpr auto high_bit_mask
Return's bit mask for the highest bit.
types::enable_if_integral_t< Type, int > field_width(Type v)
Returns' field width.
types::enable_if_integral_t< Type, int > count_set_bits(Type bits)
Counts set bits.
types::enable_if_integral_t< Type, std::string > to_dec(Type v)
Returns decimal representation.
types::enable_if_integral_t< Type, std::string > to_dec_width(Type v)
Returns decimal field width.
types::enable_if_integral_t< Type, std::string > numeric_type_info(Type)
Displays integral type's numerical information.
constexpr Type limits_min
Lowerbound number the Type can represent.
constexpr int sizeof_bits
Returns bit count of type T.
types::enable_if_signed_integral_t< Type > safe_sub(Type a, Type b)
Safe subtraction of signed integral types.
hidden::make_signed_integral_t< Type > make_signed_integral_t
hidden::enable_if_integral_t< Type, ReturnType > enable_if_integral_t
hidden::enable_if_signed_integral_t< Type, ReturnType > enable_if_signed_integral_t
hidden::common_type_t< Types... > common_type_t
hidden::enable_if_unsigned_integral_t< Type, ReturnType > enable_if_unsigned_integral_t
hidden::enable_if_real_number_t< Type, ReturnType > enable_if_real_number_t
hidden::make_unsigned_integral_t< Type > make_unsigned_integral_t
Includes subnamespace conversion.
auto minimum(Type_1 a, Type_2 b)
auto maximum(Type_1 a, Type_2 b)
Type functions are implemented.
#define Tpf_ThrowDebugException(debug_message)
Throw a debug_exception with message as argument.
#define Tpf_GetTypeName(type_arg)
A macro that returns type_arg's string name.