1 // Copyright (c) 2006, 2007 Julio M. Merino Vidal
2 // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
3 // Copyright (c) 2009 Boris Schaeling
4 // Copyright (c) 2010 Felipe Tanus, Boris Schaeling
5 // Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling
6 // Copyright (c) 2016 Klemens D. Morgenstern
8 // Distributed under the Boost Software License, Version 1.0. (See accompanying
9 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
12 * \file boost/process/execute.hpp
14 * Defines a function to execute a program.
17 #ifndef BOOST_PROCESS_EXECUTE_HPP
18 #define BOOST_PROCESS_EXECUTE_HPP
20 #include <boost/process/detail/config.hpp>
21 #include <boost/process/detail/traits.hpp>
23 #if defined(BOOST_POSIX_API)
24 #include <boost/process/detail/posix/executor.hpp>
25 #elif defined(BOOST_WINDOWS_API)
26 #include <boost/process/detail/windows/executor.hpp>
29 #include <boost/process/detail/basic_cmd.hpp>
30 #include <boost/process/detail/handler.hpp>
32 #include <boost/fusion/view.hpp>
33 #include <boost/fusion/container.hpp>
34 #include <boost/fusion/sequence.hpp>
35 #include <boost/fusion/tuple.hpp>
36 #include <boost/fusion/algorithm/transformation/filter_if.hpp>
37 #include <boost/fusion/adapted/std_tuple.hpp>
38 #include <boost/fusion/container/vector/convert.hpp>
40 #include <type_traits>
43 namespace boost { namespace process {
50 template<typename ...Args>
53 template<typename First, typename ...Args>
54 struct has_wchar<First, Args...>
56 typedef has_wchar<Args...> next;
57 typedef typename std::remove_cv<
58 typename std::remove_reference<First>::type>::type res_type;
60 constexpr static bool my_value = is_wchar_t<res_type>::value;
61 constexpr static bool value = my_value || next::value;
63 typedef std::integral_constant<bool, value> type;
66 template<typename First>
67 struct has_wchar<First>
69 typedef typename std::remove_cv<
70 typename std::remove_reference<First>::type>::type res_type;
72 constexpr static bool value = is_wchar_t<res_type>::value;
74 typedef std::integral_constant<bool, value> type;
78 #if defined(BOOST_WINDOWS_API)
79 //everything needs to be wchar_t
80 #if defined(BOOST_NO_ANSI_APIS)
81 template<bool has_wchar>
82 struct required_char_type
87 template<bool has_wchar> struct required_char_type;
88 template<> struct required_char_type<true>
92 template<> struct required_char_type<false>
98 #elif defined(BOOST_POSIX_API)
99 template<bool has_wchar>
100 struct required_char_type
106 template<typename ... Args>
107 using required_char_type_t = typename required_char_type<
108 has_wchar<Args...>::value>::type;
111 template<typename Iterator, typename End, typename ...Args>
112 struct make_builders_from_view
114 typedef boost::fusion::set<Args...> set;
115 typedef typename boost::fusion::result_of::deref<Iterator>::type ref_type;
116 typedef typename std::remove_reference<ref_type>::type res_type;
117 typedef typename initializer_tag<res_type>::type tag;
118 typedef typename initializer_builder<tag>::type builder_type;
119 typedef typename boost::fusion::result_of::has_key<set, builder_type> has_key;
121 typedef typename boost::fusion::result_of::next<Iterator>::type next_itr;
122 typedef typename make_builders_from_view<next_itr, End>::type next;
125 std::conditional<has_key::value,
126 typename make_builders_from_view<next_itr, End, Args...>::type,
127 typename make_builders_from_view<next_itr, End, Args..., builder_type>::type
132 template<typename Iterator, typename ...Args>
133 struct make_builders_from_view<Iterator, Iterator, Args...>
135 typedef boost::fusion::set<Args...> type;
138 template<typename Builders>
142 builder_ref(Builders & builders) : builders(builders) {};
145 void operator()(T && value) const
147 typedef typename initializer_tag<typename std::remove_reference<T>::type>::type tag;
148 typedef typename initializer_builder<tag>::type builder_type;
149 boost::fusion::at_key<builder_type>(builders)(std::forward<T>(value));
154 struct get_initializers_result
156 typedef typename T::result_type type;
160 struct get_initializers_result<boost::fusion::void_>
162 typedef boost::fusion::void_ type;
165 template<typename ...Args>
171 template<typename T, typename ...Stack>
172 struct invoke_get_initializer_collect_keys;
174 template<typename ...Stack>
175 struct invoke_get_initializer_collect_keys<boost::fusion::vector<>, Stack...>
177 typedef helper_vector<Stack...> type;
181 template<typename First, typename ...Args, typename ...Stack>
182 struct invoke_get_initializer_collect_keys<boost::fusion::vector<First, Args...>, Stack...>
184 typedef typename invoke_get_initializer_collect_keys<boost::fusion::vector<Args...>, Stack..., First>::type next;
185 typedef helper_vector<Stack...> stack_t;
187 typedef typename std::conditional<std::is_same<boost::fusion::void_, First>::value,
188 stack_t, next>::type type;
194 template<typename Keys>
195 struct invoke_get_initializer;
197 template<typename ...Args>
198 struct invoke_get_initializer<helper_vector<Args...>>
201 typedef boost::fusion::tuple<typename get_initializers_result<Args>::type...> result_type;
203 template<typename Sequence>
204 static result_type call(Sequence & seq)
206 return result_type(boost::fusion::at_key<Args>(seq).get_initializer()...);;
214 template<typename ...Args>
215 inline boost::fusion::tuple<typename get_initializers_result<Args>::type...>
216 get_initializers(boost::fusion::set<Args...> & builders)
218 //typedef boost::fusion::tuple<typename get_initializers_result<Args>::type...> return_type;
219 typedef typename invoke_get_initializer_collect_keys<boost::fusion::vector<Args...>>::type keys;
220 return invoke_get_initializer<keys>::call(builders);
224 template<typename Char, typename ... Args>
225 inline child basic_execute_impl(Args && ... args)
227 //create a tuple from the argument list
228 boost::fusion::tuple<typename std::remove_reference<Args>::type&...> tup(args...);
230 auto inits = boost::fusion::filter_if<
231 boost::process::detail::is_initializer<
232 typename std::remove_reference<
238 auto others = boost::fusion::filter_if<
240 boost::process::detail::is_initializer<
241 typename std::remove_reference<
248 // typename detail::make_builders_from_view<decltype(others)>::type builders;
250 //typedef typename boost::fusion::result_of::as_vector<decltype(inits)>::type inits_t;
251 typedef typename boost::fusion::result_of::as_vector<decltype(others)>::type others_t;
252 // typedef decltype(others) others_t;
253 typedef typename ::boost::process::detail::make_builders_from_view<
254 typename boost::fusion::result_of::begin<others_t>::type,
255 typename boost::fusion::result_of::end <others_t>::type>::type builder_t;
258 ::boost::process::detail::builder_ref<builder_t> builder_ref(builders);
260 boost::fusion::for_each(others, builder_ref);
261 auto other_inits = ::boost::process::detail::get_initializers(builders);
264 boost::fusion::joint_view<decltype(other_inits), decltype(inits)> complete_inits(other_inits, inits);
266 auto exec = boost::process::detail::api::make_executor<Char>(complete_inits);
270 template<typename ...Args>
271 inline child execute_impl(Args&& ... args)
273 typedef required_char_type_t<Args...> req_char_type;
275 return basic_execute_impl<req_char_type>(
276 boost::process::detail::char_converter_t<req_char_type, Args>::conv(
277 std::forward<Args>(args))...