pipe.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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/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/detail/posix/basic_pipe.hpp>
  19. #elif defined(BOOST_WINDOWS_API)
  20. #include <boost/process/detail/windows/basic_pipe.hpp>
  21. #endif
  22. namespace boost { namespace process {
  23. using ::boost::process::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. if (basic_pipebuf::is_open())
  105. basic_pipebuf::overflow(Traits::eof());
  106. }
  107. ///Move construct from a pipe.
  108. basic_pipebuf(pipe_type && p) : _pipe(std::move(p)),
  109. _write(default_buffer_size),
  110. _read(default_buffer_size)
  111. {
  112. this->setg(_read.data(), _read.data()+ 128, _read.data() + 128);
  113. this->setp(_write.data(), _write.data() + _write.size());
  114. }
  115. ///Construct from a pipe.
  116. basic_pipebuf(const pipe_type & p) : _pipe(p),
  117. _write(default_buffer_size),
  118. _read(default_buffer_size)
  119. {
  120. this->setg(_read.data(), _read.data()+ 128, _read.data() + 128);
  121. this->setp(_write.data(), _write.data() + _write.size());
  122. }
  123. ///Copy assign.
  124. basic_pipebuf& operator=(const basic_pipebuf & ) = delete;
  125. ///Move assign.
  126. basic_pipebuf& operator=(basic_pipebuf && ) = default;
  127. ///Move assign a pipe.
  128. basic_pipebuf& operator=(pipe_type && p)
  129. {
  130. _pipe = std::move(p);
  131. return *this;
  132. }
  133. ///Copy assign a pipe.
  134. basic_pipebuf& operator=(const pipe_type & p)
  135. {
  136. _pipe = p;
  137. return *this;
  138. }
  139. ///Writes characters to the associated output sequence from the put area
  140. int_type overflow(int_type ch = traits_type::eof()) override
  141. {
  142. if (_pipe.is_open() && (ch != traits_type::eof()))
  143. {
  144. if (this->pptr() == this->epptr())
  145. {
  146. bool wr = this->_write_impl();
  147. if (wr)
  148. {
  149. *this->pptr() = ch;
  150. this->pbump(1);
  151. return ch;
  152. }
  153. }
  154. else
  155. {
  156. *this->pptr() = ch;
  157. this->pbump(1);
  158. if (this->_write_impl())
  159. return ch;
  160. }
  161. }
  162. else if (ch == traits_type::eof())
  163. this->sync();
  164. return traits_type::eof();
  165. }
  166. ///Synchronizes the buffers with the associated character sequence
  167. int sync() override { return this->_write_impl() ? 0 : -1; }
  168. ///Reads characters from the associated input sequence to the get area
  169. int_type underflow() override
  170. {
  171. if (!_pipe.is_open())
  172. return traits_type::eof();
  173. if (this->egptr() == &_read.back()) //ok, so we're at the end of the buffer
  174. this->setg(_read.data(), _read.data()+ 10, _read.data() + 10);
  175. auto len = &_read.back() - this->egptr() ;
  176. auto res = _pipe.read(
  177. this->egptr(),
  178. static_cast<typename pipe_type::int_type>(len));
  179. if (res == 0)
  180. return traits_type::eof();
  181. this->setg(this->eback(), this->gptr(), this->egptr() + res);
  182. auto val = *this->gptr();
  183. return traits_type::to_int_type(val);
  184. }
  185. ///Set the pipe of the streambuf.
  186. void pipe(pipe_type&& p) {_pipe = std::move(p); }
  187. ///Set the pipe of the streambuf.
  188. void pipe(const pipe_type& p) {_pipe = p; }
  189. ///Get a reference to the pipe.
  190. pipe_type & pipe() & {return _pipe;}
  191. ///Get a const reference to the pipe.
  192. const pipe_type &pipe() const & {return _pipe;}
  193. ///Get a rvalue reference to the pipe. Qualified as rvalue.
  194. pipe_type && pipe() && {return std::move(_pipe);}
  195. ///Check if the pipe is open
  196. bool is_open() const {return _pipe.is_open(); }
  197. ///Open a new pipe
  198. basic_pipebuf<CharT, Traits>* open()
  199. {
  200. if (is_open())
  201. return nullptr;
  202. _pipe = pipe();
  203. return this;
  204. }
  205. ///Open a new named pipe
  206. basic_pipebuf<CharT, Traits>* open(const std::string & name)
  207. {
  208. if (is_open())
  209. return nullptr;
  210. _pipe = pipe(name);
  211. return this;
  212. }
  213. ///Flush the buffer & close the pipe
  214. basic_pipebuf<CharT, Traits>* close()
  215. {
  216. if (!is_open())
  217. return nullptr;
  218. overflow(Traits::eof());
  219. return this;
  220. }
  221. private:
  222. pipe_type _pipe;
  223. std::vector<char_type> _write;
  224. std::vector<char_type> _read;
  225. bool _write_impl()
  226. {
  227. if (!_pipe.is_open())
  228. return false;
  229. auto base = this->pbase();
  230. if (base == this->pptr())
  231. return true;
  232. std::ptrdiff_t wrt = _pipe.write(base,
  233. static_cast<typename pipe_type::int_type>(this->pptr() - base));
  234. std::ptrdiff_t diff = this->pptr() - base;
  235. if (wrt < diff)
  236. std::move(base + wrt, base + diff, base);
  237. else if (wrt == 0) //broken pipe
  238. return false;
  239. this->pbump(-wrt);
  240. return true;
  241. }
  242. };
  243. typedef basic_pipebuf<char> pipebuf;
  244. typedef basic_pipebuf<wchar_t> wpipebuf;
  245. /** Implementation of a reading pipe stream.
  246. *
  247. */
  248. template<
  249. class CharT,
  250. class Traits = std::char_traits<CharT>
  251. >
  252. class basic_ipstream : public std::basic_istream<CharT, Traits>
  253. {
  254. mutable basic_pipebuf<CharT, Traits> _buf;
  255. public:
  256. typedef basic_pipe<CharT, Traits> pipe_type;
  257. typedef CharT char_type ;
  258. typedef Traits traits_type;
  259. typedef typename Traits::int_type int_type ;
  260. typedef typename Traits::pos_type pos_type ;
  261. typedef typename Traits::off_type off_type ;
  262. ///Get access to the underlying stream_buf
  263. basic_pipebuf<CharT, Traits>* rdbuf() const {return &_buf;};
  264. ///Default constructor.
  265. basic_ipstream() : std::basic_istream<CharT, Traits>(nullptr)
  266. {
  267. std::basic_istream<CharT, Traits>::rdbuf(&_buf);
  268. };
  269. ///Copy constructor.
  270. basic_ipstream(const basic_ipstream & ) = delete;
  271. ///Move constructor.
  272. basic_ipstream(basic_ipstream && lhs) : std::basic_istream<CharT, Traits>(nullptr), _buf(std::move(lhs._buf))
  273. {
  274. std::basic_istream<CharT, Traits>::rdbuf(&_buf);
  275. }
  276. ///Move construct from a pipe.
  277. basic_ipstream(pipe_type && p) : std::basic_istream<CharT, Traits>(nullptr), _buf(std::move(p))
  278. {
  279. std::basic_istream<CharT, Traits>::rdbuf(&_buf);
  280. }
  281. ///Copy construct from a pipe.
  282. basic_ipstream(const pipe_type & p) : std::basic_istream<CharT, Traits>(nullptr), _buf(p)
  283. {
  284. std::basic_istream<CharT, Traits>::rdbuf(&_buf);
  285. }
  286. ///Copy assignment.
  287. basic_ipstream& operator=(const basic_ipstream & ) = delete;
  288. ///Move assignment
  289. basic_ipstream& operator=(basic_ipstream && lhs)
  290. {
  291. std::basic_istream<CharT, Traits>::operator=(std::move(lhs));
  292. _buf = std::move(lhs._buf);
  293. std::basic_istream<CharT, Traits>::rdbuf(&_buf);
  294. return *this;
  295. };
  296. ///Move assignment of a pipe.
  297. basic_ipstream& operator=(pipe_type && p)
  298. {
  299. _buf = std::move(p);
  300. return *this;
  301. }
  302. ///Copy assignment of a pipe.
  303. basic_ipstream& operator=(const pipe_type & p)
  304. {
  305. _buf = p;
  306. return *this;
  307. }
  308. ///Set the pipe of the streambuf.
  309. void pipe(pipe_type&& p) {_buf.pipe(std::move(p)); }
  310. ///Set the pipe of the streambuf.
  311. void pipe(const pipe_type& p) {_buf.pipe(p); }
  312. ///Get a reference to the pipe.
  313. pipe_type & pipe() & {return _buf.pipe();}
  314. ///Get a const reference to the pipe.
  315. const pipe_type &pipe() const & {return _buf.pipe();}
  316. ///Get a rvalue reference to the pipe. Qualified as rvalue.
  317. pipe_type && pipe() && {return std::move(_buf).pipe();}
  318. ///Check if the pipe is open
  319. bool is_open() const {return _buf.is_open();}
  320. ///Open a new pipe
  321. void open()
  322. {
  323. if (_buf.open() == nullptr)
  324. this->setstate(std::ios_base::failbit);
  325. else
  326. this->clear();
  327. }
  328. ///Open a new named pipe
  329. void open(const std::string & name)
  330. {
  331. if (_buf.open() == nullptr)
  332. this->setstate(std::ios_base::failbit);
  333. else
  334. this->clear();
  335. }
  336. ///Flush the buffer & close the pipe
  337. void close()
  338. {
  339. if (_buf.close() == nullptr)
  340. this->setstate(std::ios_base::failbit);
  341. }
  342. };
  343. typedef basic_ipstream<char> ipstream;
  344. typedef basic_ipstream<wchar_t> wipstream;
  345. /** Implementation of a write pipe stream.
  346. *
  347. */
  348. template<
  349. class CharT,
  350. class Traits = std::char_traits<CharT>
  351. >
  352. class basic_opstream : public std::basic_ostream<CharT, Traits>
  353. {
  354. mutable basic_pipebuf<CharT, Traits> _buf;
  355. public:
  356. typedef basic_pipe<CharT, Traits> pipe_type;
  357. typedef CharT char_type ;
  358. typedef Traits traits_type;
  359. typedef typename Traits::int_type int_type ;
  360. typedef typename Traits::pos_type pos_type ;
  361. typedef typename Traits::off_type off_type ;
  362. ///Get access to the underlying stream_buf
  363. basic_pipebuf<CharT, Traits>* rdbuf() const {return &_buf;};
  364. ///Default constructor.
  365. basic_opstream() : std::basic_ostream<CharT, Traits>(nullptr)
  366. {
  367. std::basic_ostream<CharT, Traits>::rdbuf(&_buf);
  368. };
  369. ///Copy constructor.
  370. basic_opstream(const basic_opstream & ) = delete;
  371. ///Move constructor.
  372. basic_opstream(basic_opstream && lhs) : std::basic_ostream<CharT, Traits>(nullptr), _buf(std::move(lhs._buf))
  373. {
  374. std::basic_ostream<CharT, Traits>::rdbuf(&_buf);
  375. }
  376. ///Move construct from a pipe.
  377. basic_opstream(pipe_type && p) : std::basic_ostream<CharT, Traits>(nullptr), _buf(std::move(p))
  378. {
  379. std::basic_ostream<CharT, Traits>::rdbuf(&_buf);
  380. };
  381. ///Copy construct from a pipe.
  382. basic_opstream(const pipe_type & p) : std::basic_ostream<CharT, Traits>(nullptr), _buf(p)
  383. {
  384. std::basic_ostream<CharT, Traits>::rdbuf(&_buf);
  385. };
  386. ///Copy assignment.
  387. basic_opstream& operator=(const basic_opstream & ) = delete;
  388. ///Move assignment
  389. basic_opstream& operator=(basic_opstream && lhs)
  390. {
  391. std::basic_ostream<CharT, Traits>::operator=(std::move(lhs));
  392. _buf = std::move(lhs._buf);
  393. std::basic_ostream<CharT, Traits>::rdbuf(&_buf);
  394. return *this;
  395. };
  396. ///Move assignment of a pipe.
  397. basic_opstream& operator=(pipe_type && p)
  398. {
  399. _buf = std::move(p);
  400. return *this;
  401. }
  402. ///Copy assignment of a pipe.
  403. basic_opstream& operator=(const pipe_type & p)
  404. {
  405. _buf = p;
  406. return *this;
  407. }
  408. ///Set the pipe of the streambuf.
  409. void pipe(pipe_type&& p) {_buf.pipe(std::move(p)); }
  410. ///Set the pipe of the streambuf.
  411. void pipe(const pipe_type& p) {_buf.pipe(p); }
  412. ///Get a reference to the pipe.
  413. pipe_type & pipe() & {return _buf.pipe();}
  414. ///Get a const reference to the pipe.
  415. const pipe_type &pipe() const & {return _buf.pipe();}
  416. ///Get a rvalue reference to the pipe. Qualified as rvalue.
  417. pipe_type && pipe() && {return std::move(_buf).pipe();}
  418. ///Open a new pipe
  419. void open()
  420. {
  421. if (_buf.open() == nullptr)
  422. this->setstate(std::ios_base::failbit);
  423. else
  424. this->clear();
  425. }
  426. ///Open a new named pipe
  427. void open(const std::string & name)
  428. {
  429. if (_buf.open() == nullptr)
  430. this->setstate(std::ios_base::failbit);
  431. else
  432. this->clear();
  433. }
  434. ///Flush the buffer & close the pipe
  435. void close()
  436. {
  437. if (_buf.close() == nullptr)
  438. this->setstate(std::ios_base::failbit);
  439. }
  440. };
  441. typedef basic_opstream<char> opstream;
  442. typedef basic_opstream<wchar_t> wopstream;
  443. /** Implementation of a read-write pipe stream.
  444. *
  445. */
  446. template<
  447. class CharT,
  448. class Traits = std::char_traits<CharT>
  449. >
  450. class basic_pstream : public std::basic_iostream<CharT, Traits>
  451. {
  452. mutable basic_pipebuf<CharT, Traits> _buf;
  453. public:
  454. typedef basic_pipe<CharT, Traits> pipe_type;
  455. typedef CharT char_type ;
  456. typedef Traits traits_type;
  457. typedef typename Traits::int_type int_type ;
  458. typedef typename Traits::pos_type pos_type ;
  459. typedef typename Traits::off_type off_type ;
  460. ///Get access to the underlying stream_buf
  461. basic_pipebuf<CharT, Traits>* rdbuf() const {return &_buf;};
  462. ///Default constructor.
  463. basic_pstream() : std::basic_iostream<CharT, Traits>(nullptr)
  464. {
  465. std::basic_iostream<CharT, Traits>::rdbuf(&_buf);
  466. };
  467. ///Copy constructor.
  468. basic_pstream(const basic_pstream & ) = delete;
  469. ///Move constructor.
  470. basic_pstream(basic_pstream && lhs) : std::basic_iostream<CharT, Traits>(nullptr), _buf(std::move(lhs._buf))
  471. {
  472. std::basic_iostream<CharT, Traits>::rdbuf(&_buf);
  473. }
  474. ///Move construct from a pipe.
  475. basic_pstream(pipe_type && p) : std::basic_iostream<CharT, Traits>(nullptr), _buf(std::move(p))
  476. {
  477. std::basic_iostream<CharT, Traits>::rdbuf(&_buf);
  478. };
  479. ///Copy construct from a pipe.
  480. basic_pstream(const pipe_type & p) : std::basic_iostream<CharT, Traits>(nullptr), _buf(p)
  481. {
  482. std::basic_iostream<CharT, Traits>::rdbuf(&_buf);
  483. };
  484. ///Copy assignment.
  485. basic_pstream& operator=(const basic_pstream & ) = delete;
  486. ///Move assignment
  487. basic_pstream& operator=(basic_pstream && lhs)
  488. {
  489. std::basic_istream<CharT, Traits>::operator=(std::move(lhs));
  490. _buf = std::move(lhs._buf);
  491. std::basic_iostream<CharT, Traits>::rdbuf(&_buf);
  492. return *this;
  493. };
  494. ///Move assignment of a pipe.
  495. basic_pstream& operator=(pipe_type && p)
  496. {
  497. _buf = std::move(p);
  498. return *this;
  499. }
  500. ///Copy assignment of a pipe.
  501. basic_pstream& operator=(const pipe_type & p)
  502. {
  503. _buf = p;
  504. return *this;
  505. }
  506. ///Set the pipe of the streambuf.
  507. void pipe(pipe_type&& p) {_buf.pipe(std::move(p)); }
  508. ///Set the pipe of the streambuf.
  509. void pipe(const pipe_type& p) {_buf.pipe(p); }
  510. ///Get a reference to the pipe.
  511. pipe_type & pipe() & {return _buf.pipe();}
  512. ///Get a const reference to the pipe.
  513. const pipe_type &pipe() const & {return _buf.pipe();}
  514. ///Get a rvalue reference to the pipe. Qualified as rvalue.
  515. pipe_type && pipe() && {return std::move(_buf).pipe();}
  516. ///Open a new pipe
  517. void open()
  518. {
  519. if (_buf.open() == nullptr)
  520. this->setstate(std::ios_base::failbit);
  521. else
  522. this->clear();
  523. }
  524. ///Open a new named pipe
  525. void open(const std::string & name)
  526. {
  527. if (_buf.open() == nullptr)
  528. this->setstate(std::ios_base::failbit);
  529. else
  530. this->clear();
  531. }
  532. ///Flush the buffer & close the pipe
  533. void close()
  534. {
  535. if (_buf.close() == nullptr)
  536. this->setstate(std::ios_base::failbit);
  537. }
  538. };
  539. typedef basic_pstream<char> pstream;
  540. typedef basic_pstream<wchar_t> wpstream;
  541. }}
  542. #endif