Bus Messages 1.0
Bus Message Serialization Library
Loading...
Searching...
No Matches
littlebuffer.h
Go to the documentation of this file.
1/*
2 * Copyright 2025 Ingemar Hedvall
3 * SPDX-License-Identifier: MIT
4 */
5
9#pragma once
10
11#include <cstring>
12#include <cstdint>
13#include <vector>
14#include <array>
15#include <bit>
16
17namespace bus {
18
23template <typename T>
24class LittleBuffer {
25 public:
26 LittleBuffer() = default;
27
32 explicit LittleBuffer(const T& value);
33
39 LittleBuffer(const std::vector<uint8_t>& buffer, size_t offset);
40
46 LittleBuffer(const uint8_t* buffer, size_t offset);
47
52 [[nodiscard]] auto cbegin() const {
53 return buffer_.cbegin();
54 }
55
60 [[nodiscard]] auto cend() const {
61 return buffer_.cend();
62 }
63
68 [[nodiscard]] const uint8_t* data() const;
69
74 [[nodiscard]] uint8_t* data();
75
80 [[nodiscard]] size_t size() const;
81
86 T value() const;
87
88 private:
89 std::array<uint8_t, sizeof(T)> buffer_;
90};
91
92template <typename T>
93LittleBuffer<T>::LittleBuffer(const T& value) {
94 if (constexpr bool big_endian = std::endian::native == std::endian::big;
95 big_endian) { // Computer uses big endian
96 std::array<uint8_t, sizeof(T)> temp = {0};
97 memcpy(temp.data(), &value, sizeof(T));
98 for (size_t index = sizeof(T); index > 0; --index) {
99 buffer_[sizeof(T) - index] = temp[index - 1];
100 }
101 } else {
102 std::memcpy(buffer_.data(), &value, sizeof(T));
103 }
104}
105
106template <typename T>
107LittleBuffer<T>::LittleBuffer(const std::vector<uint8_t>& buffer,
108 size_t offset) {
109 std::memcpy(buffer_.data(), buffer.data() + offset, sizeof(T));
110}
111
112template <typename T>
113LittleBuffer<T>::LittleBuffer(const uint8_t* buffer,
114 size_t offset) {
115 if (buffer != nullptr) {
116 std::memcpy(buffer_.data(), buffer + offset, sizeof(T));
117 }
118}
119
120template <typename T>
121const uint8_t* LittleBuffer<T>::data() const {
122 return buffer_.data();
123}
124
125template <typename T>
127 return buffer_.data();
128}
129template <typename T>
130size_t LittleBuffer<T>::size() const {
131 return buffer_.size();
132}
133
134template <typename T>
136
137 std::array<uint8_t, sizeof(T)> temp = {0};
138 if ( constexpr bool big_endian = std::endian::native == std::endian::big;
139 big_endian) {
140 for (size_t index = sizeof(T); index > 0; --index) {
141 temp[sizeof(T) - index] = buffer_[index - 1];
142 }
143 } else {
144 std::memcpy(temp.data(), buffer_.data(), sizeof(T));
145 }
146 const T* val = reinterpret_cast<const T*>(temp.data());
147 return *val;
148}
149
150} // namespace mdf
T value() const
Returns the value.
Definition littlebuffer.h:135
auto cbegin() const
Returns an iterator to the first byte.
Definition littlebuffer.h:52
const uint8_t * data() const
Returns a constant pointer to the internal byte array.
Definition littlebuffer.h:121
size_t size() const
Returns size of the value type.
Definition littlebuffer.h:130
auto cend() const
Returns the an iterator to the last byte.
Definition littlebuffer.h:60
Main namespace for the MDF library.
Definition buslogstream.h:24