Skip to content

Commit 03d368a

Browse files
committed
Update VT hook method behaviours
1 parent 3d40ab1 commit 03d368a

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

include/dynlibutils/vthook.hpp

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -318,18 +318,16 @@ class CVTMHookBase
318318

319319
auto found = Find(CVirtualTable(pThis));
320320

321-
auto &vhooks = found.second;
322-
323-
if (vhooks == End())
321+
if (found.first == found.second)
324322
{
325323
return results;
326324
}
327325

328-
results.reserve(vhooks.size());
326+
// results.reserve(vhooks.size());
329327

330-
for (auto it : vhooks)
328+
for (auto it = found.first; it != found.second; it++)
331329
{
332-
results.push_back(it->Call(pThis, args...));
330+
results.push_back(it->second.Call(pThis, args...));
333331
}
334332

335333
return results;
@@ -341,16 +339,16 @@ class CVTMHookBase
341339
{
342340
auto found = Find(CVirtualTable(pThis));
343341

344-
auto &vhooks = found.second;
345-
346-
if (vhooks == End())
342+
if (found.first == found.second)
347343
{
348344
return false;
349345
}
350346

351-
for (auto it : vhooks)
347+
// results.reserve(vhooks.size());
348+
349+
for (auto it = found.first; it != found.second; it++)
352350
{
353-
it->Call(pThis, args...);
351+
it->second.Call(pThis, args...);
354352
}
355353

356354
return true;
@@ -422,9 +420,10 @@ using CVTMHook = CVTMHookBase<CVTHook<R, Args...>>;
422420
//
423421
//=============================================================================
424422
template<typename R, class T, typename ...Args>
425-
class CVTFMHook : public CVTMHook<R, Args...>
423+
class CVTFMHook : public CVTMHook<R, T, Args...>
426424
{
427425
public:
426+
using Base_t = CVTMHook<R, T, Args...>;
428427
using Function_t = R (*)(T, Args...);
429428
using Callback_t = std::function<R (T, Args...)>;
430429

@@ -448,36 +447,38 @@ class CVTFMHook : public CVTMHook<R, Args...>
448447
void AddHook(CVirtualTable pVTable, Callback_t funcCallback) { return AddHook(pVTable, GetVirtualIndex<METHOD>(), funcCallback); }
449448
void AddHook(CVirtualTable pVTable, std::ptrdiff_t nIndex, Callback_t funcCallback)
450449
{
451-
AddHook(pVTable, nIndex,
452-
+[](T pClass, Args... args)
450+
Base_t::AddHook(pVTable, nIndex,
451+
+[](T pClass, Args... args) -> R
453452
{
454453
auto found = sm_vcallbacks.find(CVirtualTable(pClass));
455454

456455
assert(found != sm_vcallbacks.cend());
457456

458-
auto &callbacks = found.second;
457+
auto &callbacks = found->second;
459458

460-
const auto itEnd = callbacks.cend();
459+
R result {};
461460

462461
for (auto it : callbacks)
463462
{
464-
it(pClass, args...);
463+
result = it(pClass, args...);
465464
}
465+
466+
return result;
466467
}
467468
);
468469
}
469470

470471
bool RemoveHook(CVirtualTable pVTable)
471472
{
472-
sm_vcallbacks(pVTable);
473+
sm_vcallbacks.erase(pVTable);
473474

474-
return RemoveHook(pVTable) != 0;
475+
return Base_t::RemoveHook(pVTable) != 0;
475476
}
476477

477478
void Clear()
478479
{
479480
sm_vcallbacks.clear();
480-
Clear();
481+
Base_t::Clear();
481482
}
482483

483484
protected:

0 commit comments

Comments
 (0)