如果模板化"ResourceCache"需要不同的创建参数/方法,它们如何加载/创建 T 类型的资源?
How can a templated "ResourceCache" load/create resources of type T, if they require different creation parameters/methods?
我只是不知道怎么做。
基本上,如果每个模板专用化类型(T)都有不同的初始化参数,那么通用ResourceCache如何加载/创建资源?
template< class T>
class ResourceCache{
T* cache[100];
T* LoadResource(different parameters for different T ){//used in a cache miss..
Create( different parameters for different T );
}
}
如果我对IResourceParams类使用抽象,如果不使用多态性,我的ResourceCache将无法使用它自己已知的类型资源数据,这有点愚蠢,因为在运行时他知道类型是什么,我在运行时总是在编译时工具的前面做坏事。。。我想。。
在我目前的尝试中,我创建了一个模板化的IResourceDesc,它有一个虚拟的T*Create()方法,所以你需要派生以添加数据并专门化Create方法,但这很糟糕,因为我在ResourceCache类中不能有IResource desc的集合(用于比较当前加载的IResorcceDesc、通过desc获取缓存的资源等)。。。
在C++11中,使用可变模板和完美的转发:非常容易
#include <utility>
template<class... Args>
T* LoadResource(Args&&... args){
unsigned dest_index = /* pick it */ 0;
cache[dest_index] = new T(std::forward<Args>(args)...);
return cache[dest_index];
}
对于C++03,可以提供大约10个不同参数数量的过载,也可以采用现场工厂风格:
template< class T>
class ResourceCache{
T* cache[100];
template<class Factory>
T* LoadResource(Factory const& f){
unsigned dest_index = /* pick cache slot */ 0;
void* dest = operator new(sizeof(T));
cache[dest_index] = f.construct(dest);
return cache[dest_index];
}
}
template<class T, class A1>
struct in_place_factory1{
in_place_factory1(A1 const& a1) : _arg1(a1) {}
int* construct(void* dest) const{
return new (dest) T(_arg1);
}
private:
A1 const& _arg1; // make sure the original argument outlives the factory
};
// in code
ResourceCache<int> cache;
int arg = 5;
int* res = cache.LoadResource(in_place_factory1<int,int>(arg));
就地工厂基本上是完美转发可变模板函数的劣质版本,它可以将对象直接放置到容器存储中,而不需要一个已经完整的对象进行复制。
相关文章:
- 为什么加载SDF会导致Mobilizer创建闭环错误
- 在Visual Studio 2017中创建的简单DLL不会在XP中加载
- 为什么在Qt中创建主窗口之前必须加载语言包?
- 在 DirectX 中加载 WIC 后创建纹理
- OpenGL 在与呈现线程不同的线程中创建和加载 VBO 信息
- 为什么我创建的曲面无法加载?
- Vulkan-加载扩展时实例创建失败
- 使用 Visual C++ 创建的 WinRT DLL 不会在 Windows 应用商店应用中加载
- Qt 创建器无法加载图标
- C++简体版链表,当我加载数据以创建链表并输入更多数据时,似乎存在差距
- 加载在JSBN中创建的RSA公钥,然后加密消息
- 如何防止在加载程序中重新创建页面
- 如果模板化"ResourceCache"需要不同的创建参数/方法,它们如何加载/创建 T 类型的资源?
- 创建缓冲区以加载数据时出错
- 无法加载或创建项目 VS 2015.1.无法创建 Visual c# 2015 编译器
- 在加载 DLL 之前创建远程线程
- 创建垫子后,从字节向量将图像加载到垫子上
- 如何从流中加载html内容,然后如何创建样式表以在预览窗格中显示html文件(如html预览处理程序)
- Microsoft Visual Studio 2010将不会编译、加载/创建文件,也不会识别iostream
- 如何从磁盘加载Jpeg图像并创建视频文件?Avi等),而无需在c++中使用opencv