-
-
Notifications
You must be signed in to change notification settings - Fork 129
Alternative to rtti for the event bus. #1942
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,17 +29,34 @@ SOFTWARE. | |
#include <any> | ||
#include <functional> | ||
#include <memory> | ||
#include <typeindex> | ||
#include <typeinfo> | ||
#include <utility> | ||
|
||
#include "support/hashtable.h" | ||
#include "support/list.h" | ||
|
||
#if defined(_CPPRTTI) || defined(__GXX_RTTI) || defined(__cpp_rtti) | ||
#define PCSX_RTTI 1 | ||
#include <typeindex> | ||
#include <typeinfo> | ||
#define PCSX_HASH_TYPE(type) typeid(type).hash_code() | ||
#else | ||
#define PCSX_RTTI 0 | ||
#define CTTI_HAS_ENUM_AWARE_PRETTY_FUNCTION 1 | ||
#define CTTI_HAS_VARIABLE_TEMPLATES 1 | ||
#include "ctti/type_id.hpp" | ||
#define PCSX_HASH_TYPE(type) ctti::unnamed_type_id<type>().hash() | ||
#endif | ||
|
||
namespace PCSX { | ||
|
||
namespace EventBus { | ||
|
||
#if PCSX_RTTI | ||
typedef std::size_t HashType; | ||
#else | ||
typedef std::uint64_t HashType; | ||
#endif | ||
|
||
struct ListenerElementBaseEventBusList {}; | ||
struct ListenerElementBase; | ||
typedef PCSX::Intrusive::List<ListenerElementBase> ListenerBaseListType; | ||
|
@@ -70,7 +87,7 @@ class Listener { | |
}; | ||
|
||
struct ListenerElementsHashTableList; | ||
typedef PCSX::Intrusive::HashTable<std::size_t, ListenerElementsHashTableList> ListenersHashTable; | ||
typedef PCSX::Intrusive::HashTable<HashType, ListenerElementsHashTableList> ListenersHashTable; | ||
struct ListenerElementsHashTableList : public ListenersHashTable::Node { | ||
~ListenerElementsHashTableList() { list.destroyAll(); } | ||
ListenerBaseEventBusList list; | ||
|
@@ -82,7 +99,7 @@ class EventBus { | |
template <typename Event> | ||
void signal(const Event& event) { | ||
using Functor = typename ListenerElement<Event>::Functor; | ||
auto list = m_table.find(typeid(Event).hash_code()); | ||
auto list = m_table.find(PCSX_HASH_TYPE(Event)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Verification agent 🧩 Analysis chainConsider potential ABI compatibility implications. The macro usage is correct and maintains a clean interface. However, switching between RTTI and non-RTTI modes could break ABI compatibility since the hash types and algorithms differ. Consider documenting the ABI implications of this change and ensure that:
Also applies to: 112-112, 127-127 🏁 Script executed: #!/bin/bash
# Description: Check for potential mixed RTTI usage patterns in the codebase
# Look for direct typeid usage that might bypass the macro
rg "typeid\(" --type cpp --type cc --type cxx | grep -v "PCSX_HASH_TYPE"
# Check for existing RTTI-dependent code that might conflict
rg -i "rtti" --type cpp --type cc --type cxx --type cmake | head -10
# Look for build configuration that might affect RTTI settings
rg -i "frtti\|fno-rtti" --type cmake --type make Length of output: 246 The previous ripgrep invocation failed to detect “.cc” files, so let’s retry with explicit glob patterns: 🏁 Script executed: #!/bin/bash
# 1) Find any direct typeid() usages not wrapped in PCSX_HASH_TYPE
rg "typeid\\(" -g '*.cpp' -g '*.cc' -g '*.cxx' --no-heading | grep -v "PCSX_HASH_TYPE"
# 2) Scan for any RTTI mentions in source, headers, and CMake
rg -i rtti -g '*.cpp' -g '*.cc' -g '*.cxx' -g '*.h' -g '*.hpp' -g '*.cmake' -g 'CMakeLists.txt' | head -20
# 3) Look for RTTI-related compiler flags in CMakeLists
rg -i 'fno-rtti|frtti' -g '*.cmake' -g 'CMakeLists.txt' --no-heading Length of output: 2188 Document ABI implications and enforce RTTI consistency No direct
🤖 Prompt for AI Agents
|
||
if (list == m_table.end()) return; | ||
for (auto& listener : list->list) { | ||
std::any cb = listener.getCB(); | ||
|
@@ -92,7 +109,7 @@ class EventBus { | |
} | ||
|
||
private: | ||
void listen(std::size_t id, ListenerElementBase* listenerElement) { | ||
void listen(HashType id, ListenerElementBase* listenerElement) { | ||
auto list = m_table.find(id); | ||
if (list == m_table.end()) { | ||
list = m_table.insert(id, new ListenerElementsHashTableList()); | ||
|
@@ -107,7 +124,7 @@ template <typename Event> | |
void Listener::listen(typename ListenerElement<Event>::Functor&& cb) { | ||
ListenerElement<Event>* element = new ListenerElement(std::move(cb)); | ||
m_listeners.push_back(element); | ||
m_bus->listen(typeid(Event).hash_code(), element); | ||
m_bus->listen(PCSX_HASH_TYPE(Event), element); | ||
} | ||
|
||
} // namespace EventBus | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
Verify CTTI library configuration and compatibility.
The CTTI library configuration defines are hardcoded. Ensure these settings are compatible with your target environments and the version of CTTI being used.
🏁 Script executed:
Length of output: 18316
CTTI Library Integration and Compatibility Needs Attention
ctti/type_id.hpp
(or other CTTI headers) were found in the repository; confirm the CTTI library is added as a dependency (e.g. submodule or external package) and that your build system (CMake/Makefiles) includes its header directory.find_package
or include-path settings so the compiler can locate the headers.ctti::unnamed_type_id<type>().hash()
returns the intended width (e.g.,std::size_t
oruint64_t
) on all target platforms. If you depend on a 64-bit hash, guard or static-assert accordingly.🤖 Prompt for AI Agents