Chris@16
|
1 /*
|
Chris@101
|
2 * Copyright Andrey Semashev 2007 - 2015.
|
Chris@16
|
3 * Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
4 * (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
5 * http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6 */
|
Chris@16
|
7 /*!
|
Chris@16
|
8 * \file function.hpp
|
Chris@16
|
9 * \author Andrey Semashev
|
Chris@16
|
10 * \date 24.06.2007
|
Chris@16
|
11 *
|
Chris@16
|
12 * The header contains implementation of an attribute that calls a third-party function on value acquisition.
|
Chris@16
|
13 */
|
Chris@16
|
14
|
Chris@16
|
15 #ifndef BOOST_LOG_ATTRIBUTES_FUNCTION_HPP_INCLUDED_
|
Chris@16
|
16 #define BOOST_LOG_ATTRIBUTES_FUNCTION_HPP_INCLUDED_
|
Chris@16
|
17
|
Chris@16
|
18 #include <boost/static_assert.hpp>
|
Chris@16
|
19 #include <boost/utility/result_of.hpp>
|
Chris@16
|
20 #include <boost/type_traits/is_void.hpp>
|
Chris@16
|
21 #include <boost/type_traits/remove_cv.hpp>
|
Chris@16
|
22 #include <boost/type_traits/remove_reference.hpp>
|
Chris@16
|
23 #include <boost/log/detail/config.hpp>
|
Chris@16
|
24 #include <boost/log/attributes/attribute.hpp>
|
Chris@16
|
25 #include <boost/log/attributes/attribute_cast.hpp>
|
Chris@16
|
26 #include <boost/log/attributes/attribute_value_impl.hpp>
|
Chris@16
|
27 #include <boost/log/detail/header.hpp>
|
Chris@16
|
28
|
Chris@16
|
29 #ifdef BOOST_HAS_PRAGMA_ONCE
|
Chris@16
|
30 #pragma once
|
Chris@16
|
31 #endif
|
Chris@16
|
32
|
Chris@16
|
33 namespace boost {
|
Chris@16
|
34
|
Chris@16
|
35 BOOST_LOG_OPEN_NAMESPACE
|
Chris@16
|
36
|
Chris@16
|
37 namespace attributes {
|
Chris@16
|
38
|
Chris@16
|
39 /*!
|
Chris@16
|
40 * \brief A class of an attribute that acquires its value from a third-party function object
|
Chris@16
|
41 *
|
Chris@16
|
42 * The attribute calls a stored nullary function object to acquire each value.
|
Chris@16
|
43 * The result type of the function object is the attribute value type.
|
Chris@16
|
44 *
|
Chris@16
|
45 * It is not recommended to use this class directly. Use \c make_function convenience functions
|
Chris@16
|
46 * to construct the attribute instead.
|
Chris@16
|
47 */
|
Chris@16
|
48 template< typename R >
|
Chris@16
|
49 class function :
|
Chris@16
|
50 public attribute
|
Chris@16
|
51 {
|
Chris@16
|
52 BOOST_STATIC_ASSERT_MSG(!is_void< R >::value, "Boost.Log: Function object return type must not be void");
|
Chris@16
|
53
|
Chris@16
|
54 public:
|
Chris@16
|
55 //! The attribute value type
|
Chris@16
|
56 typedef R value_type;
|
Chris@16
|
57
|
Chris@16
|
58 protected:
|
Chris@16
|
59 //! Base class for factory implementation
|
Chris@16
|
60 class BOOST_LOG_NO_VTABLE BOOST_SYMBOL_VISIBLE impl :
|
Chris@16
|
61 public attribute::impl
|
Chris@16
|
62 {
|
Chris@16
|
63 };
|
Chris@16
|
64
|
Chris@16
|
65 //! Factory implementation
|
Chris@16
|
66 template< typename T >
|
Chris@16
|
67 class impl_template :
|
Chris@16
|
68 public impl
|
Chris@16
|
69 {
|
Chris@16
|
70 private:
|
Chris@16
|
71 //! Functor that returns attribute values
|
Chris@16
|
72 /*!
|
Chris@16
|
73 * \note The constness signifies that the function object should avoid
|
Chris@16
|
74 * modifying its state since it's not protected against concurrent calls.
|
Chris@16
|
75 */
|
Chris@16
|
76 const T m_Functor;
|
Chris@16
|
77
|
Chris@16
|
78 public:
|
Chris@16
|
79 /*!
|
Chris@16
|
80 * Constructor with the stored delegate initialization
|
Chris@16
|
81 */
|
Chris@16
|
82 explicit impl_template(T const& fun) : m_Functor(fun) {}
|
Chris@16
|
83
|
Chris@16
|
84 attribute_value get_value()
|
Chris@16
|
85 {
|
Chris@16
|
86 return attributes::make_attribute_value(m_Functor());
|
Chris@16
|
87 }
|
Chris@16
|
88 };
|
Chris@16
|
89
|
Chris@16
|
90 public:
|
Chris@16
|
91 /*!
|
Chris@16
|
92 * Initializing constructor
|
Chris@16
|
93 */
|
Chris@16
|
94 template< typename T >
|
Chris@16
|
95 explicit function(T const& fun) : attribute(new impl_template< T >(fun))
|
Chris@16
|
96 {
|
Chris@16
|
97 }
|
Chris@16
|
98 /*!
|
Chris@16
|
99 * Constructor for casting support
|
Chris@16
|
100 */
|
Chris@16
|
101 explicit function(cast_source const& source) :
|
Chris@16
|
102 attribute(source.as< impl >())
|
Chris@16
|
103 {
|
Chris@16
|
104 }
|
Chris@16
|
105 };
|
Chris@16
|
106
|
Chris@16
|
107 #ifndef BOOST_NO_RESULT_OF
|
Chris@16
|
108
|
Chris@16
|
109 /*!
|
Chris@16
|
110 * The function constructs \c function attribute instance with the provided function object.
|
Chris@16
|
111 *
|
Chris@16
|
112 * \param fun Nullary functional object that returns an actual stored value for an attribute value.
|
Chris@16
|
113 * \return Pointer to the attribute instance
|
Chris@16
|
114 */
|
Chris@16
|
115 template< typename T >
|
Chris@16
|
116 inline function<
|
Chris@16
|
117 typename remove_cv<
|
Chris@16
|
118 typename remove_reference<
|
Chris@16
|
119 typename boost::result_of< T() >::type
|
Chris@16
|
120 >::type
|
Chris@16
|
121 >::type
|
Chris@16
|
122 > make_function(T const& fun)
|
Chris@16
|
123 {
|
Chris@16
|
124 typedef typename remove_cv<
|
Chris@16
|
125 typename remove_reference<
|
Chris@16
|
126 typename boost::result_of< T() >::type
|
Chris@16
|
127 >::type
|
Chris@16
|
128 >::type result_type;
|
Chris@16
|
129
|
Chris@16
|
130 typedef function< result_type > function_type;
|
Chris@16
|
131 return function_type(fun);
|
Chris@16
|
132 }
|
Chris@16
|
133
|
Chris@16
|
134 #endif // BOOST_NO_RESULT_OF
|
Chris@16
|
135
|
Chris@16
|
136 #ifndef BOOST_LOG_DOXYGEN_PASS
|
Chris@16
|
137
|
Chris@16
|
138 /*!
|
Chris@16
|
139 * The function constructs \c function attribute instance with the provided function object.
|
Chris@16
|
140 * Use this version if your compiler fails to determine the result type of the function object.
|
Chris@16
|
141 *
|
Chris@16
|
142 * \param fun Nullary functional object that returns an actual stored value for an attribute value.
|
Chris@16
|
143 * \return Pointer to the attribute instance
|
Chris@16
|
144 */
|
Chris@16
|
145 template< typename R, typename T >
|
Chris@16
|
146 inline function<
|
Chris@16
|
147 typename remove_cv<
|
Chris@16
|
148 typename remove_reference< R >::type
|
Chris@16
|
149 >::type
|
Chris@16
|
150 > make_function(T const& fun)
|
Chris@16
|
151 {
|
Chris@16
|
152 typedef typename remove_cv<
|
Chris@16
|
153 typename remove_reference< R >::type
|
Chris@16
|
154 >::type result_type;
|
Chris@16
|
155
|
Chris@16
|
156 typedef function< result_type > function_type;
|
Chris@16
|
157 return function_type(fun);
|
Chris@16
|
158 }
|
Chris@16
|
159
|
Chris@16
|
160 #endif // BOOST_LOG_DOXYGEN_PASS
|
Chris@16
|
161
|
Chris@16
|
162 } // namespace attributes
|
Chris@16
|
163
|
Chris@16
|
164 BOOST_LOG_CLOSE_NAMESPACE // namespace log
|
Chris@16
|
165
|
Chris@16
|
166 } // namespace boost
|
Chris@16
|
167
|
Chris@16
|
168 #include <boost/log/detail/footer.hpp>
|
Chris@16
|
169
|
Chris@16
|
170 #endif // BOOST_LOG_ATTRIBUTES_FUNCTOR_HPP_INCLUDED_
|