pipe.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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
  6. //
  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)
  9. #ifndef BOOST_PROCESS_PIPE_HPP
  10. #define BOOST_PROCESS_PIPE_HPP
  11. #include <boost/config.hpp>
  12. #include <boost/process/v1/detail/config.hpp>
  13. #include <streambuf>
  14. #include <istream>
  15. #include <ostream>
  16. #include <vector>
  17. #if defined(BOOST_POSIX_API)
  18. #include <boost/process/v1/detail/posix/basic_pipe.hpp>
  19. #elif defined(BOOST_WINDOWS_API)
  20. #include <boost/process/v1/detail/windows/basic_pipe.hpp>
  21. #endif
  22. namespace boost { namespace process { BOOST_PROCESS_V1_INLINE namespace v1 {
  23. using ::boost::process::v1::detail::api::basic_pipe;
  24. #if defined(BOOST_PROCESS_DOXYGEN)
  25. /** Class implementation of a pipe.
  26. *
  27. */
  28. template<class CharT, class Traits = std::char_traits<CharT>>
  29. class basic_pipe
  30. {
  31. public:
  32. typedef CharT char_type ;
  33. typedef Traits traits_type;
  34. typedef typename Traits::int_type int_type ;
  35. typedef typename Traits::pos_type pos_type ;
  36. typedef typename Traits::off_type off_type ;
  37. typedef ::boost::detail::winapi::HANDLE_ native_handle;
  38. /// Default construct the pipe. Will be opened.
  39. basic_pipe();
  40. ///Construct a named pipe.
  41. inline explicit basic_pipe(const std::string & name);
  42. /** Copy construct the pipe.
  43. * \note Duplicated the handles.
  44. */
  45. inline basic_pipe(const basic_pipe& p);
  46. /** Move construct the pipe. */
  47. basic_pipe(basic_pipe&& lhs);
  48. /** Copy assign the pipe.
  49. * \note Duplicated the handles.
  50. */
  51. inline basic_pipe& operator=(const basic_pipe& p);
  52. /** Move assign the pipe. */
  53. basic_pipe& operator=(basic_pipe&& lhs);
  54. /** Destructor closes the handles. */
  55. ~basic_pipe();
  56. /** Get the native handle of the source. */
  57. native_handle native_source() const;
  58. /** Get the native handle of the sink. */
  59. native_handle native_sink () const;
  60. /** Assign a new value to the source */
  61. void assign_source(native_handle h);
  62. /** Assign a new value to the sink */
  63. void assign_sink (native_handle h);
  64. ///Write data to the pipe.
  65. int_type write(const char_type * data, int_type count);
  66. ///Read data from the pipe.
  67. int_type read(char_type * data, int_type count);
  68. ///Check if the pipe is open.
  69. bool is_open();
  70. ///Close the pipe
  71. void close();
  72. };
  73. #endif
  74. typedef basic_pipe<char> pipe;
  75. typedef basic_pipe<wchar_t> wpipe;
  76. /** Implementation of the stream buffer for a pipe.
  77. */
  78. template<
  79. class CharT,
  80. class Traits = std::char_traits<CharT>
  81. >
  82. struct basic_pipebuf : std::basic_streambuf<CharT, Traits>
  83. {
  84. typedef basic_pipe<CharT, Traits> pipe_type;
  85. typedef CharT char_type ;
  86. typedef Traits traits_type;
  87. typedef typename Traits::int_type int_type ;
  88. typedef typename Traits::pos_type pos_type ;
  89. typedef typename Traits::off_type off_type ;
  90. constexpr static int default_buffer_size = BOOST_PROCESS_PIPE_SIZE;
  91. ///Default constructor, will also construct the pipe.
  92. basic_pipebuf() : _write(default_buffer_size), _read(default_buffer_size)
  93. {
  94. this->setg(_read.data(), _read.data()+ 128, _read.data() + 128);
  95. this->setp(_write.data(), _write.data() + _write.size());
  96. }
  97. ///Copy Constructor.
  98. basic_pipebuf(const basic_pipebuf & ) = default;
  99. ///Move Constructor
  100. basic_pipebuf(basic_pipebuf && ) = default;
  101. ///Destructor -> writes the frest of the data
  102. ~basic_pipebuf()
  103. {
  104. try
  105. {
  106. if (basic_pipebuf::is_open())
  107. basic_pipebuf::overflow(Traits::eof());
  108. }
  109. catch (process_error & )
  110. {
  111. }
  112. }
  113. ///Move construct from a pipe.
  114. basic_pipebuf(pipe_type && p) : _pipe(std::move(p)),
  115. _write(default_buffer_size),
  116. _read(default_buffer_size)
  117. {
  118. this->setg(_read.data(), _read.data()+ 128, _read.data() + 128);
  119. this->setp(_write.data(), _write.data() + _write.size());
  120. }
  121. ///Construct from a pipe.
  122. basic_pipebuf(const pipe_type & p) : _pipe(p),
  123. _write(default_buffer_size),
  124. _read(default_buffer_size)
  125. {
  126. this->setg(_read.data(), _read.data()+ 128, _read.data() + 128);
  127. this->setp(_write.data(), _write.data() + _write.size());
  128. }
  129. ///Copy assign.
  130. basic_pipebuf& operator=(const basic_pipebuf & ) = delete;
  131. ///Move assign.
  132. basic_pipebuf& operator=(basic_pipebuf && ) = default;
  133. ///Move assign a pipe.
  134. basic_pipebuf& operator=(pipe_type && p)
  135. {
  136. _pipe = std::move(p);
  137. return *this;
  138. }
  139. ///Copy assign a pipe.
  140. basic_pipebuf& operator=(const pipe_type & p)
  141. {
  142. _pipe = p;
  143. return *this;
  144. }
  145. ///Writes characters to the associated output sequence from the put area
  146. int_type overflow(int_type ch = traits_type::eof()) override
  147. {
  148. if (_pipe.is_open() && (ch != traits_type::eof()))
  149. {
  150. if (this->pptr() == this->epptr())
  151. {
  152. bool wr = this->_write_impl();
  153. if (wr)
  154. {
  155. *this->pptr() = ch;
  156. this->pbump(1);
  157. return ch;
  158. }
  159. }
  160. else
  161. {
  162. *this->pptr() = ch;
  163. this->pbump(1);
  164. if (this->_write_impl())
  165. return ch;
  166. }
  167. }
  168. else if (ch == traits_type::eof())
  169. this->sync();
  170. return traits_type::eof();
  171. }
  172. ///Synchronizes the buffers with the associated character sequence
  173. int sync() override { return this->_write_impl() ? 0 : -1; }
  174. ///Reads characters from the associated input sequence to the get area
  175. int_type underflow() override
  176. {
  177. if (!_pipe.is_open())
  178. return traits_type::eof();
  179. if (this->egptr() == &_read.back()) //ok, so we're at the end of the buffer
  180. this->setg(_read.data(), _read.data()+ 10, _read.data() + 10);
  181. auto len = &_read.back() - this->egptr() ;
  182. auto res = _pipe.read(
  183. this->egptr(),
  184. static_cast<typename pipe_type::int_type>(len));
  185. if (res == 0)
  186. return traits_type::eof();
  187. this->setg(this->eback(), this->gptr(), this->egptr() + res);
  188. auto val = *this->gptr();
  189. return traits_type::to_int_type(val);
  190. }
  191. ///Set the pipe of the streambuf.
  192. void pipe(pipe_type&& p) {_pipe = std::move(p); }
  193. ///Set the pipe of the streambuf.
  194. void pipe(const pipe_type& p) {_pipe = p; }
  195. ///Get a reference to the pipe.
  196. pipe_type & pipe() & {return _pipe;}
  197. ///Get a const reference to the pipe.
  198. const pipe_type &pipe() const & {return _pipe;}
  199. ///Get a rvalue reference to the pipe. Qualified as rvalue.
  200. pipe_type && pipe() && {return std::move(_pipe);}
  201. ///Check if the pipe is open
  202. bool is_open() const {return _pipe.is_open(); }
  203. ///Open a new pipe
  204. basic_pipebuf<CharT, Traits>* open()
  205. {
  206. if (is_open())
  207. return nullptr;
  208. _pipe = pipe();
  209. return this;
  210. }
  211. ///Open a new named pipe
  212. basic_pipebuf<CharT, Traits>* open(const std::string & name)
  213. {
  214. if (is_open())
  215. return nullptr;
  216. _pipe = pipe(name);
  217. return this;
  218. }
  219. ///Flush the buffer & close the pipe
  220. basic_pipebuf<CharT, Traits>* close()
  221. {
  222. if (!is_open())
  223. return nullptr;
  224. try {
  225. overflow(Traits::eof());
  226. _pipe.close();
  227. return this;
  228. }
  229. catch(...)
  230. {
  231. _pipe.close();
  232. throw ;
  233. }
  234. return this;
  235. }
  236. private:
  237. pipe_type _pipe;
  238. std::vector<char_type> _write;
  239. std::vector<char_type> _read;
  240. bool _write_impl()
  241. {
  242. if (!_pipe.is_open())
  243. return false;
  244. auto base = this->pbase();
  245. if (base == this->pptr())
  246. return true;
  247. std::ptrdiff_t wrt = _pipe.write(base,
  248. static_cast<typename pipe_type::int_type>(this->pptr() - base));
  249. std::ptrdiff_t diff = this->pptr() - base;
  250. if (wrt < diff)
  251. std::move(base + wrt, base + diff, base);
  252. else if (wrt == 0) //broken pipe
  253. return false;
  254. this->pbump(static_cast<int>(-wrt));
  255. return true;
  256. }
  257. };
  258. typedef basic_pipebuf<char> pipebuf;
  259. typedef basic_pipebuf<wchar_t> wpipebuf;
  260. /** Implementation of a reading pipe stream.
  261. *
  262. */
  263. template<
  264. class CharT,
  265. class Traits = std::char_traits<CharT>
  266. >
  267. class basic_ipstream : public std::basic_istream<CharT, Traits>
  268. {
  269. mutable basic_pipebuf<CharT, Traits> _buf;
  270. public:
  271. typedef basic_pipe<CharT, Traits> pipe_type;
  272. typedef CharT char_type ;
  273. typedef Traits traits_type;
  274. typedef typename Traits::int_type int_type ;
  275. typedef typename Traits::pos_type pos_type ;
  276. typedef typename Traits::off_type off_type ;
  277. ///Get access to the underlying stream_buf
  278. basic_pipebuf<CharT, Traits>* rdbuf() const {return &_buf;};
  279. ///Default constructor.
  280. basic_ipstream() : std::basic_istream<CharT, Traits>(nullptr)
  281. {
  282. std::basic_istream<CharT, Traits>::rdbuf(&_buf);
  283. };
  284. ///Copy constructor.
  285. basic_ipstream(const basic_ipstream & ) = delete;
  286. ///Move constructor.
  287. basic_ipstream(basic_ipstream && lhs) : std::basic_istream<CharT, Traits>(nullptr), _buf(std::move(lhs._buf))
  288. {
  289. std::basic_istream<CharT, Traits>::rdbuf(&_buf);
  290. }
  291. ///Move construct from a pipe.
  292. basic_ipstream(pipe_type && p) : std::basic_istream<CharT, Traits>(nullptr), _buf(std::move(p))
  293. {
  294. std::basic_istream<CharT, Traits>::rdbuf(&_buf);
  295. }
  296. ///Copy construct from a pipe.
  297. basic_ipstream(const pipe_type & p) : std::basic_istream<CharT, Traits>(nullptr), _buf(p)
  298. {
  299. std::basic_istream<CharT, Traits>::rdbuf(&_buf);
  300. }
  301. ///Copy assignment.
  302. basic_ipstream& operator=(const basic_ipstream & ) = delete;
  303. ///Move assignment
  304. basic_ipstream& operator=(basic_ipstream && lhs)
  305. {
  306. std::basic_istream<CharT, Traits>::operator=(std::move(lhs));
  307. _buf = std::move(lhs._buf);
  308. std::basic_istream<CharT, Traits>::rdbuf(&_buf);
  309. return *this;
  310. };
  311. ///Move assignment of a pipe.
  312. basic_ipstream& operator=(pipe_type && p)
  313. {
  314. _buf = std::move(p);
  315. return *this;
  316. }
  317. ///Copy assignment of a pipe.
  318. basic_ipstream& operator=(const pipe_type & p)
  319. {
  320. _buf = p;
  321. return *this;
  322. }
  323. ///Set the pipe of the streambuf.
  324. void pipe(pipe_type&& p) {_buf.pipe(std::move(p)); }
  325. ///Set the pipe of the streambuf.
  326. void pipe(const pipe_type& p) {_buf.pipe(p); }
  327. ///Get a reference to the pipe.
  328. pipe_type & pipe() & {return _buf.pipe();}
  329. ///Get a const reference to the pipe.
  330. const pipe_type &pipe() const & {return _buf.pipe();}
  331. ///Get a rvalue reference to the pipe. Qualified as rvalue.
  332. pipe_type && pipe() && {return std::move(_buf).pipe();}
  333. ///Check if the pipe is open
  334. bool is_open() const {return _buf.is_open();}
  335. ///Open a new pipe
  336. void open()
  337. {
  338. if (_buf.open() == nullptr)
  339. this->setstate(std::ios_base::failbit);
  340. else
  341. this->clear();
  342. }
  343. ///Open a new named pipe
  344. void open(const std::string & name)
  345. {
  346. if (_buf.open() == nullptr)
  347. this->setstate(std::ios_base::failbit);
  348. else
  349. this->clear();
  350. }
  351. ///Flush the buffer & close the pipe
  352. void close()
  353. {
  354. if (_buf.close() == nullptr)
  355. this->setstate(std::ios_base::failbit);
  356. }
  357. };
  358. typedef basic_ipstream<char> ipstream;
  359. typedef basic_ipstream<wchar_t> wipstream;
  360. /** Implementation of a write pipe stream.
  361. *
  362. */
  363. template<
  364. class CharT,
  365. class Traits = std::char_traits<CharT>
  366. >
  367. class basic_opstream : public std::basic_ostream<CharT, Traits>
  368. {
  369. mutable basic_pipebuf<CharT, Traits> _buf;
  370. public:
  371. typedef basic_pipe<CharT, Traits> pipe_type;
  372. typedef CharT char_type ;
  373. typedef Traits traits_type;
  374. typedef typename Traits::int_type int_type ;
  375. typedef typename Traits::pos_type pos_type ;
  376. typedef typename Traits::off_type off_type ;
  377. ///Get access to the underlying stream_buf
  378. basic_pipebuf<CharT, Traits>* rdbuf() const {return &_buf;};
  379. ///Default constructor.
  380. basic_opstream() : std::basic_ostream<CharT, Traits>(nullptr)
  381. {
  382. std::basic_ostream<CharT, Traits>::rdbuf(&_buf);
  383. };
  384. ///Copy constructor.
  385. basic_opstream(const basic_opstream & ) = delete;
  386. ///Move constructor.
  387. basic_opstream(basic_opstream && lhs) : std::basic_ostream<CharT, Traits>(nullptr), _buf(std::move(lhs._buf))
  388. {
  389. std::basic_ostream<CharT, Traits>::rdbuf(&_buf);
  390. }
  391. ///Move construct from a pipe.
  392. basic_opstream(pipe_type && p) : std::basic_ostream<CharT, Traits>(nullptr), _buf(std::move(p))
  393. {
  394. std::basic_ostream<CharT, Traits>::rdbuf(&_buf);
  395. };
  396. ///Copy construct from a pipe.
  397. basic_opstream(const pipe_type & p) : std::basic_ostream<CharT, Traits>(nullptr), _buf(p)
  398. {
  399. std::basic_ostream<CharT, Traits>::rdbuf(&_buf);
  400. };
  401. ///Copy assignment.
  402. basic_opstream& operator=(const basic_opstream & ) = delete;
  403. ///Move assignment
  404. basic_opstream& operator=(basic_opstream && lhs)
  405. {
  406. std::basic_ostream<CharT, Traits>::operator=(std::move(lhs));
  407. _buf = std::move(lhs._buf);
  408. std::basic_ostream<CharT, Traits>::rdbuf(&_buf);
  409. return *this;
  410. };
  411. ///Move assignment of a pipe.
  412. basic_opstream& operator=(pipe_type && p)
  413. {
  414. _buf = std::move(p);
  415. return *this;
  416. }
  417. ///Copy assignment of a pipe.
  418. basic_opstream& operator=(const pipe_type & p)
  419. {
  420. _buf = p;
  421. return *this;
  422. }
  423. ///Set the pipe of the streambuf.
  424. void pipe(pipe_type&& p) {_buf.pipe(std::move(p)); }
  425. ///Set the pipe of the streambuf.
  426. void pipe(const pipe_type& p) {_buf.pipe(p); }
  427. ///Get a reference to the pipe.
  428. pipe_type & pipe() & {return _buf.pipe();}
  429. ///Get a const reference to the pipe.
  430. const pipe_type &pipe() const & {return _buf.pipe();}
  431. ///Get a rvalue reference to the pipe. Qualified as rvalue.
  432. pipe_type && pipe() && {return std::move(_buf).pipe();}
  433. ///Open a new pipe
  434. void open()
  435. {
  436. if (_buf.open() == nullptr)
  437. this->setstate(std::ios_base::failbit);
  438. else
  439. this->clear();
  440. }
  441. ///Open a new named pipe
  442. void open(const std::string & name)
  443. {
  444. if (_buf.open() == nullptr)
  445. this->setstate(std::ios_base::failbit);
  446. else
  447. this->clear();
  448. }
  449. ///Flush the buffer & close the pipe
  450. void close()
  451. {
  452. if (_buf.close() == nullptr)
  453. this->setstate(std::ios_base::failbit);
  454. }
  455. };
  456. typedef basic_opstream<char> opstream;
  457. typedef basic_opstream<wchar_t> wopstream;
  458. /** Implementation of a read-write pipe stream.
  459. *
  460. */
  461. template<
  462. class CharT,
  463. class Traits = std::char_traits<CharT>
  464. >
  465. class basic_pstream : public std::basic_iostream<CharT, Traits>
  466. {
  467. mutable basic_pipebuf<CharT, Traits> _buf;
  468. public:
  469. typedef basic_pipe<CharT, Traits> pipe_type;
  470. typedef CharT char_type ;
  471. typedef Traits traits_type;
  472. typedef typename Traits::int_type int_type ;
  473. typedef typename Traits::pos_type pos_type ;
  474. typedef typename Traits::off_type off_type ;
  475. ///Get access to the underlying stream_buf
  476. basic_pipebuf<CharT, Traits>* rdbuf() const {return &_buf;};
  477. ///Default constructor.
  478. basic_pstream() : std::basic_iostream<CharT, Traits>(nullptr)
  479. {
  480. std::basic_iostream<CharT, Traits>::rdbuf(&_buf);
  481. };
  482. ///Copy constructor.
  483. basic_pstream(const basic_pstream & ) = delete;
  484. ///Move constructor.
  485. basic_pstream(basic_pstream && lhs) : std::basic_iostream<CharT, Traits>(nullptr), _buf(std::move(lhs._buf))
  486. {
  487. std::basic_iostream<CharT, Traits>::rdbuf(&_buf);
  488. }
  489. ///Move construct from a pipe.
  490. basic_pstream(pipe_type && p) : std::basic_iostream<CharT, Traits>(nullptr), _buf(std::move(p))
  491. {
  492. std::basic_iostream<CharT, Traits>::rdbuf(&_buf);
  493. };
  494. ///Copy construct from a pipe.
  495. basic_pstream(const pipe_type & p) : std::basic_iostream<CharT, Traits>(nullptr), _buf(p)
  496. {
  497. std::basic_iostream<CharT, Traits>::rdbuf(&_buf);
  498. };
  499. ///Copy assignment.
  500. basic_pstream& operator=(const basic_pstream & ) = delete;
  501. ///Move assignment
  502. basic_pstream& operator=(basic_pstream && lhs)
  503. {
  504. std::basic_istream<CharT, Traits>::operator=(std::move(lhs));
  505. _buf = std::move(lhs._buf);
  506. std::basic_iostream<CharT, Traits>::rdbuf(&_buf);
  507. return *this;
  508. };
  509. ///Move assignment of a pipe.
  510. basic_pstream& operator=(pipe_type && p)
  511. {
  512. _buf = std::move(p);
  513. return *this;
  514. }
  515. ///Copy assignment of a pipe.
  516. basic_pstream& operator=(const pipe_type & p)
  517. {
  518. _buf = p;
  519. return *this;
  520. }
  521. ///Set the pipe of the streambuf.
  522. void pipe(pipe_type&& p) {_buf.pipe(std::move(p)); }
  523. ///Set the pipe of the streambuf.
  524. void pipe(const pipe_type& p) {_buf.pipe(p); }
  525. ///Get a reference to the pipe.
  526. pipe_type & pipe() & {return _buf.pipe();}
  527. ///Get a const reference to the pipe.
  528. const pipe_type &pipe() const & {return _buf.pipe();}
  529. ///Get a rvalue reference to the pipe. Qualified as rvalue.
  530. pipe_type && pipe() && {return std::move(_buf).pipe();}
  531. ///Open a new pipe
  532. void open()
  533. {
  534. if (_buf.open() == nullptr)
  535. this->setstate(std::ios_base::failbit);
  536. else
  537. this->clear();
  538. }
  539. ///Open a new named pipe
  540. void open(const std::string & name)
  541. {
  542. if (_buf.open() == nullptr)
  543. this->setstate(std::ios_base::failbit);
  544. else
  545. this->clear();
  546. }
  547. ///Flush the buffer & close the pipe
  548. void close()
  549. {
  550. if (_buf.close() == nullptr)
  551. this->setstate(std::ios_base::failbit);
  552. }
  553. };
  554. typedef basic_pstream<char> pstream;
  555. typedef basic_pstream<wchar_t> wpstream;
  556. }}}
  557. #endif