Skip to content

Commit dd303b7

Browse files
committed
BLE: Add ArrayView in utilities.
1 parent c58d7de commit dd303b7

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed

features/FEATURE_BLE/ble/ArrayView.h

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017-2017 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef BLE_ARRAY_VIEW_H_
18+
#define BLE_ARRAY_VIEW_H_
19+
20+
#include <stddef.h>
21+
#include <stdint.h>
22+
23+
namespace ble {
24+
25+
/**
26+
* Immutable view to an array.
27+
*/
28+
template<typename T>
29+
struct ArrayView {
30+
31+
/**
32+
* construct an array view to an empty array
33+
*/
34+
ArrayView() : _array(0), _size(0) { }
35+
36+
/**
37+
* construct an array view from a pointer.
38+
* and its size.
39+
*/
40+
ArrayView(T* array_ptr, size_t array_size) :
41+
_array(array_ptr), _size(array_size) { }
42+
43+
/**
44+
* Construct an array view from the reference to an array.
45+
*/
46+
template<size_t Size>
47+
ArrayView(T (&elements)[Size]):
48+
_array(elements), _size(Size) { }
49+
50+
/**
51+
* Return the size of the array viewed.
52+
*/
53+
size_t size() const {
54+
return _size;
55+
}
56+
57+
/**
58+
* Access to a mutable element of the array.
59+
*/
60+
T& operator[](size_t index) {
61+
return _array[index];
62+
}
63+
64+
/**
65+
* Access to an immutable element of the array.
66+
*/
67+
const T& operator[](size_t index) const {
68+
return _array[index];
69+
}
70+
71+
/**
72+
* Get the pointer to the array
73+
*/
74+
T* data() {
75+
return _array;
76+
}
77+
78+
/**
79+
* Get the pointer to the const array
80+
*/
81+
const T* data() const {
82+
return _array;
83+
}
84+
85+
/**
86+
* Equality operator
87+
*/
88+
friend bool operator==(const ArrayView& lhs, const ArrayView& rhs) {
89+
if (lhs.size() != rhs.size()) {
90+
return false;
91+
}
92+
93+
if (lhs.data() == rhs.data()) {
94+
return true;
95+
}
96+
97+
return memcmp(lhs.data(), rhs.data(), lhs.size()) == 0;
98+
}
99+
100+
/**
101+
* Not equal operator
102+
*/
103+
friend bool operator!=(const ArrayView& lhs, const ArrayView& rhs) {
104+
return !(lhs == rhs);
105+
}
106+
107+
private:
108+
T* const _array;
109+
const size_t _size;
110+
};
111+
112+
113+
/**
114+
* Generate an array view from a C/C++ array.
115+
* This helper avoid the typing of template parameter when ArrayView are
116+
* created 'inline'.
117+
* @param elements The array viewed.
118+
* @return The array_view to elements.
119+
*/
120+
template<typename T, size_t Size>
121+
ArrayView<T> make_ArrayView(T (&elements)[Size]) {
122+
return ArrayView<T>(elements);
123+
}
124+
125+
/**
126+
* Generate an array view from a C/C++ pointer and the size of the array.
127+
* This helper avoid the typing of template parameter when ArrayView are
128+
* created 'inline'.
129+
* @param array_ptr The pointer to the array to view.
130+
* @param array_size The size of the array.
131+
* @return The array_view to array_ptr with a size of array_size.
132+
*/
133+
template<typename T>
134+
ArrayView<T> make_ArrayView(T* array_ptr, size_t array_size) {
135+
return ArrayView<T>(array_ptr, array_size);
136+
}
137+
138+
/**
139+
* Generate a const array view from a C/C++ array.
140+
* This helper avoid the typing of template parameter when ArrayView are
141+
* created 'inline'.
142+
* @param elements The array viewed.
143+
* @return The ArrayView to elements.
144+
*/
145+
template<typename T, size_t Size>
146+
ArrayView<const T> make_const_ArrayView(T (&elements)[Size]) {
147+
return ArrayView<const T>(elements);
148+
}
149+
150+
/**
151+
* Generate a const array view from a C/C++ pointer and the size of the array.
152+
* This helper avoid the typing of template parameter when ArrayView are
153+
* created 'inline'.
154+
* @param array_ptr The pointer to the array to view.
155+
* @param array_size The size of the array.
156+
* @return The ArrayView to array_ptr with a size of array_size.
157+
*/
158+
template<typename T>
159+
ArrayView<const T> make_const_ArrayView(T* array_ptr, size_t array_size) {
160+
return ArrayView<const T>(array_ptr, array_size);
161+
}
162+
163+
} // namespace ble
164+
165+
#endif /* BLE_ARRAY_VIEW_H_ */

0 commit comments

Comments
 (0)