Skip to content

Commit ded4a5e

Browse files
RaisinTenMoLow
authored andcommitted
src: remove usage of std::shared_ptr<T>::unique()
`std::shared_ptr<T>::unique()` has been removed in C++20, so this change uses `std::shared_ptr<T>::use_count()` instead which is available in C++20. Fixes: #47311 Signed-off-by: Darshan Sen <raisinten@gmail.com> PR-URL: #47315 Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Jiawen Geng <technicalcute@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent d3e0229 commit ded4a5e

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

src/node_threadsafe_cow-inl.h

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#ifndef SRC_NODE_THREADSAFE_COW_INL_H_
2+
#define SRC_NODE_THREADSAFE_COW_INL_H_
3+
4+
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5+
6+
namespace node {
7+
8+
template <typename T>
9+
T* CopyOnWrite<T>::write() {
10+
if (data_.use_count() > 1l) {
11+
data_ = std::make_shared<T>(*data_);
12+
}
13+
return data_.get();
14+
}
15+
16+
template <typename T>
17+
ThreadsafeCopyOnWrite<T>::Read::Read(const ThreadsafeCopyOnWrite<T>* cow)
18+
: cow_(cow), lock_(cow->impl_->mutex) {}
19+
20+
template <typename T>
21+
const T& ThreadsafeCopyOnWrite<T>::Read::operator*() const {
22+
return cow_->impl_->data;
23+
}
24+
25+
template <typename T>
26+
const T* ThreadsafeCopyOnWrite<T>::Read::operator->() const {
27+
return &cow_->impl_->data;
28+
}
29+
30+
template <typename T>
31+
ThreadsafeCopyOnWrite<T>::Write::Write(ThreadsafeCopyOnWrite<T>* cow)
32+
: cow_(cow), impl_(cow->impl_.write()), lock_(impl_->mutex) {}
33+
34+
template <typename T>
35+
T& ThreadsafeCopyOnWrite<T>::Write::operator*() {
36+
return impl_->data;
37+
}
38+
39+
template <typename T>
40+
T* ThreadsafeCopyOnWrite<T>::Write::operator->() {
41+
return &impl_->data;
42+
}
43+
44+
template <typename T>
45+
ThreadsafeCopyOnWrite<T>::Impl::Impl(const Impl& other) {
46+
RwLock::ScopedReadLock lock(other.mutex);
47+
data = other.data;
48+
}
49+
50+
} // namespace node
51+
52+
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
53+
54+
#endif // SRC_NODE_THREADSAFE_COW_INL_H_

0 commit comments

Comments
 (0)