Skip to content

Commit 0622493

Browse files
committed
type_index: simplify
1 parent f43a078 commit 0622493

File tree

4 files changed

+11
-11
lines changed

4 files changed

+11
-11
lines changed

src/safe_mem/SafePtr.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,12 @@ shared_ptr<To> SafePtr<T>::cast() const noexcept
124124
return nullptr; // cast ok or null
125125
}
126126

127-
else if (type_index(typeid(To)) == realType_)
127+
else if (realType_ == typeid(To))
128128
{
129129
//HID("(SafePtr) cast void->origin");
130130
return static_pointer_cast<To>(pT_);
131131
}
132-
else if (type_index(typeid(To)) == lastType_)
132+
else if (lastType_ == typeid(To))
133133
{
134134
//HID("(SafePtr) cast void to last-type-except-void");
135135
return static_pointer_cast<To>(pT_);
@@ -152,8 +152,8 @@ void SafePtr<T>::init_(const SafePtr<From>& aSafeFrom) noexcept
152152

153153
realType_ = aSafeFrom.realType();
154154
// save last useful type
155-
if (!is_same_v<T, void> && type_index(typeid(T)) != realType_)
156-
lastType_ = type_index(typeid(T));
155+
if (!is_same_v<T, void> && realType_ != typeid(T))
156+
lastType_ = typeid(T);
157157
else
158158
lastType_ = aSafeFrom.lastType();
159159

src/thread/MtInQueue.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ ELE_TID MtInQueue::pop()
111111
// nothing
112112
auto&& it = begin_();
113113
if (it == cache_.end())
114-
return ELE_TID(nullptr, type_index(typeid(void)));
114+
return ELE_TID(nullptr, typeid(void));
115115

116116
// pop
117117
auto ele_tid = move(*it); // must copy

src/thread/MtInQueue.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ namespace RLib
3939
// - ele & its type_index(==/!= ok, but type_info* & hash_code nok)
4040
// - shared_ptr is safe to cast void since type_index (but not safe as SafePtr's create)
4141
using ELE_TID = pair<UniPtr, type_index>;
42-
using EleHdlr = function<void(UniPtr)>;
42+
using EleHdlr = function<void(UniPtr)>; // NO exception allowed
4343

4444
// ***********************************************************************************************
4545
class MtInQueue
4646
{
4747
public:
4848
~MtInQueue();
4949

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

5252
// - shall be called in main thread ONLY!!!
5353
// - high performance
@@ -68,7 +68,7 @@ class MtInQueue
6868

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

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

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

120120
// mismatch
121-
if (it->second != type_index(typeid(aEleType)))
121+
if (it->second != typeid(aEleType))
122122
return nullptr;
123123

124124
// pop

ut/thread/MtInQueueTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ TEST_F(MtInQueueTest, destruct_right_type)
198198
mt_getQ().mt_clear();
199199
ASSERT_TRUE(isDestructed) << "REQ: destruct correctly";
200200
}
201-
TEST_F(MtInQueueTest, clear_queue_cache_hdlr)
201+
TEST_F(MtInQueueTest, clean_queue_cache_hdlr)
202202
{
203203
mt_getQ().mt_push<int>(MAKE_PTR<int>(1));
204204
mt_getQ().mt_push<int>(MAKE_PTR<int>(2));

0 commit comments

Comments
 (0)