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);
37 prv_ =
new (std::nothrow) int32_t(2);
38 if (!prv_)
return ENOMEM;
42 NLIB_ASSERT(!ptr->prv_);
47 if (!prv_)
return false;
57 if (!prv_)
return true;
71 template<
class T,
class DEL = DefaultDelete<T> >
72 class LimitedSharedPtr {
76 explicit LimitedSharedPtr(nullptr_t)
NLIB_NOEXCEPT : ptr_(
nullptr) {}
80 #ifdef __cpp_rvalue_references 81 LimitedSharedPtr(LimitedSharedPtr&& rhs)
NLIB_NOEXCEPT : ptr_(rhs.ptr_),
82 status_(std::move(rhs.status_)) {
85 LimitedSharedPtr& operator=(LimitedSharedPtr&& rhs)
NLIB_NOEXCEPT {
86 if (!status_.ToUnshare()) DEL()(ptr_);
89 status_ = std::move(rhs.status_);
93 LimitedSharedPtr(LimitedSharedPtr& rhs, move_tag)
NLIB_NOEXCEPT 95 status_(rhs.status_, move_tag()) {
98 LimitedSharedPtr& assign(LimitedSharedPtr& rhs, move_tag)
NLIB_NOEXCEPT {
99 if (!status_.ToUnshare()) DEL()(ptr_);
102 status_.assign(rhs.status_, move_tag());
106 if (!status_.ToUnshare()) DEL()(ptr_);
110 NLIB_ASSERT(ptr_ !=
nullptr);
114 NLIB_ASSERT(ptr_ !=
nullptr);
119 bool IsUnique() const
NLIB_NOEXCEPT {
return status_.IsUnique(); }
129 if (!status_.ToUnshare()) DEL()(ptr_);
135 mutable internal::LspStatus status_;
139 template<
class T,
class DEL>
141 LimitedSharedPtr tmp;
143 errno_t e = p.status_.ToShare(&tmp.status_);
144 if (e != 0)
return e;
147 this->assign(tmp, move_tag());
151 template<
class T,
class DEL>
152 bool LimitedSharedPtr<T, DEL>::MakeUniquePtr(UniquePtr<T>& p)
NLIB_NOEXCEPT {
163 template<
class T,
class DEL>
164 class LimitedSharedPtr<T[], DEL> {
172 NLIB_DEFINE_STD_SWAP_T_BEGIN3(
nn, nlib, threading)
173 NLIB_DEFINE_STD_SWAP_T2(T, DEL, NLIB_NS::threading::LimitedSharedPtr)
174 NLIB_DEFINE_STD_SWAP_T_END3(
nn, nlib, threading)
176 #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.