2 * Copyright (c) 2022 Philippe Proulx <pproulx@efficios.com>
4 * SPDX-License-Identifier: MIT
7 #ifndef BABELTRACE_CPP_COMMON_BT2C_READ_FIXED_LEN_INT_HPP
8 #define BABELTRACE_CPP_COMMON_BT2C_READ_FIXED_LEN_INT_HPP
12 #include <type_traits>
19 * Reads a fixed-length integer of unknown byte order into a value of integral
20 * type `IntT` from the buffer `buf` and returns it.
22 template <typename IntT>
23 IntT readFixedLenInt(const std::uint8_t * const buf)
25 static_assert(std::is_integral<IntT>::value, "`IntT` is an integral type.");
29 std::memcpy(&val, buf, sizeof(val));
34 * Reads a fixed-length little-endian integer into a value of integral
35 * type `IntT` from the buffer `buf` and returns it.
37 template <typename IntT>
38 IntT readFixedLenIntLe(const std::uint8_t * const buf)
40 return bt2c::littleEndianToNative(readFixedLenInt<IntT>(buf));
44 * Reads a fixed-length big-endian integer into a value of integral
45 * type `IntT` from the buffer `buf` and returns it.
47 template <typename IntT>
48 IntT readFixedLenIntBe(const std::uint8_t * const buf)
50 return bt2c::bigEndianToNative(readFixedLenInt<IntT>(buf));
53 } /* namespace bt2c */
55 #endif /* BABELTRACE_CPP_COMMON_BT2C_READ_FIXED_LEN_INT_HPP */