Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:39:19

0001 /*
0002  *          Copyright Andrey Semashev 2007 - 2015.
0003  * Distributed under the Boost Software License, Version 1.0.
0004  *    (See accompanying file LICENSE_1_0.txt or copy at
0005  *          http://www.boost.org/LICENSE_1_0.txt)
0006  */
0007 /*!
0008  * \file   function.hpp
0009  * \author Andrey Semashev
0010  * \date   24.06.2007
0011  *
0012  * The header contains implementation of an attribute that calls a third-party function on value acquisition.
0013  */
0014 
0015 #ifndef BOOST_LOG_ATTRIBUTES_FUNCTION_HPP_INCLUDED_
0016 #define BOOST_LOG_ATTRIBUTES_FUNCTION_HPP_INCLUDED_
0017 
0018 #include <boost/utility/result_of.hpp>
0019 #include <boost/type_traits/is_void.hpp>
0020 #include <boost/type_traits/remove_cv.hpp>
0021 #include <boost/type_traits/remove_reference.hpp>
0022 #include <boost/log/detail/config.hpp>
0023 #include <boost/log/attributes/attribute.hpp>
0024 #include <boost/log/attributes/attribute_cast.hpp>
0025 #include <boost/log/attributes/attribute_value_impl.hpp>
0026 #include <boost/log/detail/header.hpp>
0027 
0028 #ifdef BOOST_HAS_PRAGMA_ONCE
0029 #pragma once
0030 #endif
0031 
0032 namespace boost {
0033 
0034 BOOST_LOG_OPEN_NAMESPACE
0035 
0036 namespace attributes {
0037 
0038 /*!
0039  * \brief A class of an attribute that acquires its value from a third-party function object
0040  *
0041  * The attribute calls a stored nullary function object to acquire each value.
0042  * The result type of the function object is the attribute value type.
0043  *
0044  * It is not recommended to use this class directly. Use \c make_function convenience functions
0045  * to construct the attribute instead.
0046  */
0047 template< typename R >
0048 class function :
0049     public attribute
0050 {
0051     static_assert(!is_void< R >::value, "Boost.Log: Function object return type must not be void");
0052 
0053 public:
0054     //! The attribute value type
0055     typedef R value_type;
0056 
0057 protected:
0058     //! Base class for factory implementation
0059     class BOOST_LOG_NO_VTABLE BOOST_SYMBOL_VISIBLE impl :
0060         public attribute::impl
0061     {
0062     };
0063 
0064     //! Factory implementation
0065     template< typename T >
0066     class impl_template :
0067         public impl
0068     {
0069     private:
0070         //! Functor that returns attribute values
0071         /*!
0072          * \note The constness signifies that the function object should avoid
0073          *       modifying its state since it's not protected against concurrent calls.
0074          */
0075         const T m_Functor;
0076 
0077     public:
0078         /*!
0079          * Constructor with the stored delegate initialization
0080          */
0081         explicit impl_template(T const& fun) : m_Functor(fun) {}
0082 
0083         attribute_value get_value()
0084         {
0085             return attributes::make_attribute_value(m_Functor());
0086         }
0087     };
0088 
0089 public:
0090     /*!
0091      * Initializing constructor
0092      */
0093     template< typename T >
0094     explicit function(T const& fun) : attribute(new impl_template< T >(fun))
0095     {
0096     }
0097     /*!
0098      * Constructor for casting support
0099      */
0100     explicit function(cast_source const& source) :
0101         attribute(source.as< impl >())
0102     {
0103     }
0104 };
0105 
0106 #ifndef BOOST_NO_RESULT_OF
0107 
0108 /*!
0109  * The function constructs \c function attribute instance with the provided function object.
0110  *
0111  * \param fun Nullary functional object that returns an actual stored value for an attribute value.
0112  * \return Pointer to the attribute instance
0113  */
0114 template< typename T >
0115 inline function<
0116     typename remove_cv<
0117         typename remove_reference<
0118             typename boost::result_of< T() >::type
0119         >::type
0120     >::type
0121 > make_function(T const& fun)
0122 {
0123     typedef typename remove_cv<
0124         typename remove_reference<
0125             typename boost::result_of< T() >::type
0126         >::type
0127     >::type result_type;
0128 
0129     typedef function< result_type > function_type;
0130     return function_type(fun);
0131 }
0132 
0133 #endif // BOOST_NO_RESULT_OF
0134 
0135 #ifndef BOOST_LOG_DOXYGEN_PASS
0136 
0137 /*!
0138  * The function constructs \c function attribute instance with the provided function object.
0139  * Use this version if your compiler fails to determine the result type of the function object.
0140  *
0141  * \param fun Nullary functional object that returns an actual stored value for an attribute value.
0142  * \return Pointer to the attribute instance
0143  */
0144 template< typename R, typename T >
0145 inline function<
0146     typename remove_cv<
0147         typename remove_reference< R >::type
0148     >::type
0149 > make_function(T const& fun)
0150 {
0151     typedef typename remove_cv<
0152         typename remove_reference< R >::type
0153     >::type result_type;
0154 
0155     typedef function< result_type > function_type;
0156     return function_type(fun);
0157 }
0158 
0159 #endif // BOOST_LOG_DOXYGEN_PASS
0160 
0161 } // namespace attributes
0162 
0163 BOOST_LOG_CLOSE_NAMESPACE // namespace log
0164 
0165 } // namespace boost
0166 
0167 #include <boost/log/detail/footer.hpp>
0168 
0169 #endif // BOOST_LOG_ATTRIBUTES_FUNCTOR_HPP_INCLUDED_