Skip to content

Commit 2401d1d

Browse files
committed
[ref_ptr]: _ref_ptr_from_shared_ptr_unsafe
1 parent 5635221 commit 2401d1d

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

include/itlib/ref_ptr.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
//
7373
// Future Ideas:
7474
// * void support
75+
// * static/const/dynamic casts
7576
// * strong_ref_ptr - a non-nullable ref_ptr variant
7677
//
7778
//
@@ -161,6 +162,9 @@ class ref_ptr : private std::shared_ptr<T> {
161162
std::shared_ptr<T> _as_shared_ptr_unsafe() && noexcept {
162163
return std::move(*this);
163164
}
165+
static ref_ptr<T> _from_shared_ptr_unsafe(std::shared_ptr<T> ptr) noexcept {
166+
return ref_ptr<T>(std::move(ptr));
167+
}
164168
};
165169

166170
template <typename T, typename... Args>
@@ -173,4 +177,9 @@ auto make_ref_ptr_from(T&& obj) -> ref_ptr<typename std::remove_reference<T>::ty
173177
return ref_ptr<typename std::remove_reference<T>::type>::make(std::forward<T>(obj));
174178
}
175179

180+
template <typename T>
181+
ref_ptr<T> _ref_ptr_from_shared_ptr_unsafe(std::shared_ptr<T> ptr) noexcept {
182+
return ref_ptr<T>::_from_shared_ptr_unsafe(std::move(ptr));
183+
}
184+
176185
} // namespace itlib

test/t-ref_ptr-11.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,13 @@ TEST_CASE("poly") {
9393
CHECK(a >= d);
9494
CHECK_FALSE(a < d);
9595
}
96+
97+
TEST_CASE("unsafe") {
98+
auto sp = std::make_shared<int>(55);
99+
auto rp = itlib::_ref_ptr_from_shared_ptr_unsafe(sp);
100+
CHECK(sp.get() == rp.get());
101+
CHECK_FALSE(rp.unique());
102+
auto sp2 = rp._as_shared_ptr_unsafe();
103+
CHECK(sp == sp2);
104+
CHECK(rp.use_count() == 3);
105+
}

0 commit comments

Comments
 (0)