动态改变 boost::vector 和 boost::matrix 中的分配策略
在我的新项目中,我正在构建一个数据管理模块。我想为上层提供一个简单的模板存储类型,例如
template<typename T>
class Data
{
public:
T getValue();
private:
boost::numeric::ublas::matrix<T> data;
}
我的目标是,使用一些不同的分配器(如Boost.inter进程分配器或Boost.pool分配器)更改数据分配器( Boost Ublas Matrix 和向量类将分配器作为模板参数)。并且只提供一个类和工厂方法来创建适当的分配器。虚拟基类可能很不错,但我无法处理如何将它与模板一起使用。你们提供什么样的设计模式或解决方案?
编辑:
我将使用 boost.pool 和 boost.shared_memory_allocator。简而言之,我想要具有不同分配策略的不同类。但我的观点是程序的上部部分应该对此一无所知。对我来说真正的挑战是收集不同的模板类具有相同的基类。
编辑: 对于想要将矩阵类与自定义分配器一起使用的人。
是这样的:
using boost::numeric::ublas;
template<typename T, class Allocator = boost::pool_allocator<T>>
class
{
public:
matrix<T, row_major, std::vector<T,Allocator>> mData;
}
In my new project i am building a data management module.I want to give a simple template storage type to upper layers like
template<typename T>
class Data
{
public:
T getValue();
private:
boost::numeric::ublas::matrix<T> data;
}
My aim is, to change allocator of data with some different allocators like Boost.inter process allocator or Boost.pool allocator (Boost Ublas Matrix and vector classes takes allocator as a template parameter ).And give only a single class and Factory method to create appropriate allocator under cover.A virtual base class could be sweet but i couldn't handle how to use it with templates.What kind of design patterns or solutions do you offer?
Edit:
I will use boost.pool and boost.shared_memory_allocator.Briefly i want to have different classes with different allocation strategies.But my point is upper parts of program should have no knowledge about it.Real challenge for me is to collect different template classes with an identical base classes.
Edit:
For one who want to use matrix class with custom allocator.
it is like this:
using boost::numeric::ublas;
template<typename T, class Allocator = boost::pool_allocator<T>>
class
{
public:
matrix<T, row_major, std::vector<T,Allocator>> mData;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目前尚不清楚您想要什么,但作为在黑暗中的一枪,以下内容是否有帮助?
It's not clear what you want, but as a shot in the dark, is the following helpful?
您是否尝试在编译时根据类型交换分配器? 您需要一个
if-else
模板和一些分配器类(模板)定义。如果您需要运行时分配器,那么就更容易了:您可以将基类(接口定义类)放入模板中,并根据需要满足的任何条件传递适当的子类。
Are you trying to swap allocators at compile time based on type? You'd need a
if-else
template and some allocator class (template) definitions.If you want runtime allocators, then it's easier: you'd put the base class (interface definition class) in the template and pass appropriate subclasses based on whatever condition you need to fulfill.