Skip to content

Commit 60e4b29

Browse files
committed
[utility] add transfer_view_as
1 parent 4eadbe0 commit 60e4b29

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

include/itlib/utility.hpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
//
2929
// VERSION HISTORY
3030
//
31-
// 1.03 (2025-12-15) Added transfer_view
31+
// 1.03 (2025-12-15) Added transfer_view/transfer_view_as
3232
// 1.02 (2023-11-27) Added bit_cast
3333
// 1.01 (2023-02-08) Added make_nullptr
3434
// 1.00 (2022-11-28) Initial release
@@ -52,7 +52,7 @@
5252
// * bit_cast:
5353
// A function which allows you to reinterpret_cast between types of the
5454
// same size without UB. The same as C++20's std::bit_cast
55-
// * transfer_view:
55+
// * transfer_view/transfer_view_as:
5656
// A function which allows you to transfer a view (pointer+size) from one
5757
// container to another
5858
//
@@ -96,19 +96,28 @@ constexpr Dst bit_cast(const Src& src) noexcept {
9696
return dst;
9797
}
9898

99-
template <typename View, typename SrcContainer, typename TgtContainer>
100-
View transfer_view(
99+
template <typename TgtView, typename View, typename SrcContainer, typename TgtContainer>
100+
TgtView transfer_view_as(
101101
const View& srcView,
102102
const SrcContainer& srcContainer,
103103
TgtContainer& tgtContainer
104104
) {
105105
if (srcView.data() == nullptr) {
106-
return View{};
106+
return TgtView{};
107107
}
108-
return View(
108+
return TgtView(
109109
tgtContainer.data() + (srcView.data() - srcContainer.data()),
110110
srcView.size()
111111
);
112112
}
113113

114+
template <typename View, typename SrcContainer, typename TgtContainer>
115+
View transfer_view(
116+
const View& srcView,
117+
const SrcContainer& srcContainer,
118+
TgtContainer& tgtContainer
119+
) {
120+
return transfer_view_as<View>(srcView, srcContainer, tgtContainer);
121+
}
122+
114123
}

test/t-utility-11.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,15 @@ TEST_CASE("transfer_view") {
6262
CHECK(vb.size() == 3);
6363
CHECK(vb.data() == b.data() + 3);
6464

65-
vb[0] = 100;
66-
6765
auto cvb = itlib::transfer_view(cva28, a, b);
6866
CHECK(cvb.size() == 6);
6967
CHECK(cvb.data() == b.data() + 2);
7068

69+
auto vb2 = itlib::transfer_view_as<itlib::span<int>>(cva28, a, b);
70+
71+
vb[0] = 100;
7172
CHECK(cvb[1] == 100);
73+
74+
vb2[2] = 200;
75+
CHECK(cvb[2] == 200);
7276
}

test/t-utility-20.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <doctest/doctest.h>
66
#include <vector>
77
#include <span>
8+
#include <string>
9+
#include <string_view>
810

911
TEST_CASE("transfer std::span") {
1012
std::vector<int> a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
@@ -25,3 +27,13 @@ TEST_CASE("transfer std::span") {
2527

2628
CHECK(cvb[1] == 100);
2729
}
30+
31+
TEST_CASE("transfer_view_as") {
32+
std::string a = "hello world!!";
33+
34+
auto hello = std::string_view(a).substr(0, 5);
35+
auto hello_span = itlib::transfer_view_as<std::span<char>>(hello, a, a);
36+
37+
hello_span[0] = 'H';
38+
CHECK(hello == "Hello");
39+
}

0 commit comments

Comments
 (0)