mirror of https://github.com/ARMmbed/mbed-os.git
BLE: Extend ArrayView to encode size in type.
With this change, it is possible to encode the size of the array viewed by an ArrayView into the type itself: ArrayView<T, Size>. Such objects are lighter than ArrayView of arbitrary size and allows verification of the size at compile time. This change also fix operator== and bring new make_ArrayView overloads.pull/6932/head
parent
9e1f0b34f8
commit
5761caff00
|
@ -17,8 +17,11 @@
|
|||
#ifndef BLE_ARRAY_VIEW_H_
|
||||
#define BLE_ARRAY_VIEW_H_
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include "platform/mbed_assert.h"
|
||||
|
||||
/**
|
||||
* @addtogroup ble
|
||||
|
@ -33,11 +36,18 @@
|
|||
|
||||
namespace ble {
|
||||
|
||||
/**
|
||||
* Special value for the Size parameter of ArrayView.
|
||||
* If the type use this value then the size of the array is stored in the object
|
||||
* at runtime.
|
||||
*/
|
||||
#define ARRAY_VIEW_DYNAMIC_SIZE -1
|
||||
|
||||
/**
|
||||
* Immutable view to an array.
|
||||
*
|
||||
* Array views encapsulate the pointer to an array and its size into a single
|
||||
* object; however, it does not manage the lifetime of the array viewed.
|
||||
* object or type; however, it does not manage the lifetime of the array viewed.
|
||||
* You can use instances of ArrayView to replace the traditional pair of pointer
|
||||
* and size arguments in function calls.
|
||||
*
|
||||
|
@ -48,20 +58,28 @@ namespace ble {
|
|||
* @note You can create ArrayView instances with the help of the function
|
||||
* template make_ArrayView() and make_const_ArrayView().
|
||||
*
|
||||
* @note ArrayView<T, Size> objects can be implicitly converted to ArrayView<T>
|
||||
* objects where required.
|
||||
*
|
||||
* @tparam T type of objects held by the array.
|
||||
* @tparam Size The size of the array viewed. The default value
|
||||
* ARRAY_VIEW_DYNAMIC_SIZE is special as it allows construction of ArrayView
|
||||
* objects of any size (set at runtime).
|
||||
*/
|
||||
template<typename T>
|
||||
template<typename T, ptrdiff_t Size = ARRAY_VIEW_DYNAMIC_SIZE>
|
||||
struct ArrayView {
|
||||
|
||||
MBED_STATIC_ASSERT(Size >= 0, "Invalid size for an ArrayView");
|
||||
|
||||
/**
|
||||
* Construct a view to an empty array.
|
||||
*
|
||||
* @post a call to size() will return 0, and data() will return NULL.
|
||||
*/
|
||||
ArrayView() : _array(0), _size(0) { }
|
||||
ArrayView() : _array(NULL) { }
|
||||
|
||||
/**
|
||||
* Construct an array view from a pointer to a buffer and its size.
|
||||
* Construct an array view from a pointer to a buffer.
|
||||
*
|
||||
* @param array_ptr Pointer to the array data
|
||||
* @param array_size Number of elements of T present in the array.
|
||||
|
@ -70,7 +88,9 @@ struct ArrayView {
|
|||
* array_tpr.
|
||||
*/
|
||||
ArrayView(T* array_ptr, size_t array_size) :
|
||||
_array(array_ptr), _size(array_size) { }
|
||||
_array(array_ptr) {
|
||||
MBED_ASSERT(array_size >= (size_t) Size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct an array view from the reference to an array.
|
||||
|
@ -82,9 +102,8 @@ struct ArrayView {
|
|||
* @post a call to size() will return Size, and data() will return
|
||||
* a pointer to elements.
|
||||
*/
|
||||
template<size_t Size>
|
||||
ArrayView(T (&elements)[Size]):
|
||||
_array(elements), _size(Size) { }
|
||||
_array(elements) { }
|
||||
|
||||
/**
|
||||
* Return the size of the array viewed.
|
||||
|
@ -93,7 +112,7 @@ struct ArrayView {
|
|||
*/
|
||||
size_t size() const
|
||||
{
|
||||
return _size;
|
||||
return _array ? Size : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -144,40 +163,115 @@ struct ArrayView {
|
|||
return _array;
|
||||
}
|
||||
|
||||
private:
|
||||
T* const _array;
|
||||
};
|
||||
|
||||
/**
|
||||
* ArrayView specialisation that handle dynamic array size.
|
||||
*/
|
||||
template<typename T>
|
||||
struct ArrayView<T, ARRAY_VIEW_DYNAMIC_SIZE> {
|
||||
|
||||
/**
|
||||
* Equality operator.
|
||||
* Construct a view to an empty array.
|
||||
*
|
||||
* @param lhs Left hand side of the binary operation.
|
||||
* @param rhs Right hand side of the binary operation.
|
||||
*
|
||||
* @return True if arrays in input have the same size and the same content
|
||||
* and false otherwise.
|
||||
* @post a call to size() will return 0, and data() will return NULL.
|
||||
*/
|
||||
friend bool operator==(const ArrayView& lhs, const ArrayView& rhs)
|
||||
ArrayView() : _array(0), _size(0) { }
|
||||
|
||||
/**
|
||||
* Construct an array view from a pointer to a buffer and its size.
|
||||
*
|
||||
* @param array_ptr Pointer to the array data
|
||||
* @param array_size Number of elements of T present in the array.
|
||||
*
|
||||
* @post a call to size() will return array_size and data() will return
|
||||
* array_tpr.
|
||||
*/
|
||||
ArrayView(T* array_ptr, size_t array_size) :
|
||||
_array(array_ptr), _size(array_size) { }
|
||||
|
||||
/**
|
||||
* Construct an array view from the reference to an array.
|
||||
*
|
||||
* @param elements Reference to the array viewed.
|
||||
*
|
||||
* @tparam Size Number of elements of T presents in the array.
|
||||
*
|
||||
* @post a call to size() will return Size, and data() will return
|
||||
* a pointer to elements.
|
||||
*/
|
||||
template<size_t Size>
|
||||
ArrayView(T (&elements)[Size]):
|
||||
_array(elements), _size(Size) { }
|
||||
|
||||
|
||||
/**
|
||||
* Construct a ArrayView object with a dynamic size from an ArrayView object
|
||||
* with a static size.
|
||||
* @param other The ArrayView object used to construct this.
|
||||
*/
|
||||
template<size_t Size>
|
||||
ArrayView(ArrayView<T, Size> other):
|
||||
_array(other.data()), _size(other.size()) { }
|
||||
|
||||
/**
|
||||
* Return the size of the array viewed.
|
||||
*
|
||||
* @return The number of elements present in the array viewed.
|
||||
*/
|
||||
size_t size() const
|
||||
{
|
||||
if (lhs.size() != rhs.size()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (lhs.data() == rhs.data()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return memcmp(lhs.data(), rhs.data(), lhs.size()) == 0;
|
||||
return _size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Not equal operator
|
||||
* Access to a mutable element of the array.
|
||||
*
|
||||
* @param lhs Left hand side of the binary operation.
|
||||
* @param rhs Right hand side of the binary operation.
|
||||
* @param index Element index to access.
|
||||
*
|
||||
* @return True if arrays in input do not have the same size or the same
|
||||
* content and false otherwise.
|
||||
* @return A reference to the element at the index specified in input.
|
||||
*
|
||||
* @pre index shall be less than size().
|
||||
*/
|
||||
friend bool operator!=(const ArrayView& lhs, const ArrayView& rhs)
|
||||
T& operator[](size_t index)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
return _array[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* Access to an immutable element of the array.
|
||||
*
|
||||
* @param index Element index to access.
|
||||
*
|
||||
* @return A const reference to the element at the index specified in input.
|
||||
*
|
||||
* @pre index shall be less than size().
|
||||
*/
|
||||
const T& operator[](size_t index) const
|
||||
{
|
||||
return _array[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the raw pointer to the array.
|
||||
*
|
||||
* @return The raw pointer to the array.
|
||||
*/
|
||||
T* data()
|
||||
{
|
||||
return _array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the raw const pointer to the array.
|
||||
*
|
||||
* @return The raw pointer to the array.
|
||||
*/
|
||||
const T* data() const
|
||||
{
|
||||
return _array;
|
||||
}
|
||||
|
||||
private:
|
||||
|
@ -186,6 +280,45 @@ private:
|
|||
};
|
||||
|
||||
|
||||
/**
|
||||
* Equality operator.
|
||||
*
|
||||
* @param lhs Left hand side of the binary operation.
|
||||
* @param rhs Right hand side of the binary operation.
|
||||
*
|
||||
* @return True if arrays in input have the same size and the same content
|
||||
* and false otherwise.
|
||||
*/
|
||||
template<typename T, ptrdiff_t LhsSize, ptrdiff_t RhsSize>
|
||||
bool operator==(const ArrayView<T, LhsSize>& lhs, const ArrayView<T, LhsSize>& rhs)
|
||||
{
|
||||
if (lhs.size() != rhs.size()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (lhs.data() == rhs.data()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return std::equal(lhs.data(), lhs.data() + lhs.size(), rhs.data());
|
||||
}
|
||||
|
||||
/**
|
||||
* Not equal operator
|
||||
*
|
||||
* @param lhs Left hand side of the binary operation.
|
||||
* @param rhs Right hand side of the binary operation.
|
||||
*
|
||||
* @return True if arrays in input do not have the same size or the same
|
||||
* content and false otherwise.
|
||||
*/
|
||||
template<typename T, ptrdiff_t LhsSize, ptrdiff_t RhsSize>
|
||||
bool operator!=(const ArrayView<T, LhsSize>& lhs, const ArrayView<T, LhsSize>& rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generate an array view from a reference to a C/C++ array.
|
||||
*
|
||||
|
@ -200,9 +333,28 @@ private:
|
|||
* created 'inline'.
|
||||
*/
|
||||
template<typename T, size_t Size>
|
||||
ArrayView<T> make_ArrayView(T (&elements)[Size])
|
||||
ArrayView<T, Size> make_ArrayView(T (&elements)[Size])
|
||||
{
|
||||
return ArrayView<T>(elements);
|
||||
return ArrayView<T, Size>(elements);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate an array view from a pointer to a C/C++ array.
|
||||
*
|
||||
* @tparam Size Number of items held in elements.
|
||||
* @tparam T Type of elements held in elements.
|
||||
*
|
||||
* @param elements The reference to the array viewed.
|
||||
*
|
||||
* @return The ArrayView to elements.
|
||||
*
|
||||
* @note This helper avoids the typing of template parameter when ArrayView is
|
||||
* created 'inline'.
|
||||
*/
|
||||
template<size_t Size, typename T>
|
||||
ArrayView<T, Size> make_ArrayView(T* elements)
|
||||
{
|
||||
return ArrayView<T, Size>(elements, Size);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -237,9 +389,28 @@ ArrayView<T> make_ArrayView(T* array_ptr, size_t array_size)
|
|||
* created 'inline'.
|
||||
*/
|
||||
template<typename T, size_t Size>
|
||||
ArrayView<const T> make_const_ArrayView(T (&elements)[Size])
|
||||
ArrayView<const T, Size> make_const_ArrayView(T (&elements)[Size])
|
||||
{
|
||||
return ArrayView<const T>(elements);
|
||||
return ArrayView<const T, Size>(elements);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a const array view from a pointer to a C/C++ array.
|
||||
*
|
||||
* @tparam Size Number of items held in elements.
|
||||
* @tparam T Type of elements held in elements.
|
||||
*
|
||||
* @param elements The reference to the array viewed.
|
||||
*
|
||||
* @return The ArrayView to elements.
|
||||
*
|
||||
* @note This helper avoids the typing of template parameter when ArrayView is
|
||||
* created 'inline'.
|
||||
*/
|
||||
template<size_t Size, typename T>
|
||||
ArrayView<const T, Size> make_const_ArrayView(const T* elements)
|
||||
{
|
||||
return ArrayView<const T, Size>(elements, Size);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue