模板化类对象的C ++矢量 [英] C++ Vector of templated class objects

查看:47
本文介绍了模板化类对象的C ++矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道是否有可能实现我正在尝试的目标,但是也许有人可以引导我朝着正确的方向发展. Sidenote:这是在嵌入式系统上.

I don't know if it's possible to achieve what I'm trying but maybe someone can guide me in the right direction. Sidenote: this is on an embedded system.

我有一个SPI通信,该通信返回代表一组寄存器的 std :: vector< int> .这是在一个单独的线程中完成的,因此可以异步进行.为了解决这个问题,我在每个 read 调用上创建了一个对象,该对象包含一个 std :: promise< std :: vector< int>> (以及其他一些信息),并且直接返回它的 future ,以便调用者可以等待结果.

I have an SPI communication that returns a std::vector<int> representing a set of registers. This is done in a separate thread so asynch. To handle this, I create an object on every read call containing a std::promise<std::vector<int>> (and some other information) and directly return the future of it, so the caller can wait for the result.

一旦SPI传输完成,我将相应地设置promise的值.到目前为止一切顺利.

Once the SPI transfer is complete, I set the value of the promise accordingly. So far so good.

现在我想做的是不总是返回类型为 std :: vector< int> 的Future,而是对向量进行一些预处理并返回 int double 甚至是 struct .我以为我可以通过将匿名函数交给 read 函数并将引用存储在对象中来实现.然后会在分配承诺之前调用此函数.

Now what I'd like to do is to not always return a future of type std::vector<int> but do some preprocessing on the vector and return either int or double or even a struct. I thought I can achieve that by handing an anonymous function to the read function and store a reference in the object. This function would then be called before the promise is assigned.

我现在遇到的问题是此函数的返回类型直接影响我的promise的返回类型,并且存储它们的对象不再总是相同的.所以我以为只是模板

The problem I now have is that the return type of this function directly affects the return type of my promise and the object storing them is not always the same anymore. So I thought I just template it

template <class ret_type>
class obj{
    public:
    std::promise<ret_type> prms;
    ret_type (*map_function)(std::vector<int>);
    ...
};

但是现在我必须要解决的问题是,包含这些对象的向量(SPI队列)的类型为 std :: vector< obj>

But now I have to problem that my vector containing these objects (the SPI queue) that was of type std::vector<obj> cannot hold objects of different types.

有解决这个问题的正确方法吗?

Is there a proper way to solve that?

修改:我会以类似的方式使用向量

edit: I'd use the vector in a way like

for(auto &element : vector){
    std::vector<int> answer_from_spi = spi_read();
    element.prms.set_value(element.map_function(answer_from_spi));
}

推荐答案

沼泽标准解决方案包括将整个类型更改部分移到虚拟函数中.在向量中存储(智能)指向基类的指针,并在派生类中实现虚函数.

The bog-standard solution consists of moving your entire type-varying part into a virtual function. Store (smart) pointers to the base class in your vector, and have the virtual function implemented in derived classes.

class obj {
  public:
    virtual void set_promise () = 0;
};

template <class ret_type>
class obj_impl : public obj {
    std::promise<ret_type> prms;
    ret_type (*map_function)(const std::vector&<int>);
    void set_promise () override {
        std::vector<int> answer_from_spi = spi_read();
        prms.set_value(map_function(answer_from_spi));
    }
};

std::vector<std::unique_ptr<obj>> objects;
for(auto &element : objects)
    element->set_promise();

这篇关于模板化类对象的C ++矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