.gitignore: add some more IDE / tools related file
[babeltrace.git] / src / cpp-common / bt2c / read-fixed-len-int.hpp
CommitLineData
82a36560
PP
1/*
2 * Copyright (c) 2022 Philippe Proulx <pproulx@efficios.com>
3 *
4 * SPDX-License-Identifier: MIT
5 */
6
094bf3f2
SM
7#ifndef BABELTRACE_CPP_COMMON_BT2C_READ_FIXED_LEN_INT_HPP
8#define BABELTRACE_CPP_COMMON_BT2C_READ_FIXED_LEN_INT_HPP
82a36560
PP
9
10#include <cstdint>
82a36560
PP
11#include <cstring>
12#include <type_traits>
13
094bf3f2 14#include "endian.hpp"
82a36560 15
094bf3f2 16namespace bt2c {
82a36560
PP
17
18/*
e0a3ddd4 19 * Reads a fixed-length integer of unknown byte order into a value of integral
82a36560
PP
20 * type `IntT` from the buffer `buf` and returns it.
21 */
22template <typename IntT>
e0a3ddd4 23IntT readFixedLenInt(const std::uint8_t * const buf)
82a36560
PP
24{
25 static_assert(std::is_integral<IntT>::value, "`IntT` is an integral type.");
26
27 IntT val;
28
29 std::memcpy(&val, buf, sizeof(val));
e0a3ddd4
FD
30 return val;
31}
32
33/*
34 * Reads a fixed-length little-endian integer into a value of integral
35 * type `IntT` from the buffer `buf` and returns it.
36 */
37template <typename IntT>
38IntT readFixedLenIntLe(const std::uint8_t * const buf)
39{
094bf3f2 40 return bt2c::littleEndianToNative(readFixedLenInt<IntT>(buf));
82a36560
PP
41}
42
43/*
44 * Reads a fixed-length big-endian integer into a value of integral
45 * type `IntT` from the buffer `buf` and returns it.
46 */
47template <typename IntT>
48IntT readFixedLenIntBe(const std::uint8_t * const buf)
49{
094bf3f2 50 return bt2c::bigEndianToNative(readFixedLenInt<IntT>(buf));
82a36560
PP
51}
52
094bf3f2 53} /* namespace bt2c */
82a36560 54
094bf3f2 55#endif /* BABELTRACE_CPP_COMMON_BT2C_READ_FIXED_LEN_INT_HPP */
This page took 0.039834 seconds and 4 git commands to generate.