src/cpp-common: add bt2_common::safeTo*() and bt2_common::safe*()
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Fri, 15 Apr 2022 17:08:10 +0000 (13:08 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Mon, 11 Sep 2023 15:24:02 +0000 (11:24 -0400)
commite2c3fe1ca07b187c76b5279d44613019b2efbe71
treee7bf38d84ff2e8eb843ea5789a59a489dcf6b6bc
parentfa5e9d5714433f38d2b3d8e6f1422a223d781125
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>
src/cpp-common/Makefile.am
src/cpp-common/safe-ops.hpp [new file with mode: 0644]
This page took 0.024108 seconds and 4 git commands to generate.