Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
659 views
in Technique[技术] by (71.8m points)

c++ - Function with variable parameter size: How to conditionally set some arguments?

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??" );
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...