invoke.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright Andrey Semashev 2022.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * https://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file utility/manipulators/invoke.hpp
  9. * \author Andrey Semashev
  10. * \date 27.02.2022
  11. *
  12. * The header contains implementation of a stream manipulator for invoking a user-defined function.
  13. */
  14. #ifndef BOOST_LOG_UTILITY_MANIPULATORS_INVOKE_HPP_INCLUDED_
  15. #define BOOST_LOG_UTILITY_MANIPULATORS_INVOKE_HPP_INCLUDED_
  16. #include <cstddef>
  17. #include <boost/core/enable_if.hpp>
  18. #include <boost/log/detail/config.hpp>
  19. #include <boost/type_traits/remove_cv.hpp>
  20. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  21. #include <boost/type_traits/remove_reference.hpp>
  22. #endif
  23. #include <boost/log/detail/is_ostream.hpp>
  24. #include <boost/log/detail/header.hpp>
  25. #ifdef BOOST_HAS_PRAGMA_ONCE
  26. #pragma once
  27. #endif
  28. namespace boost {
  29. BOOST_LOG_OPEN_NAMESPACE
  30. /*!
  31. * Stream manipulator for invoking a user-defined function as part of stream output.
  32. */
  33. template< typename FunctionT >
  34. class invoke_manipulator
  35. {
  36. private:
  37. mutable FunctionT m_function;
  38. public:
  39. //! Initializing constructor
  40. explicit invoke_manipulator(FunctionT const& func) :
  41. m_function(func)
  42. {
  43. }
  44. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  45. //! Initializing constructor
  46. explicit invoke_manipulator(FunctionT&& func) :
  47. m_function(static_cast< FunctionT&& >(func))
  48. {
  49. }
  50. #endif
  51. //! The method invokes the saved function with the output stream
  52. template< typename StreamT >
  53. void output(StreamT& stream) const
  54. {
  55. m_function(stream);
  56. }
  57. };
  58. /*!
  59. * Stream output operator for \c invoke_manipulator. Invokes the function saved in the manipulator.
  60. */
  61. template< typename StreamT, typename FunctionT >
  62. inline typename boost::enable_if_c< log::aux::is_ostream< StreamT >::value, StreamT& >::type operator<< (StreamT& stream, invoke_manipulator< FunctionT > const& manip)
  63. {
  64. manip.output(stream);
  65. return stream;
  66. }
  67. #if !defined(BOOST_LOG_DOXYGEN_PASS)
  68. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  69. //! Invoke manipulator generator function
  70. template< typename FunctionT >
  71. inline invoke_manipulator<
  72. typename boost::remove_cv<
  73. typename boost::remove_reference< FunctionT >::type
  74. >::type
  75. >
  76. invoke_manip(FunctionT&& func)
  77. {
  78. return invoke_manipulator<
  79. typename boost::remove_cv<
  80. typename boost::remove_reference< FunctionT >::type
  81. >::type
  82. >(static_cast< FunctionT&& >(func));
  83. }
  84. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && \
  85. !defined(BOOST_NO_CXX14_GENERIC_LAMBDAS) && \
  86. !defined(BOOST_NO_CXX14_RETURN_TYPE_DEDUCTION)
  87. //! Invoke manipulator generator function
  88. template< typename FunctionT, typename Arg0, typename... Args >
  89. inline auto invoke_manip(FunctionT&& func, Arg0&& arg0, Args&&... args)
  90. {
  91. return boost::log::invoke_manip
  92. (
  93. #if !defined(BOOST_LOG_NO_CXX20_PACK_EXPANSION_IN_LAMBDA_INIT_CAPTURE)
  94. [func = static_cast< FunctionT&& >(func), arg0 = static_cast< Arg0&& >(arg0), ...args = static_cast< Args&& >(args)](auto& stream) mutable
  95. #else
  96. [func, arg0, args...](auto& stream) mutable
  97. #endif
  98. {
  99. #if !defined(BOOST_MSVC) || BOOST_MSVC >= 1910
  100. static_cast< FunctionT&& >(func)(stream, static_cast< Arg0&& >(arg0), static_cast< Args&& >(args)...);
  101. #else
  102. // MSVC 19.0 (VS 14.0) ICEs if we use perfect forwarding here
  103. func(stream, arg0, args...);
  104. #endif
  105. }
  106. );
  107. }
  108. #endif // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) ...
  109. #else // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  110. //! Invoke manipulator generator function
  111. template< typename FunctionT >
  112. inline invoke_manipulator< typename boost::remove_cv< FunctionT >::type >
  113. invoke_manip(FunctionT const& func)
  114. {
  115. return invoke_manipulator< typename boost::remove_cv< FunctionT >::type >(func);
  116. }
  117. #endif // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  118. #else // !defined(BOOST_LOG_DOXYGEN_PASS)
  119. /*!
  120. * Invoke manipulator generator function.
  121. *
  122. * \param func User-defined function to invoke on output. The function must be callable with a reference to the output stream as the first argument, followed by \a args.
  123. * \param args Additional arguments to pass to \a func.
  124. * \returns Manipulator to be inserted into the stream.
  125. *
  126. * \note \a args are only supported since C++14.
  127. */
  128. template< typename FunctionT, typename... Args >
  129. invoke_manipulator< unspecified > invoke_manip(FunctionT&& func, Args&&... args);
  130. #endif // !defined(BOOST_LOG_DOXYGEN_PASS)
  131. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  132. } // namespace boost
  133. #include <boost/log/detail/footer.hpp>
  134. #endif // BOOST_LOG_UTILITY_MANIPULATORS_INVOKE_HPP_INCLUDED_