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.

199 lines
7.4 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/util/operator_bool.hpp
  3. *
  4. * Purpose: A robust and portable operator bool generator class.
  5. *
  6. * Created: 5th November 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/operator_bool.hpp
  40. *
  41. * \brief [C++ only] Definition of the stlsoft::operator_bool_generator
  42. * class template
  43. * (\ref group__library__utility "Utility" Library).
  44. */
  45. #ifndef STLSOFT_INCL_STLSOFT_UTIL_HPP_OPERATOR_BOOL
  46. #define STLSOFT_INCL_STLSOFT_UTIL_HPP_OPERATOR_BOOL
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define STLSOFT_VER_STLSOFT_UTIL_HPP_OPERATOR_BOOL_MAJOR 4
  49. # define STLSOFT_VER_STLSOFT_UTIL_HPP_OPERATOR_BOOL_MINOR 0
  50. # define STLSOFT_VER_STLSOFT_UTIL_HPP_OPERATOR_BOOL_REVISION 1
  51. # define STLSOFT_VER_STLSOFT_UTIL_HPP_OPERATOR_BOOL_EDIT 36
  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. /* /////////////////////////////////////////////////////////////////////////
  60. * Namespace
  61. */
  62. #ifndef _STLSOFT_NO_NAMESPACE
  63. namespace stlsoft
  64. {
  65. #endif /* _STLSOFT_NO_NAMESPACE */
  66. /* /////////////////////////////////////////////////////////////////////////
  67. * Macros
  68. */
  69. /** \brief \def STLSOFT_DEFINE_OPERATOR_BOOL_TYPES(U, G, B)
  70. * Defines the types for a type-safe boolean operator
  71. *
  72. * \ingroup group__library__utility
  73. *
  74. * \param U The Unique type. This is usually the \c class_type member type of the
  75. * class for which we are providing the boolean operator, but it could be any type.
  76. * \param G The Generator type. This is the type you use to qualify your call
  77. * to translate() inside the boolean operator
  78. * \param B The Boolean type. This is the type of the boolean operator
  79. */
  80. # define STLSOFT_DEFINE_OPERATOR_BOOL_TYPES(U, G, B) \
  81. \
  82. typedef stlsoft_ns_qual(operator_bool_generator)<U>::class_type G; \
  83. typedef G::return_type B
  84. /** \brief \def STLSOFT_DEFINE_OPERATOR_BOOL_TYPES(U, G, B)
  85. * Defines the types for a type-safe boolean operator, for use in templates
  86. *
  87. * \ingroup group__library__utility
  88. *
  89. * \param U The Unique type. This is usually the \c class_type member type of the
  90. * class for which we are providing the boolean operator, but it could be any type.
  91. * \param G The Generator type. This is the type you use to qualify your call
  92. * to translate() inside the boolean operator
  93. * \param B The Boolean type. This is the type of the boolean operator
  94. *
  95. * \note This is identical in form and purpose to STLSOFT_DEFINE_OPERATOR_BOOL_TYPES(),
  96. * except that it must be used for implementing operator boolean for templates
  97. */
  98. #if defined(STLSOFT_COMPILER_IS_BORLAND) || \
  99. ( defined(STLSOFT_COMPILER_IS_MSVC) && \
  100. _MSC_VER < 1300)
  101. # define STLSOFT_DEFINE_OPERATOR_BOOL_TYPES_T(U, G, B) \
  102. \
  103. STLSOFT_DEFINE_OPERATOR_BOOL_TYPES(U, G, B)
  104. #else /* ? compiler */
  105. # define STLSOFT_DEFINE_OPERATOR_BOOL_TYPES_T(U, G, B) \
  106. \
  107. typedef ss_typename_param_k stlsoft_ns_qual(operator_bool_generator)<U>::class_type G; \
  108. typedef ss_typename_param_k G::return_type B
  109. #endif /* compiler */
  110. /* /////////////////////////////////////////////////////////////////////////
  111. * Classes
  112. */
  113. /** \brief Template which provides the types and conversion operations for safe and
  114. * highly-portable "<code>operator bool() const</code>".
  115. *
  116. * \ingroup group__library__utility
  117. */
  118. template <ss_typename_param_k T>
  119. struct operator_bool_generator
  120. {
  121. public:
  122. typedef operator_bool_generator<T> class_type;
  123. #ifdef STLSOFT_CF_OPERATOR_BOOL_AS_OPERATOR_POINTER_TO_MEMBER_SUPPORT
  124. typedef int class_type::*return_type;
  125. /// Returns the value representing the true condition
  126. static return_type true_value()
  127. {
  128. return &class_type::i;
  129. }
  130. private:
  131. int i;
  132. public:
  133. #else
  134. typedef class_type const* return_type;
  135. /// Returns the value representing the true condition
  136. static return_type true_value()
  137. {
  138. class_type t;
  139. void *p = static_cast<void*>(&t);
  140. return static_cast<return_type>(p);
  141. }
  142. #endif // STLSOFT_CF_OPERATOR_BOOL_AS_OPERATOR_POINTER_TO_MEMBER_SUPPORT
  143. /// Returns the value representing the false condition
  144. static return_type false_value()
  145. {
  146. return static_cast<return_type>(0);
  147. }
  148. /// Does the ternary operator for you
  149. #ifdef STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  150. template <ss_typename_param_k U>
  151. static return_type translate(U b)
  152. #else /* ? STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT */
  153. static return_type translate(ss_bool_t b)
  154. #endif // STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  155. {
  156. return b ? true_value() : false_value();
  157. }
  158. private:
  159. void operator delete(void*);
  160. };
  161. /* ////////////////////////////////////////////////////////////////////// */
  162. #ifndef _STLSOFT_NO_NAMESPACE
  163. } // namespace stlsoft
  164. #endif /* _STLSOFT_NO_NAMESPACE */
  165. /* ////////////////////////////////////////////////////////////////////// */
  166. #endif /* !STLSOFT_INCL_STLSOFT_UTIL_HPP_OPERATOR_BOOL */
  167. /* ///////////////////////////// end of file //////////////////////////// */