2 * Copyright Andrey Semashev 2007 - 2014.
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)
9 * \author Andrey Semashev
12 * The header contains implementation of an attribute that calls a third-party function on value acquisition.
15 #ifndef BOOST_LOG_ATTRIBUTES_FUNCTION_HPP_INCLUDED_
16 #define BOOST_LOG_ATTRIBUTES_FUNCTION_HPP_INCLUDED_
18 #include <boost/static_assert.hpp>
19 #include <boost/utility/result_of.hpp>
20 #include <boost/type_traits/is_void.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/attributes/attribute.hpp>
25 #include <boost/log/attributes/attribute_cast.hpp>
26 #include <boost/log/attributes/attribute_value_impl.hpp>
27 #include <boost/log/detail/header.hpp>
29 #ifdef BOOST_HAS_PRAGMA_ONCE
35 BOOST_LOG_OPEN_NAMESPACE
37 namespace attributes {
40 * \brief A class of an attribute that acquires its value from a third-party function object
42 * The attribute calls a stored nullary function object to acquire each value.
43 * The result type of the function object is the attribute value type.
45 * It is not recommended to use this class directly. Use \c make_function convenience functions
46 * to construct the attribute instead.
48 template< typename R >
52 BOOST_STATIC_ASSERT_MSG(!is_void< R >::value, "Boost.Log: Function object return type must not be void");
55 //! The attribute value type
59 //! Base class for factory implementation
60 class BOOST_LOG_NO_VTABLE BOOST_SYMBOL_VISIBLE impl :
61 public attribute::impl
65 //! Factory implementation
66 template< typename T >
71 //! Functor that returns attribute values
73 * \note The constness signifies that the function object should avoid
74 * modifying its state since it's not protected against concurrent calls.
80 * Constructor with the stored delegate initialization
82 explicit impl_template(T const& fun) : m_Functor(fun) {}
84 attribute_value get_value()
86 return attributes::make_attribute_value(m_Functor());
92 * Initializing constructor
94 template< typename T >
95 explicit function(T const& fun) : attribute(new impl_template< T >(fun))
99 * Constructor for casting support
101 explicit function(cast_source const& source) :
102 attribute(source.as< impl >())
107 #ifndef BOOST_NO_RESULT_OF
110 * The function constructs \c function attribute instance with the provided function object.
112 * \param fun Nullary functional object that returns an actual stored value for an attribute value.
113 * \return Pointer to the attribute instance
115 template< typename T >
118 typename remove_reference<
119 typename boost::result_of< T() >::type
122 > make_function(T const& fun)
124 typedef typename remove_cv<
125 typename remove_reference<
126 typename boost::result_of< T() >::type
130 typedef function< result_type > function_type;
131 return function_type(fun);
134 #endif // BOOST_NO_RESULT_OF
136 #ifndef BOOST_LOG_DOXYGEN_PASS
139 * The function constructs \c function attribute instance with the provided function object.
140 * Use this version if your compiler fails to determine the result type of the function object.
142 * \param fun Nullary functional object that returns an actual stored value for an attribute value.
143 * \return Pointer to the attribute instance
145 template< typename R, typename T >
148 typename remove_reference< R >::type
150 > make_function(T const& fun)
152 typedef typename remove_cv<
153 typename remove_reference< R >::type
156 typedef function< result_type > function_type;
157 return function_type(fun);
160 #endif // BOOST_LOG_DOXYGEN_PASS
162 } // namespace attributes
164 BOOST_LOG_CLOSE_NAMESPACE // namespace log
168 #include <boost/log/detail/footer.hpp>
170 #endif // BOOST_LOG_ATTRIBUTES_FUNCTOR_HPP_INCLUDED_