src/cpp-common: add bt2_common::safeTo*() and bt2_common::safe*()
This patch adds the following function templates, under the `bt2_common`
namespace, within the new `src/cpp-common/safe-ops.hpp` header:
safeToMul():
Returns whether or not you can safely multiply two numbers without
causing an overflow.
safeToAdd():
Returns whether or not you can safely add two numbers without
causing an overflow.
safeToSub():
Returns whether or not you can safely subtract two numbers without
causing an underflow.
safeMul():
Multiplies some number by another, asserting that it's safe to do
so, and returns the result.
safeAdd():
Adds some number and another, asserting that it's safe to do so, and
returns the result.
safeSub():
Subtract some number from another, asserting that it's safe to do
so, and returns the result.
Those are similar to the existing bt_safe_*() C functions, except that:
* They're function templates, so you can use any numeric type.
* The safeTo*() function templates are `constexpr`.
* They only work with unsigned types; signed types introduce some
complexity (negative values) which I don't need to deal with right
now.
Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I29ed4b51d50ae2507f5580db20452ec6c188ef66
Reviewed-on: https://review.lttng.org/c/babeltrace/+/7916
Reviewed-on: https://review.lttng.org/c/babeltrace/+/10816
Tested-by: jenkins <jenkins@lttng.org>
This page took 0.024561 seconds and 4 git commands to generate.