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.

284 lines
10 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/util/printf_traits.hpp
  3. *
  4. * Purpose: printf_traits classes.
  5. *
  6. * Created: 16th January 2002
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2002-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/util/printf_traits.hpp
  40. *
  41. * \brief [C++ only] Definition of the stlsoft::printf_traits class
  42. * template
  43. * (\ref group__library__utility "Utility" Library).
  44. */
  45. #ifndef STLSOFT_INCL_STLSOFT_UTIL_HPP_PRINTF_TRAITS
  46. #define STLSOFT_INCL_STLSOFT_UTIL_HPP_PRINTF_TRAITS
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define STLSOFT_VER_STLSOFT_UTIL_HPP_PRINTF_TRAITS_MAJOR 5
  49. # define STLSOFT_VER_STLSOFT_UTIL_HPP_PRINTF_TRAITS_MINOR 0
  50. # define STLSOFT_VER_STLSOFT_UTIL_HPP_PRINTF_TRAITS_REVISION 1
  51. # define STLSOFT_VER_STLSOFT_UTIL_HPP_PRINTF_TRAITS_EDIT 60
  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. #ifndef STLSOFT_INCL_STLSOFT_UTIL_HPP_INTEGRAL_PRINTF_TRAITS
  60. # include <stlsoft/util/integral_printf_traits.hpp>
  61. #endif /* !STLSOFT_INCL_STLSOFT_UTIL_HPP_INTEGRAL_PRINTF_TRAITS */
  62. /* /////////////////////////////////////////////////////////////////////////
  63. * Namespace
  64. */
  65. #ifndef _STLSOFT_NO_NAMESPACE
  66. namespace stlsoft
  67. {
  68. #endif /* _STLSOFT_NO_NAMESPACE */
  69. /* /////////////////////////////////////////////////////////////////////////
  70. * Constants & definitions
  71. */
  72. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  73. #define STLSOFT_PRINTF_TRAITS_SINT8_MIN STLSOFT_INTEGRAL_PRINTF_TRAITS_SINT8_MIN
  74. #define STLSOFT_PRINTF_TRAITS_SINT8_MAX STLSOFT_INTEGRAL_PRINTF_TRAITS_SINT8_MAX
  75. #define STLSOFT_PRINTF_TRAITS_UINT8_MIN STLSOFT_INTEGRAL_PRINTF_TRAITS_UINT8_MIN
  76. #define STLSOFT_PRINTF_TRAITS_UINT8_MAX STLSOFT_INTEGRAL_PRINTF_TRAITS_UINT8_MAX
  77. #define STLSOFT_PRINTF_TRAITS_SINT16_MIN STLSOFT_INTEGRAL_PRINTF_TRAITS_SINT16_MIN
  78. #define STLSOFT_PRINTF_TRAITS_SINT16_MAX STLSOFT_INTEGRAL_PRINTF_TRAITS_SINT16_MAX
  79. #define STLSOFT_PRINTF_TRAITS_UINT16_MIN STLSOFT_INTEGRAL_PRINTF_TRAITS_UINT16_MIN
  80. #define STLSOFT_PRINTF_TRAITS_UINT16_MAX STLSOFT_INTEGRAL_PRINTF_TRAITS_UINT16_MAX
  81. #define STLSOFT_PRINTF_TRAITS_SINT32_MIN STLSOFT_INTEGRAL_PRINTF_TRAITS_SINT32_MIN
  82. #define STLSOFT_PRINTF_TRAITS_SINT32_MAX STLSOFT_INTEGRAL_PRINTF_TRAITS_SINT32_MAX
  83. #define STLSOFT_PRINTF_TRAITS_UINT32_MIN STLSOFT_INTEGRAL_PRINTF_TRAITS_UINT32_MIN
  84. #define STLSOFT_PRINTF_TRAITS_UINT32_MAX STLSOFT_INTEGRAL_PRINTF_TRAITS_UINT32_MAX
  85. #define STLSOFT_PRINTF_TRAITS_SINT64_MIN STLSOFT_INTEGRAL_PRINTF_TRAITS_SINT64_MIN
  86. #define STLSOFT_PRINTF_TRAITS_SINT64_MAX STLSOFT_INTEGRAL_PRINTF_TRAITS_SINT64_MAX
  87. #define STLSOFT_PRINTF_TRAITS_UINT64_MIN STLSOFT_INTEGRAL_PRINTF_TRAITS_UINT64_MIN
  88. #define STLSOFT_PRINTF_TRAITS_UINT64_MAX STLSOFT_INTEGRAL_PRINTF_TRAITS_UINT64_MAX
  89. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  90. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  91. # define __STLSOFT_PRINTF_TRAITS__SINT8_MIN STLSOFT_INTEGRAL_PRINTF_TRAITS_SINT8_MIN
  92. # define __STLSOFT_PRINTF_TRAITS__SINT8_MAX STLSOFT_INTEGRAL_PRINTF_TRAITS_SINT8_MAX
  93. # define __STLSOFT_PRINTF_TRAITS__UINT8_MIN STLSOFT_INTEGRAL_PRINTF_TRAITS_UINT8_MIN
  94. # define __STLSOFT_PRINTF_TRAITS__UINT8_MAX STLSOFT_INTEGRAL_PRINTF_TRAITS_UINT8_MAX
  95. # define __STLSOFT_PRINTF_TRAITS__SINT16_MIN STLSOFT_INTEGRAL_PRINTF_TRAITS_SINT16_MIN
  96. # define __STLSOFT_PRINTF_TRAITS__SINT16_MAX STLSOFT_INTEGRAL_PRINTF_TRAITS_SINT16_MAX
  97. # define __STLSOFT_PRINTF_TRAITS__UINT16_MIN STLSOFT_INTEGRAL_PRINTF_TRAITS_UINT16_MIN
  98. # define __STLSOFT_PRINTF_TRAITS__UINT16_MAX STLSOFT_INTEGRAL_PRINTF_TRAITS_UINT16_MAX
  99. # define __STLSOFT_PRINTF_TRAITS__SINT32_MIN STLSOFT_INTEGRAL_PRINTF_TRAITS_SINT32_MIN
  100. # define __STLSOFT_PRINTF_TRAITS__SINT32_MAX STLSOFT_INTEGRAL_PRINTF_TRAITS_SINT32_MAX
  101. # define __STLSOFT_PRINTF_TRAITS__UINT32_MIN STLSOFT_INTEGRAL_PRINTF_TRAITS_UINT32_MIN
  102. # define __STLSOFT_PRINTF_TRAITS__UINT32_MAX STLSOFT_INTEGRAL_PRINTF_TRAITS_UINT32_MAX
  103. # define __STLSOFT_PRINTF_TRAITS__SINT64_MIN STLSOFT_INTEGRAL_PRINTF_TRAITS_SINT64_MIN
  104. # define __STLSOFT_PRINTF_TRAITS__SINT64_MAX STLSOFT_INTEGRAL_PRINTF_TRAITS_SINT64_MAX
  105. # define __STLSOFT_PRINTF_TRAITS__UINT64_MIN STLSOFT_INTEGRAL_PRINTF_TRAITS_UINT64_MIN
  106. # define __STLSOFT_PRINTF_TRAITS__UINT64_MAX STLSOFT_INTEGRAL_PRINTF_TRAITS_UINT64_MAX
  107. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  108. /* /////////////////////////////////////////////////////////////////////////
  109. * Classes
  110. */
  111. // struct printf_traits
  112. #ifdef STLSOFT_DOCUMENTATION_SKIP_SECTION
  113. /** \brief Traits for determining the size, in printf-ed form, of the minimum and
  114. * maximum values of types
  115. *
  116. * \ingroup group__library__utility
  117. *
  118. * printf_traits is a traits class for acquiring enum values representing the
  119. * lengths, when expressed in string form, of the minimum and maximum values of
  120. * the type, and the maximum of the two. The lengths are inclusive of the
  121. * null terminator.
  122. *
  123. \code
  124. assert(stlsoft::printf_traits<ss_sint16_t>::size_min == 6);
  125. assert(stlsoft::printf_traits<ss_sint16_t>::size_max == 7);
  126. assert(stlsoft::printf_traits<ss_sint16_t>::size == 7);
  127. \endcode
  128. *
  129. * \param T The type
  130. *
  131. */
  132. template <ss_typename_param_k T>
  133. struct printf_traits
  134. {
  135. enum
  136. {
  137. size_min //!< The number of characters (& null) in the minimum value
  138. , size_max //!< The number of characters (& null) in the maximum value
  139. , size //!< The maximum of \c size_min and \c size_max
  140. };
  141. /// Returns the appropriate printf format for the type
  142. static ss_char_a_t const* format_a();
  143. /// Returns the appropriate wprintf format for the type
  144. static ss_char_w_t const* format_w();
  145. };
  146. #else /* ? STLSOFT_DOCUMENTATION_SKIP_SECTION */
  147. template <ss_typename_param_k T>
  148. struct printf_traits;
  149. STLSOFT_TEMPLATE_SPECIALISATION
  150. struct printf_traits<ss_sint8_t>
  151. : integral_printf_traits<ss_sint8_t>
  152. {};
  153. STLSOFT_TEMPLATE_SPECIALISATION
  154. struct printf_traits<ss_uint8_t>
  155. : integral_printf_traits<ss_uint8_t>
  156. {};
  157. STLSOFT_TEMPLATE_SPECIALISATION
  158. struct printf_traits<ss_sint16_t>
  159. : integral_printf_traits<ss_sint16_t>
  160. {};
  161. STLSOFT_TEMPLATE_SPECIALISATION
  162. struct printf_traits<ss_uint16_t>
  163. : integral_printf_traits<ss_uint16_t>
  164. {};
  165. STLSOFT_TEMPLATE_SPECIALISATION
  166. struct printf_traits<ss_sint32_t>
  167. : integral_printf_traits<ss_sint32_t>
  168. {};
  169. STLSOFT_TEMPLATE_SPECIALISATION
  170. struct printf_traits<ss_uint32_t>
  171. : integral_printf_traits<ss_uint32_t>
  172. {};
  173. #ifdef STLSOFT_CF_64BIT_INT_SUPPORT
  174. STLSOFT_TEMPLATE_SPECIALISATION
  175. struct printf_traits<ss_sint64_t>
  176. : integral_printf_traits<ss_sint64_t>
  177. {};
  178. STLSOFT_TEMPLATE_SPECIALISATION
  179. struct printf_traits<ss_uint64_t>
  180. : integral_printf_traits<ss_uint64_t>
  181. {};
  182. #endif /* STLSOFT_CF_64BIT_INT_SUPPORT */
  183. #ifdef STLSOFT_CF_SHORT_DISTINCT_INT_TYPE
  184. STLSOFT_TEMPLATE_SPECIALISATION
  185. struct printf_traits<short>
  186. : integral_printf_traits<short>
  187. {};
  188. STLSOFT_TEMPLATE_SPECIALISATION
  189. struct printf_traits<unsigned short>
  190. : integral_printf_traits<unsigned short>
  191. {};
  192. #endif // STLSOFT_CF_SHORT_DISTINCT_INT_TYPE
  193. #ifdef STLSOFT_CF_INT_DISTINCT_INT_TYPE
  194. STLSOFT_TEMPLATE_SPECIALISATION
  195. struct printf_traits<int>
  196. : integral_printf_traits<int>
  197. {};
  198. STLSOFT_TEMPLATE_SPECIALISATION
  199. struct printf_traits<unsigned int>
  200. : integral_printf_traits<unsigned int>
  201. {};
  202. #endif // STLSOFT_CF_INT_DISTINCT_INT_TYPE
  203. #ifdef STLSOFT_CF_LONG_DISTINCT_INT_TYPE
  204. STLSOFT_TEMPLATE_SPECIALISATION
  205. struct printf_traits<long>
  206. : integral_printf_traits<long>
  207. {};
  208. STLSOFT_TEMPLATE_SPECIALISATION
  209. struct printf_traits<unsigned long>
  210. : integral_printf_traits<unsigned long>
  211. {};
  212. #endif // STLSOFT_CF_LONG_DISTINCT_INT_TYPE
  213. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  214. ////////////////////////////////////////////////////////////////////////////
  215. // Unit-testing
  216. #ifdef STLSOFT_UNITTEST
  217. # include "./unittest/printf_traits_unittest_.h"
  218. #endif /* STLSOFT_UNITTEST */
  219. /* ////////////////////////////////////////////////////////////////////// */
  220. #ifndef _STLSOFT_NO_NAMESPACE
  221. } // namespace stlsoft
  222. #endif /* _STLSOFT_NO_NAMESPACE */
  223. /* ////////////////////////////////////////////////////////////////////// */
  224. #endif /* !STLSOFT_INCL_STLSOFT_UTIL_HPP_PRINTF_TRAITS */
  225. /* ///////////////////////////// end of file //////////////////////////// */