comparison DEPENDENCIES/generic/include/boost/log/expressions/attr.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children c530137014c0
comparison
equal deleted inserted replaced
15:663ca0da4350 16:2665513ce2d3
1 /*
2 * Copyright Andrey Semashev 2007 - 2013.
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 attr.hpp
9 * \author Andrey Semashev
10 * \date 21.07.2012
11 *
12 * The header contains implementation of a generic attribute placeholder in template expressions.
13 */
14
15 #ifndef BOOST_LOG_EXPRESSIONS_ATTR_HPP_INCLUDED_
16 #define BOOST_LOG_EXPRESSIONS_ATTR_HPP_INCLUDED_
17
18 #include <boost/mpl/bool.hpp>
19 #include <boost/utility/result_of.hpp>
20 #include <boost/phoenix/core/actor.hpp>
21 #include <boost/phoenix/core/terminal_fwd.hpp>
22 #include <boost/phoenix/core/is_nullary.hpp>
23 #include <boost/phoenix/core/environment.hpp>
24 #include <boost/fusion/sequence/intrinsic/at_c.hpp>
25 #include <boost/type_traits/remove_cv.hpp>
26 #include <boost/type_traits/remove_reference.hpp>
27 #include <boost/log/detail/config.hpp>
28 #include <boost/log/detail/custom_terminal_spec.hpp>
29 #include <boost/log/attributes/attribute_name.hpp>
30 #include <boost/log/attributes/value_extraction.hpp>
31 #include <boost/log/attributes/fallback_policy.hpp>
32 #include <boost/log/expressions/attr_fwd.hpp>
33 #include <boost/log/detail/header.hpp>
34
35 #ifdef BOOST_HAS_PRAGMA_ONCE
36 #pragma once
37 #endif
38
39 namespace boost {
40
41 BOOST_LOG_OPEN_NAMESPACE
42
43 namespace expressions {
44
45 /*!
46 * An attribute value extraction terminal
47 */
48 template< typename T, typename FallbackPolicyT, typename TagT >
49 class attribute_terminal
50 {
51 private:
52 //! Value extractor type
53 typedef value_extractor< T, FallbackPolicyT, TagT > value_extractor_type;
54 //! Self type
55 typedef attribute_terminal< T, FallbackPolicyT, TagT > this_type;
56
57 public:
58 //! Internal typedef for type categorization
59 typedef void _is_boost_log_terminal;
60
61 //! Attribute tag type
62 typedef TagT tag_type;
63 //! Attribute value type
64 typedef typename value_extractor_type::value_type value_type;
65 //! Fallback policy type
66 typedef typename value_extractor_type::fallback_policy fallback_policy;
67
68 //! Function result type
69 template< typename >
70 struct result;
71
72 template< typename ContextT >
73 struct result< this_type(ContextT) >
74 {
75 typedef typename remove_cv<
76 typename remove_reference< typename phoenix::result_of::env< ContextT >::type >::type
77 >::type env_type;
78 typedef typename env_type::args_type args_type;
79
80 typedef typename boost::result_of< value_extractor_type(attribute_name const&, typename fusion::result_of::at_c< args_type, 0 >::type) >::type type;
81 };
82
83 template< typename ContextT >
84 struct result< const this_type(ContextT) >
85 {
86 typedef typename remove_cv<
87 typename remove_reference< typename phoenix::result_of::env< ContextT >::type >::type
88 >::type env_type;
89 typedef typename env_type::args_type args_type;
90
91 typedef typename boost::result_of< const value_extractor_type(attribute_name const&, typename fusion::result_of::at_c< args_type, 0 >::type) >::type type;
92 };
93
94 private:
95 //! Attribute value name
96 const attribute_name m_name;
97 //! Attribute value extractor
98 value_extractor_type m_value_extractor;
99
100 public:
101 /*!
102 * Initializing constructor
103 */
104 explicit attribute_terminal(attribute_name const& name) : m_name(name)
105 {
106 }
107
108 /*!
109 * Initializing constructor
110 */
111 template< typename U >
112 attribute_terminal(attribute_name const& name, U const& arg) : m_name(name), m_value_extractor(arg)
113 {
114 }
115
116 /*!
117 * \returns Attribute value name
118 */
119 attribute_name get_name() const
120 {
121 return m_name;
122 }
123
124 /*!
125 * \returns Fallback policy
126 */
127 fallback_policy const& get_fallback_policy() const
128 {
129 return m_value_extractor.get_fallback_policy();
130 }
131
132 /*!
133 * The operator extracts attribute value
134 */
135 template< typename ContextT >
136 typename result< this_type(ContextT const&) >::type
137 operator() (ContextT const& ctx)
138 {
139 return m_value_extractor(m_name, fusion::at_c< 0 >(phoenix::env(ctx).args()));
140 }
141
142 /*!
143 * The operator extracts attribute value
144 */
145 template< typename ContextT >
146 typename result< const this_type(ContextT const&) >::type
147 operator() (ContextT const& ctx) const
148 {
149 return m_value_extractor(m_name, fusion::at_c< 0 >(phoenix::env(ctx).args()));
150 }
151
152 BOOST_DELETED_FUNCTION(attribute_terminal())
153 };
154
155 /*!
156 * An attribute value extraction terminal actor
157 */
158 template< typename T, typename FallbackPolicyT, typename TagT, template< typename > class ActorT >
159 class attribute_actor :
160 public ActorT< attribute_terminal< T, FallbackPolicyT, TagT > >
161 {
162 public:
163 //! Attribute tag type
164 typedef TagT tag_type;
165 //! Fallback policy
166 typedef FallbackPolicyT fallback_policy;
167 //! Base terminal type
168 typedef attribute_terminal< T, fallback_policy, tag_type > terminal_type;
169 //! Attribute value type
170 typedef typename terminal_type::value_type value_type;
171
172 //! Base actor type
173 typedef ActorT< terminal_type > base_type;
174
175 public:
176 //! Initializing constructor
177 explicit attribute_actor(base_type const& act) : base_type(act)
178 {
179 }
180
181 /*!
182 * \returns The attribute name
183 */
184 attribute_name get_name() const
185 {
186 return this->proto_expr_.child0.get_name();
187 }
188
189 /*!
190 * \returns Fallback policy
191 */
192 fallback_policy const& get_fallback_policy() const
193 {
194 return this->proto_expr_.child0.get_fallback_policy();
195 }
196
197 //! Expression with cached attribute name
198 typedef attribute_actor< value_type, fallback_to_none, tag_type, ActorT > or_none_result_type;
199
200 //! Generates an expression that extracts the attribute value or a default value
201 or_none_result_type or_none() const
202 {
203 typedef typename or_none_result_type::terminal_type result_terminal;
204 typename or_none_result_type::base_type act = {{ result_terminal(get_name()) }};
205 return or_none_result_type(act);
206 }
207
208 //! Expression with cached attribute name
209 typedef attribute_actor< value_type, fallback_to_throw, tag_type, ActorT > or_throw_result_type;
210
211 //! Generates an expression that extracts the attribute value or throws an exception
212 or_throw_result_type or_throw() const
213 {
214 typedef typename or_throw_result_type::terminal_type result_terminal;
215 typename or_throw_result_type::base_type act = {{ result_terminal(get_name()) }};
216 return or_throw_result_type(act);
217 }
218
219 //! Generates an expression that extracts the attribute value or a default value
220 template< typename DefaultT >
221 attribute_actor< value_type, fallback_to_default< DefaultT >, tag_type, ActorT > or_default(DefaultT const& def_val) const
222 {
223 typedef attribute_actor< value_type, fallback_to_default< DefaultT >, tag_type, ActorT > or_default_result_type;
224 typedef typename or_default_result_type::terminal_type result_terminal;
225 typename or_default_result_type::base_type act = {{ result_terminal(get_name(), def_val) }};
226 return or_default_result_type(act);
227 }
228 };
229
230 /*!
231 * The function generates a terminal node in a template expression. The node will extract the value of the attribute
232 * with the specified name and type.
233 */
234 template< typename AttributeValueT >
235 BOOST_FORCEINLINE attribute_actor< AttributeValueT > attr(attribute_name const& name)
236 {
237 typedef attribute_actor< AttributeValueT > result_type;
238 typedef typename result_type::terminal_type result_terminal;
239 typename result_type::base_type act = {{ result_terminal(name) }};
240 return result_type(act);
241 }
242
243 /*!
244 * The function generates a terminal node in a template expression. The node will extract the value of the attribute
245 * with the specified name and type.
246 */
247 template< typename AttributeValueT, typename TagT >
248 BOOST_FORCEINLINE attribute_actor< AttributeValueT, fallback_to_none, TagT > attr(attribute_name const& name)
249 {
250 typedef attribute_actor< AttributeValueT, fallback_to_none, TagT > result_type;
251 typedef typename result_type::terminal_type result_terminal;
252 typename result_type::base_type act = {{ result_terminal(name) }};
253 return result_type(act);
254 }
255
256 } // namespace expressions
257
258 BOOST_LOG_CLOSE_NAMESPACE // namespace log
259
260 #ifndef BOOST_LOG_DOXYGEN_PASS
261
262 namespace phoenix {
263
264 namespace result_of {
265
266 template< typename T, typename FallbackPolicyT, typename TagT >
267 struct is_nullary< custom_terminal< boost::log::expressions::attribute_terminal< T, FallbackPolicyT, TagT > > > :
268 public mpl::false_
269 {
270 };
271
272 } // namespace result_of
273
274 } // namespace phoenix
275
276 #endif
277
278 } // namespace boost
279
280 #include <boost/log/detail/footer.hpp>
281 #if defined(BOOST_LOG_EXPRESSIONS_FORMATTERS_STREAM_HPP_INCLUDED_)
282 #include <boost/log/detail/attr_output_impl.hpp>
283 #endif
284
285 #endif // BOOST_LOG_EXPRESSIONS_ATTR_HPP_INCLUDED_