16 #ifndef INCLUDE_NN_NLIB_THREADING_LIMITEDSHAREDPTR_H_ 17 #define INCLUDE_NN_NLIB_THREADING_LIMITEDSHAREDPTR_H_ 22 #include "nn/nlib/Swap.h" 34 NLIB_DEFMOVE_PIMPL(LspStatus);
41 prv_ =
new (std::nothrow) int32_t(2);
42 if (!prv_)
return ENOMEM;
46 NLIB_ASSERT(!ptr->prv_);
51 if (!prv_)
return false;
61 if (!prv_)
return true;
75 template <
class T,
class DEL = DefaultDelete<T> >
76 class LimitedSharedPtr {
80 explicit LimitedSharedPtr(nullptr_t)
NLIB_NOEXCEPT : ptr_(
nullptr) {}
84 #ifdef __cpp_rvalue_references 86 : ptr_(rhs.ptr_), status_(std::move(rhs.status_)) {
89 LimitedSharedPtr& operator=(LimitedSharedPtr&& rhs)
NLIB_NOEXCEPT {
90 if (!status_.ToUnshare()) DEL()(ptr_);
93 status_ = std::move(rhs.status_);
97 LimitedSharedPtr(LimitedSharedPtr& rhs, move_tag)
NLIB_NOEXCEPT 98 : ptr_(rhs.ptr_), status_(rhs.status_, move_tag()) {
101 LimitedSharedPtr& assign(LimitedSharedPtr& rhs, move_tag)
NLIB_NOEXCEPT {
102 if (!status_.ToUnshare()) DEL()(ptr_);
105 status_.assign(rhs.status_, move_tag());
110 swap(ptr_, rhs.ptr_);
111 swap(status_, rhs.status_);
114 if (!status_.ToUnshare()) DEL()(ptr_);
118 NLIB_ASSERT(ptr_ !=
nullptr);
122 NLIB_ASSERT(ptr_ !=
nullptr);
127 bool IsUnique() const NLIB_NOEXCEPT {
return status_.IsUnique(); }
129 LimitedSharedPtr& operator=(nullptr_t) NLIB_NOEXCEPT {
136 void release() NLIB_NOEXCEPT {
137 if (!status_.ToUnshare()) DEL()(ptr_);
143 mutable internal::LspStatus status_;
147 template <
class T,
class DEL>
148 errno_t LimitedSharedPtr<T, DEL>::MakeSharedFrom(
const LimitedSharedPtr& p)
150 LimitedSharedPtr tmp;
152 errno_t e = p.status_.ToShare(&tmp.status_);
153 if (e != 0)
return e;
156 this->assign(tmp, move_tag());
160 template <
class T,
class DEL>
161 bool LimitedSharedPtr<T, DEL>::MakeUniquePtr(UniquePtr<T>& p) NLIB_NOEXCEPT {
172 template <
class T,
class DEL>
173 class LimitedSharedPtr<T[], DEL> {
181 NLIB_DEFINE_STD_SWAP_T_BEGIN3(
nn, nlib, threading)
182 NLIB_DEFINE_STD_SWAP_T2(T, DEL, NLIB_NS::threading::LimitedSharedPtr)
183 NLIB_DEFINE_STD_SWAP_T_END3(
nn, nlib, threading)
185 #endif // INCLUDE_NN_NLIB_THREADING_LIMITEDSHAREDPTR_H_
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
#define NLIB_SAFE_BOOL(class_name, exp)
Defines a safe operator bool function in the class. Uses the C++11 explicit bool if it is available f...
Defines that class that is corresponding to std::unique_ptr.
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
TimeSpan operator*(int i, const TimeSpan &rhs) noexcept
Increases rhs by a factor of i.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.