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.

221 lines
7.8 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: comstl/conversion/method_cast.hpp
  3. *
  4. * Purpose: COM memory functions.
  5. *
  6. * Created: 20th December 2003
  7. * Updated: 26th May 2010
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2003-2010, 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/conversion/method_cast.hpp
  40. *
  41. * \brief [C++ only] Definition of the comstl::method_cast suite of
  42. * cast functions
  43. * (\ref group__library__conversion "Conversion" Library).
  44. */
  45. #ifndef COMSTL_INCL_COMSTL_CONVERSION_HPP_METHOD_CAST
  46. #define COMSTL_INCL_COMSTL_CONVERSION_HPP_METHOD_CAST
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define COMSTL_VER_COMSTL_CONVERSION_HPP_METHOD_CAST_MAJOR 2
  49. # define COMSTL_VER_COMSTL_CONVERSION_HPP_METHOD_CAST_MINOR 2
  50. # define COMSTL_VER_COMSTL_CONVERSION_HPP_METHOD_CAST_REVISION 1
  51. # define COMSTL_VER_COMSTL_CONVERSION_HPP_METHOD_CAST_EDIT 33
  52. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  53. /* /////////////////////////////////////////////////////////////////////////
  54. * Auto-generation and compatibility
  55. */
  56. /*
  57. [Incompatibilies-start]
  58. STLSOFT_COMPILER_IS_GCC: __GNUC__ < 3 || (__GNUC__ == 3 && __GNUC_MINOR__ < 4)
  59. [Incompatibilies-end]
  60. */
  61. /* /////////////////////////////////////////////////////////////////////////
  62. * Includes
  63. */
  64. #ifndef COMSTL_INCL_COMSTL_H_COMSTL
  65. # include <comstl/comstl.h>
  66. #endif /* !COMSTL_INCL_COMSTL_H_COMSTL */
  67. /* /////////////////////////////////////////////////////////////////////////
  68. * Namespace
  69. */
  70. #ifndef _COMSTL_NO_NAMESPACE
  71. # if defined(_STLSOFT_NO_NAMESPACE) || \
  72. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  73. /* There is no stlsoft namespace, so must define ::comstl */
  74. namespace comstl
  75. {
  76. # else
  77. /* Define stlsoft::comstl_project */
  78. namespace stlsoft
  79. {
  80. namespace comstl_project
  81. {
  82. # endif /* _STLSOFT_NO_NAMESPACE */
  83. #endif /* !_COMSTL_NO_NAMESPACE */
  84. /* /////////////////////////////////////////////////////////////////////////
  85. * Functions
  86. */
  87. #if 0
  88. template< ss_typename_param_k R
  89. , ss_typename_param_k V
  90. >
  91. inline R& transfer_resource(R& r, V v)
  92. {
  93. // COMSTL_MESSAGE_ASSERT("Resource destination not empty!", 0 == r);
  94. r = v;
  95. return r;
  96. }
  97. #endif /* 0 */
  98. inline ss_sint8_t& transfer_resource(ss_sint8_t& r, ss_sint8_t v) { return ((r = v), r); }
  99. inline ss_uint8_t& transfer_resource(ss_uint8_t& r, ss_uint8_t v) { return ((r = v), r); }
  100. inline ss_sint16_t& transfer_resource(ss_sint16_t& r, ss_sint16_t v) { return ((r = v), r); }
  101. inline ss_uint16_t& transfer_resource(ss_uint16_t& r, ss_uint16_t v) { return ((r = v), r); }
  102. inline ss_sint32_t& transfer_resource(ss_sint32_t& r, ss_sint32_t v) { return ((r = v), r); }
  103. inline ss_uint32_t& transfer_resource(ss_uint32_t& r, ss_uint32_t v) { return ((r = v), r); }
  104. #ifdef STLSOFT_CF_64BIT_INT_SUPPORT
  105. inline ss_sint64_t& transfer_resource(ss_sint64_t& r, ss_sint64_t v) { return ((r = v), r); }
  106. inline ss_uint64_t& transfer_resource(ss_uint64_t& r, ss_uint64_t v) { return ((r = v), r); }
  107. #endif /* !STLSOFT_CF_64BIT_INT_SUPPORT */
  108. #ifdef STLSOFT_CF_CHAR_DISTINCT_INT_TYPE
  109. inline signed char& transfer_resource(signed char& r, signed char v) { return ((r = v), r); }
  110. inline unsigned char& transfer_resource(unsigned char& r, unsigned char v) { return ((r = v), r); }
  111. #endif /* STLSOFT_CF_CHAR_DISTINCT_INT_TYPE */
  112. #ifdef STLSOFT_CF_SHORT_DISTINCT_INT_TYPE
  113. inline signed short& transfer_resource(signed short& r, signed short v) { return ((r = v), r); }
  114. inline unsigned short& transfer_resource(unsigned short& r, unsigned short v) { return ((r = v), r); }
  115. #endif /* STLSOFT_CF_SHORT_DISTINCT_INT_TYPE */
  116. #ifdef STLSOFT_CF_INT_DISTINCT_INT_TYPE
  117. inline signed int& transfer_resource(signed int& r, signed int v) { return ((r = v), r); }
  118. inline unsigned int& transfer_resource(unsigned int& r, unsigned int v) { return ((r = v), r); }
  119. #endif /* STLSOFT_CF_INT_DISTINCT_INT_TYPE */
  120. #ifdef STLSOFT_CF_LONG_DISTINCT_INT_TYPE
  121. inline signed long& transfer_resource(signed long& r, signed long v) { return ((r = v), r); }
  122. inline unsigned long& transfer_resource(unsigned long& r, unsigned long v) { return ((r = v), r); }
  123. #endif /* STLSOFT_CF_LONG_DISTINCT_INT_TYPE */
  124. inline float& transfer_resource(float& r, float v) { return ((r = v), r); }
  125. inline double& transfer_resource(double& r, double v) { return ((r = v), r); }
  126. inline long double& transfer_resource(long double& r, long double v) { return ((r = v), r); }
  127. inline bool& transfer_resource(bool& r, VARIANT_BOOL v) { return ((r = VARIANT_FALSE != v), r); }
  128. template< ss_typename_param_k R
  129. , ss_typename_param_k V
  130. >
  131. inline R*& transfer_resource(R*& r, V* v)
  132. {
  133. COMSTL_MESSAGE_ASSERT("Resource destination not empty!", 0 == r);
  134. return (r = v);
  135. }
  136. /** \brief
  137. *
  138. * \ingroup group__library__conversion
  139. */
  140. template< ss_typename_param_k R
  141. , ss_typename_param_k C
  142. , ss_typename_param_k V
  143. >
  144. inline R method_cast(C& c, HRESULT (C::*pfn)(V*))
  145. {
  146. R r = R();
  147. V v;
  148. HRESULT hr = (c.*pfn)(&v);
  149. if(SUCCEEDED(hr))
  150. {
  151. return transfer_resource(r, v);
  152. }
  153. return r;
  154. }
  155. /** \brief
  156. *
  157. * \ingroup group__library__conversion
  158. */
  159. template< ss_typename_param_k R
  160. , ss_typename_param_k C
  161. , ss_typename_param_k V
  162. >
  163. inline R method_cast(C *c, HRESULT (STDAPICALLTYPE C::*pfn)(V*))
  164. {
  165. R r = R();
  166. V v;
  167. HRESULT hr = (c->*pfn)(&v);
  168. if(SUCCEEDED(hr))
  169. {
  170. return transfer_resource(r, v);
  171. }
  172. return r;
  173. }
  174. /* ////////////////////////////////////////////////////////////////////// */
  175. #ifndef _COMSTL_NO_NAMESPACE
  176. # if defined(_STLSOFT_NO_NAMESPACE) || \
  177. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  178. } // namespace comstl
  179. # else
  180. } // namespace stlsoft::comstl_project
  181. } // namespace stlsoft
  182. # endif /* _STLSOFT_NO_NAMESPACE */
  183. #endif /* !_COMSTL_NO_NAMESPACE */
  184. /* ////////////////////////////////////////////////////////////////////// */
  185. #endif /* !COMSTL_INCL_COMSTL_CONVERSION_HPP_METHOD_CAST */
  186. /* ///////////////////////////// end of file //////////////////////////// */