well...still playing around here

This commit is contained in:
klemens-morgenstern 2016-02-14 23:27:13 +01:00
parent 40323899fc
commit 88fef81b0c
8 changed files with 103 additions and 79 deletions

View File

@ -20,11 +20,11 @@
#include <boost/process/windows/initializers/inherit_env.hpp>
#include <boost/process/windows/initializers/run_exe.hpp>
#include <boost/process/windows/initializers/set_args.hpp>
#include <boost/process/windows/initializers/set_cmd_line.hpp>
#include <boost/process/windows/initializers/set_env.hpp>
#include <boost/process/windows/initializers/set_on_error.hpp>
#include <boost/process/windows/initializers/show_window.hpp>
#include <boost/process/windows/initializers/start_dir.hpp>
#include <boost/process/detail/initializers/throw_on_error.hpp>
#include <boost/process/windows/initializers/set_cmd.hpp>
#endif

View File

@ -14,10 +14,12 @@
namespace boost { namespace process { namespace windows { namespace initializers {
struct inherit_env : public ::boost::process::detail::initializers::base
struct inherit_env_ : public ::boost::process::detail::initializers::base
{
};
static constexpr inherit_env_ inherit_env;
}}}}
#endif

View File

@ -11,16 +11,17 @@
#define BOOST_PROCESS_WINDOWS_INITIALIZERS_SET_ARGS_HPP
#include <boost/process/detail/initializers/base.hpp>
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
#include <boost/range/algorithm/copy.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/shared_array.hpp>
#include <sstream>
#include <initializer_list>
namespace boost { namespace process { namespace windows { namespace initializers {
template <class Range>
template <class Range, bool Append = false>
struct set_args_ : ::boost::process::detail::initializers::base
{
typedef typename Range::const_iterator const_iterator;
@ -30,8 +31,8 @@ struct set_args_ : ::boost::process::detail::initializers::base
explicit set_args_(const Range &args)
{
const_iterator it = boost::const_begin(args);
const_iterator end = boost::const_end(args);
const_iterator it = std::begin(args);
const_iterator end = std::end (args);
if (it != end)
{
exe_ = *it;
@ -50,50 +51,61 @@ struct set_args_ : ::boost::process::detail::initializers::base
}
os << static_cast<char_type>(' ');
}
string_type s = os.str();
cmd_line_.reset(new char_type[s.size() + 1]);
boost::copy(s, cmd_line_.get());
cmd_line_[s.size()] = 0;
cmd_line_ s = os.str();
}
else
{
cmd_line_.reset(new char_type[1]());
}
cmd_line_ = "";
}
template <class WindowsExecutor>
void on_setup(WindowsExecutor &e) const
{
e.cmd_line = cmd_line_.get();
e.cmd_line = cmd_line_.c_str();
if (!e.exe && !exe_.empty())
e.exe = exe_.c_str();
}
private:
boost::shared_array<char_type> cmd_line_;
string_type cmd_line_;
string_type exe_;
};
struct args_
{
template <class Range>
set_args_<Range> operator()(const Range &range) const
set_args_<Range> operator()(const Range &range) const
{
return set_args_<Range>(range);
}
template <class Range>
set_args_<Range> operator+=(const Range &range) const
set_args_<Range, true> operator+=(const Range &range) const
{
return set_args_<Range>(range);
}
template <class Range>
set_args_<Range> operator= (const Range &range) const
set_args_<Range> operator= (const Range &range) const
{
return set_args_<Range>(range);
}
template <class Char>
set_args_<std::vector<Char*>> operator()(const std::initializer_list<Char*> &range) const
{
return set_args_<std::vector<Char*>>(range);
}
template <class Char>
set_args_<std::vector<Char*>, true> operator+=(const std::initializer_list<Char*> &range) const
{
return set_args_<std::vector<Char*>>(range);
}
template <class Char>
set_args_<std::vector<Char*>> operator= (const std::initializer_list<Char*> &range) const
{
return set_args_<std::vector<Char*>>(range);
}
};
constexpr args_ args;
constexpr args_ set_args;

View File

@ -42,47 +42,48 @@ private:
boost::shared_array<char_type> cmd_line_;
};
struct cmd_line_
struct cmd_
{
inline set_cmd_line_<std::wstring> operator()(const wchar_t *ws)
inline set_cmd_line_<std::wstring> operator()(const wchar_t *ws) const
{
return set_cmd_line_<std::wstring>(ws);
}
inline set_cmd_line_<std::wstring> operator= (const wchar_t *ws)
inline set_cmd_line_<std::wstring> operator= (const wchar_t *ws) const
{
return set_cmd_line_<std::wstring>(ws);
}
inline set_cmd_line_<std::wstring> operator()(const std::wstring &ws)
inline set_cmd_line_<std::wstring> operator()(const std::wstring &ws) const
{
return set_cmd_line_<std::wstring>(ws);
}
inline set_cmd_line_<std::wstring> operator= (const std::wstring &ws)
inline set_cmd_line_<std::wstring> operator= (const std::wstring &ws) const
{
return set_cmd_line_<std::wstring>(ws);
}
#if !defined( BOOST_NO_ANSI_APIS )
inline set_cmd_line_<std::string> operator()(const char *s)
inline set_cmd_line_<std::string> operator()(const char *s) const
{
return set_cmd_line_<std::string>(s);
}
inline set_cmd_line_<std::string> operator= (const char *s)
inline set_cmd_line_<std::string> operator= (const char *s) const
{
return set_cmd_line_<std::string>(s);
}
inline set_cmd_line_<std::string> operator()(const std::string &s)
inline set_cmd_line_<std::string> operator()(const std::string &s) const
{
return set_cmd_line_<std::string>(s);
}
inline set_cmd_line_<std::string> operator= (const std::string &s)
inline set_cmd_line_<std::string> operator= (const std::string &s) const
{
return set_cmd_line_<std::string>(s);
}
#endif //BOOST_NO_ANSI_APIS
};
constexpr cmd_line_ cmd_line;
constexpr static cmd_ cmd;
constexpr static cmd_ set_cmd;
}}}}

