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.

348 lines
10 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/util/sign_traits.hpp
  3. *
  4. * Purpose: sign_traits classes.
  5. *
  6. * Created: 16th January 2002
  7. * Updated: 31st July 2012
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2002-2012, 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/sign_traits.hpp
  40. *
  41. * \brief [C++ only] Definition of the stlsoft::sign_traits traits class
  42. * (\ref group__library__utility "Utility" Library).
  43. */
  44. #ifndef STLSOFT_INCL_STLSOFT_UTIL_HPP_SIGN_TRAITS
  45. #define STLSOFT_INCL_STLSOFT_UTIL_HPP_SIGN_TRAITS
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define STLSOFT_VER_STLSOFT_UTIL_HPP_SIGN_TRAITS_MAJOR 4
  48. # define STLSOFT_VER_STLSOFT_UTIL_HPP_SIGN_TRAITS_MINOR 1
  49. # define STLSOFT_VER_STLSOFT_UTIL_HPP_SIGN_TRAITS_REVISION 3
  50. # define STLSOFT_VER_STLSOFT_UTIL_HPP_SIGN_TRAITS_EDIT 47
  51. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  52. /* /////////////////////////////////////////////////////////////////////////
  53. * Includes
  54. */
  55. #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
  56. # include <stlsoft/stlsoft.h>
  57. #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
  58. #ifndef STLSOFT_INCL_STLSOFT_UTIL_HPP_SIZE_TRAITS
  59. # include <stlsoft/util/size_traits.hpp>
  60. #endif /* !STLSOFT_INCL_STLSOFT_UTIL_HPP_SIZE_TRAITS */
  61. /* /////////////////////////////////////////////////////////////////////////
  62. * Namespace
  63. */
  64. #ifndef _STLSOFT_NO_NAMESPACE
  65. namespace stlsoft
  66. {
  67. #endif /* _STLSOFT_NO_NAMESPACE */
  68. /* /////////////////////////////////////////////////////////////////////////
  69. * Classes
  70. */
  71. // struct sign_traits
  72. #ifdef STLSOFT_DOCUMENTATION_SKIP_SECTION
  73. /** \brief Traits for determining the signed, unsigned and alternate-signed type
  74. *
  75. * \ingroup group__library__utility
  76. *
  77. * sign_traits is a traits class for acquiring the corresponding signed,
  78. * unsigned, and alternate-signed type eg.
  79. *
  80. \code
  81. assert(stlsoft::is_same_type<stlsoft::sign_traits<ss_sint16_t>::signed_type, ss_sint16_t>::value);
  82. assert(stlsoft::is_same_type<stlsoft::sign_traits<ss_sint16_t>::unsigned_type, ss_uint16_t>::value);
  83. assert(stlsoft::is_same_type<stlsoft::sign_traits<ss_sint16_t>::alt_sign_type, ss_uint16_t>::value);
  84. \endcode
  85. *
  86. * \param T The char type
  87. *
  88. */
  89. template <ss_typename_param_k T>
  90. struct sign_traits
  91. {
  92. enum
  93. {
  94. bytes = 0 //!< The type size, in bytes
  95. };
  96. enum
  97. {
  98. bits = 0 //!< The type size, in bits
  99. };
  100. typedef T type;
  101. typedef signed T signed_type; //!< The signed type
  102. typedef unsigned T unsigned_type; //!< The unsigned type
  103. typedef unsigned T alt_sign_type; //!< The alternate-signed type
  104. };
  105. #else
  106. template <ss_typename_param_k T>
  107. struct sign_traits;
  108. /* char */
  109. STLSOFT_TEMPLATE_SPECIALISATION
  110. struct sign_traits<ss_char_a_t>
  111. {
  112. enum { bytes = 1 };
  113. enum { bits = 8 * bytes };
  114. typedef ss_char_a_t type;
  115. typedef ss_uint8_t signed_type;
  116. typedef ss_uint8_t unsigned_type;
  117. // typedef ss_uint8_t alt_sign_type;
  118. };
  119. #ifdef STLSOFT_CF_NATIVE_WCHAR_T_SUPPORT
  120. STLSOFT_TEMPLATE_SPECIALISATION
  121. struct sign_traits<ss_char_w_t>
  122. {
  123. enum { bytes = int(sizeof(ss_char_w_t)) };
  124. enum { bits = 8 * bytes };
  125. typedef ss_char_w_t type;
  126. typedef int_size_traits<bytes>::signed_type signed_type;
  127. typedef int_size_traits<bytes>::unsigned_type unsigned_type;
  128. };
  129. #endif /* STLSOFT_CF_NATIVE_WCHAR_T_SUPPORT */
  130. /* s/uint8 */
  131. STLSOFT_TEMPLATE_SPECIALISATION
  132. struct sign_traits<ss_sint8_t>
  133. {
  134. enum { bytes = 1 };
  135. enum { bits = 8 * bytes };
  136. typedef ss_sint8_t type;
  137. typedef ss_sint8_t signed_type;
  138. typedef ss_uint8_t unsigned_type;
  139. typedef ss_uint8_t alt_sign_type;
  140. };
  141. STLSOFT_TEMPLATE_SPECIALISATION
  142. struct sign_traits<ss_uint8_t>
  143. {
  144. enum { bytes = 1 };
  145. enum { bits = 8 * bytes };
  146. typedef ss_uint8_t type;
  147. typedef ss_sint8_t signed_type;
  148. typedef ss_uint8_t unsigned_type;
  149. typedef ss_sint8_t alt_sign_type;
  150. };
  151. /* s/uint16 */
  152. STLSOFT_TEMPLATE_SPECIALISATION
  153. struct sign_traits<ss_sint16_t>
  154. {
  155. enum { bytes = 2 };
  156. enum { bits = 8 * bytes };
  157. typedef ss_sint16_t type;
  158. typedef ss_sint16_t signed_type;
  159. typedef ss_uint16_t unsigned_type;
  160. typedef ss_uint16_t alt_sign_type;
  161. };
  162. STLSOFT_TEMPLATE_SPECIALISATION
  163. struct sign_traits<ss_uint16_t>
  164. {
  165. enum { bytes = 2 };
  166. enum { bits = 8 * bytes };
  167. typedef ss_uint16_t type;
  168. typedef ss_sint16_t signed_type;
  169. typedef ss_uint16_t unsigned_type;
  170. typedef ss_sint16_t alt_sign_type;
  171. };
  172. /* s/uint32 */
  173. STLSOFT_TEMPLATE_SPECIALISATION
  174. struct sign_traits<ss_sint32_t>
  175. {
  176. enum { bytes = 4 };
  177. enum { bits = 8 * bytes };
  178. typedef ss_sint32_t type;
  179. typedef ss_sint32_t signed_type;
  180. typedef ss_uint32_t unsigned_type;
  181. typedef ss_uint32_t alt_sign_type;
  182. };
  183. STLSOFT_TEMPLATE_SPECIALISATION
  184. struct sign_traits<ss_uint32_t>
  185. {
  186. enum { bytes = 4 };
  187. enum { bits = 8 * bytes };
  188. typedef ss_uint32_t type;
  189. typedef ss_sint32_t signed_type;
  190. typedef ss_uint32_t unsigned_type;
  191. typedef ss_sint32_t alt_sign_type;
  192. };
  193. #ifdef STLSOFT_CF_64BIT_INT_SUPPORT
  194. /* s/uint64 */
  195. STLSOFT_TEMPLATE_SPECIALISATION
  196. struct sign_traits<ss_sint64_t>
  197. {
  198. enum { bytes = 8 };
  199. enum { bits = 8 * bytes };
  200. typedef ss_sint64_t type;
  201. typedef ss_sint64_t signed_type;
  202. typedef ss_uint64_t unsigned_type;
  203. typedef ss_uint64_t alt_sign_type;
  204. };
  205. STLSOFT_TEMPLATE_SPECIALISATION
  206. struct sign_traits<ss_uint64_t>
  207. {
  208. enum { bytes = 8 };
  209. enum { bits = 8 * bytes };
  210. typedef ss_uint64_t type;
  211. typedef ss_sint64_t signed_type;
  212. typedef ss_uint64_t unsigned_type;
  213. typedef ss_sint64_t alt_sign_type;
  214. };
  215. #endif /* !STLSOFT_CF_64BIT_INT_SUPPORT */
  216. #ifdef STLSOFT_CF_SHORT_DISTINCT_INT_TYPE
  217. STLSOFT_TEMPLATE_SPECIALISATION
  218. struct sign_traits<signed short>
  219. {
  220. enum { bytes = sizeof(signed short) };
  221. enum { bits = 8 * bytes };
  222. typedef signed short type;
  223. typedef signed short signed_type;
  224. typedef unsigned short unsigned_type;
  225. typedef unsigned short alt_sign_type;
  226. };
  227. STLSOFT_TEMPLATE_SPECIALISATION
  228. struct sign_traits<unsigned short>
  229. {
  230. enum { bytes = sizeof(unsigned short) };
  231. enum { bits = 8 * bytes };
  232. typedef unsigned short type;
  233. typedef signed short signed_type;
  234. typedef unsigned short unsigned_type;
  235. typedef signed short alt_sign_type;
  236. };
  237. #endif /* !STLSOFT_CF_SHORT_DISTINCT_INT_TYPE */
  238. #ifdef STLSOFT_CF_INT_DISTINCT_INT_TYPE
  239. STLSOFT_TEMPLATE_SPECIALISATION
  240. struct sign_traits<int>
  241. {
  242. enum { bytes = sizeof(int) };
  243. enum { bits = 8 * bytes };
  244. typedef int type;
  245. typedef int signed_type;
  246. typedef unsigned unsigned_type;
  247. typedef unsigned alt_sign_type;
  248. };
  249. STLSOFT_TEMPLATE_SPECIALISATION
  250. struct sign_traits<unsigned>
  251. {
  252. enum { bytes = sizeof(unsigned) };
  253. enum { bits = 8 * bytes };
  254. typedef unsigned type;
  255. typedef int signed_type;
  256. typedef unsigned unsigned_type;
  257. typedef int alt_sign_type;
  258. };
  259. #endif /* !STLSOFT_CF_INT_DISTINCT_INT_TYPE */
  260. #ifdef STLSOFT_CF_LONG_DISTINCT_INT_TYPE
  261. STLSOFT_TEMPLATE_SPECIALISATION
  262. struct sign_traits<long>
  263. {
  264. enum { bytes = int(sizeof(long)) };
  265. enum { bits = 8 * bytes };
  266. typedef long type;
  267. typedef long signed_type;
  268. typedef unsigned long unsigned_type;
  269. typedef unsigned long alt_sign_type;
  270. };
  271. STLSOFT_TEMPLATE_SPECIALISATION
  272. struct sign_traits<unsigned long>
  273. {
  274. enum { bytes = int(sizeof(unsigned long)) };
  275. enum { bits = 8 * bytes };
  276. typedef unsigned long type;
  277. typedef long signed_type;
  278. typedef unsigned long unsigned_type;
  279. typedef long alt_sign_type;
  280. };
  281. #endif /* !STLSOFT_CF_LONG_DISTINCT_INT_TYPE */
  282. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  283. /* ////////////////////////////////////////////////////////////////////// */
  284. #ifndef _STLSOFT_NO_NAMESPACE
  285. } // namespace stlsoft
  286. #endif /* _STLSOFT_NO_NAMESPACE */
  287. /* ////////////////////////////////////////////////////////////////////// */
  288. #endif /* !STLSOFT_INCL_STLSOFT_UTIL_HPP_SIGN_TRAITS */
  289. /* ///////////////////////////// end of file //////////////////////////// */