src: introduce raw_index_bytes()
This commit is contained in:
+10
-1
@@ -40,6 +40,9 @@
|
||||
template <class T>
|
||||
class BoundedPtr {
|
||||
public:
|
||||
typedef T element_type;
|
||||
typedef typename std::add_pointer<T>::type pointer;
|
||||
|
||||
~BoundedPtr() {}
|
||||
|
||||
BoundedPtr(void *base, size_t size_in_bytes, T *ptr = nullptr)
|
||||
@@ -137,9 +140,15 @@ private:
|
||||
|
||||
// raw_bytes overload
|
||||
template <class T>
|
||||
inline T *raw_bytes(const BoundedPtr<T> &a, size_t size_in_bytes) {
|
||||
inline typename BoundedPtr<T>::pointer raw_bytes(const BoundedPtr<T> &a, size_t size_in_bytes) {
|
||||
return a.raw_bytes(size_in_bytes);
|
||||
}
|
||||
template <class T>
|
||||
inline typename BoundedPtr<T>::pointer raw_index_bytes(const BoundedPtr<T> &a, size_t index,
|
||||
size_t size_in_bytes) {
|
||||
typedef typename BoundedPtr<T>::element_type element_type;
|
||||
return raw_bytes(a, mem_size(sizeof(element_type), index, size_in_bytes)) + index;
|
||||
}
|
||||
|
||||
#endif /* already included */
|
||||
|
||||
|
||||
@@ -128,6 +128,12 @@ inline typename MemBufferBase<T>::pointer raw_bytes(const MemBufferBase<T> &mbb,
|
||||
size_t size_in_bytes) {
|
||||
return mbb.raw_bytes(size_in_bytes);
|
||||
}
|
||||
template <class T>
|
||||
inline typename MemBufferBase<T>::pointer raw_index_bytes(const MemBufferBase<T> &mbb, size_t index,
|
||||
size_t size_in_bytes) {
|
||||
typedef typename MemBufferBase<T>::element_type element_type;
|
||||
return raw_bytes(mbb, mem_size(sizeof(element_type), index, size_in_bytes)) + index;
|
||||
}
|
||||
|
||||
// global operators
|
||||
// rewrite "n + membuffer" to "membuffer + n" so that this will get checked above
|
||||
|
||||
@@ -96,6 +96,15 @@ TEST_CASE("basic xspan usage") {
|
||||
CHECK(cs == bp);
|
||||
CHECK(x0 == z0p);
|
||||
CHECK(xp == z0s);
|
||||
|
||||
CHECK(raw_bytes(c0, 4) == buf);
|
||||
CHECK(raw_index_bytes(c0, 1, 3) == buf + 1);
|
||||
CHECK(raw_bytes(cp, 4) == buf);
|
||||
CHECK(raw_index_bytes(cp, 1, 3) == buf + 1);
|
||||
CHECK(raw_bytes(cs, 4) == buf);
|
||||
CHECK(raw_index_bytes(cs, 1, 3) == buf + 1);
|
||||
CHECK_THROWS(raw_bytes(cs, 5));
|
||||
CHECK_THROWS(raw_index_bytes(cs, 1, 4));
|
||||
}
|
||||
|
||||
SUBCASE("SPAN_x_VAR") {
|
||||
@@ -127,6 +136,15 @@ TEST_CASE("basic xspan usage") {
|
||||
CHECK(d0 == ds);
|
||||
CHECK(x0 == z0p);
|
||||
CHECK(xp == z0s);
|
||||
|
||||
CHECK(raw_bytes(c0, 4) == buf);
|
||||
CHECK(raw_index_bytes(c0, 1, 3) == buf + 1);
|
||||
CHECK(raw_bytes(cp, 4) == buf);
|
||||
CHECK(raw_index_bytes(cp, 1, 3) == buf + 1);
|
||||
CHECK(raw_bytes(cs, 4) == buf);
|
||||
CHECK(raw_index_bytes(cs, 1, 3) == buf + 1);
|
||||
CHECK_THROWS(raw_bytes(cs, 5));
|
||||
CHECK_THROWS(raw_index_bytes(cs, 1, 4));
|
||||
}
|
||||
|
||||
SUBCASE("xspan in class") {
|
||||
|
||||
+2
-1
@@ -60,7 +60,8 @@ using SPAN_NAMESPACE_NAME::PtrOrSpan;
|
||||
using SPAN_NAMESPACE_NAME::PtrOrSpanOrNull;
|
||||
using SPAN_NAMESPACE_NAME::Span;
|
||||
// util
|
||||
using SPAN_NAMESPACE_NAME::raw_bytes; // overloaded for all classes
|
||||
using SPAN_NAMESPACE_NAME::raw_bytes; // overloaded for all classes
|
||||
using SPAN_NAMESPACE_NAME::raw_index_bytes; // overloaded for all classes
|
||||
#endif
|
||||
|
||||
#endif // WITH_SPAN
|
||||
|
||||
@@ -191,16 +191,6 @@ struct Span;
|
||||
template <class T>
|
||||
struct Ptr;
|
||||
|
||||
template <class T>
|
||||
inline typename PtrOrSpanOrNull<T>::pointer raw_bytes(const PtrOrSpanOrNull<T> &a,
|
||||
size_t size_in_bytes);
|
||||
template <class T>
|
||||
inline typename PtrOrSpan<T>::pointer raw_bytes(const PtrOrSpan<T> &a, size_t size_in_bytes);
|
||||
template <class T>
|
||||
inline typename Span<T>::pointer raw_bytes(const Span<T> &a, size_t size_in_bytes);
|
||||
template <class T>
|
||||
inline typename Ptr<T>::pointer raw_bytes(const Ptr<T> &a, size_t size_in_bytes);
|
||||
|
||||
class SpanInternalDummyArg; // not implemented
|
||||
|
||||
SPAN_NAMESPACE_END
|
||||
|
||||
@@ -196,6 +196,12 @@ template <class T>
|
||||
inline typename Ptr<T>::pointer raw_bytes(const Ptr<T> &a, size_t size_in_bytes) {
|
||||
return a.raw_bytes(size_in_bytes);
|
||||
}
|
||||
template <class T>
|
||||
inline typename Ptr<T>::pointer raw_index_bytes(const Ptr<T> &a, size_t index,
|
||||
size_t size_in_bytes) {
|
||||
typedef typename Ptr<T>::element_type element_type;
|
||||
return raw_bytes(a, mem_size(sizeof(element_type), index, size_in_bytes)) + index;
|
||||
}
|
||||
|
||||
/*************************************************************************
|
||||
//
|
||||
|
||||
@@ -96,6 +96,12 @@ inline typename PtrOrSpanOrNull<T>::pointer raw_bytes(const PtrOrSpanOrNull<T> &
|
||||
size_t size_in_bytes) {
|
||||
return a.raw_bytes(size_in_bytes);
|
||||
}
|
||||
template <class T>
|
||||
inline typename PtrOrSpanOrNull<T>::pointer raw_index_bytes(const PtrOrSpanOrNull<T> &a,
|
||||
size_t index, size_t size_in_bytes) {
|
||||
typedef typename PtrOrSpanOrNull<T>::element_type element_type;
|
||||
return raw_bytes(a, mem_size(sizeof(element_type), index, size_in_bytes)) + index;
|
||||
}
|
||||
|
||||
/*************************************************************************
|
||||
//
|
||||
|
||||
@@ -122,6 +122,12 @@ template <class T>
|
||||
inline typename PtrOrSpan<T>::pointer raw_bytes(const PtrOrSpan<T> &a, size_t size_in_bytes) {
|
||||
return a.raw_bytes(size_in_bytes);
|
||||
}
|
||||
template <class T>
|
||||
inline typename PtrOrSpan<T>::pointer raw_index_bytes(const PtrOrSpan<T> &a, size_t index,
|
||||
size_t size_in_bytes) {
|
||||
typedef typename PtrOrSpan<T>::element_type element_type;
|
||||
return raw_bytes(a, mem_size(sizeof(element_type), index, size_in_bytes)) + index;
|
||||
}
|
||||
|
||||
/*************************************************************************
|
||||
//
|
||||
|
||||
@@ -122,6 +122,12 @@ template <class T>
|
||||
inline typename Span<T>::pointer raw_bytes(const Span<T> &a, size_t size_in_bytes) {
|
||||
return a.raw_bytes(size_in_bytes);
|
||||
}
|
||||
template <class T>
|
||||
inline typename Span<T>::pointer raw_index_bytes(const Span<T> &a, size_t index,
|
||||
size_t size_in_bytes) {
|
||||
typedef typename Span<T>::element_type element_type;
|
||||
return raw_bytes(a, mem_size(sizeof(element_type), index, size_in_bytes)) + index;
|
||||
}
|
||||
|
||||
/*************************************************************************
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user