Add `bt2_common::DataLen` class (general-purpose data length)
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Fri, 1 Apr 2022 19:47:50 +0000 (15:47 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Tue, 23 Aug 2022 16:06:15 +0000 (12:06 -0400)
commit89ba212dcf897ed49ce2ead5da8a90fa7f7fd5fc
tree1fcd7df92b5d5efc6455e217a8a2c3dcb2d2f6a4
parent6d9510bc8e457f2d6329861885fff56f21d5ad7e
Add `bt2_common::DataLen` class (general-purpose data length)

This patch adds the `bt2_common::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(bt2_common::DataLen len, ...)
    {
        using namespace bt2_common::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 `bt2_common::literals::datalen`
namespace to access handy data length user literals.

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

Because a `bt2_common::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 bt2_common::DataLen` parameter than
`const bt2_common::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
src/cpp-common/Makefile.am
src/cpp-common/data-len.hpp [new file with mode: 0644]
This page took 0.036313 seconds and 5 git commands to generate.