livox_mid360_core
C++ driver library for the Livox Mid-360 LiDAR
Loading...
Searching...
No Matches
bytes.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
4#pragma once
5
6#include <bit>
7#include <cstddef>
8#include <cstdint>
9#include <cstring>
10#include <span>
11#include <type_traits>
12
14{
15
18template <typename T>
19 requires std::is_trivially_copyable_v<T>
20[[nodiscard]] inline T read_le(std::span<const std::byte> in, std::size_t offset) noexcept
21{
22 T v{};
23 std::memcpy(&v, in.data() + offset, sizeof(T));
24 if constexpr (std::endian::native == std::endian::big && std::is_integral_v<T> && sizeof(T) > 1) {
25 v = std::byteswap(v);
26 }
27 return v;
28}
29
32template <typename T>
33 requires std::is_trivially_copyable_v<T>
34inline void write_le(std::span<std::byte> out, std::size_t offset, T v) noexcept
35{
36 if constexpr (std::endian::native == std::endian::big && std::is_integral_v<T> && sizeof(T) > 1) {
37 v = std::byteswap(v);
38 }
39 std::memcpy(out.data() + offset, &v, sizeof(T));
40}
41
43template <typename T>
44 requires std::is_trivially_copyable_v<T>
45[[nodiscard]] inline std::span<const std::byte> as_bytes(std::span<const T> s) noexcept
46{
47 return std::as_bytes(s);
48}
49
51[[nodiscard]] inline std::span<const std::byte> as_bytes(const char * s, std::size_t n) noexcept
52{
53 return {reinterpret_cast<const std::byte *>(s), n};
54}
55
56} // namespace livox::mid360::bytes
Little-endian byte helpers used by the protocol layer (bytes.hpp).
Definition bytes.hpp:14
T read_le(std::span< const std::byte > in, std::size_t offset) noexcept
Reads a little-endian trivially-copyable value from in at byte offset.
Definition bytes.hpp:20
void write_le(std::span< std::byte > out, std::size_t offset, T v) noexcept
Writes v little-endian into out at byte offset.
Definition bytes.hpp:34
std::span< const std::byte > as_bytes(std::span< const T > s) noexcept
Reinterprets a contiguous range of trivially-copyable objects as bytes.
Definition bytes.hpp:45