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.

234 lines
6.7 KiB

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