Chris@16: /* Chris@101: * Copyright Andrey Semashev 2007 - 2015. Chris@16: * Distributed under the Boost Software License, Version 1.0. Chris@16: * (See accompanying file LICENSE_1_0.txt or copy at Chris@16: * http://www.boost.org/LICENSE_1_0.txt) Chris@16: */ Chris@16: /*! Chris@16: * \file function.hpp Chris@16: * \author Andrey Semashev Chris@16: * \date 24.06.2007 Chris@16: * Chris@16: * The header contains implementation of an attribute that calls a third-party function on value acquisition. Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_LOG_ATTRIBUTES_FUNCTION_HPP_INCLUDED_ Chris@16: #define BOOST_LOG_ATTRIBUTES_FUNCTION_HPP_INCLUDED_ Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #ifdef BOOST_HAS_PRAGMA_ONCE Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: BOOST_LOG_OPEN_NAMESPACE Chris@16: Chris@16: namespace attributes { Chris@16: Chris@16: /*! Chris@16: * \brief A class of an attribute that acquires its value from a third-party function object Chris@16: * Chris@16: * The attribute calls a stored nullary function object to acquire each value. Chris@16: * The result type of the function object is the attribute value type. Chris@16: * Chris@16: * It is not recommended to use this class directly. Use \c make_function convenience functions Chris@16: * to construct the attribute instead. Chris@16: */ Chris@16: template< typename R > Chris@16: class function : Chris@16: public attribute Chris@16: { Chris@16: BOOST_STATIC_ASSERT_MSG(!is_void< R >::value, "Boost.Log: Function object return type must not be void"); Chris@16: Chris@16: public: Chris@16: //! The attribute value type Chris@16: typedef R value_type; Chris@16: Chris@16: protected: Chris@16: //! Base class for factory implementation Chris@16: class BOOST_LOG_NO_VTABLE BOOST_SYMBOL_VISIBLE impl : Chris@16: public attribute::impl Chris@16: { Chris@16: }; Chris@16: Chris@16: //! Factory implementation Chris@16: template< typename T > Chris@16: class impl_template : Chris@16: public impl Chris@16: { Chris@16: private: Chris@16: //! Functor that returns attribute values Chris@16: /*! Chris@16: * \note The constness signifies that the function object should avoid Chris@16: * modifying its state since it's not protected against concurrent calls. Chris@16: */ Chris@16: const T m_Functor; Chris@16: Chris@16: public: Chris@16: /*! Chris@16: * Constructor with the stored delegate initialization Chris@16: */ Chris@16: explicit impl_template(T const& fun) : m_Functor(fun) {} Chris@16: Chris@16: attribute_value get_value() Chris@16: { Chris@16: return attributes::make_attribute_value(m_Functor()); Chris@16: } Chris@16: }; Chris@16: Chris@16: public: Chris@16: /*! Chris@16: * Initializing constructor Chris@16: */ Chris@16: template< typename T > Chris@16: explicit function(T const& fun) : attribute(new impl_template< T >(fun)) Chris@16: { Chris@16: } Chris@16: /*! Chris@16: * Constructor for casting support Chris@16: */ Chris@16: explicit function(cast_source const& source) : Chris@16: attribute(source.as< impl >()) Chris@16: { Chris@16: } Chris@16: }; Chris@16: Chris@16: #ifndef BOOST_NO_RESULT_OF Chris@16: Chris@16: /*! Chris@16: * The function constructs \c function attribute instance with the provided function object. Chris@16: * Chris@16: * \param fun Nullary functional object that returns an actual stored value for an attribute value. Chris@16: * \return Pointer to the attribute instance Chris@16: */ Chris@16: template< typename T > Chris@16: inline function< Chris@16: typename remove_cv< Chris@16: typename remove_reference< Chris@16: typename boost::result_of< T() >::type Chris@16: >::type Chris@16: >::type Chris@16: > make_function(T const& fun) Chris@16: { Chris@16: typedef typename remove_cv< Chris@16: typename remove_reference< Chris@16: typename boost::result_of< T() >::type Chris@16: >::type Chris@16: >::type result_type; Chris@16: Chris@16: typedef function< result_type > function_type; Chris@16: return function_type(fun); Chris@16: } Chris@16: Chris@16: #endif // BOOST_NO_RESULT_OF Chris@16: Chris@16: #ifndef BOOST_LOG_DOXYGEN_PASS Chris@16: Chris@16: /*! Chris@16: * The function constructs \c function attribute instance with the provided function object. Chris@16: * Use this version if your compiler fails to determine the result type of the function object. Chris@16: * Chris@16: * \param fun Nullary functional object that returns an actual stored value for an attribute value. Chris@16: * \return Pointer to the attribute instance Chris@16: */ Chris@16: template< typename R, typename T > Chris@16: inline function< Chris@16: typename remove_cv< Chris@16: typename remove_reference< R >::type Chris@16: >::type Chris@16: > make_function(T const& fun) Chris@16: { Chris@16: typedef typename remove_cv< Chris@16: typename remove_reference< R >::type Chris@16: >::type result_type; Chris@16: Chris@16: typedef function< result_type > function_type; Chris@16: return function_type(fun); Chris@16: } Chris@16: Chris@16: #endif // BOOST_LOG_DOXYGEN_PASS Chris@16: Chris@16: } // namespace attributes Chris@16: Chris@16: BOOST_LOG_CLOSE_NAMESPACE // namespace log Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_LOG_ATTRIBUTES_FUNCTOR_HPP_INCLUDED_