ian@0
|
1 //
|
ian@0
|
2 // Boost.Process
|
ian@0
|
3 // ~~~~~~~~~~~~~
|
ian@0
|
4 //
|
ian@0
|
5 // Copyright (c) 2006, 2007 Julio M. Merino Vidal
|
ian@0
|
6 // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
|
ian@0
|
7 // Copyright (c) 2009 Boris Schaeling
|
ian@0
|
8 // Copyright (c) 2010 Felipe Tanus, Boris Schaeling
|
ian@0
|
9 //
|
ian@0
|
10 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
ian@0
|
11 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
ian@0
|
12 //
|
ian@0
|
13
|
ian@0
|
14 /**
|
ian@0
|
15 * \file boost/process/pistream.hpp
|
ian@0
|
16 *
|
ian@0
|
17 * Includes the declaration of the pistream class.
|
ian@0
|
18 */
|
ian@0
|
19
|
ian@0
|
20 #ifndef BOOST_PROCESS_PISTREAM_HPP
|
ian@0
|
21 #define BOOST_PROCESS_PISTREAM_HPP
|
ian@0
|
22
|
ian@0
|
23 #include <boost/process/handle.hpp>
|
ian@0
|
24 #include <boost/process/detail/systembuf.hpp>
|
ian@0
|
25 #include <boost/noncopyable.hpp>
|
ian@0
|
26 #include <istream>
|
ian@0
|
27
|
ian@0
|
28 namespace boost {
|
ian@0
|
29 namespace process {
|
ian@0
|
30
|
ian@0
|
31 /**
|
ian@0
|
32 * Child process' output stream.
|
ian@0
|
33 *
|
ian@0
|
34 * The pistream class represents an output communication channel with the
|
ian@0
|
35 * child process. The child process writes data to this stream and the
|
ian@0
|
36 * parent process can read it through the pistream object. In other
|
ian@0
|
37 * words, from the child's point of view, the communication channel is an
|
ian@0
|
38 * output one, but from the parent's point of view it is an input one;
|
ian@0
|
39 * hence the confusing pistream name.
|
ian@0
|
40 *
|
ian@0
|
41 * pistream objects cannot be copied because they buffer data
|
ian@0
|
42 * that flows through the communication channel.
|
ian@0
|
43 *
|
ian@0
|
44 * A pistream object behaves as a std::istream stream in all senses.
|
ian@0
|
45 * The class is only provided because it must provide a method to let
|
ian@0
|
46 * the caller explicitly close the communication channel.
|
ian@0
|
47 *
|
ian@0
|
48 * \remark Blocking remarks: Functions that read data from this
|
ian@0
|
49 * stream can block if the associated handle blocks during
|
ian@0
|
50 * the read. As this class is used to communicate with child
|
ian@0
|
51 * processes through anonymous pipes, the most typical blocking
|
ian@0
|
52 * condition happens when the child has no more data to send to
|
ian@0
|
53 * the pipe's system buffer. When this happens, the buffer
|
ian@0
|
54 * eventually empties and the system blocks until the writer
|
ian@0
|
55 * generates some data.
|
ian@0
|
56 */
|
ian@0
|
57 class pistream : public std::istream, public boost::noncopyable
|
ian@0
|
58 {
|
ian@0
|
59 public:
|
ian@0
|
60 /**
|
ian@0
|
61 * Creates a new process' output stream.
|
ian@0
|
62 */
|
ian@0
|
63 explicit pistream(boost::process::handle h)
|
ian@0
|
64 : std::istream(0),
|
ian@0
|
65 handle_(h),
|
ian@0
|
66 systembuf_(handle_.native())
|
ian@0
|
67 {
|
ian@0
|
68 rdbuf(&systembuf_);
|
ian@0
|
69 }
|
ian@0
|
70
|
ian@0
|
71 /**
|
ian@0
|
72 * Returns the handle managed by this stream.
|
ian@0
|
73 */
|
ian@0
|
74 const boost::process::handle &handle() const
|
ian@0
|
75 {
|
ian@0
|
76 return handle_;
|
ian@0
|
77 }
|
ian@0
|
78
|
ian@0
|
79 /**
|
ian@0
|
80 * Returns the handle managed by this stream.
|
ian@0
|
81 */
|
ian@0
|
82 boost::process::handle &handle()
|
ian@0
|
83 {
|
ian@0
|
84 return handle_;
|
ian@0
|
85 }
|
ian@0
|
86
|
ian@0
|
87 /**
|
ian@0
|
88 * Closes the handle managed by this stream.
|
ian@0
|
89 *
|
ian@0
|
90 * Explicitly closes the handle managed by this stream. This
|
ian@0
|
91 * function can be used by the user to tell the child process it's
|
ian@0
|
92 * not willing to receive more data.
|
ian@0
|
93 */
|
ian@0
|
94 void close()
|
ian@0
|
95 {
|
ian@0
|
96 handle_.close();
|
ian@0
|
97 }
|
ian@0
|
98
|
ian@0
|
99 private:
|
ian@0
|
100 /**
|
ian@0
|
101 * The handle managed by this stream.
|
ian@0
|
102 */
|
ian@0
|
103 boost::process::handle handle_;
|
ian@0
|
104
|
ian@0
|
105 /**
|
ian@0
|
106 * The systembuf object used to manage this stream's data.
|
ian@0
|
107 */
|
ian@0
|
108 detail::systembuf systembuf_;
|
ian@0
|
109 };
|
ian@0
|
110
|
ian@0
|
111 }
|
ian@0
|
112 }
|
ian@0
|
113
|
ian@0
|
114 #endif
|