pipe.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. ///Move construct from a pipe.
  102. basic_pipebuf(pipe_type && p) : _pipe(std::move(p)),
  103. _write(default_buffer_size),
  104. _read(default_buffer_size)
  105. {
  106. this->setg(_read.data(), _read.data()+ 128, _read.data() + 128);
  107. this->setp(_write.data(), _write.data() + _write.size());
  108. }
  109. ///Construct from a pipe.
  110. basic_pipebuf(const pipe_type & p) : _pipe(p),
  111. _write(default_buffer_size),
  112. _read(default_buffer_size)
  113. {
  114. this->setg(_read.data(), _read.data()+ 128, _read.data() + 128);
  115. this->setp(_write.data(), _write.data() + _write.size());
  116. }
  117. ///Copy assign.
  118. basic_pipebuf& operator=(const basic_pipebuf & ) = delete;
  119. ///Move assign.
  120. basic_pipebuf& operator=(basic_pipebuf && ) = default;
  121. ///Move assign a pipe.
  122. basic_pipebuf& operator=(pipe_type && p)
  123. {
  124. _pipe = std::move(p);
  125. return *this;
  126. }
  127. ///Copy assign a pipe.
  128. basic_pipebuf& operator=(const pipe_type & p)
  129. {
  130. _pipe = p;
  131. return *this;
  132. }
  133. ///Writes characters to the associated output sequence from the put area
  134. int_type overflow(int_type ch = traits_type::eof()) override
  135. {
  136. if ((ch != traits_type::eof()) && _pipe.is_open())
  137. {
  138. if (this->pptr() == this->epptr())
  139. {
  140. bool wr = this->_write_impl();
  141. *this->pptr() = ch;
  142. this->pbump(1);
  143. if (wr)
  144. return ch;
  145. }
  146. else
  147. {
  148. *this->pptr() = ch;
  149. this->pbump(1);
  150. if (this->_write_impl())
  151. return ch;
  152. }
  153. }
  154. return traits_type::eof();
  155. }
  156. ///Synchronizes the buffers with the associated character sequence
  157. int sync() override { return this->_write_impl() ? 0 : -1; }
  158. ///Reads characters from the associated input sequence to the get area
  159. int_type underflow() override
  160. {
  161. if (!_pipe.is_open())
  162. return traits_type::eof();
  163. if (this->egptr() == &_read.back()) //ok, so we're at the end of the buffer
  164. this->setg(_read.data(), _read.data()+ 10, _read.data() + 10);
  165. auto len = &_read.back() - this->egptr() ;
  166. auto res = _pipe.read(
  167. this->egptr(),
  168. static_cast<typename pipe_type::int_type>(len));
  169. if (res == 0)
  170. return traits_type::eof();
  171. this->setg(this->eback(), this->gptr(), this->egptr() + res);
  172. auto val = *this->gptr();
  173. return traits_type::to_int_type(val);
  174. }
  175. ///Set the pipe of the streambuf.
  176. void pipe(pipe_type&& p) {_pipe = std::move(p); }
  177. ///Set the pipe of the streambuf.
  178. void pipe(const pipe_type& p) {_pipe = p; }
  179. ///Get a reference to the pipe.
  180. pipe_type & pipe() & {return _pipe;}
  181. ///Get a const reference to the pipe.
  182. const pipe_type &pipe() const & {return _pipe;}
  183. ///Get a rvalue reference to the pipe. Qualified as rvalue.
  184. pipe_type && pipe() && {return std::move(_pipe);}
  185. private:
  186. pipe_type _pipe;
  187. std::vector<char_type> _write;
  188. std::vector<char_type> _read;
  189. bool _write_impl()
  190. {
  191. if (!_pipe.is_open())
  192. return false;
  193. auto base = this->pbase();
  194. std::ptrdiff_t wrt = _pipe.write(base,
  195. static_cast<typename pipe_type::int_type>(this->pptr() - base));
  196. std::ptrdiff_t diff = this->pptr() - base;
  197. if (wrt < diff)
  198. std::move(base + wrt, base + diff, base);
  199. else if (wrt == 0) //broken pipe
  200. return false;
  201. this->pbump(-wrt);
  202. return true;
  203. }
  204. };
  205. typedef basic_pipebuf<char> pipebuf;
  206. typedef basic_pipebuf<wchar_t> wpipebuf;
  207. /** Implementation of a reading pipe stream.
  208. *
  209. */
  210. template<
  211. class CharT,
  212. class Traits = std::char_traits<CharT>
  213. >
  214. class basic_ipstream : public std::basic_istream<CharT, Traits>
  215. {
  216. mutable basic_pipebuf<CharT, Traits> _buf;
  217. public:
  218. typedef basic_pipe<CharT, Traits> pipe_type;
  219. typedef CharT char_type ;
  220. typedef Traits traits_type;
  221. typedef typename Traits::int_type int_type ;
  222. typedef typename Traits::pos_type pos_type ;
  223. typedef typename Traits::off_type off_type ;
  224. ///Get access to the underlying stream_buf
  225. basic_pipebuf<CharT, Traits>* rdbuf() const {return &_buf;};
  226. ///Default constructor.
  227. basic_ipstream() : std::basic_istream<CharT, Traits>(nullptr)
  228. {
  229. std::basic_istream<CharT, Traits>::rdbuf(&_buf);
  230. };
  231. ///Copy constructor.
  232. basic_ipstream(const basic_ipstream & ) = delete;
  233. ///Move constructor.
  234. basic_ipstream(basic_ipstream && lhs) : std::basic_istream<CharT, Traits>(nullptr), _buf(std::move(lhs._buf))
  235. {
  236. std::basic_istream<CharT, Traits>::rdbuf(&_buf);
  237. }
  238. ///Move construct from a pipe.
  239. basic_ipstream(pipe_type && p) : std::basic_istream<CharT, Traits>(nullptr), _buf(std::move(p))
  240. {
  241. std::basic_istream<CharT, Traits>::rdbuf(&_buf);
  242. }
  243. ///Copy construct from a pipe.
  244. basic_ipstream(const pipe_type & p) : std::basic_istream<CharT, Traits>(nullptr), _buf(p)
  245. {
  246. std::basic_istream<CharT, Traits>::rdbuf(&_buf);
  247. }
  248. ///Copy assignment.
  249. basic_ipstream& operator=(const basic_ipstream & ) = delete;
  250. ///Move assignment
  251. basic_ipstream& operator=(basic_ipstream && lhs)
  252. {
  253. std::basic_istream<CharT, Traits>::operator=(std::move(lhs));
  254. _buf = std::move(lhs);
  255. std::basic_istream<CharT, Traits>::rdbuf(&_buf);
  256. };
  257. ///Move assignment of a pipe.
  258. basic_ipstream& operator=(pipe_type && p)
  259. {
  260. _buf = std::move(p);
  261. return *this;
  262. }
  263. ///Copy assignment of a pipe.
  264. basic_ipstream& operator=(const pipe_type & p)
  265. {
  266. _buf = p;
  267. return *this;
  268. }
  269. ///Set the pipe of the streambuf.
  270. void pipe(pipe_type&& p) {_buf.pipe(std::move(p)); }
  271. ///Set the pipe of the streambuf.
  272. void pipe(const pipe_type& p) {_buf.pipe(p); }
  273. ///Get a reference to the pipe.
  274. pipe_type & pipe() & {return _buf.pipe();}
  275. ///Get a const reference to the pipe.
  276. const pipe_type &pipe() const & {return _buf.pipe();}
  277. ///Get a rvalue reference to the pipe. Qualified as rvalue.
  278. pipe_type && pipe() && {return std::move(_buf).pipe();}
  279. };
  280. typedef basic_ipstream<char> ipstream;
  281. typedef basic_ipstream<wchar_t> wipstream;
  282. /** Implementation of a write pipe stream.
  283. *
  284. */
  285. template<
  286. class CharT,
  287. class Traits = std::char_traits<CharT>
  288. >
  289. class basic_opstream : public std::basic_ostream<CharT, Traits>
  290. {
  291. mutable basic_pipebuf<CharT, Traits> _buf;
  292. public:
  293. typedef basic_pipe<CharT, Traits> pipe_type;
  294. typedef CharT char_type ;
  295. typedef Traits traits_type;
  296. typedef typename Traits::int_type int_type ;
  297. typedef typename Traits::pos_type pos_type ;
  298. typedef typename Traits::off_type off_type ;
  299. ///Get access to the underlying stream_buf
  300. basic_pipebuf<CharT, Traits>* rdbuf() const {return &_buf;};
  301. ///Default constructor.
  302. basic_opstream() : std::basic_ostream<CharT, Traits>(nullptr)
  303. {
  304. std::basic_ostream<CharT, Traits>::rdbuf(&_buf);
  305. };
  306. ///Copy constructor.
  307. basic_opstream(const basic_opstream & ) = delete;
  308. ///Move constructor.
  309. basic_opstream(basic_opstream && lhs) : std::basic_ostream<CharT, Traits>(nullptr), _buf(std::move(lhs._buf))
  310. {
  311. std::basic_ostream<CharT, Traits>::rdbuf(&_buf);
  312. }
  313. ///Move construct from a pipe.
  314. basic_opstream(pipe_type && p) : std::basic_ostream<CharT, Traits>(nullptr), _buf(std::move(p))
  315. {
  316. std::basic_ostream<CharT, Traits>::rdbuf(&_buf);
  317. };
  318. ///Copy construct from a pipe.
  319. basic_opstream(const pipe_type & p) : std::basic_ostream<CharT, Traits>(nullptr), _buf(p)
  320. {
  321. std::basic_ostream<CharT, Traits>::rdbuf(&_buf);
  322. };
  323. ///Copy assignment.
  324. basic_opstream& operator=(const basic_opstream & ) = delete;
  325. ///Move assignment
  326. basic_opstream& operator=(basic_opstream && lhs)
  327. {
  328. std::basic_ostream<CharT, Traits>::operator=(std::move(lhs));
  329. _buf = std::move(lhs);
  330. std::basic_ostream<CharT, Traits>::rdbuf(&_buf);
  331. };
  332. ///Move assignment of a pipe.
  333. basic_opstream& operator=(pipe_type && p)
  334. {
  335. _buf = std::move(p);
  336. return *this;
  337. }
  338. ///Copy assignment of a pipe.
  339. basic_opstream& operator=(const pipe_type & p)
  340. {
  341. _buf = p;
  342. return *this;
  343. }
  344. ///Set the pipe of the streambuf.
  345. void pipe(pipe_type&& p) {_buf.pipe(std::move(p)); }
  346. ///Set the pipe of the streambuf.
  347. void pipe(const pipe_type& p) {_buf.pipe(p); }
  348. ///Get a reference to the pipe.
  349. pipe_type & pipe() & {return _buf.pipe();}
  350. ///Get a const reference to the pipe.
  351. const pipe_type &pipe() const & {return _buf.pipe();}
  352. ///Get a rvalue reference to the pipe. Qualified as rvalue.
  353. pipe_type && pipe() && {return std::move(_buf).pipe();}
  354. };
  355. typedef basic_opstream<char> opstream;
  356. typedef basic_opstream<wchar_t> wopstream;
  357. /** Implementation of a read-write pipe stream.
  358. *
  359. */
  360. template<
  361. class CharT,
  362. class Traits = std::char_traits<CharT>
  363. >
  364. class basic_pstream : public std::basic_iostream<CharT, Traits>
  365. {
  366. mutable basic_pipebuf<CharT, Traits> _buf;
  367. public:
  368. typedef basic_pipe<CharT, Traits> pipe_type;
  369. typedef CharT char_type ;
  370. typedef Traits traits_type;
  371. typedef typename Traits::int_type int_type ;
  372. typedef typename Traits::pos_type pos_type ;
  373. typedef typename Traits::off_type off_type ;
  374. ///Get access to the underlying stream_buf
  375. basic_pipebuf<CharT, Traits>* rdbuf() const {return &_buf;};
  376. ///Default constructor.
  377. basic_pstream() : std::basic_iostream<CharT, Traits>(nullptr)
  378. {
  379. std::basic_iostream<CharT, Traits>::rdbuf(&_buf);
  380. };
  381. ///Copy constructor.
  382. basic_pstream(const basic_pstream & ) = delete;
  383. ///Move constructor.
  384. basic_pstream(basic_pstream && lhs) : std::basic_iostream<CharT, Traits>(nullptr), _buf(std::move(lhs._buf))
  385. {
  386. std::basic_iostream<CharT, Traits>::rdbuf(&_buf);
  387. }
  388. ///Move construct from a pipe.
  389. basic_pstream(pipe_type && p) : std::basic_iostream<CharT, Traits>(nullptr), _buf(std::move(p))
  390. {
  391. std::basic_iostream<CharT, Traits>::rdbuf(&_buf);
  392. };
  393. ///Copy construct from a pipe.
  394. basic_pstream(const pipe_type & p) : std::basic_iostream<CharT, Traits>(nullptr), _buf(p)
  395. {
  396. std::basic_iostream<CharT, Traits>::rdbuf(&_buf);
  397. };
  398. ///Copy assignment.
  399. basic_pstream& operator=(const basic_pstream & ) = delete;
  400. ///Move assignment
  401. basic_pstream& operator=(basic_pstream && lhs)
  402. {
  403. std::basic_istream<CharT, Traits>::operator=(std::move(lhs));
  404. _buf = std::move(lhs);
  405. std::basic_iostream<CharT, Traits>::rdbuf(&_buf);
  406. };
  407. ///Move assignment of a pipe.
  408. basic_pstream& operator=(pipe_type && p)
  409. {
  410. _buf = std::move(p);
  411. return *this;
  412. }
  413. ///Copy assignment of a pipe.
  414. basic_pstream& operator=(const pipe_type & p)
  415. {
  416. _buf = p;
  417. return *this;
  418. }
  419. ///Set the pipe of the streambuf.
  420. void pipe(pipe_type&& p) {_buf.pipe(std::move(p)); }
  421. ///Set the pipe of the streambuf.
  422. void pipe(const pipe_type& p) {_buf.pipe(p); }
  423. ///Get a reference to the pipe.
  424. pipe_type & pipe() & {return _buf.pipe();}
  425. ///Get a const reference to the pipe.
  426. const pipe_type &pipe() const & {return _buf.pipe();}
  427. ///Get a rvalue reference to the pipe. Qualified as rvalue.
  428. pipe_type && pipe() && {return std::move(_buf).pipe();}
  429. };
  430. typedef basic_pstream<char> pstream;
  431. typedef basic_pstream<wchar_t> wpstream;
  432. }}
  433. #endif