Skip to content

Commit f72f128

Browse files
committed
SafePtr: practise in swm
1 parent c60d85b commit f72f128

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

src/msg_self/MsgSelf.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
#include "UniLog.hpp"
4646

47-
#define MSG_SELF (ObjAnywhere::get<MsgSelf>(*this).get())
47+
#define MSG_SELF (ObjAnywhere::get<MsgSelf>(*this))
4848

4949
using namespace std;
5050

src/safe_mem/SafePtr.hpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ class SafePtr
7474
// . no operator*() since T& is unsafe
7575
shared_ptr<T> get() const noexcept { return pT_; }
7676
shared_ptr<T> operator->() const noexcept { return pT_; } // convenient
77-
auto use_count() const noexcept { return pT_.use_count(); }
77+
explicit operator bool() const noexcept { return pT_ != nullptr; }
78+
auto use_count() const noexcept { return pT_.use_count(); }
7879

7980
// most for debug
8081
auto realType() const noexcept { return realType_; } // ret cp is safer than ref
@@ -190,7 +191,7 @@ template<typename U, typename... ConstructArgs>
190191
SafePtr<U> make_safe(ConstructArgs&&... aArgs)
191192
{
192193
SafePtr<U> safeU;
193-
safeU.pT_ = make_shared<U>(forward<ConstructArgs>(aArgs)...);
194+
safeU.pT_ = std::make_shared<U>(std::forward<ConstructArgs>(aArgs)...); // std::~, or ambiguous with boost::~
194195
//HID("new ptr=" << (void*)(safeU.pT_.get())); // too many print; void* print addr rather than content(dangeous)
195196
return safeU;
196197
}
@@ -241,6 +242,12 @@ struct std::hash<RLib::SafePtr<T>>
241242
// . SafePtr is simple
242243
// . freq cast void->any is dangeous
243244
// . so worth
245+
// . get()/etc ret shared_ptr<T>, safe?
246+
// . safer than ret T* since still under lifecycle-protect
247+
// . but not perfect as T* still available
248+
// . this is the simplest way to call T's func - compromise convenient & safe
249+
// . still worth? better than shared_ptr?
250+
// . not perfect, but SafePtr inc safe than shared_ptr - eg create, cast - so worth
244251
//
245252
// . std::any vs SafePtr
246253
// . SafePtr is safe shared_ptr that is lifecycle ptr, std::any is not ptr nor lifecycle mgmt

src/safe_mem/UniPtr.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ using namespace std;
2020
#if 0
2121
namespace RLib
2222
{
23+
// std::~, or ambiguous with boost::~
2324
using UniPtr = shared_ptr<void>;
2425
#define MAKE_PTR make_shared
2526
#define PTR shared_ptr

0 commit comments

Comments
 (0)