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.

160 lines
5.9 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: comstl/util/thread_marshal.hpp
  3. *
  4. * Purpose: Thread marshalling functions.
  5. *
  6. * Created: 25th May 2002
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2002-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 comstl/util/thread_marshal.hpp
  40. *
  41. * \brief [C++ only; requires COM] Thread marshalling functions
  42. * (\ref group__library__utility__com "COM Utility" Library).
  43. */
  44. #ifndef COMSTL_INCL_COMSTL_UTIL_HPP_THREAD_MARSHAL
  45. #define COMSTL_INCL_COMSTL_UTIL_HPP_THREAD_MARSHAL
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define COMSTL_VER_COMSTL_UTIL_HPP_THREAD_MARSHAL_MAJOR 4
  48. # define COMSTL_VER_COMSTL_UTIL_HPP_THREAD_MARSHAL_MINOR 0
  49. # define COMSTL_VER_COMSTL_UTIL_HPP_THREAD_MARSHAL_REVISION 3
  50. # define COMSTL_VER_COMSTL_UTIL_HPP_THREAD_MARSHAL_EDIT 57
  51. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  52. /* /////////////////////////////////////////////////////////////////////////
  53. * Includes
  54. */
  55. #ifndef COMSTL_INCL_COMSTL_H_COMSTL
  56. # include <comstl/comstl.h>
  57. #endif /* !COMSTL_INCL_COMSTL_H_COMSTL */
  58. #ifndef COMSTL_INCL_COMSTL_UTIL_HPP_INTERFACE_TRAITS
  59. # include <comstl/util/interface_traits.hpp>
  60. #endif /* !COMSTL_INCL_COMSTL_UTIL_HPP_INTERFACE_TRAITS */
  61. /* /////////////////////////////////////////////////////////////////////////
  62. * Namespace
  63. */
  64. #ifndef _COMSTL_NO_NAMESPACE
  65. # if defined(_STLSOFT_NO_NAMESPACE) || \
  66. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  67. /* There is no stlsoft namespace, so must define ::comstl */
  68. namespace comstl
  69. {
  70. # else
  71. /* Define stlsoft::comstl_project */
  72. namespace stlsoft
  73. {
  74. namespace comstl_project
  75. {
  76. # endif /* _STLSOFT_NO_NAMESPACE */
  77. #endif /* !_COMSTL_NO_NAMESPACE */
  78. /* /////////////////////////////////////////////////////////////////////////
  79. * Functions
  80. */
  81. /** \brief Marshal a interface to a stream
  82. *
  83. * \ingroup group__library__utility__com
  84. *
  85. * This function marshals the given interface into a new stream instance, which
  86. * is returned to the caller. The stream may then be passed directly to another
  87. * thread in the process, from which GetInterfaceAndReleaseStream() may be
  88. * called.
  89. *
  90. * \param pitf The interface pointer to marshal
  91. * \param ppstm A pointer to an IStream pointer to receive the stream
  92. * \return An HRESULT indicating success or failure
  93. * \retval E_OUTOFMEMORY Sufficient memory could not be acquired
  94. * \retval S_OK The operation completed successfully
  95. */
  96. template <ss_typename_param_k I>
  97. inline HRESULT MarshalInterThreadInterfaceInStream(I *pitf, LPSTREAM *ppstm)
  98. {
  99. return ::CoMarshalInterThreadInterfaceInStream(IID_traits<I>().iid(), pitf, ppstm);
  100. }
  101. /** \brief Retrieve a marshaled interface pointer from a stream
  102. *
  103. * \ingroup group__library__utility__com
  104. *
  105. * This function loads a serialised marshalled interface pointer from the given
  106. * stream, queries for the interface of the given pointer, and returns the
  107. * pointer if successful, and an error code if not. The stream is always
  108. * released, irrespective of the success status of the function as a whole.
  109. *
  110. * \param pstm An IStream pointer from which object is to be unmarshaled
  111. * \param ppitf A pointer to the interface pointer to be unmarshaled
  112. * \return An HRESULT indicating success or failure
  113. * \retval E_INVALIDARG The argument was invalid
  114. * \retval S_OK The operation completed successfully
  115. */
  116. template <ss_typename_param_k I>
  117. inline HRESULT GetInterfaceAndReleaseStream(LPSTREAM pstm, I **ppitf)
  118. {
  119. return ::CoGetInterfaceAndReleaseStream(pstm, IID_traits<I>().iid(), reinterpret_cast<void**>(ppitf));
  120. }
  121. ////////////////////////////////////////////////////////////////////////////
  122. // Unit-testing
  123. #ifdef STLSOFT_UNITTEST
  124. # include "./unittest/thread_marshal_unittest_.h"
  125. #endif /* STLSOFT_UNITTEST */
  126. /* ////////////////////////////////////////////////////////////////////// */
  127. #ifndef _COMSTL_NO_NAMESPACE
  128. # if defined(_STLSOFT_NO_NAMESPACE) || \
  129. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  130. } // namespace comstl
  131. # else
  132. } // namespace comstl_project
  133. } // namespace stlsoft
  134. # endif /* _STLSOFT_NO_NAMESPACE */
  135. #endif /* !_COMSTL_NO_NAMESPACE */
  136. /* ////////////////////////////////////////////////////////////////////// */
  137. #endif /* !COMSTL_INCL_COMSTL_UTIL_HPP_THREAD_MARSHAL */
  138. /* ///////////////////////////// end of file //////////////////////////// */