Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:39:27

0001 /*
0002  *             Copyright Andrey Semashev 2022.
0003  * Distributed under the Boost Software License, Version 1.0.
0004  *    (See accompanying file LICENSE_1_0.txt or copy at
0005  *          https://www.boost.org/LICENSE_1_0.txt)
0006  */
0007 /*!
0008  * \file   utility/manipulators/invoke.hpp
0009  * \author Andrey Semashev
0010  * \date   27.02.2022
0011  *
0012  * The header contains implementation of a stream manipulator for invoking a user-defined function.
0013  */
0014 
0015 #ifndef BOOST_LOG_UTILITY_MANIPULATORS_INVOKE_HPP_INCLUDED_
0016 #define BOOST_LOG_UTILITY_MANIPULATORS_INVOKE_HPP_INCLUDED_
0017 
0018 #include <cstddef>
0019 #include <boost/core/enable_if.hpp>
0020 #include <boost/log/detail/config.hpp>
0021 #include <boost/type_traits/remove_cv.hpp>
0022 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
0023 #include <boost/type_traits/remove_reference.hpp>
0024 #endif
0025 #include <boost/log/detail/is_ostream.hpp>
0026 #include <boost/log/detail/header.hpp>
0027 
0028 #ifdef BOOST_HAS_PRAGMA_ONCE
0029 #pragma once
0030 #endif
0031 
0032 namespace boost {
0033 
0034 BOOST_LOG_OPEN_NAMESPACE
0035 
0036 /*!
0037  * Stream manipulator for invoking a user-defined function as part of stream output.
0038  */
0039 template< typename FunctionT >
0040 class invoke_manipulator
0041 {
0042 private:
0043     mutable FunctionT m_function;
0044 
0045 public:
0046     //! Initializing constructor
0047     explicit invoke_manipulator(FunctionT const& func) :
0048         m_function(func)
0049     {
0050     }
0051 
0052 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
0053     //! Initializing constructor
0054     explicit invoke_manipulator(FunctionT&& func) :
0055         m_function(static_cast< FunctionT&& >(func))
0056     {
0057     }
0058 #endif
0059 
0060     //! The method invokes the saved function with the output stream
0061     template< typename StreamT >
0062     void output(StreamT& stream) const
0063     {
0064         m_function(stream);
0065     }
0066 };
0067 
0068 /*!
0069  * Stream output operator for \c invoke_manipulator. Invokes the function saved in the manipulator.
0070  */
0071 template< typename StreamT, typename FunctionT >
0072 inline typename boost::enable_if_c< log::aux::is_ostream< StreamT >::value, StreamT& >::type operator<< (StreamT& stream, invoke_manipulator< FunctionT > const& manip)
0073 {
0074     manip.output(stream);
0075     return stream;
0076 }
0077 
0078 #if !defined(BOOST_LOG_DOXYGEN_PASS)
0079 
0080 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
0081 
0082 //! Invoke manipulator generator function
0083 template< typename FunctionT >
0084 inline invoke_manipulator<
0085     typename boost::remove_cv<
0086         typename boost::remove_reference< FunctionT >::type
0087     >::type
0088 >
0089 invoke_manip(FunctionT&& func)
0090 {
0091     return invoke_manipulator<
0092         typename boost::remove_cv<
0093             typename boost::remove_reference< FunctionT >::type
0094         >::type
0095     >(static_cast< FunctionT&& >(func));
0096 }
0097 
0098 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && \
0099     !defined(BOOST_NO_CXX14_GENERIC_LAMBDAS) && \
0100     !defined(BOOST_NO_CXX14_RETURN_TYPE_DEDUCTION)
0101 
0102 //! Invoke manipulator generator function
0103 template< typename FunctionT, typename Arg0, typename... Args >
0104 inline auto invoke_manip(FunctionT&& func, Arg0&& arg0, Args&&... args)
0105 {
0106     return boost::log::invoke_manip
0107     (
0108 #if !defined(BOOST_LOG_NO_CXX20_PACK_EXPANSION_IN_LAMBDA_INIT_CAPTURE)
0109         [func = static_cast< FunctionT&& >(func), arg0 = static_cast< Arg0&& >(arg0), ...args = static_cast< Args&& >(args)](auto& stream) mutable
0110 #else
0111         [func, arg0, args...](auto& stream) mutable
0112 #endif
0113         {
0114 #if !defined(BOOST_MSVC) || BOOST_MSVC >= 1910
0115             static_cast< FunctionT&& >(func)(stream, static_cast< Arg0&& >(arg0), static_cast< Args&& >(args)...);
0116 #else
0117             // MSVC 19.0 (VS 14.0) ICEs if we use perfect forwarding here
0118             func(stream, arg0, args...);
0119 #endif
0120         }
0121     );
0122 }
0123 
0124 #endif // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) ...
0125 
0126 #else // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
0127 
0128 //! Invoke manipulator generator function
0129 template< typename FunctionT >
0130 inline invoke_manipulator< typename boost::remove_cv< FunctionT >::type >
0131 invoke_manip(FunctionT const& func)
0132 {
0133     return invoke_manipulator< typename boost::remove_cv< FunctionT >::type >(func);
0134 }
0135 
0136 #endif // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
0137 
0138 #else // !defined(BOOST_LOG_DOXYGEN_PASS)
0139 
0140 /*!
0141  * Invoke manipulator generator function.
0142  *
0143  * \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.
0144  * \param args Additional arguments to pass to \a func.
0145  * \returns Manipulator to be inserted into the stream.
0146  *
0147  * \note \a args are only supported since C++14.
0148  */
0149 template< typename FunctionT, typename... Args >
0150 invoke_manipulator< unspecified > invoke_manip(FunctionT&& func, Args&&... args);
0151 
0152 #endif // !defined(BOOST_LOG_DOXYGEN_PASS)
0153 
0154 BOOST_LOG_CLOSE_NAMESPACE // namespace log
0155 
0156 } // namespace boost
0157 
0158 #include <boost/log/detail/footer.hpp>
0159 
0160 #endif // BOOST_LOG_UTILITY_MANIPULATORS_INVOKE_HPP_INCLUDED_