-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdata_span.hpp
79 lines (71 loc) · 1.99 KB
/
data_span.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//================================================================================================
/// @file can_message_data.hpp
///
/// @brief Contains common types and functions for working with an arbitrary amount of items.
/// @author Daan Steenbergen
///
/// @copyright 2023 Open Agriculture
//================================================================================================
#ifndef DATA_SPAN_HPP
#define DATA_SPAN_HPP
#include <array>
#include <cstddef>
#include <vector>
namespace isobus
{
//================================================================================================
/// @class DataSpan
///
/// @brief A class that represents a span of data of arbitrary length.
//================================================================================================
template<typename T>
class DataSpan
{
public:
/// @brief Construct a new DataSpan object of a writeable array.
/// @param ptr pointer to the buffer to use.
/// @param len The number of elements in the buffer.
DataSpan(T *ptr, std::size_t size) :
ptr(ptr),
_size(size)
{
}
/// @brief Get the element at the given index.
/// @param index The index of the element to get.
/// @return The element at the given index.
T &operator[](std::size_t index)
{
return ptr[index * sizeof(T)];
}
/// @brief Get the element at the given index.
/// @param index The index of the element to get.
/// @return The element at the given index.
T const &operator[](std::size_t index) const
{
return ptr[index * sizeof(T)];
}
/// @brief Get the size of the data span.
/// @return The size of the data span.
std::size_t size() const
{
return _size;
}
/// @brief Get the begin iterator.
/// @return The begin iterator.
T *begin() const
{
return ptr;
}
/// @brief Get the end iterator.
/// @return The end iterator.
T *end() const
{
return ptr + _size * sizeof(T);
}
private:
T *ptr;
std::size_t _size;
bool _isConst;
};
}
#endif // DATA_SPAN_HPP