这是学EnTT时的一些笔记
一、basic_registry
basic_registry包含basic_storage,basic_storage继承自basic_sparse_set。
Sparse Set
稀疏集
basic_storage
比稀疏集多了payload用于追踪entity对应的component,payload的下标与稠密数组下标一一对应
其中该类声明:
template<typename Type, typename Entity, typename Allocator, typename>
class basic_storage: public basic_sparse_set<Entity, typename std::allocator_traits<Allocator>::template rebind_alloc<Entity>> {//...
}
std::allocator_traits 是一个标准库中的类模板,用于提取和修改分配器的特性,rebind_alloc 是一个类型别名。当使用std::allocator_traits<Allocator>::rebind_alloc<Entity> 时,C++ 编译器需要知道 rebind_alloc 是一个模板。因此,typename 告诉编译器 rebind_alloc 是一个类型。而template 告诉编译器 rebind_alloc 是一个模板,因此后面可以使用 <Type> 来指定模板参数。
而在std::allocator_traits的声明中也发现一个有意思的东西:
_EXPORT_STD template <class _Alloc>
struct allocator_traits : conditional_t<_Is_default_allocator<_Alloc>::value, _Default_allocator_traits<_Alloc>,
_Normal_allocator_traits<_Alloc>> {};
使用了 conditional_t,它是一个类型特征,用于根据条件选择类型。_Is_default_allocator<_Alloc>::value 是一个类型特征,检查 _Alloc 是否是一个默认分配器(例如,std::allocator)。如果 _Alloc 是默认分配器,则 allocator_traits 将继承自 _Default_allocator_traits<_Alloc>;否则,它将继承自 _Normal_allocator_traits<_Alloc>。