3 #ifndef INCLUDE_NN_NLIB_THREADING_LIMITEDSHAREDPTR_H_ 4 #define INCLUDE_NN_NLIB_THREADING_LIMITEDSHAREDPTR_H_ 8 #include "nn/nlib/Swap.h" 20 NLIB_MOVE_MEMBER_HELPER_1(LspStatus, value_)
23 value_ =
new (std::nothrow) int32_t(2);
24 if (!value_)
return ENOMEM;
28 NLIB_ASSERT(!ptr->value_);
32 bool ToUnshare() NLIB_NOEXCEPT {
33 if (!value_)
return false;
42 bool IsUnique() NLIB_NOEXCEPT {
43 if (!value_)
return true;
46 void swap(LspStatus& rhs) NLIB_NOEXCEPT {
48 swap(value_, rhs.value_);
60 template <
class T,
class DEL = DefaultDelete<T> >
61 class LimitedSharedPtr {
63 LimitedSharedPtr() NLIB_NOEXCEPT : ptr_(NULL) {}
64 ~LimitedSharedPtr() NLIB_NOEXCEPT { release(); }
65 explicit LimitedSharedPtr(nullptr_t) NLIB_NOEXCEPT : ptr_(NULL) {}
66 explicit LimitedSharedPtr(T* p) NLIB_NOEXCEPT : ptr_(p) {
69 NLIB_MOVE_MEMBER_HELPER_2(LimitedSharedPtr, ptr_, status_)
70 void reset(T* p = 0) NLIB_NOEXCEPT { LimitedSharedPtr<T>(p).swap(*
this); }
72 NLIB_ASSERT(ptr_ != NULL);
75 T* operator->() const NLIB_NOEXCEPT {
76 NLIB_ASSERT(ptr_ != NULL);
79 T*
get()
const NLIB_NOEXCEPT {
return ptr_; }
81 bool IsUnique() const NLIB_NOEXCEPT {
return status_.IsUnique(); }
83 void swap(LimitedSharedPtr& rhs) NLIB_NOEXCEPT {
86 status_.swap(rhs.status_);
88 LimitedSharedPtr& operator=(nullptr_t) NLIB_NOEXCEPT {
95 void release() NLIB_NOEXCEPT {
96 if (!status_.ToUnshare()) DEL()(ptr_);
102 mutable internal::LspStatus status_;
106 template <
class T,
class DEL>
107 errno_t LimitedSharedPtr<T, DEL>::MakeSharedFrom(
const LimitedSharedPtr& p)
109 LimitedSharedPtr tmp;
111 errno_t e = p.status_.ToShare(&tmp.status_);
112 if (e != 0)
return e;
119 template <
class T,
class DEL>
120 bool LimitedSharedPtr<T, DEL>::MakeUniquePtr(UniquePtr<T>& p) NLIB_NOEXCEPT {
131 template <
class T,
class DEL>
132 class LimitedSharedPtr<T[], DEL> {
140 #ifndef NLIB_STD_SWAP_WORKAROUND 144 NLIB_DEFINE_STD_SWAP_T_BEGIN1(threading)
145 NLIB_DEFINE_STD_SWAP_T2(T, DEL, NLIB_NS::threading::LimitedSharedPtr)
146 NLIB_DEFINE_STD_SWAP_T_END1(threading)
148 #ifndef NLIB_STD_SWAP_WORKAROUND 152 #endif // INCLUDE_NN_NLIB_THREADING_LIMITEDSHAREDPTR_H_
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
#define NLIB_SAFE_BOOL(class_name, exp)
クラス内に安全なoperator bool()を定義します。 可能であればC++11のexplicit boolを利用します。 ...
std::unique_ptrに相当するクラスが定義されています。
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
#define NLIB_CEXPR
利用可能であればconstexprが定義されます。そうでない場合は空文字列です。
TimeSpan operator*(int i, const TimeSpan &rhs) noexcept
rhs を i 倍します。