Communication Library for Autonomous Systems v1.0
Reliable and secure communication library for autonomous vehicle systems
Loading...
Searching...
No Matches
buffer.h
Go to the documentation of this file.
1#ifndef BUFFER_H
2#define BUFFER_H
3
4#include <cstring>
5#include <cstdint>
6
7template <typename T>
8class Buffer{
9
10 public:
11 static constexpr unsigned int MAX_SIZE = sizeof(T);
12
13 public:
16
17 T* data();
18 void setData(const void* data, unsigned int size);
19 const unsigned int size() const;
20 void clear();
21 void setRX(std::int64_t rx);
22 const std::int64_t rx();
23
24 private:
25 void setSize(unsigned int size);
26
27 private:
28 std::uint8_t _data[MAX_SIZE];
29 unsigned int _size;
30 std::int64_t _rx_time;
31};
32
33template <typename T>
34Buffer<T>::Buffer() : _size(0) {
35 std::memset(_data, 0, MAX_SIZE);
36}
37
38template <typename T>
40 clear();
41}
42
43template <typename T>
45 if (_size == 0)
46 return nullptr;
47
48 return reinterpret_cast<T*>(_data);
49}
50
51template <typename T>
52const unsigned int Buffer<T>::size() const {
53 return _size;
54}
55
56template <typename T>
57void Buffer<T>::setData(const void* data, unsigned int size) {
58 setSize(size);
59 std::memcpy(_data, data, _size);
60}
61
62template <typename T>
63void Buffer<T>::setSize(unsigned int size) {
64 _size = (size > MAX_SIZE) ? MAX_SIZE : size; // Ensures that it does not exceed MAX_SIZE
65}
66
67template <typename T>
69 std::memset(_data, 0, MAX_SIZE);
70 _size = 0;
71}
72
73template <typename T>
74void Buffer<T>::setRX(std::int64_t rx) {
75 _rx_time = rx;
76}
77
78template <typename T>
79const std::int64_t Buffer<T>::rx() {
80 return _rx_time;
81}
82
83#endif // BUFFER_H
Definition buffer.h:8
const unsigned int size() const
Definition buffer.h:52
void setRX(std::int64_t rx)
Definition buffer.h:74
void clear()
Definition buffer.h:68
~Buffer()
Definition buffer.h:39
static constexpr unsigned int MAX_SIZE
Definition buffer.h:11
Buffer()
Definition buffer.h:34
void setData(const void *data, unsigned int size)
Definition buffer.h:57
const std::int64_t rx()
Definition buffer.h:79
T * data()
Definition buffer.h:44
Select_Debug<(Traits< T >::debugged &&Traits< Debug >::error)> db(Debug_Error l)
Definition debug.h:166
T * data()
Definition protocol.h:24