function.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 function.hpp
  9. * \author Andrey Semashev
  10. * \date 24.06.2007
  11. *
  12. * The header contains implementation of an attribute that calls a third-party function on value acquisition.
  13. */
  14. #ifndef BOOST_LOG_ATTRIBUTES_FUNCTION_HPP_INCLUDED_
  15. #define BOOST_LOG_ATTRIBUTES_FUNCTION_HPP_INCLUDED_
  16. #include <boost/utility/result_of.hpp>
  17. #include <boost/type_traits/is_void.hpp>
  18. #include <boost/type_traits/remove_cv.hpp>
  19. #include <boost/type_traits/remove_reference.hpp>
  20. #include <boost/log/detail/config.hpp>
  21. #include <boost/log/attributes/attribute.hpp>
  22. #include <boost/log/attributes/attribute_cast.hpp>
  23. #include <boost/log/attributes/attribute_value_impl.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. namespace attributes {
  31. /*!
  32. * \brief A class of an attribute that acquires its value from a third-party function object
  33. *
  34. * The attribute calls a stored nullary function object to acquire each value.
  35. * The result type of the function object is the attribute value type.
  36. *
  37. * It is not recommended to use this class directly. Use \c make_function convenience functions
  38. * to construct the attribute instead.
  39. */
  40. template< typename R >
  41. class function :
  42. public attribute
  43. {
  44. static_assert(!is_void< R >::value, "Boost.Log: Function object return type must not be void");
  45. public:
  46. //! The attribute value type
  47. typedef R value_type;
  48. protected:
  49. //! Base class for factory implementation
  50. class BOOST_LOG_NO_VTABLE BOOST_SYMBOL_VISIBLE impl :
  51. public attribute::impl
  52. {
  53. };
  54. //! Factory implementation
  55. template< typename T >
  56. class impl_template :
  57. public impl
  58. {
  59. private:
  60. //! Functor that returns attribute values
  61. /*!
  62. * \note The constness signifies that the function object should avoid
  63. * modifying its state since it's not protected against concurrent calls.
  64. */
  65. const T m_Functor;
  66. public:
  67. /*!
  68. * Constructor with the stored delegate initialization
  69. */
  70. explicit impl_template(T const& fun) : m_Functor(fun) {}
  71. attribute_value get_value()
  72. {
  73. return attributes::make_attribute_value(m_Functor());
  74. }
  75. };
  76. public:
  77. /*!
  78. * Initializing constructor
  79. */
  80. template< typename T >
  81. explicit function(T const& fun) : attribute(new impl_template< T >(fun))
  82. {
  83. }
  84. /*!
  85. * Constructor for casting support
  86. */
  87. explicit function(cast_source const& source) :
  88. attribute(source.as< impl >())
  89. {
  90. }
  91. };
  92. #ifndef BOOST_NO_RESULT_OF
  93. /*!
  94. * The function constructs \c function attribute instance with the provided function object.
  95. *
  96. * \param fun Nullary functional object that returns an actual stored value for an attribute value.
  97. * \return Pointer to the attribute instance
  98. */
  99. template< typename T >
  100. inline function<
  101. typename remove_cv<
  102. typename remove_reference<
  103. typename boost::result_of< T() >::type
  104. >::type
  105. >::type
  106. > make_function(T const& fun)
  107. {
  108. typedef typename remove_cv<
  109. typename remove_reference<
  110. typename boost::result_of< T() >::type
  111. >::type
  112. >::type result_type;
  113. typedef function< result_type > function_type;
  114. return function_type(fun);
  115. }
  116. #endif // BOOST_NO_RESULT_OF
  117. #ifndef BOOST_LOG_DOXYGEN_PASS
  118. /*!
  119. * The function constructs \c function attribute instance with the provided function object.
  120. * Use this version if your compiler fails to determine the result type of the function object.
  121. *
  122. * \param fun Nullary functional object that returns an actual stored value for an attribute value.
  123. * \return Pointer to the attribute instance
  124. */
  125. template< typename R, typename T >
  126. inline function<
  127. typename remove_cv<
  128. typename remove_reference< R >::type
  129. >::type
  130. > make_function(T const& fun)
  131. {
  132. typedef typename remove_cv<
  133. typename remove_reference< R >::type
  134. >::type result_type;
  135. typedef function< result_type > function_type;
  136. return function_type(fun);
  137. }
  138. #endif // BOOST_LOG_DOXYGEN_PASS
  139. } // namespace attributes
  140. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  141. } // namespace boost
  142. #include <boost/log/detail/footer.hpp>
  143. #endif // BOOST_LOG_ATTRIBUTES_FUNCTOR_HPP_INCLUDED_