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.

220 lines
6.2 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/conversion/any_cast.hpp
  3. *
  4. * Purpose: A dangerous, but sometimes necessary, tool for handling bad
  5. * libraries.
  6. *
  7. * Created: 12th May 2004
  8. * Updated: 10th August 2009
  9. *
  10. * Home: http://stlsoft.org/
  11. *
  12. * Copyright (c) 2004-2009, Matthew Wilson and Synesis Software
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright notice, this
  19. * list of conditions and the following disclaimer.
  20. * - Redistributions in binary form must reproduce the above copyright notice,
  21. * this list of conditions and the following disclaimer in the documentation
  22. * and/or other materials provided with the distribution.
  23. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  24. * any contributors may be used to endorse or promote products derived from
  25. * this software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  28. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  31. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  32. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  33. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  34. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  35. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  36. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37. * POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. * ////////////////////////////////////////////////////////////////////// */
  40. /** \file stlsoft/conversion/any_cast.hpp
  41. *
  42. * \brief [C++ only] Definition of the stlsoft::any_caster cast class
  43. * (\ref group__library__conversion "Conversion" Library).
  44. */
  45. #ifndef STLSOFT_INCL_STLSOFT_CONVERSION_HPP_ANY_CAST
  46. #define STLSOFT_INCL_STLSOFT_CONVERSION_HPP_ANY_CAST
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define STLSOFT_VER_STLSOFT_CONVERSION_HPP_ANY_CAST_MAJOR 4
  49. # define STLSOFT_VER_STLSOFT_CONVERSION_HPP_ANY_CAST_MINOR 0
  50. # define STLSOFT_VER_STLSOFT_CONVERSION_HPP_ANY_CAST_REVISION 1
  51. # define STLSOFT_VER_STLSOFT_CONVERSION_HPP_ANY_CAST_EDIT 33
  52. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  53. /* /////////////////////////////////////////////////////////////////////////
  54. * Includes
  55. */
  56. #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
  57. # include <stlsoft/stlsoft.h>
  58. #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
  59. /* /////////////////////////////////////////////////////////////////////////
  60. * Namespace
  61. */
  62. #ifndef _STLSOFT_NO_NAMESPACE
  63. namespace stlsoft
  64. {
  65. #endif /* _STLSOFT_NO_NAMESPACE */
  66. /* /////////////////////////////////////////////////////////////////////////
  67. * Classes
  68. */
  69. /** \brief Helper class used by any_caster, to define invalid conversions.
  70. *
  71. * \ingroup group__library__conversion
  72. *
  73. */
  74. template <ss_size_t N>
  75. struct any_caster_invalid_type
  76. {
  77. /// \name Member Types
  78. /// @{
  79. public:
  80. typedef any_caster_invalid_type<N> class_type;
  81. /// @}
  82. /// \name Not to be implemented
  83. /// @{
  84. #if defined(STLSOFT_CF_COMPILER_WARNS_NO_PUBLIC_DTOR)
  85. protected:
  86. #else /* ? compiler */
  87. private:
  88. #endif /* compiler */
  89. any_caster_invalid_type();
  90. any_caster_invalid_type(class_type const&);
  91. /// @}
  92. };
  93. /** \brief A dangerous, but sometimes necessary, tool for handling bad libraries.
  94. *
  95. * \ingroup group__library__conversion
  96. *
  97. * This class helps overcome problems when external libraries have errors in
  98. * their function parameter declarations, resulting in compilation-time type
  99. * errors with some versions of the libraries, and not with others.
  100. *
  101. * \note See inetstl::filesystem_traits<char>::find_first_file() for an
  102. * example of its use.
  103. */
  104. template< ss_typename_param_k T
  105. , ss_typename_param_k T1
  106. , ss_typename_param_k T2
  107. , ss_typename_param_k T3 = any_caster_invalid_type<3>*
  108. , ss_typename_param_k T4 = any_caster_invalid_type<4>*
  109. , ss_typename_param_k T5 = any_caster_invalid_type<5>*
  110. , ss_typename_param_k T6 = any_caster_invalid_type<6>*
  111. , ss_typename_param_k T7 = any_caster_invalid_type<7>*
  112. , ss_typename_param_k T8 = any_caster_invalid_type<8>*
  113. >
  114. union any_caster
  115. {
  116. /// \name Member Types
  117. /// @{
  118. public:
  119. typedef any_caster<T, T1, T2, T3, T4, T5, T6, T7, T8> class_type;
  120. /// @}
  121. /// \name Construction
  122. /// @{
  123. public:
  124. /// Constructs an instance from the source type
  125. ss_explicit_k any_caster(T t_)
  126. {
  127. this->t = t_;
  128. }
  129. /// @}
  130. /// \name Conversion
  131. /// @{
  132. public:
  133. operator T1 () const
  134. {
  135. return t1;
  136. }
  137. operator T2 () const
  138. {
  139. return t2;
  140. }
  141. operator T3 () const
  142. {
  143. return t3;
  144. }
  145. operator T4 () const
  146. {
  147. return t4;
  148. }
  149. operator T5 () const
  150. {
  151. return t5;
  152. }
  153. operator T6 () const
  154. {
  155. return t6;
  156. }
  157. operator T7 () const
  158. {
  159. return t7;
  160. }
  161. operator T8 () const
  162. {
  163. return t8;
  164. }
  165. /// @}
  166. /// \name Members
  167. /// @{
  168. private:
  169. T t;
  170. T1 t1;
  171. T2 t2;
  172. T3 t3;
  173. T4 t4;
  174. T5 t5;
  175. T6 t6;
  176. T7 t7;
  177. T8 t8;
  178. /// @}
  179. /// \name Not to be implemented
  180. /// @{
  181. private:
  182. any_caster(class_type const& rhs);
  183. class_type& operator =(class_type const& rhs);
  184. /// @}
  185. };
  186. ////////////////////////////////////////////////////////////////////////////
  187. // Unit-testing
  188. #ifdef STLSOFT_UNITTEST
  189. # include "./unittest/any_caster_unittest_.h"
  190. #endif /* STLSOFT_UNITTEST */
  191. /* ////////////////////////////////////////////////////////////////////// */
  192. #ifndef _STLSOFT_NO_NAMESPACE
  193. } // namespace stlsoft
  194. #endif /* _STLSOFT_NO_NAMESPACE */
  195. /* ////////////////////////////////////////////////////////////////////// */
  196. #endif /* !STLSOFT_INCL_STLSOFT_CONVERSION_HPP_ANY_CAST */
  197. /* ///////////////////////////// end of file //////////////////////////// */