summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2020-10-24 19:57:51 +0300
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2020-10-26 20:39:08 +0200
commit5aabf85d2c2075f912b6ffb6f90ff9583f20bab1 (patch)
tree5736c15234b04a02d73a5092920b2a0f0909d363 /include
parent5e738eb4d52a694eff808cd075bb49115e974d07 (diff)
libcamera: span: Make constructors explicit as required by C++20
The C++20 std::span class, after which Span is modelled, specifies four constructors as explicit when extent is not dynamic_extent. Align our implementation with those requirements. A careful reviewer may notice that this change addresses five constructors, not four. The reason is that the two constructors taking Container and const Container parameters are not specified in C++20, which uses a single constructor taking a range parameter instead. As ranges are not available in C++17, the Container constructors are our best effort at providing a similar feature. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
Diffstat (limited to 'include')
-rw-r--r--include/libcamera/span.h74
1 files changed, 43 insertions, 31 deletions
diff --git a/include/libcamera/span.h b/include/libcamera/span.h
index c0e43992..91e9f974 100644
--- a/include/libcamera/span.h
+++ b/include/libcamera/span.h
@@ -113,12 +113,12 @@ public:
{
}
- constexpr Span(pointer ptr, [[maybe_unused]] size_type count)
+ explicit constexpr Span(pointer ptr, [[maybe_unused]] size_type count)
: data_(ptr)
{
}
- constexpr Span(pointer first, [[maybe_unused]] pointer last)
+ explicit constexpr Span(pointer first, [[maybe_unused]] pointer last)
: data_(first)
{
}
@@ -154,35 +154,35 @@ public:
}
template<class Container>
- constexpr Span(Container &cont,
- std::enable_if_t<!details::is_span<Container>::value &&
- !details::is_array<Container>::value &&
- !std::is_array<Container>::value &&
- std::is_convertible<std::remove_pointer_t<decltype(utils::data(cont))> (*)[],
- element_type (*)[]>::value,
- std::nullptr_t> = nullptr)
+ explicit constexpr Span(Container &cont,
+ std::enable_if_t<!details::is_span<Container>::value &&
+ !details::is_array<Container>::value &&
+ !std::is_array<Container>::value &&
+ std::is_convertible<std::remove_pointer_t<decltype(utils::data(cont))> (*)[],
+ element_type (*)[]>::value,
+ std::nullptr_t> = nullptr)
: data_(utils::data(cont))
{
}
template<class Container>
- constexpr Span(const Container &cont,
- std::enable_if_t<!details::is_span<Container>::value &&
- !details::is_array<Container>::value &&
- !std::is_array<Container>::value &&
- std::is_convertible<std::remove_pointer_t<decltype(utils::data(cont))> (*)[],
- element_type (*)[]>::value,
- std::nullptr_t> = nullptr)
+ explicit constexpr Span(const Container &cont,
+ std::enable_if_t<!details::is_span<Container>::value &&
+ !details::is_array<Container>::value &&
+ !std::is_array<Container>::value &&
+ std::is_convertible<std::remove_pointer_t<decltype(utils::data(cont))> (*)[],
+ element_type (*)[]>::value,
+ std::nullptr_t> = nullptr)
: data_(utils::data(cont))
{
static_assert(utils::size(cont) == Extent, "Size mismatch");
}
template<class U, std::size_t N>
- constexpr Span(const Span<U, N> &s,
- std::enable_if_t<std::is_convertible<U (*)[], element_type (*)[]>::value &&
- N == Extent,
- std::nullptr_t> = nullptr) noexcept
+ explicit constexpr Span(const Span<U, N> &s,
+ std::enable_if_t<std::is_convertible<U (*)[], element_type (*)[]>::value &&
+ N == Extent,
+ std::nullptr_t> = nullptr) noexcept
: data_(s.data())
{
}
@@ -212,24 +212,24 @@ public:
constexpr Span<element_type, Count> first() const
{
static_assert(Count <= Extent, "Count larger than size");
- return { data(), Count };
+ return Span<element_type, Count>{ data(), Count };
}
constexpr Span<element_type, dynamic_extent> first(std::size_t Count) const
{
- return { data(), Count };
+ return Span<element_type, dynamic_extent>{ data(), Count };
}
template<std::size_t Count>
constexpr Span<element_type, Count> last() const
{
static_assert(Count <= Extent, "Count larger than size");
- return { data() + size() - Count, Count };
+ return Span<element_type, Count>{ data() + size() - Count, Count };
}
constexpr Span<element_type, dynamic_extent> last(std::size_t Count) const
{
- return { data() + size() - Count, Count };
+ return Span<element_type, dynamic_extent>{ data() + size() - Count, Count };
}
template<std::size_t Offset, std::size_t Count = dynamic_extent>
@@ -238,13 +238,19 @@ public:
static_assert(Offset <= Extent, "Offset larger than size");
static_assert(Count == dynamic_extent || Count + Offset <= Extent,
"Offset + Count larger than size");
- return { data() + Offset, Count == dynamic_extent ? size() - Offset : Count };
+ return Span<element_type, Count != dynamic_extent ? Count : Extent - Offset>{
+ data() + Offset,
+ Count == dynamic_extent ? size() - Offset : Count
+ };
}
constexpr Span<element_type, dynamic_extent>
subspan(std::size_t Offset, std::size_t Count = dynamic_extent) const
{
- return { data() + Offset, Count == dynamic_extent ? size() - Offset : Count };
+ return Span<element_type, dynamic_extent>{
+ data() + Offset,
+ Count == dynamic_extent ? size() - Offset : Count
+ };
}
private:
@@ -371,7 +377,7 @@ public:
template<std::size_t Count>
constexpr Span<element_type, Count> first() const
{
- return { data(), Count };
+ return Span<element_type, Count>{ data(), Count };
}
constexpr Span<element_type, dynamic_extent> first(std::size_t Count) const
@@ -382,24 +388,30 @@ public:
template<std::size_t Count>
constexpr Span<element_type, Count> last() const
{
- return { data() + size() - Count, Count };
+ return Span<element_type, Count>{ data() + size() - Count, Count };
}
constexpr Span<element_type, dynamic_extent> last(std::size_t Count) const
{
- return { data() + size() - Count, Count };
+ return Span<element_type, dynamic_extent>{ data() + size() - Count, Count };
}
template<std::size_t Offset, std::size_t Count = dynamic_extent>
constexpr Span<element_type, Count> subspan() const
{
- return { data() + Offset, Count == dynamic_extent ? size() - Offset : Count };
+ return Span<element_type, Count>{
+ data() + Offset,
+ Count == dynamic_extent ? size() - Offset : Count
+ };
}
constexpr Span<element_type, dynamic_extent>
subspan(std::size_t Offset, std::size_t Count = dynamic_extent) const
{
- return { data() + Offset, Count == dynamic_extent ? size() - Offset : Count };
+ return Span<element_type, dynamic_extent>{
+ data() + Offset,
+ Count == dynamic_extent ? size() - Offset : Count
+ };
}
private: