From b0bf46a7942611729dd8d9d8b8d78cedf26c4685 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 13 Apr 2023 14:42:36 +0200 Subject: Add process_print_callback() function in addition to *_{start,run}_*() --- libbutl/process-run.txx | 69 +++++++++++++++++++++++++++++++++++++++---------- libbutl/process.hxx | 9 +++++++ 2 files changed, 64 insertions(+), 14 deletions(-) (limited to 'libbutl') diff --git a/libbutl/process-run.txx b/libbutl/process-run.txx index 67426f0..6c903a8 100644 --- a/libbutl/process-run.txx +++ b/libbutl/process-run.txx @@ -131,13 +131,13 @@ namespace butl typename... A, typename std::size_t... index> process - process_start (std::index_sequence, - const C& cmdc, - I&& in, - O&& out, - E&& err, - const process_env& env, - A&&... args) + process_start_impl (std::index_sequence, + const C& cmdc, + I&& in, + O&& out, + E&& err, + const process_env& env, + A&&... args) { // Map stdin/stdout/stderr arguments to their integer values, as expected // by the process constructor. @@ -188,13 +188,13 @@ namespace butl const process_env& env, A&&... args) { - return process_start (std::index_sequence_for (), - cmdc, - std::forward (in), - std::forward (out), - std::forward (err), - env, - std::forward (args)...); + return process_start_impl (std::index_sequence_for (), + cmdc, + std::forward (in), + std::forward (out), + std::forward (err), + env, + std::forward (args)...); } template (args)...); } + + template + void + process_print_impl (std::index_sequence, + const C& cmdc, + const process_env& env, + A&&... args) + { + // Construct the command line array. + // + const std::size_t args_size (sizeof... (args)); + + small_vector cmd; + + assert (env.path != nullptr); + cmd.push_back (env.path->recall_string ()); + + std::string storage[args_size != 0 ? args_size : 1]; + + const char* dummy[] = { + nullptr, process_args_as_wrapper (cmd, args, storage[index])... }; + + cmd.push_back (dummy[0]); // NULL (and get rid of unused warning). + + cmdc (cmd.data (), cmd.size ()); + } + + template + inline void + process_print_callback (const C& cmdc, + const process_env& env, + A&&... args) + { + process_print_impl (std::index_sequence_for (), + cmdc, + env, + std::forward (args)...); + } } diff --git a/libbutl/process.hxx b/libbutl/process.hxx index 8648690..5a6837f 100644 --- a/libbutl/process.hxx +++ b/libbutl/process.hxx @@ -827,6 +827,15 @@ namespace butl const process_env&, A&&... args); + // Call the callback without actually running/starting anything. + // + template + void + process_print_callback (const C&, + const process_env&, + A&&... args); + // Conversion of types to their C string representations. Can be overloaded // (including via ADL) for custom types. The default implementation calls // to_string() which covers all the numeric values via std::to_string () and -- cgit v1.1