OpenVPN 3 Core Library
Loading...
Searching...
No Matches
openvpn::numeric_util Namespace Reference

Functions

template<typename OutT , typename InT >
OutT clamp_to_typerange (InT inVal)
 Clamps the input value to the legal range for the output type.
 
template<typename OutT , typename InT >
OutT clamp_to_default (InT inVal, OutT defVal)
 Adjusts the input value to the default if the input value exceeds the range of the output type.
 
template<typename OutT , typename InT , typename FuncT >
OutT clamp_notify (InT inVal, FuncT cb)
 Calls FuncT cb if the input value exceeds the range of the output type.
 
template<typename OutT , typename InT >
OutT numeric_cast (InT inVal)
 Tests attempted casts to ensure the input value does not exceed the capacity of the output type.
 
template<typename OutT , typename InT >
constexpr bool is_int_conversion ()
 
template<typename OutT , typename InT >
constexpr bool is_int_u2s ()
 
template<typename OutT , typename InT >
constexpr bool is_int_s2u ()
 
template<typename OutT , typename InT >
constexpr bool is_int_rangesafe ()
 
template<typename OutT , typename InT >
bool is_safe_conversion (InT inVal)
 Returns true if the given value can be contained by the out type.
 

Function Documentation

◆ clamp_notify()

template<typename OutT , typename InT , typename FuncT >
OutT openvpn::numeric_util::clamp_notify ( InT  inVal,
FuncT  cb 
)

Calls FuncT cb if the input value exceeds the range of the output type.

Template Parameters
OutTOutput type
InTInput type
FuncTInvokable type that accepts InT and returns OutT
Parameters
inValInput value
cbNotification callback
Returns
Result of the FuncT callback

Definition at line 104 of file clamp_typerange.hpp.

◆ clamp_to_default()

template<typename OutT , typename InT >
OutT openvpn::numeric_util::clamp_to_default ( InT  inVal,
OutT  defVal 
)

Adjusts the input value to the default if the input value exceeds the range of the output type.

Template Parameters
OutTOutput type
InTInput type
Parameters
inValInput value
defValInput value
Returns
OutT safely converted from InT, potentially adjusted

Definition at line 77 of file clamp_typerange.hpp.

Here is the caller graph for this function:

◆ clamp_to_typerange()

template<typename OutT , typename InT >
OutT openvpn::numeric_util::clamp_to_typerange ( InT  inVal)

Clamps the input value to the legal range for the output type.

Template Parameters
OutTOutput type
InTInput type
Parameters
inValInput value
Returns
OutT safely converted from InT, range limited to fit.

Definition at line 37 of file clamp_typerange.hpp.

Here is the caller graph for this function:

◆ is_int_conversion()

template<typename OutT , typename InT >
constexpr bool openvpn::numeric_util::is_int_conversion ( )
constexpr

Definition at line 26 of file numeric_util.hpp.

◆ is_int_rangesafe()

template<typename OutT , typename InT >
constexpr bool openvpn::numeric_util::is_int_rangesafe ( )
constexpr

Definition at line 47 of file numeric_util.hpp.

◆ is_int_s2u()

template<typename OutT , typename InT >
constexpr bool openvpn::numeric_util::is_int_s2u ( )
constexpr

Definition at line 40 of file numeric_util.hpp.

◆ is_int_u2s()

template<typename OutT , typename InT >
constexpr bool openvpn::numeric_util::is_int_u2s ( )
constexpr

Definition at line 33 of file numeric_util.hpp.

◆ is_safe_conversion()

template<typename OutT , typename InT >
bool openvpn::numeric_util::is_safe_conversion ( InT  inVal)

Returns true if the given value can be contained by the out type.

Template Parameters
OutTOutput type
InTInput type
Parameters
inValInput value
Returns
true | false

Definition at line 69 of file numeric_util.hpp.

Here is the caller graph for this function:

◆ numeric_cast()

template<typename OutT , typename InT >
OutT openvpn::numeric_util::numeric_cast ( InT  inVal)

Tests attempted casts to ensure the input value does not exceed the capacity of the output type.

If the types are the same, or the range of the output type equals or exceeds the range of the input type we just cast and return the value which should ideally optimize away completely. Otherwise we do appropriate range checks and if those succeed we cast, otherwise the failure exception openvpn::numeric_out_of_range is thrown.

Example:

int64_t s64 = std::numeric_limits<int64_t>::max();
EXPECT_THROW(numeric_cast<int16_t>(s64), numeric_out_of_range);
Parameters
inValThe value to be converted.
Returns
The safely converted inVal.
Template Parameters
InTSource (input) type, inferred from 'inVal'
OutTDesired result type
Exceptions
numeric_out_of_rangeif the conversion is not safe

Definition at line 54 of file numeric_cast.hpp.

Here is the caller graph for this function: