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.

228 lines
6.6 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/functional/composite_predicates.hpp
  3. *
  4. * Purpose: Definition of composite predicates function templates.
  5. *
  6. * Created: 27th March 2007
  7. * Updated: 4th July 2011
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2007-2011, 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/functional/composite_predicates.hpp
  40. *
  41. * \brief [C++ only] Definition of the composite predicates function templates
  42. * (\ref group__library__functional "Functional" Library).
  43. */
  44. #ifndef STLSOFT_INCL_STLSOFT_FUNCTIONAL_HPP_COMPOSITE_PREDICATES
  45. #define STLSOFT_INCL_STLSOFT_FUNCTIONAL_HPP_COMPOSITE_PREDICATES
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define STLSOFT_VER_STLSOFT_FUNCTIONAL_HPP_COMPOSITE_PREDICATES_MAJOR 1
  48. # define STLSOFT_VER_STLSOFT_FUNCTIONAL_HPP_COMPOSITE_PREDICATES_MINOR 1
  49. # define STLSOFT_VER_STLSOFT_FUNCTIONAL_HPP_COMPOSITE_PREDICATES_REVISION 2
  50. # define STLSOFT_VER_STLSOFT_FUNCTIONAL_HPP_COMPOSITE_PREDICATES_EDIT 5
  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. #ifdef STLSOFT_CF_std_NAMESPACE
  59. # include <functional>
  60. #endif /* STLSOFT_CF_std_NAMESPACE */
  61. /* /////////////////////////////////////////////////////////////////////////
  62. * Namespace
  63. */
  64. #ifndef _STLSOFT_NO_NAMESPACE
  65. namespace stlsoft
  66. {
  67. #endif /* _STLSOFT_NO_NAMESPACE */
  68. /* /////////////////////////////////////////////////////////////////////////
  69. * Classes
  70. */
  71. /** \brief Composite predicate providing logical OR for two individual
  72. * predicate types.
  73. *
  74. * \ingroup group__library__functional
  75. *
  76. */
  77. template<
  78. ss_typename_param_k P1
  79. , ss_typename_param_k P2
  80. >
  81. // [[synesis:class:function-class:unary-predicate: any_of]]
  82. struct any_of_tester
  83. #ifdef STLSOFT_CF_std_NAMESPACE
  84. : public stlsoft_ns_qual_std(unary_function)<ss_typename_type_k P1::argument_type, ss_bool_t>
  85. #endif /* STLSOFT_CF_std_NAMESPACE */
  86. {
  87. public:
  88. typedef ss_bool_t bool_type;
  89. typedef ss_typename_type_k P1::argument_type arg_type;
  90. public:
  91. any_of_tester(P1 p1, P2 p2)
  92. : m_p1(p1)
  93. , m_p2(p2)
  94. {}
  95. public:
  96. /// Returns true if either predicate is satisfied
  97. bool_type operator ()(arg_type a) const
  98. {
  99. return m_p1(a) || m_p2(a);
  100. }
  101. private:
  102. P1 m_p1;
  103. P2 m_p2;
  104. };
  105. template<
  106. ss_typename_param_k P1
  107. , ss_typename_param_k P2
  108. >
  109. inline any_of_tester<P1, P2> any_of(P1 p1, P2 p2)
  110. {
  111. return any_of_tester<P1, P2>(p1, p2);
  112. }
  113. template<
  114. ss_typename_param_k P1
  115. , ss_typename_param_k P2
  116. , ss_typename_param_k P3
  117. >
  118. inline any_of_tester<P1, any_of_tester<P2, P3> > any_of(P1 p1, P2 p2, P3 p3)
  119. {
  120. return any_of(p1, any_of(p2, p3));
  121. }
  122. template<
  123. ss_typename_param_k P1
  124. , ss_typename_param_k P2
  125. , ss_typename_param_k P3
  126. , ss_typename_param_k P4
  127. >
  128. inline any_of_tester<any_of_tester<P1, P2>, any_of_tester<P3, P4> > any_of(P1 p1, P2 p2, P3 p3, P4 p4)
  129. {
  130. return any_of(any_of(p1, p2), any_of(p3, p4));
  131. }
  132. /** \brief Composite predicate providing logical AND for two individual
  133. * predicate types.
  134. *
  135. * \ingroup group__library__functional
  136. *
  137. */
  138. template<
  139. ss_typename_param_k P1
  140. , ss_typename_param_k P2
  141. >
  142. // [[synesis:class:function-class:unary-predicate: all_of]]
  143. struct all_of_tester
  144. #ifdef STLSOFT_CF_std_NAMESPACE
  145. : public stlsoft_ns_qual_std(unary_function)<ss_typename_type_k P1::argument_type, ss_bool_t>
  146. #endif /* STLSOFT_CF_std_NAMESPACE */
  147. {
  148. public:
  149. typedef ss_bool_t bool_type;
  150. typedef ss_typename_type_k P1::argument_type arg_type;
  151. public:
  152. all_of_tester(P1 p1, P2 p2)
  153. : m_p1(p1)
  154. , m_p2(p2)
  155. {}
  156. public:
  157. /// Returns true if both predicates are satisfied
  158. bool_type operator ()(arg_type a) const
  159. {
  160. return m_p1(a) && m_p2(a);
  161. }
  162. private:
  163. P1 m_p1;
  164. P2 m_p2;
  165. };
  166. template<
  167. ss_typename_param_k P1
  168. , ss_typename_param_k P2
  169. >
  170. inline all_of_tester<P1, P2> all_of(P1 p1, P2 p2)
  171. {
  172. return all_of_tester<P1, P2>(p1, p2);
  173. }
  174. template<
  175. ss_typename_param_k P1
  176. , ss_typename_param_k P2
  177. , ss_typename_param_k P3
  178. >
  179. inline all_of_tester<P1, all_of_tester<P2, P3> > all_of(P1 p1, P2 p2, P3 p3)
  180. {
  181. return all_of(p1, all_of(p2, p3));
  182. }
  183. template<
  184. ss_typename_param_k P1
  185. , ss_typename_param_k P2
  186. , ss_typename_param_k P3
  187. , ss_typename_param_k P4
  188. >
  189. inline all_of_tester<all_of_tester<P1, P2>, all_of_tester<P3, P4> > all_of(P1 p1, P2 p2, P3 p3, P4 p4)
  190. {
  191. return all_of(all_of(p1, p2), all_of(p3, p4));
  192. }
  193. /* ////////////////////////////////////////////////////////////////////// */
  194. #ifndef _STLSOFT_NO_NAMESPACE
  195. } // namespace stlsoft
  196. #endif /* _STLSOFT_NO_NAMESPACE */
  197. /* ////////////////////////////////////////////////////////////////////// */
  198. #endif /* !STLSOFT_INCL_STLSOFT_FUNCTIONAL_HPP_COMPOSITE_PREDICATES */
  199. /* ///////////////////////////// end of file //////////////////////////// */