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
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef BOOST_PROCESS_PIPE_HPP
12 #define BOOST_PROCESS_PIPE_HPP
14 #include <boost/config.hpp>
15 #include <boost/process/detail/config.hpp>
21 #if defined(BOOST_POSIX_API)
22 #include <boost/process/detail/posix/basic_pipe.hpp>
23 #elif defined(BOOST_WINDOWS_API)
24 #include <boost/process/detail/windows/basic_pipe.hpp>
27 namespace boost { namespace process {
29 using ::boost::process::detail::api::basic_pipe;
31 #if defined(BOOST_PROCESS_DOXYGEN)
32 /** Class implementation of a pipe.
35 template<class CharT, class Traits = std::char_traits<CharT>>
39 typedef CharT char_type ;
40 typedef Traits traits_type;
41 typedef typename Traits::int_type int_type ;
42 typedef typename Traits::pos_type pos_type ;
43 typedef typename Traits::off_type off_type ;
44 typedef ::boost::detail::winapi::HANDLE_ native_handle;
46 /// Default construct the pipe. Will be opened.
49 ///Construct a named pipe.
50 inline explicit basic_pipe(const std::string & name);
51 /** Copy construct the pipe.
52 * \note Duplicated the handles.
54 inline basic_pipe(const basic_pipe& p);
55 /** Move construct the pipe. */
56 basic_pipe(basic_pipe&& lhs);
57 /** Copy assign the pipe.
58 * \note Duplicated the handles.
60 inline basic_pipe& operator=(const basic_pipe& p);
61 /** Move assign the pipe. */
62 basic_pipe& operator=(basic_pipe&& lhs);
63 /** Destructor closes the handles. */
65 /** Get the native handle of the source. */
66 native_handle native_source() const;
67 /** Get the native handle of the sink. */
68 native_handle native_sink () const;
70 /** Assign a new value to the source */
71 void assign_source(native_handle h);
72 /** Assign a new value to the sink */
73 void assign_sink (native_handle h);
76 ///Write data to the pipe.
77 int_type write(const char_type * data, int_type count);
78 ///Read data from the pipe.
79 int_type read(char_type * data, int_type count);
80 ///Check if the pipe is open.
90 typedef basic_pipe<char> pipe;
91 typedef basic_pipe<wchar_t> wpipe;
94 /** Implementation of the stream buffer for a pipe.
98 class Traits = std::char_traits<CharT>
100 struct basic_pipebuf : std::basic_streambuf<CharT, Traits>
102 typedef basic_pipe<CharT, Traits> pipe_type;
104 typedef CharT char_type ;
105 typedef Traits traits_type;
106 typedef typename Traits::int_type int_type ;
107 typedef typename Traits::pos_type pos_type ;
108 typedef typename Traits::off_type off_type ;
110 constexpr static int default_buffer_size = BOOST_PROCESS_PIPE_SIZE;
112 ///Default constructor, will also construct the pipe.
113 basic_pipebuf() : _write(default_buffer_size), _read(default_buffer_size)
115 this->setg(_read.data(), _read.data()+ 128, _read.data() + 128);
116 this->setp(_write.data(), _write.data() + _write.size());
119 basic_pipebuf(const basic_pipebuf & ) = default;
121 basic_pipebuf(basic_pipebuf && ) = default;
123 ///Destructor -> writes the frest of the data
126 if (basic_pipebuf::is_open())
127 basic_pipebuf::overflow(Traits::eof());
130 ///Move construct from a pipe.
131 basic_pipebuf(pipe_type && p) : _pipe(std::move(p)),
132 _write(default_buffer_size),
133 _read(default_buffer_size)
135 this->setg(_read.data(), _read.data()+ 128, _read.data() + 128);
136 this->setp(_write.data(), _write.data() + _write.size());
138 ///Construct from a pipe.
139 basic_pipebuf(const pipe_type & p) : _pipe(p),
140 _write(default_buffer_size),
141 _read(default_buffer_size)
143 this->setg(_read.data(), _read.data()+ 128, _read.data() + 128);
144 this->setp(_write.data(), _write.data() + _write.size());
147 basic_pipebuf& operator=(const basic_pipebuf & ) = delete;
149 basic_pipebuf& operator=(basic_pipebuf && ) = default;
150 ///Move assign a pipe.
151 basic_pipebuf& operator=(pipe_type && p)
153 _pipe = std::move(p);
156 ///Copy assign a pipe.
157 basic_pipebuf& operator=(const pipe_type & p)
162 ///Writes characters to the associated output sequence from the put area
163 int_type overflow(int_type ch = traits_type::eof()) override
165 if (_pipe.is_open() && (ch != traits_type::eof()))
167 if (this->pptr() == this->epptr())
169 bool wr = this->_write_impl();
181 if (this->_write_impl())
185 else if (ch == traits_type::eof())
188 return traits_type::eof();
190 ///Synchronizes the buffers with the associated character sequence
191 int sync() override { return this->_write_impl() ? 0 : -1; }
193 ///Reads characters from the associated input sequence to the get area
194 int_type underflow() override
196 if (!_pipe.is_open())
197 return traits_type::eof();
199 if (this->egptr() == &_read.back()) //ok, so we're at the end of the buffer
200 this->setg(_read.data(), _read.data()+ 10, _read.data() + 10);
203 auto len = &_read.back() - this->egptr() ;
204 auto res = _pipe.read(
206 static_cast<typename pipe_type::int_type>(len));
208 return traits_type::eof();
210 this->setg(this->eback(), this->gptr(), this->egptr() + res);
211 auto val = *this->gptr();
213 return traits_type::to_int_type(val);
217 ///Set the pipe of the streambuf.
218 void pipe(pipe_type&& p) {_pipe = std::move(p); }
219 ///Set the pipe of the streambuf.
220 void pipe(const pipe_type& p) {_pipe = p; }
221 ///Get a reference to the pipe.
222 pipe_type & pipe() & {return _pipe;}
223 ///Get a const reference to the pipe.
224 const pipe_type &pipe() const & {return _pipe;}
225 ///Get a rvalue reference to the pipe. Qualified as rvalue.
226 pipe_type && pipe() && {return std::move(_pipe);}
228 ///Check if the pipe is open
229 bool is_open() const {return _pipe.is_open(); }
232 basic_pipebuf<CharT, Traits>* open()
240 ///Open a new named pipe
241 basic_pipebuf<CharT, Traits>* open(const std::string & name)
249 ///Flush the buffer & close the pipe
250 basic_pipebuf<CharT, Traits>* close()
254 overflow(Traits::eof());
259 std::vector<char_type> _write;
260 std::vector<char_type> _read;
264 if (!_pipe.is_open())
267 auto base = this->pbase();
269 if (base == this->pptr())
272 std::ptrdiff_t wrt = _pipe.write(base,
273 static_cast<typename pipe_type::int_type>(this->pptr() - base));
275 std::ptrdiff_t diff = this->pptr() - base;
278 std::move(base + wrt, base + diff, base);
279 else if (wrt == 0) //broken pipe
288 typedef basic_pipebuf<char> pipebuf;
289 typedef basic_pipebuf<wchar_t> wpipebuf;
291 /** Implementation of a reading pipe stream.
296 class Traits = std::char_traits<CharT>
298 class basic_ipstream : public std::basic_istream<CharT, Traits>
300 mutable basic_pipebuf<CharT, Traits> _buf;
303 typedef basic_pipe<CharT, Traits> pipe_type;
305 typedef CharT char_type ;
306 typedef Traits traits_type;
307 typedef typename Traits::int_type int_type ;
308 typedef typename Traits::pos_type pos_type ;
309 typedef typename Traits::off_type off_type ;
311 ///Get access to the underlying stream_buf
312 basic_pipebuf<CharT, Traits>* rdbuf() const {return &_buf;};
314 ///Default constructor.
315 basic_ipstream() : std::basic_istream<CharT, Traits>(nullptr)
317 std::basic_istream<CharT, Traits>::rdbuf(&_buf);
320 basic_ipstream(const basic_ipstream & ) = delete;
322 basic_ipstream(basic_ipstream && lhs) : std::basic_istream<CharT, Traits>(nullptr), _buf(std::move(lhs._buf))
324 std::basic_istream<CharT, Traits>::rdbuf(&_buf);
327 ///Move construct from a pipe.
328 basic_ipstream(pipe_type && p) : std::basic_istream<CharT, Traits>(nullptr), _buf(std::move(p))
330 std::basic_istream<CharT, Traits>::rdbuf(&_buf);
333 ///Copy construct from a pipe.
334 basic_ipstream(const pipe_type & p) : std::basic_istream<CharT, Traits>(nullptr), _buf(p)
336 std::basic_istream<CharT, Traits>::rdbuf(&_buf);
340 basic_ipstream& operator=(const basic_ipstream & ) = delete;
342 basic_ipstream& operator=(basic_ipstream && lhs)
344 std::basic_istream<CharT, Traits>::operator=(std::move(lhs));
345 _buf = std::move(lhs._buf);
346 std::basic_istream<CharT, Traits>::rdbuf(&_buf);
349 ///Move assignment of a pipe.
350 basic_ipstream& operator=(pipe_type && p)
355 ///Copy assignment of a pipe.
356 basic_ipstream& operator=(const pipe_type & p)
361 ///Set the pipe of the streambuf.
362 void pipe(pipe_type&& p) {_buf.pipe(std::move(p)); }
363 ///Set the pipe of the streambuf.
364 void pipe(const pipe_type& p) {_buf.pipe(p); }
365 ///Get a reference to the pipe.
366 pipe_type & pipe() & {return _buf.pipe();}
367 ///Get a const reference to the pipe.
368 const pipe_type &pipe() const & {return _buf.pipe();}
369 ///Get a rvalue reference to the pipe. Qualified as rvalue.
370 pipe_type && pipe() && {return std::move(_buf).pipe();}
371 ///Check if the pipe is open
372 bool is_open() const {return _buf.is_open();}
377 if (_buf.open() == nullptr)
378 this->setstate(std::ios_base::failbit);
383 ///Open a new named pipe
384 void open(const std::string & name)
386 if (_buf.open() == nullptr)
387 this->setstate(std::ios_base::failbit);
392 ///Flush the buffer & close the pipe
395 if (_buf.close() == nullptr)
396 this->setstate(std::ios_base::failbit);
400 typedef basic_ipstream<char> ipstream;
401 typedef basic_ipstream<wchar_t> wipstream;
403 /** Implementation of a write pipe stream.
408 class Traits = std::char_traits<CharT>
410 class basic_opstream : public std::basic_ostream<CharT, Traits>
412 mutable basic_pipebuf<CharT, Traits> _buf;
414 typedef basic_pipe<CharT, Traits> pipe_type;
416 typedef CharT char_type ;
417 typedef Traits traits_type;
418 typedef typename Traits::int_type int_type ;
419 typedef typename Traits::pos_type pos_type ;
420 typedef typename Traits::off_type off_type ;
423 ///Get access to the underlying stream_buf
424 basic_pipebuf<CharT, Traits>* rdbuf() const {return &_buf;};
426 ///Default constructor.
427 basic_opstream() : std::basic_ostream<CharT, Traits>(nullptr)
429 std::basic_ostream<CharT, Traits>::rdbuf(&_buf);
432 basic_opstream(const basic_opstream & ) = delete;
434 basic_opstream(basic_opstream && lhs) : std::basic_ostream<CharT, Traits>(nullptr), _buf(std::move(lhs._buf))
436 std::basic_ostream<CharT, Traits>::rdbuf(&_buf);
438 ///Move construct from a pipe.
439 basic_opstream(pipe_type && p) : std::basic_ostream<CharT, Traits>(nullptr), _buf(std::move(p))
441 std::basic_ostream<CharT, Traits>::rdbuf(&_buf);
443 ///Copy construct from a pipe.
444 basic_opstream(const pipe_type & p) : std::basic_ostream<CharT, Traits>(nullptr), _buf(p)
446 std::basic_ostream<CharT, Traits>::rdbuf(&_buf);
449 basic_opstream& operator=(const basic_opstream & ) = delete;
451 basic_opstream& operator=(basic_opstream && lhs)
453 std::basic_ostream<CharT, Traits>::operator=(std::move(lhs));
454 _buf = std::move(lhs._buf);
455 std::basic_ostream<CharT, Traits>::rdbuf(&_buf);
459 ///Move assignment of a pipe.
460 basic_opstream& operator=(pipe_type && p)
465 ///Copy assignment of a pipe.
466 basic_opstream& operator=(const pipe_type & p)
471 ///Set the pipe of the streambuf.
472 void pipe(pipe_type&& p) {_buf.pipe(std::move(p)); }
473 ///Set the pipe of the streambuf.
474 void pipe(const pipe_type& p) {_buf.pipe(p); }
475 ///Get a reference to the pipe.
476 pipe_type & pipe() & {return _buf.pipe();}
477 ///Get a const reference to the pipe.
478 const pipe_type &pipe() const & {return _buf.pipe();}
479 ///Get a rvalue reference to the pipe. Qualified as rvalue.
480 pipe_type && pipe() && {return std::move(_buf).pipe();}
485 if (_buf.open() == nullptr)
486 this->setstate(std::ios_base::failbit);
491 ///Open a new named pipe
492 void open(const std::string & name)
494 if (_buf.open() == nullptr)
495 this->setstate(std::ios_base::failbit);
500 ///Flush the buffer & close the pipe
503 if (_buf.close() == nullptr)
504 this->setstate(std::ios_base::failbit);
508 typedef basic_opstream<char> opstream;
509 typedef basic_opstream<wchar_t> wopstream;
512 /** Implementation of a read-write pipe stream.
517 class Traits = std::char_traits<CharT>
519 class basic_pstream : public std::basic_iostream<CharT, Traits>
521 mutable basic_pipebuf<CharT, Traits> _buf;
523 typedef basic_pipe<CharT, Traits> pipe_type;
525 typedef CharT char_type ;
526 typedef Traits traits_type;
527 typedef typename Traits::int_type int_type ;
528 typedef typename Traits::pos_type pos_type ;
529 typedef typename Traits::off_type off_type ;
532 ///Get access to the underlying stream_buf
533 basic_pipebuf<CharT, Traits>* rdbuf() const {return &_buf;};
535 ///Default constructor.
536 basic_pstream() : std::basic_iostream<CharT, Traits>(nullptr)
538 std::basic_iostream<CharT, Traits>::rdbuf(&_buf);
541 basic_pstream(const basic_pstream & ) = delete;
543 basic_pstream(basic_pstream && lhs) : std::basic_iostream<CharT, Traits>(nullptr), _buf(std::move(lhs._buf))
545 std::basic_iostream<CharT, Traits>::rdbuf(&_buf);
547 ///Move construct from a pipe.
548 basic_pstream(pipe_type && p) : std::basic_iostream<CharT, Traits>(nullptr), _buf(std::move(p))
550 std::basic_iostream<CharT, Traits>::rdbuf(&_buf);
552 ///Copy construct from a pipe.
553 basic_pstream(const pipe_type & p) : std::basic_iostream<CharT, Traits>(nullptr), _buf(p)
555 std::basic_iostream<CharT, Traits>::rdbuf(&_buf);
558 basic_pstream& operator=(const basic_pstream & ) = delete;
560 basic_pstream& operator=(basic_pstream && lhs)
562 std::basic_istream<CharT, Traits>::operator=(std::move(lhs));
563 _buf = std::move(lhs._buf);
564 std::basic_iostream<CharT, Traits>::rdbuf(&_buf);
567 ///Move assignment of a pipe.
568 basic_pstream& operator=(pipe_type && p)
573 ///Copy assignment of a pipe.
574 basic_pstream& operator=(const pipe_type & p)
579 ///Set the pipe of the streambuf.
580 void pipe(pipe_type&& p) {_buf.pipe(std::move(p)); }
581 ///Set the pipe of the streambuf.
582 void pipe(const pipe_type& p) {_buf.pipe(p); }
583 ///Get a reference to the pipe.
584 pipe_type & pipe() & {return _buf.pipe();}
585 ///Get a const reference to the pipe.
586 const pipe_type &pipe() const & {return _buf.pipe();}
587 ///Get a rvalue reference to the pipe. Qualified as rvalue.
588 pipe_type && pipe() && {return std::move(_buf).pipe();}
593 if (_buf.open() == nullptr)
594 this->setstate(std::ios_base::failbit);
599 ///Open a new named pipe
600 void open(const std::string & name)
602 if (_buf.open() == nullptr)
603 this->setstate(std::ios_base::failbit);
608 ///Flush the buffer & close the pipe
611 if (_buf.close() == nullptr)
612 this->setstate(std::ios_base::failbit);
616 typedef basic_pstream<char> pstream;
617 typedef basic_pstream<wchar_t> wpstream;