cpp-common/bt2c: add `DataLen` class (general-purpose data length)
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Mon, 5 Feb 2024 17:39:07 +0000 (12:39 -0500)
committerSimon Marchi <simon.marchi@efficios.com>
Wed, 17 Apr 2024 17:57:53 +0000 (13:57 -0400)
commitca92fb3746f7333a28549a39f148eab9c8402f89
treeeabfee121101d85fc96520901c84ebe389b66e8b
parentc942e7a26ff9579f3135ff181f54d1bc691c954c
cpp-common/bt2c: add `DataLen` class (general-purpose data length)

This patch adds the `bt2c::DataLen` class so that you can make sure your
function accepts an expected data length instead of specifying that you
need bits or bytes:

    Something buildSomething(bt2c::DataLen len, ...)
    {
        using namespace bt2c::literals::datalen;

        if (...) {
            len += 16_MiBytes;
        } else {
            len *= 2;
        }

        // ...
    }

    // ...

    const auto something = buildSomething(DataLen::fromBytes(sz), ...);

As you can see above, you can use the `bt2c::literals::datalen`
namespace to access handy data length user literals.

The `bt2c::DataLen` constructor is private: you need to use the
bt2c::DataLen::fromBits() and bt2c::DataLen::fromBytes()
static methods to build an instance.

Because a `bt2c::DataLen` instance is just an `unsigned long long`
value in memory, the intention of this API is to copy data length
instances as much as possible. Therefore, I suppose it's cheaper to
accept a `const bt2c::DataLen` parameter than
`const bt2c::DataLen&` which would add an indirection.

The constructor and many methods are `constexpr` to make this useable in
constant context.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I1375fb5697c0d952f3669dc587603839d511dd18
Reviewed-on: https://review.lttng.org/c/babeltrace/+/7726
Reviewed-on: https://review.lttng.org/c/babeltrace/+/12250
src/Makefile.am
src/cpp-common/bt2c/data-len.hpp [new file with mode: 0644]
This page took 0.026479 seconds and 4 git commands to generate.