Added features to epee::span<T> :

- Support for classes
  - Added `remove_prefix` function
  - Added `to_mut_span` and `as_mut_byte_span`
This commit is contained in:
Lee Clagett 2018-08-01 23:08:59 -04:00
parent 702a41034d
commit 26a42fe54a
2 changed files with 96 additions and 7 deletions

View File

@ -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)};
}
}

View File

@ -166,12 +166,17 @@ TEST(Span, Traits)
TEST(Span, MutableConstruction)
{
struct no_conversion{};
struct inherited : no_conversion {};
EXPECT_TRUE(std::is_constructible<epee::span<char>>());
EXPECT_TRUE((std::is_constructible<epee::span<char>, char*, std::size_t>()));
EXPECT_FALSE((std::is_constructible<epee::span<char>, const char*, std::size_t>()));
EXPECT_FALSE((std::is_constructible<epee::span<char>, unsigned char*, std::size_t>()));
EXPECT_TRUE(std::is_constructible<epee::span<no_conversion>>());
EXPECT_TRUE((std::is_constructible<epee::span<no_conversion>, no_conversion*, std::size_t>()));
EXPECT_FALSE((std::is_constructible<epee::span<no_conversion>, inherited*, std::size_t>()));
EXPECT_TRUE((can_construct<epee::span<char>, std::nullptr_t>()));
EXPECT_TRUE((can_construct<epee::span<char>, char(&)[1]>()));
@ -193,12 +198,19 @@ TEST(Span, MutableConstruction)
TEST(Span, ImmutableConstruction)
{
struct no_conversion{};
struct inherited : no_conversion {};
EXPECT_TRUE(std::is_constructible<epee::span<const char>>());
EXPECT_TRUE((std::is_constructible<epee::span<const char>, char*, std::size_t>()));
EXPECT_TRUE((std::is_constructible<epee::span<const char>, const char*, std::size_t>()));
EXPECT_FALSE((std::is_constructible<epee::span<const char>, unsigned char*, std::size_t>()));
EXPECT_TRUE(std::is_constructible<epee::span<const no_conversion>>());
EXPECT_TRUE((std::is_constructible<epee::span<const no_conversion>, const no_conversion*, std::size_t>()));
EXPECT_TRUE((std::is_constructible<epee::span<const no_conversion>, no_conversion*, std::size_t>()));
EXPECT_FALSE((std::is_constructible<epee::span<const no_conversion>, const inherited*, std::size_t>()));
EXPECT_FALSE((std::is_constructible<epee::span<const no_conversion>, inherited*, std::size_t>()));
EXPECT_FALSE((can_construct<epee::span<const char>, std::string>()));
EXPECT_FALSE((can_construct<epee::span<const char>, std::vector<char>>()));
EXPECT_FALSE((can_construct<epee::span<const char>, const std::vector<char>>()));
@ -231,7 +243,6 @@ TEST(Span, NoExcept)
const epee::span<char> clvalue(data);
EXPECT_TRUE(noexcept(epee::span<char>()));
EXPECT_TRUE(noexcept(epee::span<char>(nullptr)));
EXPECT_TRUE(noexcept(epee::span<char>(nullptr, 0)));
EXPECT_TRUE(noexcept(epee::span<char>(data)));
EXPECT_TRUE(noexcept(epee::span<char>(lvalue)));
EXPECT_TRUE(noexcept(epee::span<char>(clvalue)));
@ -284,6 +295,25 @@ TEST(Span, Writing)
EXPECT_TRUE(boost::range::equal(expected, span));
}
TEST(Span, RemovePrefix)
{
const std::array<unsigned, 4> expected{0, 1, 2, 3};
auto span = epee::to_span(expected);
EXPECT_EQ(expected.begin(), span.begin());
EXPECT_EQ(expected.end(), span.end());
EXPECT_EQ(2u, span.remove_prefix(2));
EXPECT_EQ(expected.begin() + 2, span.begin());
EXPECT_EQ(expected.end(), span.end());
EXPECT_EQ(2u, span.remove_prefix(3));
EXPECT_EQ(span.begin(), span.end());
EXPECT_EQ(expected.end(), span.begin());
EXPECT_EQ(0u, span.remove_prefix(100));
}
TEST(Span, ToByteSpan)
{
const char expected[] = {56, 44, 11, 5};
@ -318,6 +348,30 @@ TEST(Span, AsByteSpan)
);
}
TEST(Span, AsMutByteSpan)
{
struct some_pod { char value[4]; };
some_pod actual {};
auto span = epee::as_mut_byte_span(actual);
boost::range::iota(span, 1);
EXPECT_TRUE(
boost::range::equal(
std::array<unsigned char, 4>{{1, 2, 3, 4}}, actual.value
)
);
}
TEST(Span, ToMutSpan)
{
std::vector<unsigned> mut;
mut.resize(4);
auto span = epee::to_mut_span(mut);
boost::range::iota(span, 1);
EXPECT_EQ((std::vector<unsigned>{1, 2, 3, 4}), mut);
}
TEST(ToHex, String)
{
EXPECT_TRUE(epee::to_hex::string(nullptr).empty());
@ -330,6 +384,7 @@ TEST(ToHex, String)
EXPECT_EQ(
std_to_hex(all_bytes), epee::to_hex::string(epee::to_span(all_bytes))
);
}
TEST(ToHex, Array)