aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2015-09-04 13:38:27 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2015-09-04 13:38:27 +0200
commitf3f46a1656207a1c681e7c53cc3bd2c9a28fa887 (patch)
treee3f35ce96d8af5c7d0ec500abe502c3b681092e6
parentdecd319bc1bc7d427f92feba29d408eb2edb973d (diff)
Allow calling process::wait() multiple types, make process move-only
-rw-r--r--butl/process11
-rw-r--r--butl/process.cxx12
-rw-r--r--butl/process.ixx11
3 files changed, 28 insertions, 6 deletions
diff --git a/butl/process b/butl/process
index c810b58..534ad88 100644
--- a/butl/process
+++ b/butl/process
@@ -67,23 +67,32 @@ namespace butl
// Wait for the process to terminate. Return true if the process
// terminated normally and with the zero exit status. Throw
- // process_error if anything goes wrong.
+ // process_error if anything goes wrong. This function can be
+ // called multiple times with subsequent calls simply returning
+ // the status.
//
bool
wait ();
~process () {if (id != 0) wait ();}
+ // Move constructible-only type.
+ //
+ process (process&&);
+ process& operator= (process&&) = delete;
+
process (const process&) = delete;
process& operator= (const process&) = delete;
#ifndef _WIN32
typedef pid_t id_type;
+ typedef int status_type;
#else
typedef void* id_type; // Win32 HANDLE.
#endif
id_type id;
+ status_type status;
int out_fd; // Write to this fd to send to the new process' stdin.
int in_ofd; // Read from this fd to receive from the new process' stdout.
diff --git a/butl/process.cxx b/butl/process.cxx
index 9025bdb..194ea4e 100644
--- a/butl/process.cxx
+++ b/butl/process.cxx
@@ -108,12 +108,14 @@ namespace butl
bool process::
wait ()
{
- int status;
- int r (waitpid (id, &status, 0));
- id = 0; // We have tried.
+ if (id != 0)
+ {
+ int r (waitpid (id, &status, 0));
+ id = 0; // We have tried.
- if (r == -1)
- throw process_error (errno, false);
+ if (r == -1)
+ throw process_error (errno, false);
+ }
return WIFEXITED (status) && WEXITSTATUS (status) == 0;
}
diff --git a/butl/process.ixx b/butl/process.ixx
index 0506793..a0948e1 100644
--- a/butl/process.ixx
+++ b/butl/process.ixx
@@ -11,4 +11,15 @@ namespace butl
inline process::
process (char const* args[], process& in, int out, int err)
: process (nullptr, args, in, out, err) {}
+
+ inline process::
+ process (process&& p)
+ : id (p.id),
+ status (p.status),
+ out_fd (p.out_fd),
+ in_ofd (p.in_ofd),
+ in_efd (p.in_efd)
+ {
+ p.id = 0;
+ }
}