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.

266 lines
7.6 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/util/null.hpp (stlsoft_null.h
  3. *
  4. * Purpose: NULL_v template class. Include stlsoft_nulldef.h for NULL to be
  5. * automatically the strong NULL.
  6. *
  7. * Created: 8th September 2002
  8. * Updated: 10th August 2009
  9. *
  10. * Home: http://stlsoft.org/
  11. *
  12. * Copyright (c) 2002-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 stlsoft/util/null.hpp
  41. *
  42. * \brief [C++ only] Definition of the stlsoft::NULL_v class
  43. * (\ref group__library__utility "Utility" Library).
  44. */
  45. #ifndef STLSOFT_INCL_STLSOFT_UTIL_HPP_NULL
  46. #define STLSOFT_INCL_STLSOFT_UTIL_HPP_NULL
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define STLSOFT_VER_STLSOFT_UTIL_HPP_NULL_MAJOR 4
  49. # define STLSOFT_VER_STLSOFT_UTIL_HPP_NULL_MINOR 0
  50. # define STLSOFT_VER_STLSOFT_UTIL_HPP_NULL_REVISION 1
  51. # define STLSOFT_VER_STLSOFT_UTIL_HPP_NULL_EDIT 50
  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. /* _STLSOFT_NULL_v_DEFINED */
  60. #ifdef _STLSOFT_NULL_v_DEFINED
  61. # undef _STLSOFT_NULL_v_DEFINED
  62. #endif /* _STLSOFT_NULL_v_DEFINED */
  63. #define _STLSOFT_NULL_v_DEFINED
  64. #if defined(STLSOFT_COMPILER_IS_DMC) && \
  65. __DMC__ < 0x0840
  66. # undef _STLSOFT_NULL_v_DEFINED
  67. #elif defined(STLSOFT_COMPILER_IS_MSVC) && \
  68. _MSC_VER < 1310
  69. # undef _STLSOFT_NULL_v_DEFINED
  70. #elif defined(STLSOFT_COMPILER_IS_WATCOM)
  71. # undef _STLSOFT_NULL_v_DEFINED
  72. #endif /* compiler */
  73. /* _STLSOFT_NULL_v_DEFINED_PTR_TO_MEMBER_SUPPORT */
  74. #ifdef _STLSOFT_NULL_v_DEFINED_PTR_TO_MEMBER_SUPPORT
  75. # undef _STLSOFT_NULL_v_DEFINED_PTR_TO_MEMBER_SUPPORT
  76. #endif /* _STLSOFT_NULL_v_DEFINED_PTR_TO_MEMBER_SUPPORT */
  77. #define _STLSOFT_NULL_v_DEFINED_PTR_TO_MEMBER_SUPPORT
  78. #if defined(STLSOFT_COMPILER_IS_GCC)
  79. # undef _STLSOFT_NULL_v_DEFINED_PTR_TO_MEMBER_SUPPORT
  80. #elif defined(STLSOFT_COMPILER_IS_MWERKS)
  81. # undef _STLSOFT_NULL_v_DEFINED_PTR_TO_MEMBER_SUPPORT
  82. #endif /* compiler */
  83. /* /////////////////////////////////////////////////////////////////////////
  84. * Namespace
  85. */
  86. #ifndef _STLSOFT_NO_NAMESPACE
  87. namespace stlsoft
  88. {
  89. #endif /* _STLSOFT_NO_NAMESPACE */
  90. /* /////////////////////////////////////////////////////////////////////////
  91. * Classes
  92. */
  93. #ifdef _STLSOFT_NULL_v_DEFINED
  94. /** \brief Represents a type that can be an active replacement for NULL
  95. *
  96. * \ingroup group__library__utility
  97. *
  98. * This class can act as a replacement for the NULL macro, by being validly
  99. * assigned to or equated with pointer types only, as in
  100. *
  101. * int i = NULL; // error
  102. * int *p = NULL; // OK
  103. *
  104. * if(i == NULL) {} // error
  105. * if(NULL == i) {} // error
  106. *
  107. * if(p == NULL) {} // OK
  108. * if(NULL == p) {} // OK
  109. *
  110. *
  111. * When used via inclusion of the file stlsoft_nulldef.h, the macro NULL is
  112. * redefined as NULL_v(), such that expressions containing NULL will be valid
  113. * against pointers only.
  114. */
  115. struct NULL_v
  116. {
  117. // Construction
  118. public:
  119. /// Default constructor
  120. NULL_v()
  121. {}
  122. /** \brief Static creation
  123. *
  124. * \ingroup group__library__utility
  125. */
  126. public:
  127. static NULL_v create()
  128. {
  129. return NULL_v();
  130. }
  131. // Conversion
  132. public:
  133. /// Implicit conversion operator (convertible to any pointer type)
  134. template <ss_typename_param_k T>
  135. operator T *() const
  136. {
  137. return 0;
  138. }
  139. #ifdef _STLSOFT_NULL_v_DEFINED_PTR_TO_MEMBER_SUPPORT
  140. /// Implicit conversion operator (convertible to any pointer type)
  141. template< ss_typename_param_k T2
  142. , ss_typename_param_k C
  143. >
  144. operator T2 C::*() const
  145. {
  146. return 0;
  147. }
  148. #endif /* _STLSOFT_NULL_v_DEFINED_PTR_TO_MEMBER_SUPPORT */
  149. /// Evaluates whether an instance of a type is null
  150. ///
  151. /// \param rhs A reference arbitrary type which will be compared to null
  152. template <ss_typename_param_k T>
  153. ss_bool_t equal(T const& rhs) const
  154. {
  155. return rhs == 0;
  156. }
  157. /// \deprecated
  158. template <ss_typename_param_k T>
  159. ss_bool_t equals(T const& rhs) const
  160. {
  161. return equal(rhs);
  162. }
  163. // Not to be implemented
  164. private:
  165. void operator &() const;
  166. #if !defined(STLSOFT_COMPILER_IS_GCC)
  167. NULL_v(NULL_v const&);
  168. NULL_v const& operator =(NULL_v const&);
  169. #endif /* compiler */
  170. };
  171. #if 0
  172. /** \brief operator == for NULL_v and an arbitrary type
  173. *
  174. * \ingroup group__library__utility
  175. */
  176. template <ss_typename_param_k T>
  177. inline ss_bool_t operator ==(NULL_v const& lhs, T const& rhs)
  178. {
  179. STLSOFT_STATIC_ASSERT(sizeof(rhs) == 4);
  180. return lhs.equal(rhs);
  181. }
  182. /** \brief operator == for an arbitrary type and NULL_v
  183. *
  184. * \ingroup group__library__utility
  185. */
  186. template <ss_typename_param_k T>
  187. inline ss_bool_t operator ==(T const& lhs, NULL_v const& rhs)
  188. {
  189. STLSOFT_STATIC_ASSERT(sizeof(lhs) == 4);
  190. return rhs.equal(lhs);
  191. }
  192. /** \brief operator != for NULL_v and an arbitrary type
  193. *
  194. * \ingroup group__library__utility
  195. */
  196. template <ss_typename_param_k T>
  197. inline ss_bool_t operator !=(NULL_v const& lhs, T const& rhs)
  198. {
  199. STLSOFT_STATIC_ASSERT(sizeof(rhs) == 4);
  200. return !lhs.equal(rhs);
  201. }
  202. /** \brief operator != for an arbitrary type and NULL_v
  203. *
  204. * \ingroup group__library__utility
  205. */
  206. template <ss_typename_param_k T>
  207. inline ss_bool_t operator !=(T const& lhs, NULL_v const& rhs)
  208. {
  209. STLSOFT_STATIC_ASSERT(sizeof(lhs) == 4);
  210. return !rhs.equal(lhs);
  211. }
  212. #endif /* 0 */
  213. #endif /* _STLSOFT_NULL_v_DEFINED */
  214. ////////////////////////////////////////////////////////////////////////////
  215. // Unit-testing
  216. #ifdef STLSOFT_UNITTEST
  217. # include "./unittest/null_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_NULL */
  225. /* ///////////////////////////// end of file //////////////////////////// */