参数大小可变的函数:如何有条件地设置一些参数? [英] Function with variable parameter size: How to conditionally set some arguments?

查看:92
本文介绍了参数大小可变的函数:如何有条件地设置一些参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要创建带有输出重定向的boost :: process,您应该执行以下操作:

To create a boost::process with output redirection, you should do:

bp::ipstream out;
bp::child c("c++filt", std_out > out);

现在,有条件地控制重定向的语法是什么:

Now, what would be the syntax to conditionally control the redirection:

bool redirect = true; // or false
bp::ipstream out;
bp::child c("c++filt", (redirect) ? std_out > out : "what should I put here??" );

推荐答案

花哨的boost::process API围绕提供实现on_setupon_erroron_successhandler对象(可能还有更多,具体取决于当前OS)方法,这些方法将在某些内部流程启动器的上下文中在流程构造调用时执行,并且将能够更改流程启动器的行为. std_out > out是重载运算符,它将返回此类处理程序.有关详细信息,请参见增强流程扩展说明文件.

The fancy boost::process API revolves around supplying handler objects implementing on_setup, on_error, on_success (and potentially some more, depending on current OS) methods that will be executed at process construction call in context of some internal process launcher and will be able to alter process launcher behavior. std_out > out is an overloaded operator that will return such handler. More information can be found at boost process Extensions documentation.

因此,有条件地控制重定向和其他参数的通用方法是编写通用处理程序代理,以接受可选的真实处理程序并调用真实处理程序的适当方法:

So the generic way to conditionally control the redirection and other parameters would be to write generic handler proxy accepting optional real handler and calling appropriate methods of real handler:

#include <boost/process.hpp>
#include <boost/process/extend.hpp>
#include <boost/optional.hpp>

#include <memory>
#include <utility>

template<typename TRealHandler> class
t_OptionalHandler
:    public ::boost::process::extend::handler
{
    private: using t_OptionalRealHandler = ::boost::optional<TRealHandler>;

    private: t_OptionalRealHandler m_real_handler;

    public:
    t_OptionalHandler(void)
    :   m_real_handler{}
    {}

    public: explicit
    t_OptionalHandler(TRealHandler && real_handler)
    :   m_real_handler{::std::move(real_handler)}
    {}

    template<typename TExecutor>
    void on_setup(TExecutor & e) const
    {
        if(m_real_handler)
        {
            m_real_handler.get().on_setup(e);
        }
    }

    template<typename TExecutor>
    void on_error(TExecutor & e, const std::error_code & code) const
    {
        if(m_real_handler)
        {
            m_real_handler.get().on_error(e, code);
        }
    }

    template<typename TExecutor>
    void on_success(TExecutor & e) const
    {
        if(m_real_handler)
        {
            m_real_handler.get().on_success(e);
        }
    }
};

int main()
{
    bool const need_to_redirect{false};
    ::std::unique_ptr<::boost::process::ipstream> const p_stream
    {
        need_to_redirect?
        new ::boost::process::ipstream{}
        :
        nullptr
    };
    using t_OptionalStdOutRedirectionHandler = t_OptionalHandler
    <
        decltype(::boost::process::std_out > *p_stream)
     >;
    ::boost::process::child ch
    (
        "cmd"
    ,   need_to_redirect?
        t_OptionalStdOutRedirectionHandler{::boost::process::std_out > *p_stream}
        :
        t_OptionalStdOutRedirectionHandler{}
    );
    ch.wait();
    return(0);
}

这篇关于参数大小可变的函数:如何有条件地设置一些参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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