function.hpp 4.5 KB

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