View File

@ -44,7 +44,7 @@ struct async_stdin : ::boost::process::detail::initializers::base
};
struct close_stdin : public ::boost::process::detail::initializers::base
struct close_stdin_ : public ::boost::process::detail::initializers::base
{
template <class WindowsExecutor>
void on_setup(WindowsExecutor &e) const
@ -54,22 +54,24 @@ struct close_stdin : public ::boost::process::detail::initializers::base
}
};
static constexpr close_stdin_ close_stdin;
struct std_in_
{
close_stdin operator()(std::nullptr_t) const { return close_stdin();}
close_stdin operator= (std::nullptr_t) const { return close_stdin();}
sync_stdin operator()(const boost::iostreams::file_descriptor_source & source) const {return sync_stdin(source);}
sync_stdin operator= (const boost::iostreams::file_descriptor_source & source) const {return sync_stdin(source);}
sync_stdin operator()(const boost::iostreams::file_descriptor & descr) const {return sync_stdin(descr.handle());}
sync_stdin operator= (const boost::iostreams::file_descriptor & descr) const {return sync_stdin(descr.handle());}
sync_stdin operator()(boost::iostreams::stream<boost::iostreams::file_descriptor> & strm) const {return sync_stdin(strm->handle());}
sync_stdin operator= (boost::iostreams::stream<boost::iostreams::file_descriptor> & strm) const {return sync_stdin(strm->handle());}
sync_stdin operator()(boost::process::windows::pipe& pipe_) const {return sync_stdin(boost::iostreams::file_descriptor_source(pipe_.source));}
sync_stdin operator= (boost::process::windows::pipe& pipe_) const {return sync_stdin(boost::iostreams::file_descriptor_source(pipe_.source));}
close_stdin_ operator()(std::nullptr_t) const { return close_stdin_();}
close_stdin_ operator= (std::nullptr_t) const { return close_stdin_();}
sync_stdin operator()(const boost::iostreams::file_descriptor_source & source) const {return sync_stdin(source);}
sync_stdin operator= (const boost::iostreams::file_descriptor_source & source) const {return sync_stdin(source);}
sync_stdin operator()(const boost::iostreams::file_descriptor & descr) const {return sync_stdin(descr.handle());}
sync_stdin operator= (const boost::iostreams::file_descriptor & descr) const {return sync_stdin(descr.handle());}
sync_stdin operator()(boost::iostreams::stream<boost::iostreams::file_descriptor> & strm) const {return sync_stdin(strm->handle());}
sync_stdin operator= (boost::iostreams::stream<boost::iostreams::file_descriptor> & strm) const {return sync_stdin(strm->handle());}
sync_stdin operator()(boost::process::windows::pipe& pipe_) const {return sync_stdin(boost::iostreams::file_descriptor_source(pipe_.source));}
sync_stdin operator= (boost::process::windows::pipe& pipe_) const {return sync_stdin(boost::iostreams::file_descriptor_source(pipe_.source));}
async_stdin operator()(std::istream & ostr) const {return async_stdin(ostr);}
async_stdin operator= (std::istream & ostr) const {return async_stdin(ostr);}
async_stdin operator()(std::istream & ostr) const {return async_stdin(ostr);}
async_stdin operator= (std::istream & ostr) const {return async_stdin(ostr);}
//alright, that may overflow. if it should not do that, use a fixed-size array or a functor.
template<template<class> class Container> async_stdin operator()(Container<char> & buffer) const {return async_stdin(buffer);};

View File

@ -7,33 +7,32 @@
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#define BOOST_TEST_MAIN
#define BOOST_TEST_IGNORE_SIGCHLD
#include <boost/test/included/unit_test.hpp>
#include <boost/core/lightweight_test.hpp>
#include <iostream>
#include <boost/process.hpp>
#include <boost/system/error_code.hpp>
namespace bp = boost::process;
namespace bpi = boost::process::initializers;
BOOST_AUTO_TEST_CASE(run_exe_success)
{
using boost::unit_test::framework::master_test_suite;
boost::system::error_code ec;
bp::execute(
bpi::run_exe(master_test_suite().argv[1]),
bpi::set_on_error(ec)
);
BOOST_CHECK(!ec);
}
BOOST_AUTO_TEST_CASE(run_exe_error)
int main(int argc, char* argv[])
{
boost::system::error_code ec;
bp::execute(
bpi::run_exe("doesnt-exist"),
bpi::set_on_error(ec)
BOOST_TEST(!ec);
auto c = bp::execute(
bpi::exe(argv[1]),
bpi::error = ec
);
BOOST_CHECK(ec);
BOOST_TEST(!ec);
auto c2 = bp::execute(
bpi::exe = "doesnt-exist",
bpi::error(ec)
);
BOOST_TEST(ec);
return boost::report_errors();
}

View File

@ -7,21 +7,22 @@
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#define BOOST_TEST_MAIN
#define BOOST_TEST_IGNORE_SIGCHLD
#include <boost/test/included/unit_test.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/process.hpp>
#include <boost/system/error_code.hpp>
namespace bp = boost::process;
namespace bpi = boost::process::initializers;
BOOST_AUTO_TEST_CASE(set_on_error_test)
int main(int argc, char* argv[])
{
boost::system::error_code ec;
bp::execute(
bpi::run_exe("doesnt-exist"),
bpi::set_on_error(ec)
bpi::exe("doesnt-exist"),
bpi::error(ec)
);
BOOST_CHECK(ec);
BOOST_TEST(ec);
return boost::report_errors();
}

View File

@ -7,22 +7,29 @@
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#define BOOST_TEST_MAIN
#define BOOST_TEST_IGNORE_SIGCHLD
#include <boost/test/included/unit_test.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/process.hpp>
#include <boost/system/system_error.hpp>
namespace bp = boost::process;
namespace bpi = boost::process::initializers;
BOOST_AUTO_TEST_CASE(throw_on_error_test)
int main(int argc, char* argv[])
{
BOOST_CHECK_THROW(
bool thrown = false;
try {
bp::execute(
bpi::run_exe("doesnt-exist"),
bpi::throw_on_error()
),
boost::system::system_error
);
bpi::cmd="doesnt-exist",
bpi::throw_on_error
);
thrown = false;
}
catch(boost::system::system_error & )
{
thrown = true;
}
BOOST_TEST(thrown);
return boost::report_errors();
}