NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
registry.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_REFLECT_REGISTRY_HPP__
2#define NEFORCE_CORE_REFLECT_REGISTRY_HPP__
3
11
14NEFORCE_BEGIN_NAMESPACE__
15
16class signal_base;
17
18NEFORCE_BEGIN_REFLECT__
19
25
33class NEFORCE_API registry {
34private:
35 mutex mutex_;
37 unordered_map<type_id, type_id> name_hash_to_type_id_;
38
39 registry() = default;
40
41public:
46 static registry& instance();
47
57 template <typename T>
60
61 lock<mutex> lk(mutex_);
62 auto it = types_.find(id);
63 if (it != types_.end()) {
64 return *it->second;
65 }
66
67 auto name_hash = name.to_hash();
68 auto new_it = types_.emplace(id, make_unique<meta_type>(name, id, sizeof(T))).first;
69 name_hash_to_type_id_.emplace(name_hash, id);
70 return *new_it->second;
71 }
72
81 auto name_hash = name.to_hash();
82 auto map_it = name_hash_to_type_id_.find(name_hash);
83 if (map_it == name_hash_to_type_id_.end()) {
84 return nullptr;
85 }
86 auto it = types_.find(map_it->second);
87 return it != types_.end() ? it->second.get() : nullptr;
88 }
89
96 lock<mutex> lock(mutex_);
97 return find_unlocked(name);
98 }
99
106 lock<mutex> lock(mutex_);
107 const auto it = types_.find(id);
108 return it != types_.end() ? it->second.get() : nullptr;
109 }
110
118 template <typename Func>
119 void for_each(Func&& func) {
120 lock<mutex> lock(mutex_);
121 for (auto& type: types_) {
122 func(*type.second);
123 }
124 }
125
133 lock<mutex> lk(mutex_);
134 for (const auto& type: types_) {
135 type.second->resolve_bases_unlocked(this);
136 }
137 }
138
155 static bool dynamic_connect(void* sender_obj, string_view sender_type, string_view signal_name,
156 size_t signal_offset, void* receiver_obj, string_view receiver_type,
157 string_view slot_name) {
158 const auto* s_meta = instance().find(sender_type);
159 const auto* r_meta = instance().find(receiver_type);
160 if ((s_meta == nullptr) || (r_meta == nullptr)) {
161 return false;
162 }
163
164 const auto* slot = r_meta->get_function(slot_name);
165 if (slot == nullptr) {
166 return false;
167 }
168
169 auto* sig_base = reinterpret_cast<signal_base*>(static_cast<char*>(sender_obj) + signal_offset);
170 if (sig_base == nullptr) {
171 return false;
172 }
173
174 return connect_signal_to_slot(sig_base, slot, receiver_obj);
175 }
176
186 static bool connect_signal_to_slot(signal_base* sig, const meta_function* slot, void* receiver);
187};
188 // Reflection
190
191NEFORCE_END_REFLECT__
192NEFORCE_END_NAMESPACE__
193#endif // NEFORCE_CORE_REFLECT_REGISTRY_HPP__
锁管理器模板
非递归互斥锁
类型反射元数据类
void for_each(Func &&func)
遍历所有注册的类型
meta_type * find(type_id id)
查找类型
meta_type * find(string_view name)
查找类型
static bool dynamic_connect(void *sender_obj, string_view sender_type, string_view signal_name, size_t signal_offset, void *receiver_obj, string_view receiver_type, string_view slot_name)
查找信号并连接到槽函数
static registry & instance()
获取单例实例
void resolve_all_bases()
解析所有类型的基类关系
meta_type & register_type(string_view name)
注册类型
static bool connect_signal_to_slot(signal_base *sig, const meta_function *slot, void *receiver)
将信号连接至反射槽函数
meta_type * find_unlocked(string_view name)
查找类型
信号类型擦除基类
size_t type_id
类型标识符
constexpr type_id type_id_for() noexcept
计算类型 T 的运行时期类型标识
basic_string_view< char > string_view
字符字符串视图
constexpr unique_ptr< T > make_unique(Args &&... args)
创建unique_ptr
互斥锁
类型反射元数据