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.

248 lines
7.2 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: comstl/util/refcount_functions.h
  3. *
  4. * Purpose: Reference-counting helper functions.
  5. *
  6. * Created: 25th June 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/refcount_functions.h
  40. *
  41. * \brief [C++ only; requires COM] Reference-counting helper functions
  42. * (\ref group__library__utility__com "COM Utility" Library).
  43. */
  44. #ifndef COMSTL_INCL_COMSTL_UTIL_H_REFCOUNT_FUNCTIONS
  45. #define COMSTL_INCL_COMSTL_UTIL_H_REFCOUNT_FUNCTIONS
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define COMSTL_VER_COMSTL_UTIL_H_REFCOUNT_FUNCTIONS_MAJOR 4
  48. # define COMSTL_VER_COMSTL_UTIL_H_REFCOUNT_FUNCTIONS_MINOR 1
  49. # define COMSTL_VER_COMSTL_UTIL_H_REFCOUNT_FUNCTIONS_REVISION 2
  50. # define COMSTL_VER_COMSTL_UTIL_H_REFCOUNT_FUNCTIONS_EDIT 59
  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] Adds a reference on the interface pointer
  79. *
  80. * \ingroup group__library__utility__com
  81. *
  82. * \param punk The interface pointer on which to add the reference. Cannot be null
  83. *
  84. * \pre \c punk must not be NULL.
  85. */
  86. STLSOFT_INLINE void comstl__addref(LPUNKNOWN punk)
  87. {
  88. COMSTL_MESSAGE_ASSERT("Cannot call AddRef() on NULL interface pointer", NULL != punk);
  89. COMSTL_ITF_CALL(punk)->AddRef(COMSTL_ITF_THIS0(punk));
  90. }
  91. /** \brief [C only] Releases a reference on the interface pointer
  92. *
  93. * \ingroup group__library__utility__com
  94. *
  95. * \param punk The interface pointer on which to release the reference. Cannot be null
  96. */
  97. STLSOFT_INLINE void comstl__release(LPUNKNOWN punk)
  98. {
  99. COMSTL_ITF_CALL(punk)->Release(COMSTL_ITF_THIS0(punk));
  100. }
  101. /** \brief [C only] Adds a reference on the interface pointer
  102. *
  103. * \ingroup group__library__utility__com
  104. *
  105. * \param punk The interface pointer on which to add the reference. Can be null
  106. */
  107. STLSOFT_INLINE void comstl__safe_addref(LPUNKNOWN punk)
  108. {
  109. if(NULL != punk)
  110. {
  111. comstl__addref(punk);
  112. }
  113. }
  114. /** \brief [C only] Releases a reference on the interface pointer
  115. *
  116. * \ingroup group__library__utility__com
  117. *
  118. * \param punk The interface pointer on which to release the reference. Can be null
  119. */
  120. STLSOFT_INLINE void comstl__safe_release(LPUNKNOWN punk)
  121. {
  122. if(NULL != punk)
  123. {
  124. comstl__release(punk);
  125. }
  126. }
  127. /* /////////////////////////////////////////////////////////////////////////
  128. * Namespace
  129. */
  130. #ifdef STLSOFT_DOCUMENTATION_SKIP_SECTION
  131. namespace comstl
  132. {
  133. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  134. /* /////////////////////////////////////////////////////////////////////////
  135. * C++ functions
  136. */
  137. #ifdef __cplusplus
  138. /** \brief Adds a reference on the interface pointer
  139. *
  140. * \ingroup group__library__utility__com
  141. *
  142. * \param punk The interface pointer on which to add the reference. Cannot be null
  143. *
  144. * \pre \c punk must not be NULL.
  145. */
  146. inline void addref(LPUNKNOWN punk)
  147. {
  148. comstl__addref(punk);
  149. }
  150. /** \brief Releases a reference on the interface pointer
  151. *
  152. * \ingroup group__library__utility__com
  153. *
  154. * \param punk The interface pointer on which to release the reference. Cannot be null
  155. */
  156. inline void release(LPUNKNOWN punk)
  157. {
  158. comstl__release(punk);
  159. }
  160. /** \brief Adds a reference on the interface pointer
  161. *
  162. * \ingroup group__library__utility__com
  163. *
  164. * \param punk The interface pointer on which to add the reference. Can be null
  165. */
  166. inline void safe_addref(LPUNKNOWN punk)
  167. {
  168. comstl__safe_addref(punk);
  169. }
  170. /** \brief Releases a reference on the interface pointer
  171. *
  172. * \ingroup group__library__utility__com
  173. *
  174. * \param punk The interface pointer on which to release the reference. Can be null
  175. */
  176. inline void safe_release(LPUNKNOWN punk)
  177. {
  178. comstl__safe_release(punk);
  179. }
  180. /** \brief Releases a reference on the interface pointer, and resets the pointer
  181. *
  182. * \ingroup group__library__utility__com
  183. *
  184. * \param pt The interface pointer on which to release the reference. Can be null
  185. */
  186. template <ss_typename_param_k T>
  187. inline void release_set_null(T *&pt)
  188. {
  189. if(NULL != pt)
  190. {
  191. release(pt);
  192. pt = NULL;
  193. }
  194. }
  195. #endif /* __cplusplus */
  196. /* /////////////////////////////////////////////////////////////////////////
  197. * Unit-testing
  198. */
  199. #ifdef STLSOFT_UNITTEST
  200. # include "./unittest/refcount_functions_unittest_.h"
  201. #endif /* STLSOFT_UNITTEST */
  202. /* ////////////////////////////////////////////////////////////////////// */
  203. #ifndef _COMSTL_NO_NAMESPACE
  204. # if defined(_STLSOFT_NO_NAMESPACE) || \
  205. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  206. } /* namespace comstl */
  207. # else
  208. } /* namespace comstl_project */
  209. } /* namespace stlsoft */
  210. # endif /* _STLSOFT_NO_NAMESPACE */
  211. #endif /* !_COMSTL_NO_NAMESPACE */
  212. /* ////////////////////////////////////////////////////////////////////// */
  213. #endif /* !COMSTL_INCL_COMSTL_UTIL_H_REFCOUNT_FUNCTIONS */
  214. /* ///////////////////////////// end of file //////////////////////////// */