src/cpp-common: add fixed-length integer reading functions
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Mon, 25 Apr 2022 15:10:04 +0000 (11:10 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Mon, 11 Sep 2023 15:24:02 +0000 (11:24 -0400)
commit82a36560253182a9e8bacf6a7a9fc30fc2826f71
tree2cd196b744c0a63c994acb7ca0f1e323e75d97e2
parent2f1bfb2a34b73d37444f0707292b4427d6db72c9
src/cpp-common: add fixed-length integer reading functions

This patch adds `src/cpp-common/read-fixed-len-int.hpp` which offers
function templates to read fixed-length integers from a buffer.

The bt2_common::readFixedLenIntLe() (little endian) and
bt2_common::readFixedLenIntBe() (big endian) function templates accept
one template parameter which is the integral type of the integer to read
(and the return type of the function). For example,
bt2_common::readFixedLenIntLe<std::int32_t>() reads a 32-bit signed
little-endian integer and returns it.

The function templates use bt2_common::littleEndianToNative() and
bt2_common::bigEndianToNative() as needed.

You may combine this with `bt2_common::StdIntT` to have the length in
bits and the signedness as independent template parameters, for
example:

    const auto val = readFixedLenIntBe<StdIntT<16, true>>(myBuf);

which is equivalent to:

    const auto val = readFixedLenIntBe<std::int16_t>(myBuf);

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I9c684bede60f7b6324a3140c5fd498c21ad513ab
Reviewed-on: https://review.lttng.org/c/babeltrace/+/7939
Reviewed-by: Francis Deslauriers <francis.deslauriers@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/10840
Tested-by: jenkins <jenkins@lttng.org>
src/cpp-common/Makefile.am
src/cpp-common/read-fixed-len-int.hpp [new file with mode: 0644]
This page took 0.02506 seconds and 4 git commands to generate.