Skip to content

Commit 476e177

Browse files
committed
add unique_ref::to_unique_ptr()
1 parent f44d494 commit 476e177

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/utki/unique_ref.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,17 @@ class unique_ref
102102
utki::assert(this->p, SL);
103103
}
104104

105+
/**
106+
* @brief Move this unique_ref to unique_ptr.
107+
* This method leaves the unique_ref in "nullptr" state, so it can only be called
108+
* on r-value.
109+
* @return non-null std::unique_ptr.
110+
*/
111+
std::unique_ptr<object_type> to_unique_ptr() &&
112+
{
113+
return std::move(this->p);
114+
}
115+
105116
/**
106117
* @brief Get reference to the object.
107118
*

tests/unit/src/unique_ref.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,5 +200,17 @@ const tst::set set("unique_ref", [](tst::suite& suite) {
200200
tst::check(utki::contains(set, v2r), SL);
201201
tst::check(!utki::contains(set, v3r), SL);
202202
});
203+
204+
suite.add("to_unique_ptr", [](){
205+
constexpr auto str = "Hello world!";
206+
utki::unique_ref<std::string> us = utki::make_unique<std::string>(str);
207+
208+
tst::check_eq(us.get(), std::string(str), SL);
209+
210+
auto usptr = std::move(us).to_unique_ptr();
211+
212+
tst::check(usptr, SL);
213+
tst::check_eq(*usptr, std::string(str), SL);
214+
});
203215
});
204216
} // namespace

0 commit comments

Comments
 (0)