具有多个输出端口的C ++流水线段 - 类型匹配 [英] C++ pipeline segment with multiple output ports - type matching

查看:146
本文介绍了具有多个输出端口的C ++流水线段 - 类型匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为我的项目设计一个(合理)通用管道。我宽松地依靠VTK管道概念。但是,有很大的区别。

I am trying to design a (reasonably) generic pipeline for my project. I am loosely relying on the VTK pipeline concept. However, there are major differences.

在我的设计中,输入 - 输出连接类型匹配是使用可变参数模板和递归继承(类似于CRTP)完成的。这允许我通过将抽象基类的列表传递给基本过滤器/映射器类来手动定义哪些段可以与哪些段连接(概念要求)。本身不会产生任何问题。

In my design the input-output connection type matching was done using variadic templates and recursive inheritance (similar to CRTP). This allows me to manually define which segments can be connected with which segments (a concept requirement) by passing a list of abstract base classes to the base filter/mapper classes. In itself, this does not cause any problems.

我需要能够使用自定义(不一定 std / primitive)数据类型。 VTK管道将指针传递到通过管道段从VTK类( vtkAlgorithmObject )之一派生的对象。这允许具有多个连接的管线输入/输出接口的非常自然的实现。这是我的问题所在。

I need to be able to work with custom (not necessarily std/primitive) data types. The VTK pipeline, passes pointers to objects that derive from one of the VTK classes (vtkAlgorithmObject) through pipeline segments. This allows quite a natural implementation of the pipeline input/output interface with multiple connections. This is where my problem lies.

输出端口函数的VTK实现是:

The VTK implementation of the output port function is:

vtkAlgorithmOutput* vtkAlgorithm::GetOutputPort (int index) 

从输出端口的自定义类。不知何故,我需要一个(可能是重载的)函数 getOutputPort ,它将根据预定义的索引返回一个不同的类型。

Unfortunately, I cannot return a custom class from the output port. Somehow, I need to have a single (possibly overloaded) function getOutputPort that will return a different type based on the pre-defined index.

这样做的一种方法是使用模板参数和重载的组合:

One way of doing this is using a combination of template arguments and overloading:

template<int N>
void getOutputPort(int& i)
{
    if (N == 0)
        i = 10;
    else if (N == 2)
        i = 20;
    else
        throw "Wrong template argument for the return type.";
}

template<int N>
void getOutputPort(string& str)
{
    if (N == 1)
        str = "asdf";
    else
        throw "Wrong template argument for the return type.";
}

然而,除了明显的将定义和索引选择函数,我必须通过引用返回,这不是我喜欢做的事情。在概念上,我想通过 std :: unique_ptr 传递对象。这将确保流水线下游的两个段永远不会尝试使用相同的资源。

However, apart from the obvious problem of splitting the definition and index selection over multiple functions, I have to return by reference, which is not something that I would prefer to do. At the concept, I would like to pass the objects via std::unique_ptr. This will ensure that two segments downstream the pipeline will never make attempts to use the same resource.

因此,我试图做类似于代码中显示的东西

Thus, I am trying to do something similar to what is shown in the code below

template<int N>
auto getOutputPort2()
{
    if (N == 0)
        return std::unique_ptr<int>(new int(10));
    else if (N == 1)
        return std::unique_ptr<string>(new string("asdf"));
    else if (N == 2)
        return std::unique_ptr<int>(new int(20));
    else
        throw "Wrong template argument.";
}

由于显而易见的原因,这会导致错误auto'不一致扣除。有没有办法解决问题(不一定使用 auto )?

For obvious reasons, this results in the error 'inconsistent deduction for auto'. Are there any ways around the problem (not necessarily using auto)?

推荐答案

p>谢谢大家的意见和解答。不幸的是,在我的问题中,我没有明确提到几件事情,但他们是相当相关的问题,我试图解决(在问的时候,我不知道)。显然, getOutputPort2()应该是表示管道段的类的成员。然而,我只是在这个问题的定义中简单和模糊地提到该类将是一个模板。因此,基于显式专门化的解决方案不能直接工作(即,您必须专门处理类和成员函数模板)。

Thank you everyone for you comments and answers. Unfortunately, there were several things that I did not explicitly mention in my question, but they were quite relevant to the problem I was trying to solve (at the time of asking I was not aware of that). Obviously, getOutputPort2() was supposed to be a member of a class that represents a pipeline segment. However I only mentioned briefly and vaguely in the definition of the question that the class would be a template. Thus, the solutions based on the explicit specialisation cannot work directly (i.e. you have to specialise both the class and the member function template).

我决定去的解决方案最后是基于以下问题的答案:显式返回类型特殊化问题。专用于我的问题的代码可以在下面找到:

The solution that I decided to go with in the end is based on an answer to the following question: explicit return type specialisation question. The code specialised for my problem can be found below:

template <typename T>
class A
{

public:

    // the Get Value we want
    template <int N>
    auto getValue()
        {
            return get_value_impl<N>::apply(*this);
        }

    // the general get value struct
    template<int N, typename = void>
    struct get_value_impl
    {
        static auto apply(A a)
            {
                throw "Wrong template argument.";
            }
    };

    // partial specialization 1
    template <typename S>
    struct get_value_impl<0, S>
    {
        static std::unique_ptr<double> apply(A a)
            {
                return std::unique_ptr<double>(new double(10.0));
            }
    };

    // partial specialization 2
    template <typename S>
    struct get_value_impl<1, S>
    {
        static std::unique_ptr<string> apply(A a)
            {
                return std::unique_ptr<string>(new string("Hellow!"));
            }
    };

};

这篇关于具有多个输出端口的C ++流水线段 - 类型匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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