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.

333 lines
13 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/util/zero.hpp
  3. *
  4. * Purpose: ZERO_v template class.
  5. *
  6. * Created: 29th July 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/util/zero.hpp
  40. *
  41. * \brief [C++ only] Definition of the stlsoft::ZERO_v class
  42. * (\ref group__library__utility "Utility" Library).
  43. */
  44. #ifndef STLSOFT_INCL_STLSOFT_UTIL_HPP_ZERO
  45. #define STLSOFT_INCL_STLSOFT_UTIL_HPP_ZERO
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define STLSOFT_VER_STLSOFT_UTIL_HPP_ZERO_MAJOR 4
  48. # define STLSOFT_VER_STLSOFT_UTIL_HPP_ZERO_MINOR 1
  49. # define STLSOFT_VER_STLSOFT_UTIL_HPP_ZERO_REVISION 1
  50. # define STLSOFT_VER_STLSOFT_UTIL_HPP_ZERO_EDIT 45
  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. /* /////////////////////////////////////////////////////////////////////////
  59. * Namespace
  60. */
  61. #ifndef _STLSOFT_NO_NAMESPACE
  62. namespace stlsoft
  63. {
  64. #endif /* _STLSOFT_NO_NAMESPACE */
  65. /* /////////////////////////////////////////////////////////////////////////
  66. * Classes
  67. */
  68. /** \brief Represents a type that can be an active replacement for ZERO
  69. *
  70. * \ingroup group__library__utility
  71. *
  72. * This class can act as a replacement for the ZERO macro, by being validly
  73. * assigned to or equated with pointer types only, as in
  74. *
  75. * int i = ZERO; // error
  76. * int *p = ZERO; // OK
  77. *
  78. * if(i == ZERO) {} // error
  79. * if(ZERO == i) {} // error
  80. *
  81. * if(p == ZERO) {} // OK
  82. * if(ZERO == p) {} // OK
  83. *
  84. *
  85. * When used via inclusion of the file stlsoft_zerodef.h, the macro ZERO is
  86. * redefined as ZERO_v(), such that expressions containing ZERO will be valid
  87. * against pointers only.
  88. */
  89. struct ZERO_v
  90. {
  91. // Construction
  92. public:
  93. /// Default constructor
  94. ZERO_v()
  95. {}
  96. /** \brief Static creation
  97. *
  98. * \ingroup group__library__utility
  99. */
  100. public:
  101. static ZERO_v create()
  102. {
  103. return ZERO_v();
  104. }
  105. // Conversion
  106. public:
  107. /// A zero (of type ss_sint8_t)
  108. operator ss_sint8_t () const
  109. {
  110. return 0;
  111. }
  112. /// A zero (of type ss_uint8_t)
  113. operator ss_uint8_t () const
  114. {
  115. return 0;
  116. }
  117. /// A zero (of type ss_sint16_t)
  118. operator ss_sint16_t () const
  119. {
  120. return 0;
  121. }
  122. /// A zero (of type ss_uint16_t)
  123. operator ss_uint16_t () const
  124. {
  125. return 0;
  126. }
  127. /// A zero (of type ss_sint32_t)
  128. operator ss_sint32_t () const
  129. {
  130. return 0;
  131. }
  132. /// A zero (of type ss_uint32_t)
  133. operator ss_uint32_t () const
  134. {
  135. return 0;
  136. }
  137. #ifdef STLSOFT_CF_64BIT_INT_SUPPORT
  138. /// A zero (of type ss_sint64_t)
  139. operator ss_sint64_t () const
  140. {
  141. return 0;
  142. }
  143. /// A zero (of type ss_uint64_t)
  144. operator ss_uint64_t () const
  145. {
  146. return 0;
  147. }
  148. #endif /* STLSOFT_CF_64BIT_INT_SUPPORT */
  149. #ifdef STLSOFT_CF_INT_DISTINCT_INT_TYPE
  150. /// A zero (of type signed int)
  151. operator signed int () const
  152. {
  153. return 0;
  154. }
  155. /// A zero (of type unsigned int)
  156. operator unsigned int () const
  157. {
  158. return 0;
  159. }
  160. #endif /* STLSOFT_CF_INT_DISTINCT_INT_TYPE */
  161. #ifdef STLSOFT_CF_LONG_DISTINCT_INT_TYPE
  162. /// A zero (of type signed long)
  163. operator signed long () const
  164. {
  165. return 0;
  166. }
  167. /// A zero (of type unsigned long)
  168. operator unsigned long () const
  169. {
  170. return 0;
  171. }
  172. #endif /* STLSOFT_CF_LONG_DISTINCT_INT_TYPE */
  173. /// A zero (of type float)
  174. operator float () const
  175. {
  176. return 0.0f;
  177. }
  178. /// A zero (of type double)
  179. operator double () const
  180. {
  181. return 0.0;
  182. }
  183. /// A zero (of type long double)
  184. operator long double () const
  185. {
  186. return 0.0;
  187. }
  188. // Not to be implemented
  189. private:
  190. void operator &() const;
  191. ZERO_v(ZERO_v const&);
  192. ZERO_v const& operator =(ZERO_v const&);
  193. };
  194. /** \brief operator == for ZERO_v and integral types
  195. *
  196. * \ingroup group__library__utility
  197. */
  198. inline ss_bool_t operator ==(ZERO_v const& /* lhs */, ss_sint8_t i) { return i == 0; }
  199. inline ss_bool_t operator ==(ZERO_v const& /* lhs */, ss_uint8_t i) { return i == 0; }
  200. inline ss_bool_t operator ==(ZERO_v const& /* lhs */, ss_sint16_t i) { return i == 0; }
  201. inline ss_bool_t operator ==(ZERO_v const& /* lhs */, ss_uint16_t i) { return i == 0; }
  202. inline ss_bool_t operator ==(ZERO_v const& /* lhs */, ss_sint32_t i) { return i == 0; }
  203. inline ss_bool_t operator ==(ZERO_v const& /* lhs */, ss_uint32_t i) { return i == 0; }
  204. #ifdef STLSOFT_CF_64BIT_INT_SUPPORT
  205. inline ss_bool_t operator ==(ZERO_v const& /* lhs */, ss_sint64_t const& i) { return i == 0; }
  206. inline ss_bool_t operator ==(ZERO_v const& /* lhs */, ss_uint64_t const& i) { return i == 0; }
  207. #endif /* STLSOFT_CF_64BIT_INT_SUPPORT */
  208. #ifdef STLSOFT_CF_INT_DISTINCT_INT_TYPE
  209. inline ss_bool_t operator ==(ZERO_v const& /* lhs */, signed int i) { return i == 0; }
  210. inline ss_bool_t operator ==(ZERO_v const& /* lhs */, unsigned int i) { return i == 0; }
  211. #endif /* STLSOFT_CF_INT_DISTINCT_INT_TYPE */
  212. #ifdef STLSOFT_CF_LONG_DISTINCT_INT_TYPE
  213. inline ss_bool_t operator ==(ZERO_v const& /* lhs */, signed long i) { return i == 0; }
  214. inline ss_bool_t operator ==(ZERO_v const& /* lhs */, unsigned long i) { return i == 0; }
  215. #endif /* STLSOFT_CF_LONG_DISTINCT_INT_TYPE */
  216. inline ss_bool_t operator ==(ZERO_v const& /* lhs */, float const& i) { return i == 0; }
  217. inline ss_bool_t operator ==(ZERO_v const& /* lhs */, double const& i) { return i == 0; }
  218. inline ss_bool_t operator ==(ZERO_v const& /* lhs */, long double const& i) { return i == 0; }
  219. /** \brief operator == for an integral type and ZERO_v
  220. *
  221. * \ingroup group__library__utility
  222. */
  223. inline ss_bool_t operator ==(ss_sint8_t i, ZERO_v const& /* lhs */) { return i == 0; }
  224. inline ss_bool_t operator ==(ss_uint8_t i, ZERO_v const& /* lhs */) { return i == 0; }
  225. inline ss_bool_t operator ==(ss_sint16_t i, ZERO_v const& /* lhs */) { return i == 0; }
  226. inline ss_bool_t operator ==(ss_uint16_t i, ZERO_v const& /* lhs */) { return i == 0; }
  227. inline ss_bool_t operator ==(ss_sint32_t i, ZERO_v const& /* lhs */) { return i == 0; }
  228. inline ss_bool_t operator ==(ss_uint32_t i, ZERO_v const& /* lhs */) { return i == 0; }
  229. #ifdef STLSOFT_CF_64BIT_INT_SUPPORT
  230. inline ss_bool_t operator ==(ss_sint64_t const& i, ZERO_v const& /* lhs */) { return i == 0; }
  231. inline ss_bool_t operator ==(ss_uint64_t const& i, ZERO_v const& /* lhs */) { return i == 0; }
  232. #endif /* STLSOFT_CF_64BIT_INT_SUPPORT */
  233. #ifdef STLSOFT_CF_INT_DISTINCT_INT_TYPE
  234. inline ss_bool_t operator ==(signed int i, ZERO_v const& /* lhs */) { return i == 0; }
  235. inline ss_bool_t operator ==(unsigned int i, ZERO_v const& /* lhs */) { return i == 0; }
  236. #endif /* STLSOFT_CF_INT_DISTINCT_INT_TYPE */
  237. #ifdef STLSOFT_CF_LONG_DISTINCT_INT_TYPE
  238. inline ss_bool_t operator ==(signed long i, ZERO_v const& /* lhs */) { return i == 0; }
  239. inline ss_bool_t operator ==(unsigned long i,ZERO_v const& /* lhs */) { return i == 0; }
  240. #endif /* STLSOFT_CF_LONG_DISTINCT_INT_TYPE */
  241. inline ss_bool_t operator ==(float const& i, ZERO_v const& /* lhs */) { return i == 0; }
  242. inline ss_bool_t operator ==(double const& i, ZERO_v const& /* lhs */) { return i == 0; }
  243. inline ss_bool_t operator ==(long double const& i, ZERO_v const& /* lhs */) { return i == 0; }
  244. /** \brief operator != for ZERO_v and an integral type
  245. *
  246. * \ingroup group__library__utility
  247. */
  248. inline ss_bool_t operator !=(ZERO_v const& /* lhs */, ss_sint8_t i) { return i != 0; }
  249. inline ss_bool_t operator !=(ZERO_v const& /* lhs */, ss_uint8_t i) { return i != 0; }
  250. inline ss_bool_t operator !=(ZERO_v const& /* lhs */, ss_sint16_t i) { return i != 0; }
  251. inline ss_bool_t operator !=(ZERO_v const& /* lhs */, ss_uint16_t i) { return i != 0; }
  252. inline ss_bool_t operator !=(ZERO_v const& /* lhs */, ss_sint32_t i) { return i != 0; }
  253. inline ss_bool_t operator !=(ZERO_v const& /* lhs */, ss_uint32_t i) { return i != 0; }
  254. #ifdef STLSOFT_CF_64BIT_INT_SUPPORT
  255. inline ss_bool_t operator !=(ZERO_v const& /* lhs */, ss_sint64_t const& i) { return i != 0; }
  256. inline ss_bool_t operator !=(ZERO_v const& /* lhs */, ss_uint64_t const& i) { return i != 0; }
  257. #endif /* STLSOFT_CF_64BIT_INT_SUPPORT */
  258. #ifdef STLSOFT_CF_INT_DISTINCT_INT_TYPE
  259. inline ss_bool_t operator !=(ZERO_v const& /* lhs */, signed int i) { return i != 0; }
  260. inline ss_bool_t operator !=(ZERO_v const& /* lhs */, unsigned int i) { return i != 0; }
  261. #endif /* STLSOFT_CF_INT_DISTINCT_INT_TYPE */
  262. #ifdef STLSOFT_CF_LONG_DISTINCT_INT_TYPE
  263. inline ss_bool_t operator !=(ZERO_v const& /* lhs */, signed long i) { return i != 0; }
  264. inline ss_bool_t operator !=(ZERO_v const& /* lhs */, unsigned long i) { return i != 0; }
  265. #endif /* STLSOFT_CF_LONG_DISTINCT_INT_TYPE */
  266. inline ss_bool_t operator !=(ZERO_v const& /* lhs */, float const& i) { return i != 0; }
  267. inline ss_bool_t operator !=(ZERO_v const& /* lhs */, double const& i) { return i != 0; }
  268. inline ss_bool_t operator !=(ZERO_v const& /* lhs */, long double const& i) { return i != 0; }
  269. /** \brief operator != for an integral type and ZERO_v
  270. *
  271. * \ingroup group__library__utility
  272. */
  273. inline ss_bool_t operator !=(ss_sint8_t i, ZERO_v const& /* lhs */) { return i != 0; }
  274. inline ss_bool_t operator !=(ss_uint8_t i, ZERO_v const& /* lhs */) { return i != 0; }
  275. inline ss_bool_t operator !=(ss_sint16_t i, ZERO_v const& /* lhs */) { return i != 0; }
  276. inline ss_bool_t operator !=(ss_uint16_t i, ZERO_v const& /* lhs */) { return i != 0; }
  277. inline ss_bool_t operator !=(ss_sint32_t i, ZERO_v const& /* lhs */) { return i != 0; }
  278. inline ss_bool_t operator !=(ss_uint32_t i, ZERO_v const& /* lhs */) { return i != 0; }
  279. #ifdef STLSOFT_CF_64BIT_INT_SUPPORT
  280. inline ss_bool_t operator !=(ss_sint64_t const& i, ZERO_v const& /* lhs */) { return i != 0; }
  281. inline ss_bool_t operator !=(ss_uint64_t const& i, ZERO_v const& /* lhs */) { return i != 0; }
  282. #endif /* STLSOFT_CF_64BIT_INT_SUPPORT */
  283. #ifdef STLSOFT_CF_INT_DISTINCT_INT_TYPE
  284. inline ss_bool_t operator !=(signed int i, ZERO_v const& /* lhs */) { return i != 0; }
  285. inline ss_bool_t operator !=(unsigned int i, ZERO_v const& /* lhs */) { return i != 0; }
  286. #endif /* STLSOFT_CF_INT_DISTINCT_INT_TYPE */
  287. #ifdef STLSOFT_CF_LONG_DISTINCT_INT_TYPE
  288. inline ss_bool_t operator !=(signed long i, ZERO_v const& /* lhs */) { return i != 0; }
  289. inline ss_bool_t operator !=(unsigned long i,ZERO_v const& /* lhs */) { return i != 0; }
  290. #endif /* STLSOFT_CF_LONG_DISTINCT_INT_TYPE */
  291. inline ss_bool_t operator !=(float const& i, ZERO_v const& /* lhs */) { return i != 0; }
  292. inline ss_bool_t operator !=(double const& i, ZERO_v const& /* lhs */) { return i != 0; }
  293. inline ss_bool_t operator !=(long double const& i, ZERO_v const& /* lhs */) { return i != 0; }
  294. ////////////////////////////////////////////////////////////////////////////
  295. // Unit-testing
  296. #ifdef STLSOFT_UNITTEST
  297. # include "./unittest/zero_unittest_.h"
  298. #endif /* STLSOFT_UNITTEST */
  299. /* ////////////////////////////////////////////////////////////////////// */
  300. #ifndef _STLSOFT_NO_NAMESPACE
  301. } // namespace stlsoft
  302. #endif /* _STLSOFT_NO_NAMESPACE */
  303. /* ////////////////////////////////////////////////////////////////////// */
  304. #endif /* !STLSOFT_INCL_STLSOFT_UTIL_HPP_ZERO */
  305. /* ///////////////////////////// end of file //////////////////////////// */