Add Slice: a (pointer, size) array view that acts like a container
This commit is contained in:
parent
bfaed1ab2e
commit
833bc08583
2 changed files with 41 additions and 0 deletions
|
@ -312,6 +312,7 @@ libbitcoin_consensus_a_SOURCES = \
|
||||||
script/script_error.cpp \
|
script/script_error.cpp \
|
||||||
script/script_error.h \
|
script/script_error.h \
|
||||||
serialize.h \
|
serialize.h \
|
||||||
|
span.h \
|
||||||
tinyformat.h \
|
tinyformat.h \
|
||||||
uint256.cpp \
|
uint256.cpp \
|
||||||
uint256.h \
|
uint256.h \
|
||||||
|
|
40
src/span.h
Normal file
40
src/span.h
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
// Copyright (c) 2018 The Bitcoin Core developers
|
||||||
|
// Distributed under the MIT software license, see the accompanying
|
||||||
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
|
#ifndef BITCOIN_SPAN_H
|
||||||
|
#define BITCOIN_SPAN_H
|
||||||
|
|
||||||
|
#include <type_traits>
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
|
/** A Span is an object that can refer to a contiguous sequence of objects.
|
||||||
|
*
|
||||||
|
* It implements a subset of C++20's std::span.
|
||||||
|
*/
|
||||||
|
template<typename C>
|
||||||
|
class Span
|
||||||
|
{
|
||||||
|
C* m_data;
|
||||||
|
std::ptrdiff_t m_size;
|
||||||
|
|
||||||
|
public:
|
||||||
|
constexpr Span() noexcept : m_data(nullptr), m_size(0) {}
|
||||||
|
constexpr Span(C* data, std::ptrdiff_t size) noexcept : m_data(data), m_size(size) {}
|
||||||
|
|
||||||
|
constexpr C* data() const noexcept { return m_data; }
|
||||||
|
constexpr std::ptrdiff_t size() const noexcept { return m_size; }
|
||||||
|
};
|
||||||
|
|
||||||
|
/** Create a span to a container exposing data() and size().
|
||||||
|
*
|
||||||
|
* This correctly deals with constness: the returned Span's element type will be
|
||||||
|
* whatever data() returns a pointer to. If either the passed container is const,
|
||||||
|
* or its element type is const, the resulting span will have a const element type.
|
||||||
|
*
|
||||||
|
* std::span will have a constructor that implements this functionality directly.
|
||||||
|
*/
|
||||||
|
template<typename V>
|
||||||
|
constexpr Span<typename std::remove_pointer<decltype(std::declval<V>().data())>::type> MakeSpan(V& v) { return Span<typename std::remove_pointer<decltype(std::declval<V>().data())>::type>(v.data(), v.size()); }
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in a new issue