You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

226 lines
6.4 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: unixstl/filesystem/pipe.hpp
  3. *
  4. * Purpose: pipe class, based on Windows anonymous pipe.
  5. *
  6. * Created: 19th June 2004
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2004-2009, Matthew Wilson and Synesis Software
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  23. * any contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * ////////////////////////////////////////////////////////////////////// */
  39. /** \file unixstl/filesystem/pipe.hpp
  40. *
  41. * \brief [C++ only] Definition of the unixstl::pipe class
  42. * (\ref group__library__filesystem "File System" Library).
  43. */
  44. #ifndef UNIXSTL_INCL_UNIXSTL_FILESYSTEM_HPP_PIPE
  45. #define UNIXSTL_INCL_UNIXSTL_FILESYSTEM_HPP_PIPE
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define UNIXSTL_VER_UNIXSTL_FILESYSTEM_HPP_PIPE_MAJOR 4
  48. # define UNIXSTL_VER_UNIXSTL_FILESYSTEM_HPP_PIPE_MINOR 1
  49. # define UNIXSTL_VER_UNIXSTL_FILESYSTEM_HPP_PIPE_REVISION 1
  50. # define UNIXSTL_VER_UNIXSTL_FILESYSTEM_HPP_PIPE_EDIT 43
  51. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  52. /* /////////////////////////////////////////////////////////////////////////
  53. * Includes
  54. */
  55. #ifndef UNIXSTL_INCL_UNIXSTL_H_UNIXSTL
  56. # include <unixstl/unixstl.h>
  57. #endif /* !UNIXSTL_INCL_UNIXSTL_H_UNIXSTL */
  58. #ifndef UNIXSTL_INCL_UNIXSTL_HPP_ERROR_UNIX_EXCEPTIONS
  59. # include <unixstl/error/exceptions.hpp>
  60. #endif /* !UNIXSTL_INCL_UNIXSTL_ERROR_HPP_UNIX_EXCEPTIONS */
  61. /* /////////////////////////////////////////////////////////////////////////
  62. * Namespace
  63. */
  64. #ifndef _UNIXSTL_NO_NAMESPACE
  65. # if defined(_STLSOFT_NO_NAMESPACE) || \
  66. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  67. /* There is no stlsoft namespace, so must define ::unixstl */
  68. namespace unixstl
  69. {
  70. # else
  71. /* Define stlsoft::unixstl_project */
  72. namespace stlsoft
  73. {
  74. namespace unixstl_project
  75. {
  76. # endif /* _STLSOFT_NO_NAMESPACE */
  77. #endif /* !_UNIXSTL_NO_NAMESPACE */
  78. /* /////////////////////////////////////////////////////////////////////////
  79. * Classes
  80. */
  81. #ifdef __SYNSOFT_DBS_COMPILER_SUPPORTS_PRAGMA_MESSAGE
  82. # pragma message(_sscomp_fileline_message("This needs to be parameterised with a unixstl::system_resource_policy, which would control whether to throw if MX create fails"))
  83. #endif /* __SYNSOFT_DBS_COMPILER_SUPPORTS_PRAGMA_MESSAGE */
  84. /** \brief Class which wraps the UNIX pipe() function
  85. *
  86. * \ingroup group__library__filesystem
  87. */
  88. class pipe
  89. {
  90. /// \name Member Types
  91. /// @{
  92. public:
  93. /// The class type
  94. typedef pipe class_type;
  95. /// The exception policy type
  96. typedef unix_exception_policy exception_policy_type;
  97. /// @}
  98. /// \name Construction
  99. /// @{
  100. public:
  101. pipe()
  102. {
  103. #if defined(_WIN32) && \
  104. ( defined(_MSC_VER) || \
  105. defined(STLSOFT_COMPILER_IS_DMC))
  106. if(0 != ::_pipe(&m_handles[0], 10240, _O_TEXT))
  107. #else /* ? _WIN32 */
  108. if(0 != ::pipe(&m_handles[0]))
  109. #endif /* _WIN32 */
  110. {
  111. exception_policy_type()(errno);
  112. m_handles[0] = -1;
  113. m_handles[1] = -1;
  114. }
  115. }
  116. ~pipe() stlsoft_throw_0()
  117. {
  118. if(-1 != read_handle())
  119. {
  120. ::close(m_handles[0]);
  121. }
  122. if(-1 != write_handle())
  123. {
  124. ::close(m_handles[1]);
  125. }
  126. }
  127. /// @}
  128. /// \name Accessors
  129. /// @{
  130. public:
  131. /// \brief Returns the read handle of the pipe
  132. int read_handle() const
  133. {
  134. return m_handles[0];
  135. }
  136. int write_handle() const
  137. {
  138. return m_handles[1];
  139. }
  140. /// @}
  141. /// \name Operations
  142. /// @{
  143. public:
  144. /// \brief Closes the read handle, if not already closed
  145. void close_read()
  146. {
  147. if(-1 != read_handle())
  148. {
  149. ::close(m_handles[0]);
  150. m_handles[0] = -1;
  151. }
  152. }
  153. /// \brief Closes the write handle, if not already closed
  154. void close_write()
  155. {
  156. if(-1 != write_handle())
  157. {
  158. ::close(m_handles[1]);
  159. m_handles[1] = -1;
  160. }
  161. }
  162. /// \brief Closes the read and write handles, if not already closed
  163. void close()
  164. {
  165. close_read();
  166. close_write();
  167. }
  168. /// @}
  169. /// \name Members
  170. /// @{
  171. private:
  172. int m_handles[2];
  173. /// @}
  174. /// \name Not to be implemented
  175. /// @{
  176. private:
  177. pipe(class_type const&);
  178. class_type& operator =(class_type const&);
  179. /// @}
  180. };
  181. ////////////////////////////////////////////////////////////////////////////
  182. // Unit-testing
  183. #ifdef STLSOFT_UNITTEST
  184. # include "./unittest/pipe_unittest_.h"
  185. #endif /* STLSOFT_UNITTEST */
  186. /* ////////////////////////////////////////////////////////////////////// */
  187. #ifndef _UNIXSTL_NO_NAMESPACE
  188. # if defined(_STLSOFT_NO_NAMESPACE) || \
  189. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  190. } // namespace unixstl
  191. # else
  192. } // namespace unixstl_project
  193. } // namespace stlsoft
  194. # endif /* _STLSOFT_NO_NAMESPACE */
  195. #endif /* !_UNIXSTL_NO_NAMESPACE */
  196. /* ////////////////////////////////////////////////////////////////////// */
  197. #endif /* !UNIXSTL_INCL_UNIXSTL_FILESYSTEM_HPP_PIPE */
  198. /* ///////////////////////////// end of file //////////////////////////// */