在C ++中完全自定义的流运算符 [英] Completely custom stream operator in c++

查看:121
本文介绍了在C ++中完全自定义的流运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在c ++中为我的视频稳定项目创建一个完全自定义的流管道.

I want to create a completely custom stream pipeline for my video stabilization project in c++.

最终结果应如下所示:

videofilelocation >> preprocessing() >> analyze() >> stabilize() >> video_out(outputfilename).flush();

因此,preprocessing应该接受输入字符串并加载视频,提取帧等.之后,它应返回自定义结构framevector,该结构应传递给analyze,依此类推.

Therefore, preprocessing should accept an input string and load the video, extract frames, etc. Afterward, it should return a custom struct framevector which should be passed to analyze and so on.

不幸的是,关于如何实现完全自定义的流运算符,没有明确的教程/说明. (仅用于std::ostream等)

Unfortunately, there is no clear tutorial/explanation on how to implement completely custom stream operators. (Just for std::ostream, etc)

这怎么办?

推荐答案

不幸的是,关于如何实现完全自定义的流运算符,没有明确的教程/说明.

Unfortunately, there is no clear tutorial/explanation on how to implement completely custom stream operators.

好.这是一个简短的.

operator>>是二进制运算符.为了使其具有灵活性,您需要将其编写为自由函数重载,这意味着您必须在每个步骤中自定义其含义.

the operator>> is a binary operator. In order to make it flexible, you will want to write it as a free function overload, which means you get to customise its meaning at each step.

为了获得上面的语法,您正在寻找建立一个调用top operator>>的调用链,以使一个的输出是下一个的第一个参数.

In order to get the syntax above, you're looking to build a chain of calls top operator>> such that the ouput of one is the first argument to the next.

所以a >> b >> c的真正含义是:operator>>(operator>>(a, b), c)注意a >> b的输出是(a >> b) >> c的第一个输入.

so a >> b >> c really means: operator>>(operator>>(a, b), c) note that the output of a >> b is the first input of (a >> b) >> c.

这是一个非常简化的编译链.您会发现我到处都使用了值语义.如果处理步骤对象是严格的功能对象,则可以通过const&传递它们.如果它们保留了以前的使用状态,那么&& r值引用将需要重载.

Here's a very simplified chain which compiles. You will notice I have used value semantics everywhere. If your processing step objects are strict function objects, you could pass them by const&. If they carry state over from previous uses, then you'll need overloads for && r-value references.

#include<fstream>
#include<string>

// a representation of video data
// note-a value type so it will want to own its actual data through a 
// unique_ptr or similar.
struct video_data
{
};

struct preprocessing
{
    void process(video_data&);
};

struct analyze
{
    void process(video_data&);
};

struct stabilize
{
    void process(video_data&);
};

struct video_out
{
    video_out(std::string const& where);
    void process(video_data&);
};

struct flush
{
    void process(video_out&);
};

// now define the interactions
auto operator>>(std::string const& path, preprocessing proc) -> video_data
{
    video_data dat;
    proc.process(dat);
    return dat;
}

auto operator>>(video_data dat, analyze proc) -> video_data
{
    proc.process(dat);
    return dat;
}

auto operator>>(video_data dat, stabilize proc) -> video_data
{
    proc.process(dat);
    return dat;
}

auto operator>>(video_data dat, video_out proc) -> video_out
{
    proc.process(dat);
    return std::move(proc);
}

auto operator>>(video_out dat, flush proc) -> video_out
{
    proc.process(dat);
    return std::move(dat);
}

// now build a chain
int test(std::string const& videofilelocation, std::string const& outputfilename)
{
    videofilelocation >> preprocessing() >> analyze() >> stabilize() >> video_out(outputfilename) >> flush();
} 

这篇关于在C ++中完全自定义的流运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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