aboutsummaryrefslogtreecommitdiff
path: root/contrib/epee/include/span.h
diff options
context:
space:
mode:
authorLee Clagett <code@leeclagett.com>2018-08-01 23:08:59 -0400
committerLee Clagett <code@leeclagett.com>2018-08-03 20:33:07 -0400
commit26a42fe54ae0a7e6f348c205cfc7d7b4bf3c012b (patch)
tree87cf9ebb7fc4fcd4428bd68fa9b9323cc4e6248c /contrib/epee/include/span.h
parentMerge pull request #4088 (diff)
downloadmonero-26a42fe54ae0a7e6f348c205cfc7d7b4bf3c012b.tar.xz
Added features to epee::span<T> :
- Support for classes - Added `remove_prefix` function - Added `to_mut_span` and `as_mut_byte_span`
Diffstat (limited to '')
-rw-r--r--contrib/epee/include/span.h46
1 files changed, 40 insertions, 6 deletions
diff --git a/contrib/epee/include/span.h b/contrib/epee/include/span.h
index 452cc088f..174915ecf 100644
--- a/contrib/epee/include/span.h
+++ b/contrib/epee/include/span.h
@@ -28,6 +28,7 @@
#pragma once
+#include <algorithm>
#include <cstdint>
#include <memory>
#include <type_traits>
@@ -52,11 +53,15 @@ namespace epee
template<typename T>
class span
{
- /* Supporting class types is tricky - the {ptr,len} constructor will allow
- derived-to-base conversions. This is NOT desireable because an array of
- derived types is not an array of base types. It is possible to handle
- this case, implement when/if needed. */
- static_assert(!std::is_class<T>(), "no class types are currently allowed");
+ template<typename U>
+ static constexpr bool safe_conversion() noexcept
+ {
+ // Allow exact matches or `T*` -> `const T*`.
+ using with_const = typename std::add_const<U>::type;
+ return std::is_same<T, U>() ||
+ (std::is_const<T>() && std::is_same<T, with_const>());
+ }
+
public:
using value_type = T;
using size_type = std::size_t;
@@ -71,7 +76,9 @@ namespace epee
constexpr span() noexcept : ptr(nullptr), len(0) {}
constexpr span(std::nullptr_t) noexcept : span() {}
- constexpr span(T* const src_ptr, const std::size_t count) noexcept
+ //! Prevent derived-to-base conversions; invalid in this context.
+ template<typename U, typename = typename std::enable_if<safe_conversion<U>()>::type>
+ constexpr span(U* const src_ptr, const std::size_t count) noexcept
: ptr(src_ptr), len(count) {}
//! Conversion from C-array. Prevents common bugs with sizeof + arrays.
@@ -81,6 +88,16 @@ namespace epee
constexpr span(const span&) noexcept = default;
span& operator=(const span&) noexcept = default;
+ /*! Try to remove `amount` elements from beginning of span.
+ \return Number of elements removed. */
+ std::size_t remove_prefix(std::size_t amount) noexcept
+ {
+ amount = std::min(len, amount);
+ ptr += amount;
+ len -= amount;
+ return amount;
+ }
+
constexpr iterator begin() const noexcept { return ptr; }
constexpr const_iterator cbegin() const noexcept { return ptr; }
@@ -105,6 +122,14 @@ namespace epee
return {src.data(), src.size()};
}
+ //! \return `span<T::value_type>` from a STL compatible `src`.
+ template<typename T>
+ constexpr span<typename T::value_type> to_mut_span(T& src)
+ {
+ // compiler provides diagnostic if size() is not size_t.
+ return {src.data(), src.size()};
+ }
+
template<typename T>
constexpr bool has_padding() noexcept
{
@@ -127,4 +152,13 @@ namespace epee
static_assert(!has_padding<T>(), "source type may have padding");
return {reinterpret_cast<const std::uint8_t*>(std::addressof(src)), sizeof(T)};
}
+
+ //! \return `span<std::uint8_t>` which represents the bytes at `&src`.
+ template<typename T>
+ span<std::uint8_t> as_mut_byte_span(T& src) noexcept
+ {
+ static_assert(!std::is_empty<T>(), "empty types will not work -> sizeof == 1");
+ static_assert(!has_padding<T>(), "source type may have padding");
+ return {reinterpret_cast<std::uint8_t*>(std::addressof(src)), sizeof(T)};
+ }
}