Skip to content

Commit

Permalink
SafePtr: no operator*() since unsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
fchn289 committed May 31, 2024
1 parent a92ba19 commit c60d85b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/obj_anywhere/ObjAnywhere.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ PTR<aObjType> ObjAnywhere::get(UniLog& oneLog)
if (not isInit())
return nullptr;

auto&& idx_obj = idx_obj_S_->find(type_index(typeid(aObjType)));
auto&& idx_obj = idx_obj_S_->find(typeid(aObjType));
if (idx_obj != idx_obj_S_->end())
return static_pointer_cast<aObjType>(idx_obj->second);

Expand All @@ -95,7 +95,7 @@ void ObjAnywhere::set(PTR<aObjType> aSharedObj, UniLog& oneLog)
return;
}

const auto& objIndex = type_index(typeid(aObjType));
const type_index& objIndex = typeid(aObjType);
if (aSharedObj.get() == nullptr)
{
idx_obj_S_->erase(objIndex); // natural expectation
Expand Down
4 changes: 3 additions & 1 deletion src/safe_mem/SafePtr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ class SafePtr
template<typename To, typename From>
friend SafePtr<To> dynamic_pointer_cast(const SafePtr<From>&) noexcept; // ret ok or null

// safe usage: convenient, equivalent & min (vs shared_ptr)
// safe usage: convenient(compatible shared_ptr), equivalent & min
// . ret shared_ptr is safer than T* (but not safest since to call T's func easily)
// . 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(); }
Expand Down
13 changes: 7 additions & 6 deletions ut/safe_mem/SafePtrTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <gtest/gtest.h>
#include <map>
#include <type_traits>
#include <typeindex>
#include <unordered_map>

#include "SafePtr.hpp"
Expand All @@ -22,14 +23,14 @@ TEST(SafePtrTest, GOLD_safeCreate_normal)
SafePtr<int> i = make_safe<int>(42);
EXPECT_EQ(1, i.use_count()) << "REQ: compatible shared_ptr";

shared_ptr<int> content = i.get();
auto content = i.get();
EXPECT_EQ(42, *content) << "REQ: valid construct & get";
EXPECT_EQ(2, i.use_count()) << "REQ: compatible shared_ptr";

*content = 43;
EXPECT_EQ(43, *i.get()) << "REQ: valid update";

content.reset();
content = nullptr;
EXPECT_EQ(nullptr, content) << "REQ: reset OK";
EXPECT_EQ(43, *i.get()) << "REQ: outside reset not impact SafePtr";
EXPECT_EQ(1, i.use_count()) << "REQ: compatible shared_ptr";
Expand Down Expand Up @@ -86,7 +87,7 @@ TEST(SafePtrTest, invalidCast_retNull)
{
EXPECT_EQ(nullptr, dynamic_pointer_cast<Derive>(make_safe<Base>()).get()) << "REQ: invalid base->derived";

//make_safe<int>(7).cast<char>()); // invalid cast, will compile err (safer than ret nullptr)
//dynamic_pointer_cast<char>(make_safe<int>(7)); // invalid cast, will compile err (safer than ret nullptr)

EXPECT_EQ(nullptr, dynamic_pointer_cast<Base>(dynamic_pointer_cast<void>(make_safe<Derive>())).get()) << "REQ: invalid derived->void->base";

Expand Down Expand Up @@ -210,12 +211,12 @@ TEST(SafePtrTest, safe_assign) // operator=() is auto-gen, just simple test 1 c
EXPECT_EQ(42, *(one.get())) << "REQ: valid get after assigner is reset";
}

#define SAFE
#define LIKE_SHARED_PTR
// ***********************************************************************************************
TEST(SafePtrTest, get_isMemSafe_afterDelOrigin)
{
SafePtr<string> safe = make_safe<string>("hello");
shared_ptr<string> get = safe.get();
auto get = safe.get();
EXPECT_EQ("hello", *get) << "get succ";

safe = nullptr;
Expand All @@ -227,7 +228,7 @@ TEST(SafePtrTest, get_isMemSafe_afterDelOrigin)
TEST(SafePtrTest, getPtr_isMemSafe_afterDelOrigin)
{
SafePtr<string> safe = make_safe<string>("hello");
shared_ptr<string> get = safe.operator->(); // diff
auto get = safe.operator->(); // get ptr
EXPECT_EQ("hello", *get) << "REQ: get succ";

safe = nullptr;
Expand Down

0 comments on commit c60d85b

Please sign in to comment.