VBVX 0.1.0
Header-only C++23 library for safe, zero-copy parsing of packet buffers.
Loading...
Searching...
No Matches
utils.hxx
Go to the documentation of this file.
1#pragma once
2
3#include <bit>
4#include <concepts>
5#include <cstdint>
6#include <cstring>
7
8namespace vbvx {
9
11template <typename _Tp>
12 requires std::integral<_Tp>
13constexpr _Tp autoswap(_Tp tp) {
14 if constexpr (std::endian::native == std::endian::little) {
15 return std::byteswap(tp);
16 } else {
17 return tp;
18 }
19}
20
22template <typename _Tp>
23 requires std::is_trivially_copyable_v<_Tp>
24constexpr _Tp read_from_bytes(const uint8_t* src) {
25 _Tp tp;
26 std::memcpy(&tp, src, sizeof(_Tp));
27 return tp;
28}
29
30} // namespace vbvx
Definition arp.hxx:11
constexpr _Tp read_from_bytes(const uint8_t *src)
Read a trivially copyable type from a byte array.
Definition utils.hxx:24
constexpr _Tp autoswap(_Tp tp)
Byte-swap a value if the host is little-endian.
Definition utils.hxx:13