Skip to content

Commit

Permalink
SafePtr: practise in swm
Browse files Browse the repository at this point in the history
  • Loading branch information
fchn289 committed Jun 1, 2024
1 parent c60d85b commit f72f128
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/msg_self/MsgSelf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

#include "UniLog.hpp"

#define MSG_SELF (ObjAnywhere::get<MsgSelf>(*this).get())
#define MSG_SELF (ObjAnywhere::get<MsgSelf>(*this))

using namespace std;

Expand Down
11 changes: 9 additions & 2 deletions src/safe_mem/SafePtr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ class SafePtr
// . no operator*() since T& is unsafe
shared_ptr<T> get() const noexcept { return pT_; }
shared_ptr<T> operator->() const noexcept { return pT_; } // convenient
auto use_count() const noexcept { return pT_.use_count(); }
explicit operator bool() const noexcept { return pT_ != nullptr; }
auto use_count() const noexcept { return pT_.use_count(); }

// most for debug
auto realType() const noexcept { return realType_; } // ret cp is safer than ref
Expand Down Expand Up @@ -190,7 +191,7 @@ template<typename U, typename... ConstructArgs>
SafePtr<U> make_safe(ConstructArgs&&... aArgs)
{
SafePtr<U> safeU;
safeU.pT_ = make_shared<U>(forward<ConstructArgs>(aArgs)...);
safeU.pT_ = std::make_shared<U>(std::forward<ConstructArgs>(aArgs)...); // std::~, or ambiguous with boost::~
//HID("new ptr=" << (void*)(safeU.pT_.get())); // too many print; void* print addr rather than content(dangeous)
return safeU;
}
Expand Down Expand Up @@ -241,6 +242,12 @@ struct std::hash<RLib::SafePtr<T>>
// . SafePtr is simple
// . freq cast void->any is dangeous
// . so worth
// . get()/etc ret shared_ptr<T>, safe?
// . safer than ret T* since still under lifecycle-protect
// . but not perfect as T* still available
// . this is the simplest way to call T's func - compromise convenient & safe
// . still worth? better than shared_ptr?
// . not perfect, but SafePtr inc safe than shared_ptr - eg create, cast - so worth
//
// . std::any vs SafePtr
// . SafePtr is safe shared_ptr that is lifecycle ptr, std::any is not ptr nor lifecycle mgmt
Expand Down
1 change: 1 addition & 0 deletions src/safe_mem/UniPtr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ using namespace std;
#if 0
namespace RLib
{
// std::~, or ambiguous with boost::~
using UniPtr = shared_ptr<void>;
#define MAKE_PTR make_shared
#define PTR shared_ptr
Expand Down

0 comments on commit f72f128

Please sign in to comment.