Skip to content

Commit

Permalink
SafePtr: clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
fchn289 committed Feb 10, 2024
1 parent 54cff5c commit 6c0b59b
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions src/safe_mem/SafePtr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,8 @@ template<class T> class SafePtr
}
}

template<typename To> shared_ptr<To> get() const
{
if (is_convertible<T*, To*>::value) // Derive -> Base
return static_pointer_cast<To>(pT_);
if (is_same<To, void>::value) // any -> void (for storing same type=SafePtr<void>)
return static_pointer_cast<To>(pT_);
if (is_same<T, void>::value && &typeid(To) == preVoidType_) // void -> back
return static_pointer_cast<To>(pT_);
if (&typeid(To) == realType_) // any -> origin
return static_pointer_cast<To>(pT_);
return nullptr;
}
template<typename To> shared_ptr<To> get() const;
shared_ptr<void> get() const;

const type_info* preVoidType() const { return preVoidType_; }
const type_info* realType() const { return realType_; }
Expand All @@ -83,6 +73,37 @@ template<class T> class SafePtr
const type_info* realType_ = &typeid(T); // that pT_ point to, can safely cast to
};

// ***********************************************************************************************
template<class T>
template<class To>
shared_ptr<To> SafePtr<T>::get() const
{
if (is_convertible<T*, To*>::value)
{
HID("Derive to Base (include to self)");
return static_pointer_cast<To>(pT_);
}
if (&typeid(To) == realType_)
{
HID("any to origin");
return static_pointer_cast<To>(pT_);
}
return nullptr;
}
template<class T>
shared_ptr<void> SafePtr<T>::get() const
{
HID("any to void (for container to store diff types)");
return static_pointer_cast<void>(pT_);
}
template<>
template<class To>
shared_ptr<To> SafePtr<void>::get() const
{
HID("back from void");
return static_pointer_cast<To>(pT_);
}

// ***********************************************************************************************
template<class U, class... Args> SafePtr<U> make_safe(Args&&... aArgs)
{
Expand Down

0 comments on commit 6c0b59b

Please sign in to comment.