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 15 #ifndef BOOST_LOG_ATTRIBUTES_FUNCTION_HPP_INCLUDED_ 16 #define BOOST_LOG_ATTRIBUTES_FUNCTION_HPP_INCLUDED_ 17 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> 28 29 #ifdef BOOST_HAS_PRAGMA_ONCE 30 #pragma once 31 #endif 32 33 namespace boost { 34 35 BOOST_LOG_OPEN_NAMESPACE 36 37 namespace attributes { 38 39 /*! 40 * \brief A class of an attribute that acquires its value from a third-party function object 41 * 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. 44 * 45 * It is not recommended to use this class directly. Use \c make_function convenience functions 46 * to construct the attribute instead. 47 */ 48 template< typename R > 49 class function : 50 public attribute 51 { 52 BOOST_STATIC_ASSERT_MSG(!is_void< R >::value, "Boost.Log: Function object return type must not be void"); 53 54 public: 55 //! The attribute value type 56 typedef R value_type; 57 58 protected: 59 //! Base class for factory implementation 60 class BOOST_LOG_NO_VTABLE BOOST_SYMBOL_VISIBLE impl : 61 public attribute::impl 62 { 63 }; 64 65 //! Factory implementation 66 template< typename T > 67 class impl_template : 68 public impl 69 { 70 private: 71 //! Functor that returns attribute values 72 /*! 73 * \note The constness signifies that the function object should avoid 74 * modifying its state since it's not protected against concurrent calls. 75 */ 76 const T m_Functor; 77 78 public: 79 /*! 80 * Constructor with the stored delegate initialization 81 */ impl_template(T const & fun)82 explicit impl_template(T const& fun) : m_Functor(fun) {} 83 get_value()84 attribute_value get_value() 85 { 86 return attributes::make_attribute_value(m_Functor()); 87 } 88 }; 89 90 public: 91 /*! 92 * Initializing constructor 93 */ 94 template< typename T > function(T const & fun)95 explicit function(T const& fun) : attribute(new impl_template< T >(fun)) 96 { 97 } 98 /*! 99 * Constructor for casting support 100 */ function(cast_source const & source)101 explicit function(cast_source const& source) : 102 attribute(source.as< impl >()) 103 { 104 } 105 }; 106 107 #ifndef BOOST_NO_RESULT_OF 108 109 /*! 110 * The function constructs \c function attribute instance with the provided function object. 111 * 112 * \param fun Nullary functional object that returns an actual stored value for an attribute value. 113 * \return Pointer to the attribute instance 114 */ 115 template< typename T > 116 inline function< 117 typename remove_cv< 118 typename remove_reference< 119 typename boost::result_of< T() >::type 120 >::type 121 >::type make_function(T const & fun)122> make_function(T const& fun) 123 { 124 typedef typename remove_cv< 125 typename remove_reference< 126 typename boost::result_of< T() >::type 127 >::type 128 >::type result_type; 129 130 typedef function< result_type > function_type; 131 return function_type(fun); 132 } 133 134 #endif // BOOST_NO_RESULT_OF 135 136 #ifndef BOOST_LOG_DOXYGEN_PASS 137 138 /*! 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. 141 * 142 * \param fun Nullary functional object that returns an actual stored value for an attribute value. 143 * \return Pointer to the attribute instance 144 */ 145 template< typename R, typename T > 146 inline function< 147 typename remove_cv< 148 typename remove_reference< R >::type 149 >::type make_function(T const & fun)150> make_function(T const& fun) 151 { 152 typedef typename remove_cv< 153 typename remove_reference< R >::type 154 >::type result_type; 155 156 typedef function< result_type > function_type; 157 return function_type(fun); 158 } 159 160 #endif // BOOST_LOG_DOXYGEN_PASS 161 162 } // namespace attributes 163 164 BOOST_LOG_CLOSE_NAMESPACE // namespace log 165 166 } // namespace boost 167 168 #include <boost/log/detail/footer.hpp> 169 170 #endif // BOOST_LOG_ATTRIBUTES_FUNCTOR_HPP_INCLUDED_ 171