20
20
#include < stddef.h>
21
21
#include < stdint.h>
22
22
23
+ /* *
24
+ * @addtogroup ble
25
+ * @{
26
+ * @addtogroup common
27
+ * @{
28
+ */
29
+
30
+ /* *
31
+ * @file
32
+ */
33
+
23
34
namespace ble {
24
35
25
36
/* *
26
37
* Immutable view to an array.
38
+ *
39
+ * Array views encapsulate the pointer to an array and its size into a single
40
+ * object; however, it does not manage the lifetime of the array viewed.
41
+ * You can use instances of ArrayView to replace the traditional pair of pointer
42
+ * and size arguments in function calls.
43
+ *
44
+ * You can use the size member function to query the number of elements present
45
+ * in the array, and overloads of the subscript operator allow code using
46
+ * this object to access to the content of the array viewed.
47
+ *
48
+ * @note You can create ArrayView instances with the help of the function
49
+ * template make_ArrayView() and make_const_ArrayView().
50
+ *
51
+ * @tparam T type of objects held by the array.
27
52
*/
28
53
template <typename T>
29
54
struct ArrayView {
30
55
31
56
/* *
32
- * construct an array view to an empty array
57
+ * Construct a view to an empty array.
58
+ *
59
+ * @post a call to size() will return 0, and data() will return NULL.
33
60
*/
34
61
ArrayView () : _array(0 ), _size(0 ) { }
35
62
36
63
/* *
37
- * construct an array view from a pointer.
38
- * and its size.
64
+ * Construct an array view from a pointer to a buffer and its size.
65
+ *
66
+ * @param array_ptr Pointer to the array data
67
+ * @param array_size Number of elements of T present in the array.
68
+ *
69
+ * @post a call to size() will return array_size and data() will return
70
+ * array_tpr.
39
71
*/
40
72
ArrayView (T* array_ptr, size_t array_size) :
41
73
_array (array_ptr), _size(array_size) { }
42
74
43
75
/* *
44
76
* Construct an array view from the reference to an array.
77
+ *
78
+ * @param elements Reference to the array viewed.
79
+ *
80
+ * @tparam Size Number of elements of T presents in the array.
81
+ *
82
+ * @post a call to size() will return Size, and data() will return
83
+ * a pointer to elements.
45
84
*/
46
85
template <size_t Size>
47
86
ArrayView (T (&elements)[Size]):
48
87
_array (elements), _size(Size) { }
49
88
50
89
/* *
51
90
* Return the size of the array viewed.
91
+ *
92
+ * @return The number of elements present in the array viewed.
52
93
*/
53
- size_t size () const {
94
+ size_t size () const
95
+ {
54
96
return _size;
55
97
}
56
98
57
99
/* *
58
100
* Access to a mutable element of the array.
101
+ *
102
+ * @param index Element index to access.
103
+ *
104
+ * @return A reference to the element at the index specified in input.
105
+ *
106
+ * @pre index shall be less than size().
59
107
*/
60
- T& operator [](size_t index) {
108
+ T& operator [](size_t index)
109
+ {
61
110
return _array[index];
62
111
}
63
112
64
113
/* *
65
114
* Access to an immutable element of the array.
115
+ *
116
+ * @param index Element index to access.
117
+ *
118
+ * @return A const reference to the element at the index specified in input.
119
+ *
120
+ * @pre index shall be less than size().
66
121
*/
67
- const T& operator [](size_t index) const {
122
+ const T& operator [](size_t index) const
123
+ {
68
124
return _array[index];
69
125
}
70
126
71
127
/* *
72
- * Get the pointer to the array
128
+ * Get the raw pointer to the array.
129
+ *
130
+ * @return The raw pointer to the array.
73
131
*/
74
- T* data () {
132
+ T* data ()
133
+ {
75
134
return _array;
76
135
}
77
136
78
137
/* *
79
- * Get the pointer to the const array
138
+ * Get the raw const pointer to the array.
139
+ *
140
+ * @return The raw pointer to the array.
80
141
*/
81
- const T* data () const {
142
+ const T* data () const
143
+ {
82
144
return _array;
83
145
}
84
146
85
147
/* *
86
- * Equality operator
148
+ * Equality operator.
149
+ *
150
+ * @param lhs Left hand side of the binary operation.
151
+ * @param rhs Right hand side of the binary operation.
152
+ *
153
+ * @return True if arrays in input have the same size and the same content
154
+ * and false otherwise.
87
155
*/
88
- friend bool operator ==(const ArrayView& lhs, const ArrayView& rhs) {
156
+ friend bool operator ==(const ArrayView& lhs, const ArrayView& rhs)
157
+ {
89
158
if (lhs.size () != rhs.size ()) {
90
159
return false ;
91
160
}
@@ -99,8 +168,15 @@ struct ArrayView {
99
168
100
169
/* *
101
170
* Not equal operator
171
+ *
172
+ * @param lhs Left hand side of the binary operation.
173
+ * @param rhs Right hand side of the binary operation.
174
+ *
175
+ * @return True if arrays in input do not have the same size or the same
176
+ * content and false otherwise.
102
177
*/
103
- friend bool operator !=(const ArrayView& lhs, const ArrayView& rhs) {
178
+ friend bool operator !=(const ArrayView& lhs, const ArrayView& rhs)
179
+ {
104
180
return !(lhs == rhs);
105
181
}
106
182
@@ -111,55 +187,86 @@ struct ArrayView {
111
187
112
188
113
189
/* *
114
- * Generate an array view from a C/C++ array.
115
- * This helper avoid the typing of template parameter when ArrayView are
190
+ * Generate an array view from a reference to a C/C++ array.
191
+ *
192
+ * @tparam T Type of elements held in elements.
193
+ * @tparam Size Number of items held in elements.
194
+ *
195
+ * @param elements The reference to the array viewed.
196
+ *
197
+ * @return The ArrayView to elements.
198
+ *
199
+ * @note This helper avoids the typing of template parameter when ArrayView is
116
200
* created 'inline'.
117
- * @param elements The array viewed.
118
- * @return The array_view to elements.
119
201
*/
120
202
template <typename T, size_t Size>
121
- ArrayView<T> make_ArrayView (T (&elements)[Size]) {
203
+ ArrayView<T> make_ArrayView (T (&elements)[Size])
204
+ {
122
205
return ArrayView<T>(elements);
123
206
}
124
207
125
208
/* *
126
209
* 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
210
+ *
211
+ * @tparam T Type of elements held in array_ptr.
212
+ *
213
+ * @param array_ptr The pointer to the array to viewed.
214
+ * @param array_size The number of T elements in the array.
215
+ *
216
+ * @return The ArrayView to array_ptr with a size of array_size.
217
+ *
218
+ * @note This helper avoids the typing of template parameter when ArrayView is
128
219
* 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
220
*/
133
221
template <typename T>
134
- ArrayView<T> make_ArrayView (T* array_ptr, size_t array_size) {
222
+ ArrayView<T> make_ArrayView (T* array_ptr, size_t array_size)
223
+ {
135
224
return ArrayView<T>(array_ptr, array_size);
136
225
}
137
226
138
227
/* *
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'.
228
+ * Generate a const array view from a reference to a C/C++ array.
229
+ *
230
+ * @tparam T Type of elements held in elements.
231
+ * @tparam Size Number of items held in elements.
232
+ *
142
233
* @param elements The array viewed.
143
234
* @return The ArrayView to elements.
235
+ *
236
+ * @note This helper avoids the typing of template parameter when ArrayView is
237
+ * created 'inline'.
144
238
*/
145
239
template <typename T, size_t Size>
146
- ArrayView<const T> make_const_ArrayView (T (&elements)[Size]) {
240
+ ArrayView<const T> make_const_ArrayView (T (&elements)[Size])
241
+ {
147
242
return ArrayView<const T>(elements);
148
243
}
149
244
150
245
/* *
151
246
* 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.
247
+ *
248
+ * @tparam T Type of elements held in array_ptr.
249
+ *
250
+ * @param array_ptr The pointer to the array to viewed.
251
+ * @param array_size The number of T elements in the array.
252
+ *
156
253
* @return The ArrayView to array_ptr with a size of array_size.
254
+ *
255
+ * @note This helper avoids the typing of template parameter when ArrayView is
256
+ * created 'inline'.
157
257
*/
158
258
template <typename T>
159
- ArrayView<const T> make_const_ArrayView (T* array_ptr, size_t array_size) {
259
+ ArrayView<const T> make_const_ArrayView (T* array_ptr, size_t array_size)
260
+ {
160
261
return ArrayView<const T>(array_ptr, array_size);
161
262
}
162
263
163
264
} // namespace ble
164
265
266
+ /* *
267
+ * @}
268
+ * @}
269
+ */
270
+
271
+
165
272
#endif /* BLE_ARRAY_VIEW_H_ */
0 commit comments