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)
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]

index c9c07af4649baad35f8a731bdf38f4ca0b656c22..534e63888639fd9ef7f2c5b028ec8d1bb78560ee 100644 (file)
@@ -19,4 +19,5 @@ EXTRA_DIST = bt2 \
        align.hpp \
        uuid.hpp \
        vector.hpp \
-       std-int.hpp
+       std-int.hpp \
+       read-fixed-len-int.hpp
diff --git a/src/cpp-common/read-fixed-len-int.hpp b/src/cpp-common/read-fixed-len-int.hpp
new file mode 100644 (file)
index 0000000..27799e1
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2022 Philippe Proulx <pproulx@efficios.com>
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#ifndef BABELTRACE_CPP_COMMON_READ_FIXED_LEN_INT_HPP
+#define BABELTRACE_CPP_COMMON_READ_FIXED_LEN_INT_HPP
+
+#include <cstdint>
+#include <cstdlib>
+#include <cstring>
+#include <type_traits>
+
+#include "cpp-common/endian.hpp"
+
+namespace bt2_common {
+
+/*
+ * Reads a fixed-length little-endian integer into a value of integral
+ * type `IntT` from the buffer `buf` and returns it.
+ */
+template <typename IntT>
+IntT readFixedLenIntLe(const std::uint8_t * const buf)
+{
+    static_assert(std::is_integral<IntT>::value, "`IntT` is an integral type.");
+
+    IntT val;
+
+    std::memcpy(&val, buf, sizeof(val));
+    return bt2_common::littleEndianToNative(val);
+}
+
+/*
+ * Reads a fixed-length big-endian integer into a value of integral
+ * type `IntT` from the buffer `buf` and returns it.
+ */
+template <typename IntT>
+IntT readFixedLenIntBe(const std::uint8_t * const buf)
+{
+    static_assert(std::is_integral<IntT>::value, "`IntT` is an integral type.");
+
+    IntT val;
+
+    std::memcpy(&val, buf, sizeof(val));
+    return bt2_common::bigEndianToNative(val);
+}
+
+} /* namespace bt2_common */
+
+#endif /* BABELTRACE_CPP_COMMON_READ_FIXED_LEN_INT_HPP */
This page took 0.02517 seconds and 4 git commands to generate.