/src/bag/api/bag_uint8array.h
Line | Count | Source (jump to first uncovered line) |
1 | | #ifndef BAG_UINT8ARRAY_H |
2 | | #define BAG_UINT8ARRAY_H |
3 | | |
4 | | #include <cstdint> |
5 | | #include <memory> |
6 | | #include <stdexcept> |
7 | | |
8 | | |
9 | | namespace BAG |
10 | | { |
11 | | |
12 | | //! Class to get past SWIG not being able to handle std::unique_ptr<uint8_t[]>. |
13 | | class UInt8Array final |
14 | | { |
15 | | public: |
16 | | UInt8Array() = default; |
17 | | explicit UInt8Array(size_t len) |
18 | 0 | : m_array(std::make_unique<uint8_t[]>(len)), m_len(len) |
19 | 0 | {} |
20 | | |
21 | | UInt8Array(const UInt8Array&) = delete; |
22 | | UInt8Array(UInt8Array&&) = default; |
23 | | |
24 | 0 | ~UInt8Array() = default; |
25 | | |
26 | | UInt8Array& operator=(const UInt8Array&) = delete; |
27 | | UInt8Array& operator=(UInt8Array&& rhs) noexcept |
28 | 0 | { |
29 | 0 | if (this == &rhs) |
30 | 0 | return *this; |
31 | 0 |
|
32 | 0 | this->m_array = std::move(rhs.m_array); |
33 | 0 | this->m_len = rhs.m_len; |
34 | 0 |
|
35 | 0 | return *this; |
36 | 0 | } |
37 | | |
38 | 0 | bool operator==(const UInt8Array &rhs) const noexcept { |
39 | 0 | return m_array == rhs.m_array && |
40 | 0 | m_len == rhs.m_len; |
41 | 0 | } |
42 | | |
43 | 0 | bool operator!=(const UInt8Array &rhs) const noexcept { |
44 | 0 | return !(rhs == *this); |
45 | 0 | } |
46 | | |
47 | | uint8_t& operator[](size_t index) & |
48 | 0 | { |
49 | 0 | if (index >= m_len) |
50 | 0 | throw std::out_of_range{"Invalid index."}; |
51 | | |
52 | 0 | return *(m_array.get() + index); |
53 | 0 | } |
54 | | |
55 | | const uint8_t& operator[](size_t index) const & |
56 | 0 | { |
57 | 0 | if (index >= m_len) |
58 | 0 | throw std::out_of_range{"Invalid index."}; |
59 | 0 |
|
60 | 0 | return *(m_array.get() + index); |
61 | 0 | } |
62 | | |
63 | | explicit operator bool() const noexcept |
64 | 0 | { |
65 | 0 | return static_cast<bool>(m_array); |
66 | 0 | } |
67 | | |
68 | | uint8_t* data() & noexcept |
69 | 0 | { |
70 | 0 | return m_array.get(); |
71 | 0 | } |
72 | | |
73 | | const uint8_t* data() const & noexcept |
74 | 0 | { |
75 | 0 | return m_array.get(); |
76 | 0 | } |
77 | | |
78 | | uint8_t* release() noexcept |
79 | 0 | { |
80 | 0 | return m_array.release(); |
81 | 0 | } |
82 | | |
83 | | size_t size() const noexcept |
84 | 0 | { |
85 | 0 | return m_len; |
86 | 0 | } |
87 | | |
88 | | private: |
89 | | std::unique_ptr<uint8_t[]> m_array; |
90 | | size_t m_len = 0; |
91 | | }; |
92 | | |
93 | | } // namespace BAG |
94 | | |
95 | | #endif // BAG_UINT8ARRAY_H |
96 | | |