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.

133 lines
4.6 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: dotnetstl/conversion/check_cast.hpp
  3. *
  4. * Purpose: A cast operator function that performs runtime verification
  5. * on the cast instance in debug builds.
  6. *
  7. * Created: 9th August 2006
  8. * Updated: 10th August 2009
  9. *
  10. * Home: http://stlsoft.org/
  11. *
  12. * Copyright (c) 2006-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 dotnetstl/conversion/check_cast.hpp
  41. *
  42. * \brief [C++ only] Definition of the dotnetstl::check_cast cast function
  43. * (\ref group__library__conversion "Conversion" Library).
  44. */
  45. #ifndef DOTNETSTL_INCL_DOTNETSTL_CONVERSION_HPP_CHECK_CAST
  46. #define DOTNETSTL_INCL_DOTNETSTL_CONVERSION_HPP_CHECK_CAST
  47. #ifndef DOTNETSTL_DOCUMENTATION_SKIP_SECTION
  48. # define DOTNETSTL_VER_DOTNETSTL_CONVERSION_HPP_CHECK_CAST_MAJOR 1
  49. # define DOTNETSTL_VER_DOTNETSTL_CONVERSION_HPP_CHECK_CAST_MINOR 0
  50. # define DOTNETSTL_VER_DOTNETSTL_CONVERSION_HPP_CHECK_CAST_REVISION 1
  51. # define DOTNETSTL_VER_DOTNETSTL_CONVERSION_HPP_CHECK_CAST_EDIT 4
  52. #endif /* !DOTNETSTL_DOCUMENTATION_SKIP_SECTION */
  53. /* /////////////////////////////////////////////////////////////////////////
  54. * Includes
  55. */
  56. #ifndef DOTNETSTL_INCL_DOTNETSTL_HPP_DOTNETSTL
  57. # include <dotnetstl/dotnetstl.hpp>
  58. #endif /* !DOTNETSTL_INCL_DOTNETSTL_HPP_DOTNETSTL */
  59. #ifdef STLSOFT_UNITTEST
  60. # include <exception>
  61. #endif /* STLSOFT_UNITTEST */
  62. /* /////////////////////////////////////////////////////////////////////////
  63. * Namespace
  64. */
  65. #ifdef _STLSOFT_NO_NAMESPACE
  66. /* There is no stlsoft namespace, so must define ::dotnetstl */
  67. namespace dotnetstl
  68. {
  69. #else
  70. /* Define stlsoft::dotnet_project */
  71. namespace stlsoft
  72. {
  73. namespace dotnetstl_project
  74. {
  75. #endif /* _STLSOFT_NO_NAMESPACE */
  76. /* /////////////////////////////////////////////////////////////////////////
  77. * Functions
  78. */
  79. /** \brief A cast operator function that performs runtime verification
  80. * on the cast instance in debug builds.
  81. *
  82. * \ingroup group__library__conversion
  83. *
  84. * In debug builds, application of check_cast is equivalent to using
  85. * __try_cast. In release builds it is equivalent to using static_cast.
  86. */
  87. template< ss_typename_param_k TO
  88. , ss_typename_param_k FROM
  89. >
  90. inline TO check_cast(FROM from)
  91. {
  92. #ifdef _DEBUG
  93. return __try_cast<TO>(from);
  94. #else // ? _DEBUG
  95. return static_cast<TO>(from);
  96. #endif // _DEBUG
  97. }
  98. ////////////////////////////////////////////////////////////////////////////
  99. // Unit-testing
  100. #ifdef STLSOFT_UNITTEST
  101. # include "./unittest/check_cast_unittest_.h"
  102. #endif /* STLSOFT_UNITTEST */
  103. /* ////////////////////////////////////////////////////////////////////// */
  104. #ifdef _STLSOFT_NO_NAMESPACE
  105. } // namespace dotnetstl
  106. #else
  107. } // namespace dotnetstl_project
  108. } // namespace stlsoft
  109. #endif /* _STLSOFT_NO_NAMESPACE */
  110. /* ////////////////////////////////////////////////////////////////////// */
  111. #endif /* !STLSOFT_INCL_STLSOFT_CONVERSION_HPP_CHECK_CAST */
  112. /* ///////////////////////////// end of file //////////////////////////// */