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.

192 lines
7.6 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/conversion/sap_cast.hpp
  3. *
  4. * Purpose: A cast operator function that casts between non void pointers of
  5. * the same cv-qualification.
  6. *
  7. * Created: 25th February 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/sap_cast.hpp
  41. *
  42. * \brief [C++ only] Definition of the stlsoft::sap_cast cast function
  43. * (\ref group__library__conversion "Conversion" Library).
  44. */
  45. #ifndef STLSOFT_INCL_STLSOFT_CONVERSION_HPP_SAP_CAST
  46. #define STLSOFT_INCL_STLSOFT_CONVERSION_HPP_SAP_CAST
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define STLSOFT_VER_STLSOFT_CONVERSION_HPP_SAP_CAST_MAJOR 4
  49. # define STLSOFT_VER_STLSOFT_CONVERSION_HPP_SAP_CAST_MINOR 0
  50. # define STLSOFT_VER_STLSOFT_CONVERSION_HPP_SAP_CAST_REVISION 2
  51. # define STLSOFT_VER_STLSOFT_CONVERSION_HPP_SAP_CAST_EDIT 46
  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. #if ( defined(STLSOFT_COMPILER_IS_GCC) && \
  60. __GNUC__ < 3) || \
  61. ( defined(STLSOFT_COMPILER_IS_MSVC) && \
  62. _MSC_VER < 1200) || \
  63. defined(STLSOFT_COMPILER_IS_WATCOM)
  64. # define STLSOFT_NO_SAP_CAST
  65. #endif /* compiler */
  66. #if !defined(STLSOFT_NO_SAP_CAST) && \
  67. defined(STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT)
  68. # ifndef STLSOFT_INCL_STLSOFT_UTIL_HPP_CONSTRAINTS
  69. # include <stlsoft/util/constraints.hpp>
  70. # endif /* !STLSOFT_INCL_STLSOFT_UTIL_HPP_CONSTRAINTS */
  71. # ifndef STLSOFT_INCL_STLSOFT_META_HPP_BASE_TYPE_TRAITS
  72. # include <stlsoft/meta/base_type_traits.hpp>
  73. # endif /* !STLSOFT_INCL_STLSOFT_META_HPP_BASE_TYPE_TRAITS */
  74. # ifndef STLSOFT_INCL_STLSOFT_META_HPP_SELECT_FIRST_TYPE_IF
  75. # include <stlsoft/meta/select_first_type_if.hpp>
  76. # endif /* !STLSOFT_INCL_STLSOFT_META_HPP_SELECT_FIRST_TYPE_IF */
  77. #endif /* !STLSOFT_NO_SAP_CAST && STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT */
  78. /* /////////////////////////////////////////////////////////////////////////
  79. * Namespace
  80. */
  81. #ifndef _STLSOFT_NO_NAMESPACE
  82. namespace stlsoft
  83. {
  84. #endif /* _STLSOFT_NO_NAMESPACE */
  85. /* /////////////////////////////////////////////////////////////////////////
  86. * Functions
  87. */
  88. /** \brief A cast operator function that casts between non void pointers of
  89. * the same cv-qualification.
  90. *
  91. * \ingroup group__library__conversion
  92. *
  93. * The implementation of the operator uses compile-time enforcement of
  94. * various constraints to ensure that:
  95. *
  96. * - the FROM and TO types are both pointers
  97. * - no cv-qualifiers are stripped from the FROM type
  98. *
  99. * For example, this cast is allowed:
  100. \code
  101. int* pi = . . .;
  102. short const* ps = stlsoft::sap_cast<short const*>(pi);
  103. \endcode
  104. *
  105. * but this cast is not:
  106. \code
  107. int const* pi = . . .;
  108. short* ps = stlsoft::sap_cast<short*>(pi);
  109. \endcode
  110. *
  111. * \param from The pointer to cast from.
  112. *
  113. * \remarks The cast operator was inspired by an item in the
  114. * Sutter/Alexandrescu coding standards book, and its names stands for
  115. * Sutter-Alexandrescu-Pointer cast. The acronym is overloaded, since one
  116. * might also be said to be a sap if one made injudicious use of the cast,
  117. * due to its inherent dangers.
  118. */
  119. #if defined(STLSOFT_NO_SAP_CAST)
  120. # define sap_cast reinterpret_cast
  121. #else /* ? STLSOFT_NO_SAP_CAST */
  122. template< ss_typename_param_k TO
  123. , ss_typename_param_k FROM
  124. >
  125. inline TO sap_cast(FROM from)
  126. {
  127. # if defined(STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT) && \
  128. ( !defined(STLSOFT_COMPILER_IS_BORLAND))
  129. // Both types must be pointer types
  130. STLSOFT_STATIC_ASSERT(0 != base_type_traits<FROM>::is_pointer);
  131. STLSOFT_STATIC_ASSERT(0 != base_type_traits<TO>::is_pointer);
  132. typedef ss_typename_type_k base_type_traits<FROM>::base_type from_base_type;
  133. typedef ss_typename_type_k base_type_traits<TO>::base_type to_base_type;
  134. // The intermediate type might be void *, void const*, void volatile * or
  135. // void const volatile *
  136. typedef ss_typename_type_k select_first_type_if<void const*
  137. , void*
  138. , base_type_traits<FROM>::is_const
  139. >::type non_volatile_type;
  140. typedef ss_typename_type_k select_first_type_if<void const volatile*
  141. , void volatile*
  142. , base_type_traits<FROM>::is_const
  143. >::type volatile_type;
  144. typedef ss_typename_type_k select_first_type_if<volatile_type
  145. , non_volatile_type
  146. , base_type_traits<FROM>::is_volatile
  147. >::type pointer_type;
  148. // "static_cast" to void (const) (volatile) *
  149. pointer_type pv = from;
  150. # else /* STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT */
  151. void const volatile *p1 = from;
  152. void *pv = const_cast<void*>(p1);
  153. # endif /* STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT */
  154. // static_cast to destination type
  155. return static_cast<TO>(pv);
  156. }
  157. #endif /* STLSOFT_NO_SAP_CAST */
  158. ////////////////////////////////////////////////////////////////////////////
  159. // Unit-testing
  160. #ifdef STLSOFT_UNITTEST
  161. # include "./unittest/sap_cast_unittest_.h"
  162. #endif /* STLSOFT_UNITTEST */
  163. /* ////////////////////////////////////////////////////////////////////// */
  164. #ifndef _STLSOFT_NO_NAMESPACE
  165. } // namespace stlsoft
  166. #endif /* _STLSOFT_NO_NAMESPACE */
  167. /* ////////////////////////////////////////////////////////////////////// */
  168. #endif /* !STLSOFT_INCL_STLSOFT_CONVERSION_HPP_SAP_CAST */
  169. /* ///////////////////////////// end of file //////////////////////////// */