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.

188 lines
6.5 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/functional/procedure_adaptors.hpp
  3. *
  4. * Purpose: Contains the adaptors to allow functions to be used as procedures in algorithms.
  5. *
  6. * Created: 13th June 1999
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 1999-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/functional/procedure_adaptors.hpp
  40. *
  41. * \brief [C++ only] Function classes that adapt non-member procedures (and
  42. * handle different calling conventions)
  43. * (\ref group__library__functional "Functional" Library).
  44. */
  45. #ifndef STLSOFT_INCL_STLSOFT_FUNCTIONAL_HPP_PROCEDURE_ADAPTORS
  46. #define STLSOFT_INCL_STLSOFT_FUNCTIONAL_HPP_PROCEDURE_ADAPTORS
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define STLSOFT_VER_STLSOFT_FUNCTIONAL_HPP_PROCEDURE_ADAPTORS_MAJOR 2
  49. # define STLSOFT_VER_STLSOFT_FUNCTIONAL_HPP_PROCEDURE_ADAPTORS_MINOR 0
  50. # define STLSOFT_VER_STLSOFT_FUNCTIONAL_HPP_PROCEDURE_ADAPTORS_REVISION 2
  51. # define STLSOFT_VER_STLSOFT_FUNCTIONAL_HPP_PROCEDURE_ADAPTORS_EDIT 14
  52. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  53. /* /////////////////////////////////////////////////////////////////////////
  54. * Auto-generation and compatibility
  55. */
  56. /* /////////////////////////////////////////////////////////////////////////
  57. * Includes
  58. */
  59. #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
  60. # include <stlsoft/stlsoft.h>
  61. #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
  62. #ifndef STLSOFT_INCL_FUNCTIONAL
  63. # define STLSOFT_INCL_FUNCTIONAL
  64. # include <functional>
  65. #endif /* !STLSOFT_INCL_FUNCTIONAL */
  66. /* /////////////////////////////////////////////////////////////////////////
  67. * Namespace
  68. */
  69. #ifndef _STLSOFT_NO_NAMESPACE
  70. namespace stlsoft
  71. {
  72. #endif /* _STLSOFT_NO_NAMESPACE */
  73. /* /////////////////////////////////////////////////////////////////////////
  74. * Classes
  75. */
  76. /** \brief Adapts a unary function into a unary procedure - one in which the return
  77. * type of the function call operator is void
  78. *
  79. * \ingroup group__library__functional
  80. */
  81. // [[synesis:class:function-class:unary-function: unary_procedure_adaptor<T<F>>]]
  82. template <ss_typename_param_k F>
  83. struct unary_procedure_adaptor
  84. : public stlsoft_ns_qual_std(unary_function)< ss_typename_type_k F::argument_type
  85. , void>
  86. {
  87. private:
  88. typedef F adapted_function_type;
  89. public:
  90. typedef ss_typename_type_k F::argument_type argument_type;
  91. typedef void result_type;
  92. public:
  93. unary_procedure_adaptor(adapted_function_type func)
  94. : m_func(func)
  95. {}
  96. public:
  97. void operator ()(argument_type arg) const
  98. {
  99. static_cast<void>(m_func(arg));
  100. }
  101. private:
  102. adapted_function_type m_func;
  103. };
  104. /** \brief Adapts a biary function into a unary procedure - one in which the return
  105. * type of the function call operator is void
  106. *
  107. * \ingroup group__library__functional
  108. */
  109. // [[synesis:class:function-class:unary-function: binary_procedure_adaptor<T<F>>]]
  110. template <ss_typename_param_k F>
  111. struct binary_procedure_adaptor
  112. : public std::binary_function< ss_typename_type_k F::first_argument_type
  113. , ss_typename_type_k F::second_argument_type
  114. , void>
  115. {
  116. private:
  117. typedef F adapted_function_type;
  118. public:
  119. typedef ss_typename_type_k F::first_argument_type first_argument_type;
  120. typedef ss_typename_type_k F::second_argument_type second_argument_type;
  121. typedef void result_type;
  122. public:
  123. binary_procedure_adaptor(adapted_function_type func)
  124. : m_func(func)
  125. {}
  126. public:
  127. void operator ()(first_argument_type arg1, second_argument_type arg2) const
  128. {
  129. static_cast<void>(m_func(arg1, arg2));
  130. }
  131. private:
  132. adapted_function_type m_func;
  133. };
  134. /* /////////////////////////////////////////////////////////////////////////
  135. * Creator functions
  136. */
  137. /** \brief Creator function for the unary_procedure_adaptor
  138. *
  139. * \ingroup group__library__functional
  140. */
  141. template <ss_typename_param_k F>
  142. inline unary_procedure_adaptor<F> adapt_unary_procedure(F func)
  143. {
  144. return unary_procedure_adaptor<F>(func);
  145. }
  146. /** \brief Creator function for the binary_procedure_adaptor
  147. *
  148. * \ingroup group__library__functional
  149. */
  150. template <ss_typename_param_k F>
  151. inline binary_procedure_adaptor<F> adapt_binary_procedure(F func)
  152. {
  153. return binary_procedure_adaptor<F>(func);
  154. }
  155. /* ////////////////////////////////////////////////////////////////////// */
  156. #ifndef _STLSOFT_NO_NAMESPACE
  157. } // namespace stlsoft
  158. #endif /* _STLSOFT_NO_NAMESPACE */
  159. /* ////////////////////////////////////////////////////////////////////// */
  160. #endif /* !STLSOFT_INCL_STLSOFT_FUNCTIONAL_HPP_PROCEDURE_ADAPTORS */
  161. /* ///////////////////////////// end of file //////////////////////////// */