unary_function_terminal.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2013.
  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/custom_terminal_spec.hpp>
  25. #include <boost/log/detail/header.hpp>
  26. #ifdef BOOST_HAS_PRAGMA_ONCE
  27. #pragma once
  28. #endif
  29. namespace boost {
  30. BOOST_LOG_OPEN_NAMESPACE
  31. namespace expressions {
  32. namespace aux {
  33. /*!
  34. * \brief An adapter for a unary function to be used as a terminal in a Boost.Phoenix expression
  35. *
  36. * This class is an adapter between Boost.Phoenix expression invocation protocol and
  37. * a unary function. It forwards the call to the base function, passing only the first argument
  38. * from the original call. This allows to embed value extractors in template expressions.
  39. */
  40. template< typename FunT >
  41. class unary_function_terminal
  42. {
  43. private:
  44. //! Adopted function type
  45. typedef FunT function_type;
  46. //! Self type
  47. typedef unary_function_terminal< function_type > this_type;
  48. public:
  49. //! Internal typedef for type categorization
  50. typedef void _is_boost_log_terminal;
  51. //! Function result type
  52. template< typename >
  53. struct result;
  54. template< typename ContextT >
  55. struct result< this_type(ContextT) >
  56. {
  57. typedef typename remove_cv<
  58. typename remove_reference< typename phoenix::result_of::env< ContextT >::type >::type
  59. >::type env_type;
  60. typedef typename env_type::args_type args_type;
  61. typedef typename boost::result_of< function_type(typename fusion::result_of::at_c< args_type, 0 >::type) >::type type;
  62. };
  63. template< typename ContextT >
  64. struct result< const this_type(ContextT) >
  65. {
  66. typedef typename remove_cv<
  67. typename remove_reference< typename phoenix::result_of::env< ContextT >::type >::type
  68. >::type env_type;
  69. typedef typename env_type::args_type args_type;
  70. typedef typename boost::result_of< const function_type(typename fusion::result_of::at_c< args_type, 0 >::type) >::type type;
  71. };
  72. private:
  73. //! Adopted function
  74. function_type m_fun;
  75. public:
  76. //! Default constructor
  77. BOOST_DEFAULTED_FUNCTION(unary_function_terminal(), {})
  78. //! Copy constructor
  79. unary_function_terminal(unary_function_terminal const& that) : m_fun(that.m_fun) {}
  80. //! Initializing constructor
  81. template< typename ArgT1 >
  82. explicit unary_function_terminal(ArgT1 const& arg1) : m_fun(arg1) {}
  83. //! Initializing constructor
  84. template< typename ArgT1, typename ArgT2 >
  85. unary_function_terminal(ArgT1 const& arg1, ArgT2 const& arg2) : m_fun(arg1, arg2) {}
  86. //! Initializing constructor
  87. template< typename ArgT1, typename ArgT2, typename ArgT3 >
  88. unary_function_terminal(ArgT1 const& arg1, ArgT2 const& arg2, ArgT3 const& arg3) : m_fun(arg1, arg2, arg3) {}
  89. //! The operator forwards the call to the base function
  90. template< typename ContextT >
  91. typename result< this_type(ContextT const&) >::type
  92. operator() (ContextT const& ctx)
  93. {
  94. return m_fun(fusion::at_c< 0 >(phoenix::env(ctx).args()));
  95. }
  96. //! The operator forwards the call to the base function
  97. template< typename ContextT >
  98. typename result< const this_type(ContextT const&) >::type
  99. operator() (ContextT const& ctx) const
  100. {
  101. return m_fun(fusion::at_c< 0 >(phoenix::env(ctx).args()));
  102. }
  103. };
  104. } // namespace aux
  105. } // namespace expressions
  106. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  107. #ifndef BOOST_LOG_DOXYGEN_PASS
  108. namespace phoenix {
  109. namespace result_of {
  110. template< typename FunT >
  111. struct is_nullary< custom_terminal< boost::log::expressions::aux::unary_function_terminal< FunT > > > :
  112. public mpl::false_
  113. {
  114. };
  115. } // namespace result_of
  116. } // namespace phoenix
  117. #endif // BOOST_LOG_DOXYGEN_PASS
  118. } // namespace boost
  119. #include <boost/log/detail/footer.hpp>
  120. #endif // BOOST_LOG_DETAIL_UNARY_FUNCTION_TERMINAL_HPP_INCLUDED_