pipe.hpp 18 KB

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