Skip to content

Commit a3f094a

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

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

include/itlib/utility.hpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// itlib-utility v1.03
1+
// itlib-utility v1.04
22
//
33
// Utility functions to extend <utility>
44
//
@@ -28,6 +28,7 @@
2828
//
2929
// VERSION HISTORY
3030
//
31+
// 1.04 (2025-12-15) Added transfer_view_as
3132
// 1.03 (2025-12-15) Added transfer_view
3233
// 1.02 (2023-11-27) Added bit_cast
3334
// 1.01 (2023-02-08) Added make_nullptr
@@ -52,7 +53,7 @@
5253
// * bit_cast:
5354
// A function which allows you to reinterpret_cast between types of the
5455
// same size without UB. The same as C++20's std::bit_cast
55-
// * transfer_view:
56+
// * transfer_view/transfer_view_as:
5657
// A function which allows you to transfer a view (pointer+size) from one
5758
// container to another
5859
//
@@ -96,19 +97,28 @@ constexpr Dst bit_cast(const Src& src) noexcept {
9697
return dst;
9798
}
9899

99-
template <typename View, typename SrcContainer, typename TgtContainer>
100-
View transfer_view(
100+
template <typename TgtView, typename View, typename SrcContainer, typename TgtContainer>
101+
TgtView transfer_view_as(
101102
const View& srcView,
102103
const SrcContainer& srcContainer,
103104
TgtContainer& tgtContainer
104105
) {
105106
if (srcView.data() == nullptr) {
106-
return View{};
107+
return TgtView{};
107108
}
108-
return View(
109+
return TgtView(
109110
tgtContainer.data() + (srcView.data() - srcContainer.data()),
110111
srcView.size()
111112
);
112113
}
113114

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

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)