Skip to content

Commit

Permalink
type_index: simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
fchn289 committed Jul 2, 2024
1 parent f43a078 commit 0622493
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/safe_mem/SafePtr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ shared_ptr<To> SafePtr<T>::cast() const noexcept
return nullptr; // cast ok or null
}

else if (type_index(typeid(To)) == realType_)
else if (realType_ == typeid(To))
{
//HID("(SafePtr) cast void->origin");
return static_pointer_cast<To>(pT_);
}
else if (type_index(typeid(To)) == lastType_)
else if (lastType_ == typeid(To))
{
//HID("(SafePtr) cast void to last-type-except-void");
return static_pointer_cast<To>(pT_);
Expand All @@ -152,8 +152,8 @@ void SafePtr<T>::init_(const SafePtr<From>& aSafeFrom) noexcept

realType_ = aSafeFrom.realType();
// save last useful type
if (!is_same_v<T, void> && type_index(typeid(T)) != realType_)
lastType_ = type_index(typeid(T));
if (!is_same_v<T, void> && realType_ != typeid(T))
lastType_ = typeid(T);
else
lastType_ = aSafeFrom.lastType();

Expand Down
2 changes: 1 addition & 1 deletion src/thread/MtInQueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ ELE_TID MtInQueue::pop()
// nothing
auto&& it = begin_();
if (it == cache_.end())
return ELE_TID(nullptr, type_index(typeid(void)));
return ELE_TID(nullptr, typeid(void));

// pop
auto ele_tid = move(*it); // must copy
Expand Down
10 changes: 5 additions & 5 deletions src/thread/MtInQueue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ namespace RLib
// - ele & its type_index(==/!= ok, but type_info* & hash_code nok)
// - shared_ptr is safe to cast void since type_index (but not safe as SafePtr's create)
using ELE_TID = pair<UniPtr, type_index>;
using EleHdlr = function<void(UniPtr)>;
using EleHdlr = function<void(UniPtr)>; // NO exception allowed

// ***********************************************************************************************
class MtInQueue
{
public:
~MtInQueue();

template<class aEleType> void mt_push(PTR<aEleType>&& aEle);
template<class aEleType> void mt_push(PTR<aEleType>&& aEle); // aEle may not mt-safe, so NOT share to other thread

// - shall be called in main thread ONLY!!!
// - high performance
Expand All @@ -68,7 +68,7 @@ class MtInQueue

// -------------------------------------------------------------------------------------------
deque<ELE_TID> queue_; // unlimited ele; most suitable container
deque<ELE_TID> cache_;
deque<ELE_TID> cache_; // main-thread use ONLY (so no mutex protect)
mutex mutex_;

unordered_map<type_index, EleHdlr> tid_hdlr_S_;
Expand Down Expand Up @@ -100,7 +100,7 @@ void MtInQueue::mt_push(PTR<aEleType>&& aEle)
// push
{
lock_guard<mutex> guard(mutex_);
queue_.push_back(ELE_TID(move(aEle), type_index(typeid(aEleType))));
queue_.push_back(ELE_TID(move(aEle), typeid(aEleType)));
HID("(MtQ) ptr=" << aEle.get() << ", nRef=" << aEle.use_count()); // HID supports MT
}

Expand All @@ -118,7 +118,7 @@ PTR<aEleType> MtInQueue::pop()
return nullptr;

// mismatch
if (it->second != type_index(typeid(aEleType)))
if (it->second != typeid(aEleType))
return nullptr;

// pop
Expand Down
2 changes: 1 addition & 1 deletion ut/thread/MtInQueueTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ TEST_F(MtInQueueTest, destruct_right_type)
mt_getQ().mt_clear();
ASSERT_TRUE(isDestructed) << "REQ: destruct correctly";
}
TEST_F(MtInQueueTest, clear_queue_cache_hdlr)
TEST_F(MtInQueueTest, clean_queue_cache_hdlr)
{
mt_getQ().mt_push<int>(MAKE_PTR<int>(1));
mt_getQ().mt_push<int>(MAKE_PTR<int>(2));
Expand Down

0 comments on commit 0622493

Please sign in to comment.