- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
出于学习目的,我正在尝试使用 C++ 实现通用 ECS 库。我想了很多方法来实现,但我总是遇到问题。所以,如果你能帮我解决这个问题:
假设我有一个 constexpr hana::tuple
的 hana::type_c
组件,比如:
struct C1 {};
struct C2 {};
struct C3 {};
constexpr auto components = hana::to_tuple(hana::tuple_t<C1, C2, C3>);
现在我有一个组件存储类型,这在这里不是问题,所以我们称它为存储(每个组件的类型不同):
struct Storage {};
我想链接每个组件或每个组件组,以及它们的 Storage
类型。所以简单的方法是做类似的事情:
constexpr auto component_storage = hana::make_tuple(
hana::make_pair(hana::to_tuple(hana::tuple_t<C1, C2>), type_c<Storage>),
hana::make_pair(hana::to_tuple(hana::tuple_t<C3>), type_c<Storage>)
);
但现在的问题是运行时。如果我初始化那个元组但使用真正的存储而不是 type_c<Storage>
,我必须遍历元组才能找到 Storage
我需要的。所有这些都在运行时不是吗?这真的很糟糕,我的上一个版本有类似 Component::getStorage()
的东西它是免费的(但限制更多)。
所以问题是:我怎样才能设法拥有一些 getStorage<Component>()
函数在运行时不会产生任何成本?我的意思是仅返回存储的引用。
编辑:到目前为止,我认为的唯一方法非常简单(听起来不错)。
伪代码
struct LinkedStorage {
hana::tuple<...> storages;
hana::tuple<hana::pair...> index;
};
至少是这样的:
constexpr auto components = hana::to_tuple(hana::tuple_t<C1, C2, C3>);
constexpr auto storage = hana::to_tuple(hana::tuple_t<Storage, Storage>);
constexpr auto index = hana::make_tuple(
hana::make_pair(hana::to_tuple(hana::tuple_t<C1>, 0),
hana::make_pair(hana::to_tuple(hana::tuple_t<C2, C3>, 1)
);
这样我应该能够在编译时找到索引并在运行时访问正确的元素。但我是元编程方面的新手,所以我想有人可以做出更好的东西。
最佳答案
首先,不需要使用to_tuple(tuple_t<...>)
;你可以只使用 tuple_t<...>
.现在,我认为您真正想要做的(因为您似乎需要运行时存储,这是有道理的)是:
// "map" of a set of types to a storage of some type
using StorageMap = hana::tuple<
hana::pair<hana::tuple<hana::type<C1>, hana::type<C2>>, StorageA>,
hana::pair<hana::tuple<hana::type<C3>>, StorageB>
>;
// Actual object that contains the runtime storage (and the free mapping between types)
StorageMap map;
现在,您可以实现您的 getStorage<Component>()
像这样的功能:
template <typename Component>
decltype(auto) getStorage() {
auto found = index_if(map, [](auto const& pair) {
return hana::contains(hana::first(pair), hana::type<Component>{});
});
return hana::second(hana::at(map, found));
}
哪里index_if
是所提供函数的一个简单变体 in this answer这将适用于任意谓词而不是特定元素。当我有空闲时间时,此功能将添加到 Hana(请参阅 related ticket)。
关于c++ - 改运行时研究一个编译时间一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43089587/
我试图理解基数排序,但在理解实现实际代码时改 rebase 数时遇到问题。这是我用来学习基数排序的代码,我会尝试解释我不明白的地方。 此代码由 GeeksForGeeks 提供: // C++ imp
话不多说,请看代码: ? 1
本文实例讲述了mysql语句实现简单的增、删、改、查操作。分享给大家供大家参考,具体如下: 1、创建db_shop数据库,如果该数据库不存在则创建 ?
使用oracle触发器 实现对某个表的增改删的监控操作,并记录到另一个表中。 代码: 复制代码代码如下: create or replace trigger test_trigge
java连接数据库增、删、改、查工具类 数据库操作工具类,因为各厂家数据库的分页条件不同,目前支持Mysql、Oracle、Postgresql的分页查询 在Postgresql环境测试过了,其他
1、修改数据 复制代码代码如下: DataRow dr =
注册表可以用来进行存储一些程序的信息,例如用户的权限、或者某些值等,可以根据个人需要进行存储和删减。 当前注册表主目录: 引用包 Wesky.Net.OpenTools 1.0.5或
是否可以将 pdf 页面的页眉更改为与当前所选书签同名的名称?我正在为我的 pdf 生成使用 Flying Saucer 。你能举个例子吗?提前致谢。 最佳答案 这对我适用于 flyingsaucer
好家伙,写后端,这多是一件美逝. 关于这个项目的代码前面的博客有写 我的第一个独立项目 - 随笔分类 - 养肥胖虎 - 博客园 (cnblogs.com
准备工作: 增、删、改、查的方法有很多很多种,这里只展示出常用的几种。 ?
需要5个类: 1.实体类:Person.java 2.抽象类:SQLOperate.java(封装了对数据库的操作) 3.助手类:DBOpenHelper.java(继承SQLiteOpenH
首先是是一个简单的例子,单链表的建立和输出。 程序1.1 复制代码 代码如下: #include<iostream> #include<string> using na
数据库操纵基本流程为: 1、连接数据库服务器 2、选择数据库 3、执行SQL语句 4、处理结果集 5、打印操作信息 其中用到的相关函数有 •resource m
我需要为 iPad 和 iPhone 设置不同颜色的标签,我知道我们可以为不同的尺寸类别更改字体大小,但是有什么方法可以根据尺寸类别的值设置不同的颜色 我知道有可用的代码解决方案,但我想知道 size
假设我有一个物体相对于相机的坐标 X、Y、Z 和方向 Rx、Ry、Rz。此外,我有这个相机在世界上的坐标 U、V、W 和方向 Ru、Rv、Rw。 如何将对象的位置(位置和旋转)转换为其在世界中的位置?
CRUD是Create(创建)、Read(读取)、Update(更新)和Delete(删除)的缩写,它是普通应用程序的缩影。如果您掌握了某框架的CRUD编写,那么意味可以使用该框架创建普通应用程序了
项目结构: 添加页面: &
本文实例讲述了android操作sqlite数据库(增、删、改、查、分页等)及listview显示数据的方法。分享给大家供大家参考,具体如下: 由于刚接触android开发,故此想把学到的基础知识
总括 pandas的索引函数主要有三种: loc 标签索引,行和列的名称 iloc 整型索引(绝对位置索引),绝对意义上的几行几列,起始索引为0 ix 是 iloc 和 loc的合体 at
我是一名优秀的程序员,十分优秀!