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
7.8 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/conversion/literal_cast.hpp
  3. *
  4. * Purpose: Compile-time checking for literal conversions.
  5. *
  6. * Created: 5th September 2003
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2003-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 stlsoft/conversion/literal_cast.hpp
  40. *
  41. * \brief [C++ only] Definition of the stlsoft::literal_cast cast class
  42. * (\ref group__library__conversion "Conversion" Library).
  43. */
  44. #ifndef STLSOFT_INCL_STLSOFT_CONVERSION_HPP_LITERAL_CAST
  45. #define STLSOFT_INCL_STLSOFT_CONVERSION_HPP_LITERAL_CAST
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define STLSOFT_VER_STLSOFT_CONVERSION_HPP_LITERAL_CAST_MAJOR 4
  48. # define STLSOFT_VER_STLSOFT_CONVERSION_HPP_LITERAL_CAST_MINOR 0
  49. # define STLSOFT_VER_STLSOFT_CONVERSION_HPP_LITERAL_CAST_REVISION 4
  50. # define STLSOFT_VER_STLSOFT_CONVERSION_HPP_LITERAL_CAST_EDIT 44
  51. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  52. /* /////////////////////////////////////////////////////////////////////////
  53. * Auto-generation and compatibility
  54. */
  55. /*
  56. [<[STLSOFT-AUTO:NO-UNITTEST]>]
  57. */
  58. /* /////////////////////////////////////////////////////////////////////////
  59. * Includes
  60. */
  61. #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
  62. # include <stlsoft/stlsoft.h>
  63. #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
  64. #ifndef STLSOFT_INCL_STLSOFT_UTIL_H_LIMIT_TRAITS
  65. # include <stlsoft/util/limit_traits.h>
  66. #endif /* !STLSOFT_INCL_STLSOFT_UTIL_H_LIMIT_TRAITS */
  67. /* /////////////////////////////////////////////////////////////////////////
  68. * Namespace
  69. */
  70. #ifndef _STLSOFT_NO_NAMESPACE
  71. namespace stlsoft
  72. {
  73. #endif /* _STLSOFT_NO_NAMESPACE */
  74. /* /////////////////////////////////////////////////////////////////////////
  75. * Functions
  76. */
  77. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  78. # ifdef STLSOFT_CF_64BIT_INT_SUPPORT
  79. typedef ss_sint64_t literal_cast_int_t;
  80. typedef ss_uint64_t invalid_int_t;
  81. # else /* ? STLSOFT_CF_64BIT_INT_SUPPORT */
  82. typedef ss_sint32_t literal_cast_int_t;
  83. typedef ss_uint32_t invalid_int_t;
  84. # endif /* STLSOFT_CF_64BIT_INT_SUPPORT */
  85. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  86. #if 0
  87. template< ss_typename_param_k T
  88. , literal_cast_int_t V
  89. >
  90. inline T literal_cast()
  91. {
  92. #if defined(STLSOFT_CF_MEMBER_CONSTANT_SUPPORT)
  93. /* If member constants are supported, we can do compile-time assertions */
  94. const int __literal_cast_value_too_large__ = V <= limit_traits<T>::maximum_value;
  95. const int __literal_cast_value_too_small__ = V >= limit_traits<T>::minimum_value;
  96. STLSOFT_STATIC_ASSERT(__literal_cast_value_too_large__);
  97. STLSOFT_STATIC_ASSERT(__literal_cast_value_too_small__);
  98. #else /* ? STLSOFT_CF_MEMBER_CONSTANT_SUPPORT */
  99. /* If not, then we are restricted to runtime assertions */
  100. const int __literal_cast_value_too_large__ = V <= limit_traits<T>::maximum();
  101. const int __literal_cast_value_too_small__ = V >= limit_traits<T>::minimum();
  102. STLSOFT_MESSAGE_ASSERT("Literal cast value is too large", __literal_cast_value_too_large__);
  103. STLSOFT_MESSAGE_ASSERT("Literal cast value is too small", __literal_cast_value_too_small__);
  104. #endif /* STLSOFT_CF_MEMBER_CONSTANT_SUPPORT */
  105. return T(V);
  106. }
  107. #else /* ? 0 */
  108. /** \brief Provides compile-time validation for casts of literal values.
  109. *
  110. * \ingroup group__library__conversion
  111. *
  112. * \param T The type to cast to
  113. * \param V The literal value to be cast
  114. *
  115. \code
  116. short s1 = stlsoft::literal_cast<short, 10>(); // Ok: 10 fits into short
  117. short s2 = stlsoft::literal_cast<short, 1000000000>(); // Compile error: value too large for short
  118. \endcode
  119. *
  120. * \note Thanks to Kevlin Henney (http://www.curbralan.com/) for insights
  121. * into the solution.
  122. */
  123. template< ss_typename_param_k T
  124. , literal_cast_int_t V
  125. >
  126. class literal_cast
  127. {
  128. public:
  129. operator T () const
  130. {
  131. #if defined(STLSOFT_CF_MEMBER_CONSTANT_SUPPORT)
  132. /* If member constants are supported, we can do compile-time assertions */
  133. const int __literal_cast_value_too_large__ = V <= limit_traits<T>::maximum_value;
  134. const int __literal_cast_value_too_small__ = V >= limit_traits<T>::minimum_value;
  135. STLSOFT_STATIC_ASSERT(__literal_cast_value_too_large__);
  136. STLSOFT_STATIC_ASSERT(__literal_cast_value_too_small__);
  137. #else /* ? STLSOFT_CF_MEMBER_CONSTANT_SUPPORT */
  138. /* If not, then we are restricted to runtime assertions */
  139. const int __literal_cast_value_too_large__ = V <= limit_traits<T>::maximum();
  140. const int __literal_cast_value_too_small__ = V >= limit_traits<T>::minimum();
  141. STLSOFT_MESSAGE_ASSERT("Literal cast value is too large", __literal_cast_value_too_large__);
  142. STLSOFT_MESSAGE_ASSERT("Literal cast value is too small", __literal_cast_value_too_small__);
  143. #endif /* STLSOFT_CF_MEMBER_CONSTANT_SUPPORT */
  144. return T(V);
  145. }
  146. };
  147. #ifdef STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT
  148. /** Partial specialisation to
  149. */
  150. template<literal_cast_int_t V>
  151. class literal_cast<invalid_int_t, V>
  152. {
  153. public:
  154. # if 0
  155. literal_cast()
  156. {
  157. const int STLSOFT_LITERAL_CAST_INVALID_TYPE_WARNING = 0;
  158. STLSOFT_STATIC_ASSERT(STLSOFT_LITERAL_CAST_INVALID_TYPE_WARNING);
  159. }
  160. # endif /* 0 */
  161. private:
  162. operator invalid_int_t () const
  163. {
  164. const int __literal_cast_cannot_cast_to_largest_unsigned_integer_type__ = 0;
  165. STLSOFT_STATIC_ASSERT(__literal_cast_cannot_cast_to_largest_unsigned_integer_type__);
  166. return 0;
  167. }
  168. # if defined(STLSOFT_COMPILER_IS_GCC)
  169. public: static void f() {}
  170. # endif /* compiler */
  171. };
  172. #endif /* STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT */
  173. #endif /* 0 */
  174. ////////////////////////////////////////////////////////////////////////////
  175. // Unit-testing
  176. #ifdef STLSOFT_UNITTEST
  177. # include "./unittest/literal_cast_unittest_.h"
  178. #endif /* STLSOFT_UNITTEST */
  179. /* ////////////////////////////////////////////////////////////////////// */
  180. #ifndef _STLSOFT_NO_NAMESPACE
  181. } // namespace stlsoft
  182. #endif /* _STLSOFT_NO_NAMESPACE */
  183. /* ////////////////////////////////////////////////////////////////////// */
  184. #endif /* !STLSOFT_INCL_STLSOFT_CONVERSION_HPP_LITERAL_CAST */
  185. /* ///////////////////////////// end of file //////////////////////////// */