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.

303 lines
10 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: comstl/util/object_functions.h (originally MOComFns.h)
  3. *
  4. * Purpose: Reference-counting helper functions.
  5. *
  6. * Created: 2nd March 1996
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 1996-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/object_functions.h
  40. *
  41. * \brief [C, C++] Definition of several interface and identity functions
  42. * (\ref group__library__utility__com "COM Utility" Library).
  43. */
  44. #ifndef COMSTL_INCL_COMSTL_UTIL_H_OBJECT_FUNCTIONS
  45. #define COMSTL_INCL_COMSTL_UTIL_H_OBJECT_FUNCTIONS
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define COMSTL_VER_COMSTL_UTIL_H_OBJECT_FUNCTIONS_MAJOR 3
  48. # define COMSTL_VER_COMSTL_UTIL_H_OBJECT_FUNCTIONS_MINOR 1
  49. # define COMSTL_VER_COMSTL_UTIL_H_OBJECT_FUNCTIONS_REVISION 2
  50. # define COMSTL_VER_COMSTL_UTIL_H_OBJECT_FUNCTIONS_EDIT 66
  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. /* /////////////////////////////////////////////////////////////////////////
  59. * Namespace
  60. */
  61. #if !defined(_COMSTL_NO_NAMESPACE) && \
  62. !defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  63. # if defined(_STLSOFT_NO_NAMESPACE)
  64. /* There is no stlsoft namespace, so must define ::comstl */
  65. namespace comstl
  66. {
  67. # else
  68. /* Define stlsoft::comstl_project */
  69. namespace stlsoft
  70. {
  71. namespace comstl_project
  72. {
  73. # endif /* _STLSOFT_NO_NAMESPACE */
  74. #endif /* !_COMSTL_NO_NAMESPACE */
  75. /* /////////////////////////////////////////////////////////////////////////
  76. * C functions
  77. */
  78. /** \brief [C only] Evaluates an object's identity
  79. *
  80. * \param p The pointer to the object whose identity will be evaluated
  81. * \param identity
  82. *
  83. * \return A status code indicating whether the identity was retrieved
  84. * \retval S_OK The pointers refer to the same object
  85. * \retval E_POINTER If p is null
  86. * \retval E_INVALIDARG If identity is null
  87. * \retval any-other-code Indicates an error from the call to QueryInterface()
  88. *
  89. * \pre \c p is a NULL pointer, or a valid pointer to a COM interface.
  90. *
  91. * \remarks If the function success, the caller must release the reference count
  92. * associated with the interface pointer in \c *identity.
  93. *
  94. * \note Intended for use in C-compilation units only. For C++, use comstl::get_object_identity().
  95. */
  96. STLSOFT_INLINE HRESULT comstl__get_object_identity(/* [in] */ LPUNKNOWN p, /* [in] */ LPUNKNOWN *identity)
  97. {
  98. HRESULT hr;
  99. if(NULL == identity)
  100. {
  101. hr = E_INVALIDARG;
  102. }
  103. else if(NULL == p)
  104. {
  105. hr = E_POINTER;
  106. }
  107. else
  108. {
  109. hr = COMSTL_ITF_CALL(p)->QueryInterface(COMSTL_ITF_THIS(p) COMSTL_IID_2_REF(IID_IUnknown), stlsoft_reinterpret_cast(void**, identity));
  110. }
  111. return hr;
  112. }
  113. /** \brief [C only] Determines whether two interfaces refer to the same object
  114. *
  115. * \return A status code indicating whether the two pointers refer to the same
  116. * object
  117. * \retval S_OK The pointers refer to the same object
  118. * \retval S_FALSE The pointers refer to different objects
  119. * \retval E_POINTER If either/both pointers are null
  120. * \retval any-other-code Indicates an error from one of the calls to QueryInterface()
  121. *
  122. * \pre \c p1 and \c p2 are NULL pointers, or are valid pointers to COM interfaces.
  123. */
  124. STLSOFT_INLINE HRESULT comstl__is_same_object(/* [in] */ LPUNKNOWN p1, /* [in] */ LPUNKNOWN p2)
  125. {
  126. LPUNKNOWN punk1 = NULL;
  127. LPUNKNOWN punk2 = NULL;
  128. HRESULT hr = S_OK;
  129. if( NULL == p1 ||
  130. NULL == p2)
  131. {
  132. hr = E_POINTER;
  133. }
  134. if(SUCCEEDED(hr))
  135. {
  136. hr = COMSTL_ITF_CALL(p1)->QueryInterface(COMSTL_ITF_THIS(p1) COMSTL_IID_2_REF(IID_IUnknown), stlsoft_reinterpret_cast(void**, &punk1));
  137. }
  138. if(SUCCEEDED(hr))
  139. {
  140. hr = COMSTL_ITF_CALL(p2)->QueryInterface(COMSTL_ITF_THIS(p2) COMSTL_IID_2_REF(IID_IUnknown), stlsoft_reinterpret_cast(void**, &punk2));
  141. }
  142. if(SUCCEEDED(hr))
  143. {
  144. if( NULL == punk1 ||
  145. NULL == punk2 ||
  146. punk1 != punk2)
  147. {
  148. hr = S_FALSE;
  149. }
  150. }
  151. if(NULL != punk1)
  152. {
  153. COMSTL_ITF_CALL(punk1)->Release(COMSTL_ITF_THIS0(punk1));
  154. }
  155. if(NULL != punk2)
  156. {
  157. COMSTL_ITF_CALL(punk2)->Release(COMSTL_ITF_THIS0(punk2));
  158. }
  159. return hr;
  160. }
  161. /** \brief [C only] Determines whether an object implements a given interface without adding a reference count
  162. *
  163. * \return A status code indicating whether the given interface is implemented
  164. * \retval S_OK The interface is implemented and accessible
  165. * \retval E_NOINTERFACE The interface is not implemented
  166. * \retval any-other-code Indicates an error from the call to QueryInterface()
  167. *
  168. * \pre \c p is a non-NULL pointer to a COM interface.
  169. */
  170. STLSOFT_INLINE HRESULT comstl__is_interface_implemented(/* [in] */ LPUNKNOWN p, /* [in] */ REFIID riid)
  171. {
  172. LPUNKNOWN punk = NULL;
  173. HRESULT hr;
  174. COMSTL_MESSAGE_ASSERT("is_interface_implemented() does not accept a NULL pointer", NULL != p);
  175. hr = COMSTL_ITF_CALL(p)->QueryInterface(COMSTL_ITF_THIS(p) riid, stlsoft_reinterpret_cast(void**, &punk));
  176. if(NULL != punk)
  177. {
  178. COMSTL_ITF_CALL(punk)->Release(COMSTL_ITF_THIS0(punk));
  179. }
  180. return hr;
  181. }
  182. /* /////////////////////////////////////////////////////////////////////////
  183. * Namespace
  184. */
  185. #ifdef STLSOFT_DOCUMENTATION_SKIP_SECTION
  186. namespace comstl
  187. {
  188. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  189. /* /////////////////////////////////////////////////////////////////////////
  190. * C++ functions
  191. */
  192. #ifdef __cplusplus
  193. /** \brief Evaluates an object's identity
  194. *
  195. * \param p The pointer to the object whose identity will be evaluated
  196. * \param identity
  197. *
  198. * \return A status code indicating whether the identity was retrieved
  199. * \retval S_OK The pointers refer to the same object
  200. * \retval E_POINTER If p is null
  201. * \retval E_INVALIDARG If identity is null
  202. * \retval any-other-code Indicates an error from the call to QueryInterface()
  203. *
  204. * \pre \c p is a NULL pointer, or a valid pointer to a COM interface.
  205. *
  206. * \remarks If the function success, the caller must release the reference count
  207. * associated with the interface pointer in \c *identity.
  208. *
  209. * \note Implemented in terms of the C-compatible function comstl__get_object_identity().
  210. */
  211. inline HRESULT get_object_identity(/* [in] */ LPUNKNOWN p, /* [in] */ LPUNKNOWN *identity)
  212. {
  213. return comstl__get_object_identity(p, identity);
  214. }
  215. /** \brief Determines whether two interfaces refer to the same object
  216. *
  217. * \return A status code indicating whether the two pointers refer to the same
  218. * object
  219. * \retval S_OK The pointers refer to the same object
  220. * \retval S_FALSE The pointers refer to different objects
  221. * \retval E_POINTER One or both pointers are NULL.
  222. * \retval any-other-code Indicates an error from one of the calls to QueryInterface()
  223. *
  224. * \pre \c p1 and \c p2 are NULL pointers, or are valid pointers to COM interfaces.
  225. */
  226. inline HRESULT is_same_object(/* [in] */ LPUNKNOWN p1, /* [in] */ LPUNKNOWN p2)
  227. {
  228. return comstl__is_same_object(p1, p2);
  229. }
  230. /** \brief Determines whether an object implements a given interface without adding a reference count
  231. *
  232. * \return A status code indicating whether the given interface is implemented
  233. * \retval S_OK The interface is implemented and accessible
  234. * \retval E_NOINTERFACE The interface is not implemented
  235. * \retval any-other-code Indicates an error from the call to QueryInterface()
  236. *
  237. * \pre \c p is a non-NULL pointer to a COM interface.
  238. */
  239. inline HRESULT is_interface_implemented(/* [in] */ LPUNKNOWN p, /* [in] */ REFIID riid)
  240. {
  241. return comstl__is_interface_implemented(p, riid);
  242. }
  243. #endif /* __cplusplus */
  244. /* /////////////////////////////////////////////////////////////////////////
  245. * Unit-testing
  246. */
  247. #ifdef STLSOFT_UNITTEST
  248. # include "./unittest/object_functions_unittest_.h"
  249. #endif /* STLSOFT_UNITTEST */
  250. /* ////////////////////////////////////////////////////////////////////// */
  251. #ifndef _COMSTL_NO_NAMESPACE
  252. # if defined(_STLSOFT_NO_NAMESPACE) || \
  253. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  254. } /* namespace comstl */
  255. # else
  256. } /* namespace comstl_project */
  257. } /* namespace stlsoft */
  258. # endif /* _STLSOFT_NO_NAMESPACE */
  259. #endif /* !_COMSTL_NO_NAMESPACE */
  260. /* ////////////////////////////////////////////////////////////////////// */
  261. #endif /* !COMSTL_INCL_COMSTL_UTIL_H_OBJECT_FUNCTIONS */
  262. /* ///////////////////////////// end of file //////////////////////////// */