|
7 | 7 |
|
8 | 8 | #pragma once |
9 | 9 |
|
| 10 | +#include "memaddr.hpp" |
| 11 | + |
10 | 12 | #include <cstddef> |
| 13 | +#include <cstdint> |
| 14 | +#include <bit> |
| 15 | +#include <type_traits> |
| 16 | + |
| 17 | +#define DYNLIB_INVALID_VMETHOD_INDEX -1 |
11 | 18 |
|
12 | 19 | namespace DynLibUtils { |
13 | 20 |
|
14 | | -class CVirtualTable |
| 21 | +template<auto METHOD, class T> |
| 22 | +inline std::ptrdiff_t GetVirtualMethodIndex(T *pClass) noexcept |
| 23 | +{ |
| 24 | + static_assert(std::is_member_function_pointer_v<decltype(METHOD)>, "Templated method must be a pointer-to-member-function"); |
| 25 | + |
| 26 | +#if defined(_MSC_VER) |
| 27 | + // --- MSVC ABI: runtime scan vtable (no constexpr!) --- |
| 28 | + struct MSVCPMF { void* ptr; std::ptrdiff_t adj; }; |
| 29 | + union { decltype(METHOD) m; MSVCPMF pmf; } u{ METHOD }; |
| 30 | + void* target = u.pmf.ptr; |
| 31 | + |
| 32 | + void** vtbl = *reinterpret_cast<void***>(pClass); |
| 33 | + |
| 34 | + constexpr std::size_t header = 2; // [RTTI][offset-to-top] |
| 35 | + |
| 36 | + for (std::size_t n = header; ; ++n) |
| 37 | + if (vtbl[n] == target) |
| 38 | + return n - header; |
| 39 | + |
| 40 | +#elif defined(__GNUG__) || defined(__clang__) |
| 41 | + // --- Itanium C++ ABI: PMF.ptr = 1 + offset_in_bytes for virtual ones --- |
| 42 | + struct ItaniumPMF { void* ptr; std::ptrdiff_t adj; }; |
| 43 | + union { decltype(METHOD) m; ItaniumPMF pmf; } u{ METHOD }; |
| 44 | + |
| 45 | + constexpr auto raw = reinterpret_cast<std::uintptr_t>(u.pmf.ptr); |
| 46 | + |
| 47 | + static_assert((raw & 1u) != 0, "Not a virtual member function"); |
| 48 | + |
| 49 | + return (raw - 1u) / sizeof(void*); |
| 50 | +#else |
| 51 | + static_assert(false, "Unsupported compiler"); |
| 52 | +#endif |
| 53 | + |
| 54 | + return DYNLIB_INVALID_VMETHOD_INDEX; |
| 55 | +} |
| 56 | + |
| 57 | +class CVirtualTable : public CMemoryView<void *> |
15 | 58 | { |
16 | 59 | public: // Types. |
| 60 | + using CBase = CMemoryView<void *>; |
17 | 61 | using CThis = CVirtualTable; |
18 | 62 |
|
19 | 63 | public: // Constructors. |
20 | | - CVirtualTable() : m_pVTFs(nullptr) {} |
21 | | - template<class T> CVirtualTable(T *pClass) : m_pVTFs(*reinterpret_cast<void ***>(pClass)) {} |
| 64 | + CVirtualTable() : CBase(nullptr) {} |
| 65 | + template<class T> CVirtualTable(T *pClass) : CBase(*reinterpret_cast<void **>(pClass)) {} |
22 | 66 |
|
23 | 67 | public: // Getters. |
24 | | - template<typename R> R &GetMethod(std::ptrdiff_t nIndex) { return reinterpret_cast<R &>(m_pVTFs[nIndex]); } |
25 | | - template<typename R> R GetMethod(std::ptrdiff_t nIndex) const { return reinterpret_cast<R>(m_pVTFs[nIndex]); } |
| 68 | + template<typename R> R &GetMethod(std::ptrdiff_t nIndex) { return CBase::Offset(nIndex).RCast<R &>(); } |
| 69 | + template<typename R> R GetMethod(std::ptrdiff_t nIndex) const { return CBase::Offset(nIndex).RCast<R>(); } |
26 | 70 |
|
27 | 71 | public: // Callers. |
28 | 72 | template<typename R, typename... Args> R CallMethod(std::ptrdiff_t nIndex, Args... args) { return GetMethod<R (*)(void *, Args...)>(nIndex)(this, args...); } |
29 | 73 | template<typename R, typename... Args> R CallMethod(std::ptrdiff_t nIndex, Args... args) const { return const_cast<CThis *>(this)->CallMethod(nIndex, args...); } |
30 | | - |
31 | | - void **m_pVTFs; |
32 | 74 | }; // class CVirtualTable |
33 | 75 |
|
34 | 76 | class VirtualTable final : public CVirtualTable |
|
0 commit comments