unary_function_terminal.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2015.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file unary_function_terminal.hpp
  9. * \author Andrey Semashev
  10. * \date 21.07.2012
  11. *
  12. * The header contains attribute value extractor adapter for constructing expression template terminals.
  13. */
  14. #ifndef BOOST_LOG_DETAIL_UNARY_FUNCTION_TERMINAL_HPP_INCLUDED_
  15. #define BOOST_LOG_DETAIL_UNARY_FUNCTION_TERMINAL_HPP_INCLUDED_
  16. #include <boost/mpl/bool.hpp>
  17. #include <boost/utility/result_of.hpp>
  18. #include <boost/fusion/sequence/intrinsic/at_c.hpp>
  19. #include <boost/phoenix/core/is_nullary.hpp>
  20. #include <boost/phoenix/core/environment.hpp>
  21. #include <boost/type_traits/remove_cv.hpp>
  22. #include <boost/type_traits/remove_reference.hpp>
  23. #include <boost/log/detail/config.hpp>
  24. #include <boost/log/detail/copy_cv.hpp>
  25. #include <boost/log/detail/custom_terminal_spec.hpp>
  26. #include <boost/log/detail/header.hpp>
  27. #ifdef BOOST_HAS_PRAGMA_ONCE
  28. #pragma once
  29. #endif
  30. namespace boost {
  31. BOOST_LOG_OPEN_NAMESPACE
  32. namespace expressions {
  33. namespace aux {
  34. /*!
  35. * \brief An adapter for a unary function to be used as a terminal in a Boost.Phoenix expression
  36. *
  37. * This class is an adapter between Boost.Phoenix expression invocation protocol and
  38. * a unary function. It forwards the call to the base function, passing only the first argument
  39. * from the original call. This allows to embed value extractors in template expressions.
  40. */
  41. template< typename FunT >
  42. class unary_function_terminal
  43. {
  44. private:
  45. //! Adopted function type
  46. typedef FunT function_type;
  47. //! Self type
  48. typedef unary_function_terminal< function_type > this_type;
  49. public:
  50. //! Internal typedef for type categorization
  51. typedef void _is_boost_log_terminal;
  52. //! Function result type
  53. template< typename >
  54. struct result;
  55. template< typename ThisT, typename ContextT >
  56. struct result< ThisT(ContextT) >
  57. {
  58. typedef typename remove_cv<
  59. typename remove_reference< typename phoenix::result_of::env< ContextT >::type >::type
  60. >::type env_type;
  61. typedef typename env_type::args_type args_type;
  62. typedef typename boost::log::aux::copy_cv< ThisT, function_type >::type cv_function_type;
  63. typedef typename boost::result_of< cv_function_type(typename fusion::result_of::at_c< args_type, 0 >::type) >::type type;
  64. };
  65. private:
  66. //! Adopted function
  67. function_type m_fun;
  68. public:
  69. //! Default constructor
  70. BOOST_DEFAULTED_FUNCTION(unary_function_terminal(), {})
  71. //! Copy constructor
  72. unary_function_terminal(unary_function_terminal const& that) : m_fun(that.m_fun) {}
  73. //! Initializing constructor
  74. template< typename ArgT1 >
  75. explicit unary_function_terminal(ArgT1 const& arg1) : m_fun(arg1) {}
  76. //! Initializing constructor
  77. template< typename ArgT1, typename ArgT2 >
  78. unary_function_terminal(ArgT1 const& arg1, ArgT2 const& arg2) : m_fun(arg1, arg2) {}
  79. //! Initializing constructor
  80. template< typename ArgT1, typename ArgT2, typename ArgT3 >
  81. unary_function_terminal(ArgT1 const& arg1, ArgT2 const& arg2, ArgT3 const& arg3) : m_fun(arg1, arg2, arg3) {}
  82. //! The operator forwards the call to the base function
  83. template< typename ContextT >
  84. typename result< this_type(ContextT const&) >::type
  85. operator() (ContextT const& ctx)
  86. {
  87. return m_fun(fusion::at_c< 0 >(phoenix::env(ctx).args()));
  88. }
  89. //! The operator forwards the call to the base function
  90. template< typename ContextT >
  91. typename result< const this_type(ContextT const&) >::type
  92. operator() (ContextT const& ctx) const
  93. {
  94. return m_fun(fusion::at_c< 0 >(phoenix::env(ctx).args()));
  95. }
  96. };
  97. } // namespace aux
  98. } // namespace expressions
  99. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  100. #ifndef BOOST_LOG_DOXYGEN_PASS
  101. namespace phoenix {
  102. namespace result_of {
  103. template< typename FunT >
  104. struct is_nullary< custom_terminal< boost::log::expressions::aux::unary_function_terminal< FunT > > > :
  105. public mpl::false_
  106. {
  107. };
  108. } // namespace result_of
  109. } // namespace phoenix
  110. #endif // BOOST_LOG_DOXYGEN_PASS
  111. } // namespace boost
  112. #include <boost/log/detail/footer.hpp>
  113. #endif // BOOST_LOG_DETAIL_UNARY_FUNCTION_TERMINAL_HPP_INCLUDED_