|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r65432 - in sandbox/SOC/2010/process: boost/process libs/process/example libs/process/test libs/process/test/inclusion
From: boris_at_[hidden]
Date: 2010-09-15 17:40:21
Author: bschaeling
Date: 2010-09-15 17:40:19 EDT (Wed, 15 Sep 2010)
New Revision: 65432
URL: http://svn.boost.org/trac/boost/changeset/65432
Log:
Added boost::process::stream_ends to improve readability
Added:
sandbox/SOC/2010/process/boost/process/stream_ends.hpp (contents, props changed)
sandbox/SOC/2010/process/libs/process/test/inclusion/stream_ends.cpp (contents, props changed)
Text files modified:
sandbox/SOC/2010/process/boost/process/all.hpp | 1
sandbox/SOC/2010/process/boost/process/context.hpp | 9 +++----
sandbox/SOC/2010/process/boost/process/operations.hpp | 31 ++++++++++++++-------------
sandbox/SOC/2010/process/boost/process/stream_behavior.hpp | 24 ++++++++++----------
sandbox/SOC/2010/process/libs/process/example/file_descriptors_setup.cpp | 18 +++++++---------
sandbox/SOC/2010/process/libs/process/example/redirect_to.cpp | 29 +++++++++-----------------
sandbox/SOC/2010/process/libs/process/test/child.cpp | 44 +++++++++++++++++----------------------
sandbox/SOC/2010/process/libs/process/test/handle.cpp | 6 ++--
8 files changed, 73 insertions(+), 89 deletions(-)
Modified: sandbox/SOC/2010/process/boost/process/all.hpp
==============================================================================
--- sandbox/SOC/2010/process/boost/process/all.hpp (original)
+++ sandbox/SOC/2010/process/boost/process/all.hpp 2010-09-15 17:40:19 EDT (Wed, 15 Sep 2010)
@@ -33,5 +33,6 @@
#include <boost/process/self.hpp>
#include <boost/process/status.hpp>
#include <boost/process/stream_behavior.hpp>
+#include <boost/process/stream_ends.hpp>
#endif
Modified: sandbox/SOC/2010/process/boost/process/context.hpp
==============================================================================
--- sandbox/SOC/2010/process/boost/process/context.hpp (original)
+++ sandbox/SOC/2010/process/boost/process/context.hpp 2010-09-15 17:40:19 EDT (Wed, 15 Sep 2010)
@@ -28,13 +28,12 @@
# include <windows.h>
#endif
-#include <boost/process/handle.hpp>
+#include <boost/process/stream_ends.hpp>
#include <boost/process/environment.hpp>
#include <boost/process/self.hpp>
#include <boost/process/stream_behavior.hpp>
#include <boost/function.hpp>
#include <string>
-#include <utility>
namespace boost {
namespace process {
@@ -51,17 +50,17 @@
/**
* Behavior of the standard input stream.
*/
- boost::function<std::pair<handle, handle> (bool)> stdin_behavior;
+ boost::function<stream_ends (bool)> stdin_behavior;
/**
* Behavior of the standard output stream.
*/
- boost::function<std::pair<handle, handle> (bool)> stdout_behavior;
+ boost::function<stream_ends (bool)> stdout_behavior;
/**
* Behavior of the standard error stream.
*/
- boost::function<std::pair<handle, handle> (bool)> stderr_behavior;
+ boost::function<stream_ends (bool)> stderr_behavior;
/**
* Process name.
Modified: sandbox/SOC/2010/process/boost/process/operations.hpp
==============================================================================
--- sandbox/SOC/2010/process/boost/process/operations.hpp (original)
+++ sandbox/SOC/2010/process/boost/process/operations.hpp 2010-09-15 17:40:19 EDT (Wed, 15 Sep 2010)
@@ -44,6 +44,7 @@
#include <boost/process/child.hpp>
#include <boost/process/context.hpp>
+#include <boost/process/stream_ends.hpp>
#include <boost/process/handle.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/algorithm/string/predicate.hpp>
@@ -187,15 +188,15 @@
inline child create_child(const std::string &executable, Arguments args,
Context ctx)
{
- std::pair<handle, handle> stdin_pair;
+ stream_ends stdin_pair;
if (ctx.stdin_behavior)
stdin_pair = ctx.stdin_behavior(true);
- std::pair<handle, handle> stdout_pair;
+ stream_ends stdout_pair;
if (ctx.stdout_behavior)
stdout_pair = ctx.stdout_behavior(false);
- std::pair<handle, handle> stderr_pair;
+ stream_ends stderr_pair;
if (ctx.stderr_behavior)
stderr_pair = ctx.stderr_behavior(false);
@@ -234,9 +235,9 @@
_exit(127);
}
- handle hstdin = stdin_pair.first;
- handle hstdout = stdout_pair.first;
- handle hstderr = stderr_pair.first;
+ handle hstdin = stdin_pair.child;
+ handle hstdout = stdout_pair.child;
+ handle hstderr = stderr_pair.child;
if (hstdin.valid())
{
@@ -329,18 +330,18 @@
delete[] envp.second;
return child(pid,
- stdin_pair.second,
- stdout_pair.second,
- stderr_pair.second);
+ stdin_pair.parent,
+ stdout_pair.parent,
+ stderr_pair.parent);
}
#elif defined(BOOST_WINDOWS_API)
STARTUPINFOA startup_info;
ZeroMemory(&startup_info, sizeof(startup_info));
startup_info.cb = sizeof(startup_info);
startup_info.dwFlags |= STARTF_USESTDHANDLES;
- startup_info.hStdInput = stdin_pair.first.native();
- startup_info.hStdOutput = stdout_pair.first.native();
- startup_info.hStdError = stderr_pair.first.native();
+ startup_info.hStdInput = stdin_pair.child.native();
+ startup_info.hStdOutput = stdout_pair.child.native();
+ startup_info.hStdError = stderr_pair.child.native();
ctx.setup(startup_info);
@@ -369,9 +370,9 @@
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("CloseHandle() failed");
return child(hprocess,
- stdin_pair.second,
- stdout_pair.second,
- stderr_pair.second);
+ stdin_pair.parent,
+ stdout_pair.parent,
+ stderr_pair.parent);
#endif
}
Modified: sandbox/SOC/2010/process/boost/process/stream_behavior.hpp
==============================================================================
--- sandbox/SOC/2010/process/boost/process/stream_behavior.hpp (original)
+++ sandbox/SOC/2010/process/boost/process/stream_behavior.hpp 2010-09-15 17:40:19 EDT (Wed, 15 Sep 2010)
@@ -31,10 +31,10 @@
# include <rpc.h>
#endif
+#include <boost/process/stream_ends.hpp>
#include <boost/process/handle.hpp>
#include <string>
#include <algorithm>
-#include <utility>
namespace boost {
namespace process {
@@ -48,9 +48,9 @@
class close
{
public:
- std::pair<handle, handle> operator()(bool) const
+ stream_ends operator()(bool) const
{
- return std::make_pair(handle(), handle());
+ return stream_ends();
}
};
@@ -67,9 +67,9 @@
{
}
- std::pair<handle, handle> operator()(bool) const
+ stream_ends operator()(bool) const
{
- return std::make_pair(h_, handle());
+ return stream_ends(h_, handle());
}
private:
@@ -84,7 +84,7 @@
class pipe
{
public:
- std::pair<handle, handle> operator()(bool in) const
+ stream_ends operator()(bool in) const
{
handle::native_type ends[2];
#if defined(BOOST_POSIX_API)
@@ -107,7 +107,7 @@
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR(
"SetHandleInformation() failed");
#endif
- return std::make_pair(child_end, parent_end);
+ return stream_ends(child_end, parent_end);
}
};
@@ -126,7 +126,7 @@
{
}
- std::pair<handle, handle> operator()(bool in) const
+ stream_ends operator()(bool in) const
{
#if defined(BOOST_POSIX_API)
if (mkfifo(name_.c_str(), S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1)
@@ -166,7 +166,7 @@
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR(
"SetHandleInformation() failed");
#endif
- return std::make_pair(child_end, parent_end);
+ return stream_ends(child_end, parent_end);
}
private:
@@ -187,7 +187,7 @@
class async_pipe
{
public:
- std::pair<handle, handle> operator()(bool in) const
+ stream_ends operator()(bool in) const
{
UUID uuid;
RPC_STATUS s = UuidCreateSequential(&uuid);
@@ -223,7 +223,7 @@
class null
{
public:
- std::pair<handle, handle> operator()(bool in) const
+ stream_ends operator()(bool in) const
{
#if defined(BOOST_POSIX_API)
std::string filename = in ? "/dev/zero" : "/dev/null";
@@ -241,7 +241,7 @@
HANDLE_FLAG_INHERIT))
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("SetHandleInformation() failed");
#endif
- return std::make_pair(child_end, handle());
+ return stream_ends(child_end, handle());
}
};
Added: sandbox/SOC/2010/process/boost/process/stream_ends.hpp
==============================================================================
--- (empty file)
+++ sandbox/SOC/2010/process/boost/process/stream_ends.hpp 2010-09-15 17:40:19 EDT (Wed, 15 Sep 2010)
@@ -0,0 +1,68 @@
+//
+// Boost.Process
+// ~~~~~~~~~~~~~
+//
+// Copyright (c) 2006, 2007 Julio M. Merino Vidal
+// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
+// Copyright (c) 2009 Boris Schaeling
+// Copyright (c) 2010 Felipe Tanus, Boris Schaeling
+//
+// 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)
+//
+
+/**
+ * \file boost/process/stream_ends.hpp
+ *
+ * Includes the declaration of the stream_ends class.
+ */
+
+#ifndef BOOST_PROCESS_STREAM_ENDS_HPP
+#define BOOST_PROCESS_STREAM_ENDS_HPP
+
+#include <boost/process/config.hpp>
+#include <boost/process/handle.hpp>
+
+namespace boost {
+namespace process {
+
+/**
+ * A pair of handles to configure streams.
+ *
+ * Stream behaviors return a pair of handles to specify how a child's stream
+ * should be configured and possibly the opposite end of a child's end. This
+ * is the end remaining in the parent process and which can be used for example
+ * to communicate with a child process through its standard streams.
+ */
+struct stream_ends {
+ /**
+ * The child's end.
+ */
+ handle child;
+
+ /**
+ * The parent's end.
+ */
+ handle parent;
+
+ /**
+ * Standard constructor creating two invalid handles.
+ */
+ stream_ends()
+ {
+ }
+
+ /**
+ * Helper constructor to easily initialize handles.
+ */
+ stream_ends(handle c, handle p)
+ : child(c),
+ parent(p)
+ {
+ }
+};
+
+}
+}
+
+#endif
Modified: sandbox/SOC/2010/process/libs/process/example/file_descriptors_setup.cpp
==============================================================================
--- sandbox/SOC/2010/process/libs/process/example/file_descriptors_setup.cpp (original)
+++ sandbox/SOC/2010/process/libs/process/example/file_descriptors_setup.cpp 2010-09-15 17:40:19 EDT (Wed, 15 Sep 2010)
@@ -23,24 +23,22 @@
#include <unistd.h>
//[file_descriptors_context
-std::pair<boost::process::handle, boost::process::handle> address =
- boost::process::behavior::pipe()(false);
-std::pair<boost::process::handle, boost::process::handle> pid =
- boost::process::behavior::pipe()(false);
+boost::process::stream_ends address = boost::process::behavior::pipe()(false);
+boost::process::stream_ends pid = boost::process::behavior::pipe()(false);
class context : public boost::process::context
{
public:
void setup(std::vector<bool> &closeflags)
{
- if (dup2(address.first.native(), 3) == -1)
+ if (dup2(address.child.native(), 3) == -1)
{
write(STDERR_FILENO, "dup2() failed\n", 14);
_exit(127);
}
closeflags[3] = false;
- if (dup2(pid.first.native(), 4) == -1)
+ if (dup2(pid.child.native(), 4) == -1)
{
write(STDERR_FILENO, "dup2() failed\n", 14);
_exit(127);
@@ -58,11 +56,11 @@
("--session")("--print-address=3")("--print-pid=4");
context ctx;
boost::process::create_child(exe, args, ctx);
- address.first.close();
- pid.first.close();
- boost::process::pistream isaddress(address.second);
+ address.child.close();
+ pid.child.close();
+ boost::process::pistream isaddress(address.parent);
std::cout << isaddress.rdbuf() << std::endl;
- boost::process::pistream ispid(pid.second);
+ boost::process::pistream ispid(pid.parent);
std::cout << ispid.rdbuf() << std::endl;
//]
}
Modified: sandbox/SOC/2010/process/libs/process/example/redirect_to.cpp
==============================================================================
--- sandbox/SOC/2010/process/libs/process/example/redirect_to.cpp (original)
+++ sandbox/SOC/2010/process/libs/process/example/redirect_to.cpp 2010-09-15 17:40:19 EDT (Wed, 15 Sep 2010)
@@ -27,29 +27,21 @@
#include <stdexcept>
//[redirect_to_stream
-std::pair<boost::process::handle, boost::process::handle> redirect_to(
- boost::process::handle h)
+boost::process::stream_ends redirect_to(boost::process::handle h)
{
-#if defined(BOOST_POSIX_API)
- h = dup(h.native());
- if (!h.valid())
- throw std::runtime_error("dup(2) failed");
-#elif defined(BOOST_WINDOWS_API)
- HANDLE h2;
- if (!DuplicateHandle(GetCurrentProcess(), h.native(),
- GetCurrentProcess(), &h2, 0, TRUE, DUPLICATE_SAME_ACCESS))
+#if defined(BOOST_WINDOWS_API)
+ if (!SetHandleInformation(h.native(), HANDLE_FLAG_INHERIT,
+ HANDLE_FLAG_INHERIT))
throw std::runtime_error("DuplicateHandle() failed");
- h = h2;
#endif
- return std::make_pair(h, boost::process::handle());
+ return boost::process::stream_ends(h, boost::process::handle());
}
//]
//[redirect_to_main
-std::pair<boost::process::handle, boost::process::handle> forward(
- std::pair<boost::process::handle, boost::process::handle> p)
+boost::process::stream_ends forward(boost::process::stream_ends ends)
{
- return p;
+ return ends;
}
int main()
@@ -59,12 +51,11 @@
std::vector<std::string> args;
- std::pair<boost::process::handle, boost::process::handle> p =
- boost::process::behavior::pipe()(false);
+ boost::process::stream_ends ends = boost::process::behavior::pipe()(false);
boost::process::context ctx;
- ctx.stdout_behavior = boost::bind(forward, p);
- ctx.stderr_behavior = boost::bind(redirect_to, p.first);
+ ctx.stdout_behavior = boost::bind(forward, ends);
+ ctx.stderr_behavior = boost::bind(redirect_to, ends.child);
boost::process::child c = boost::process::create_child(
executable, args, ctx);
Modified: sandbox/SOC/2010/process/libs/process/test/child.cpp
==============================================================================
--- sandbox/SOC/2010/process/libs/process/test/child.cpp (original)
+++ sandbox/SOC/2010/process/libs/process/test/child.cpp 2010-09-15 17:40:19 EDT (Wed, 15 Sep 2010)
@@ -236,25 +236,19 @@
#endif
}
-std::pair<bp::handle, bp::handle> redirect_to(bp::handle h)
+bp::stream_ends redirect_to(bp::handle h)
{
-#if defined(BOOST_POSIX_API)
- h = dup(h.native());
- if (!h.valid())
- BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("dup(2) failed");
-#elif defined(BOOST_WINDOWS_API)
- HANDLE h2;
- if (!DuplicateHandle(GetCurrentProcess(), h.native(),
- GetCurrentProcess(), &h2, 0, TRUE, DUPLICATE_SAME_ACCESS))
- BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("DuplicateHandle() failed");
- h = h2;
+#if defined(BOOST_WINDOWS_API)
+ if (!SetHandleInformation(h.native(), HANDLE_FLAG_INHERIT,
+ HANDLE_FLAG_INHERIT))
+ BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("SetHandleInformation() failed");
#endif
- return std::make_pair(h, bp::handle());
+ return bp::stream_ends(h, bp::handle());
}
-std::pair<bp::handle, bp::handle> forward(std::pair<bp::handle, bp::handle> p)
+bp::stream_ends forward(bp::stream_ends ends)
{
- return p;
+ return ends;
}
BOOST_AUTO_TEST_CASE(test_redirect_err_to_out)
@@ -263,11 +257,11 @@
args.push_back("echo-stdout-stderr");
args.push_back("message-to-two-streams");
- std::pair<bp::handle, bp::handle> p = bpb::pipe()(false);
+ bp::stream_ends ends = bpb::pipe()(false);
bp::context ctx;
- ctx.stdout_behavior = boost::bind(forward, p);
- ctx.stderr_behavior = boost::bind(redirect_to, p.first);
+ ctx.stdout_behavior = boost::bind(forward, ends);
+ ctx.stderr_behavior = boost::bind(redirect_to, ends.child);
bp::child c = bp::create_child(get_helpers_path(), args, ctx);
@@ -610,13 +604,13 @@
{
public:
context()
- : p(bpb::pipe()(false))
+ : ends(bpb::pipe()(false))
{
}
void setup(std::vector<bool> &closeflags)
{
- if (dup2(p.first.native(), 10) == -1)
+ if (dup2(ends.child.native(), 10) == -1)
{
write(STDERR_FILENO, "dup2() failed\n", 14);
_exit(127);
@@ -624,7 +618,7 @@
closeflags[10] = false;
}
- std::pair<bp::handle, bp::handle> p;
+ bp::stream_ends ends;
};
BOOST_AUTO_TEST_CASE(test_posix)
@@ -640,7 +634,7 @@
bp::child c = bp::create_child(get_helpers_path(), args, ctx);
std::string word;
- bp::pistream is(ctx.p.second);
+ bp::pistream is(ctx.ends.parent);
is >> word;
BOOST_CHECK_EQUAL(word, "test");
@@ -682,13 +676,13 @@
{
public:
context2()
- : p(bpb::pipe()(false))
+ : ends(bpb::pipe()(false))
{
}
void setup(std::vector<bool> &closeflags)
{
- if (dup2(p.first.native(), 3) == -1)
+ if (dup2(ends.child.native(), 3) == -1)
{
write(STDERR_FILENO, "dup2() failed\n", 14);
_exit(127);
@@ -696,7 +690,7 @@
closeflags[3] = false;
}
- std::pair<bp::handle, bp::handle> p;
+ bp::stream_ends ends;
};
BOOST_AUTO_TEST_CASE(test_posix2)
@@ -726,7 +720,7 @@
bp::child c = bp::create_child(get_helpers_path(), args, ctx);
- int res = dup2(ctx.p.second.native(), 0);
+ int res = dup2(ctx.ends.parent.native(), 0);
std::string word;
if (res != -1)
std::cin >> word;
Modified: sandbox/SOC/2010/process/libs/process/test/handle.cpp
==============================================================================
--- sandbox/SOC/2010/process/libs/process/test/handle.cpp (original)
+++ sandbox/SOC/2010/process/libs/process/test/handle.cpp 2010-09-15 17:40:19 EDT (Wed, 15 Sep 2010)
@@ -28,10 +28,10 @@
BOOST_AUTO_TEST_CASE(test_handle_readwrite)
{
- std::pair<bp::handle, bp::handle> p = bpb::pipe()(true);
+ bp::stream_ends ends = bpb::pipe()(true);
- bp::handle read_end = p.first;
- bp::handle write_end = p.second;
+ bp::handle read_end = ends.child;
+ bp::handle write_end = ends.parent;
bp::handle read_end2 = read_end;
bp::handle write_end2 = write_end;
Added: sandbox/SOC/2010/process/libs/process/test/inclusion/stream_ends.cpp
==============================================================================
--- (empty file)
+++ sandbox/SOC/2010/process/libs/process/test/inclusion/stream_ends.cpp 2010-09-15 17:40:19 EDT (Wed, 15 Sep 2010)
@@ -0,0 +1,14 @@
+//
+// Boost.Process
+// ~~~~~~~~~~~~~
+//
+// Copyright (c) 2006, 2007 Julio M. Merino Vidal
+// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
+// Copyright (c) 2009 Boris Schaeling
+// Copyright (c) 2010 Felipe Tanus, Boris Schaeling
+//
+// 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)
+//
+
+#include <boost/process/stream_ends.hpp>
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk