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)
9 * \author Andrey Semashev
12 * The header contains implementation of a generic attribute placeholder in template expressions.
15 #ifndef BOOST_LOG_EXPRESSIONS_ATTR_HPP_INCLUDED_
16 #define BOOST_LOG_EXPRESSIONS_ATTR_HPP_INCLUDED_
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/copy_cv.hpp>
29 #include <boost/log/detail/custom_terminal_spec.hpp>
30 #include <boost/log/attributes/attribute_name.hpp>
31 #include <boost/log/attributes/value_extraction.hpp>
32 #include <boost/log/attributes/fallback_policy.hpp>
33 #include <boost/log/expressions/attr_fwd.hpp>
34 #include <boost/log/detail/header.hpp>
36 #ifdef BOOST_HAS_PRAGMA_ONCE
42 BOOST_LOG_OPEN_NAMESPACE
44 namespace expressions {
47 * An attribute value extraction terminal
49 template< typename T, typename FallbackPolicyT, typename TagT >
50 class attribute_terminal
53 //! Value extractor type
54 typedef value_extractor< T, FallbackPolicyT, TagT > value_extractor_type;
56 typedef attribute_terminal< T, FallbackPolicyT, TagT > this_type;
59 #ifndef BOOST_LOG_DOXYGEN_PASS
60 //! Internal typedef for type categorization
61 typedef void _is_boost_log_terminal;
64 //! Attribute tag type
65 typedef TagT tag_type;
66 //! Attribute value type
67 typedef typename value_extractor_type::value_type value_type;
68 //! Fallback policy type
69 typedef typename value_extractor_type::fallback_policy fallback_policy;
71 //! Function result type
75 template< typename ThisT, typename ContextT >
76 struct result< ThisT(ContextT) >
78 typedef typename remove_cv<
79 typename remove_reference< typename phoenix::result_of::env< ContextT >::type >::type
81 typedef typename env_type::args_type args_type;
82 typedef typename boost::log::aux::copy_cv< ThisT, value_extractor_type >::type cv_value_extractor_type;
84 typedef typename boost::result_of< cv_value_extractor_type(attribute_name const&, typename fusion::result_of::at_c< args_type, 0 >::type) >::type type;
88 //! Attribute value name
89 const attribute_name m_name;
90 //! Attribute value extractor
91 value_extractor_type m_value_extractor;
95 * Initializing constructor
97 explicit attribute_terminal(attribute_name const& name) : m_name(name)
102 * Initializing constructor
104 template< typename U >
105 attribute_terminal(attribute_name const& name, U const& arg) : m_name(name), m_value_extractor(arg)
110 * \returns Attribute value name
112 attribute_name get_name() const
118 * \returns Fallback policy
120 fallback_policy const& get_fallback_policy() const
122 return m_value_extractor.get_fallback_policy();
126 * The operator extracts attribute value
128 template< typename ContextT >
129 typename result< this_type(ContextT const&) >::type
130 operator() (ContextT const& ctx)
132 return m_value_extractor(m_name, fusion::at_c< 0 >(phoenix::env(ctx).args()));
136 * The operator extracts attribute value
138 template< typename ContextT >
139 typename result< const this_type(ContextT const&) >::type
140 operator() (ContextT const& ctx) const
142 return m_value_extractor(m_name, fusion::at_c< 0 >(phoenix::env(ctx).args()));
145 BOOST_DELETED_FUNCTION(attribute_terminal())
149 * An attribute value extraction terminal actor
151 template< typename T, typename FallbackPolicyT, typename TagT, template< typename > class ActorT >
152 class attribute_actor :
153 public ActorT< attribute_terminal< T, FallbackPolicyT, TagT > >
156 //! Attribute tag type
157 typedef TagT tag_type;
159 typedef FallbackPolicyT fallback_policy;
160 //! Base terminal type
161 typedef attribute_terminal< T, fallback_policy, tag_type > terminal_type;
162 //! Attribute value type
163 typedef typename terminal_type::value_type value_type;
166 typedef ActorT< terminal_type > base_type;
169 //! Initializing constructor
170 explicit attribute_actor(base_type const& act) : base_type(act)
175 * \returns The attribute name
177 attribute_name get_name() const
179 return this->proto_expr_.child0.get_name();
183 * \returns Fallback policy
185 fallback_policy const& get_fallback_policy() const
187 return this->proto_expr_.child0.get_fallback_policy();
190 //! Expression with cached attribute name
191 typedef attribute_actor< value_type, fallback_to_none, tag_type, ActorT > or_none_result_type;
193 //! Generates an expression that extracts the attribute value or a default value
194 or_none_result_type or_none() const
196 typedef typename or_none_result_type::terminal_type result_terminal;
197 typename or_none_result_type::base_type act = {{ result_terminal(get_name()) }};
198 return or_none_result_type(act);
201 //! Expression with cached attribute name
202 typedef attribute_actor< value_type, fallback_to_throw, tag_type, ActorT > or_throw_result_type;
204 //! Generates an expression that extracts the attribute value or throws an exception
205 or_throw_result_type or_throw() const
207 typedef typename or_throw_result_type::terminal_type result_terminal;
208 typename or_throw_result_type::base_type act = {{ result_terminal(get_name()) }};
209 return or_throw_result_type(act);
212 //! Generates an expression that extracts the attribute value or a default value
213 template< typename DefaultT >
214 attribute_actor< value_type, fallback_to_default< DefaultT >, tag_type, ActorT > or_default(DefaultT const& def_val) const
216 typedef attribute_actor< value_type, fallback_to_default< DefaultT >, tag_type, ActorT > or_default_result_type;
217 typedef typename or_default_result_type::terminal_type result_terminal;
218 typename or_default_result_type::base_type act = {{ result_terminal(get_name(), def_val) }};
219 return or_default_result_type(act);
224 * The function generates a terminal node in a template expression. The node will extract the value of the attribute
225 * with the specified name and type.
227 template< typename AttributeValueT >
228 BOOST_FORCEINLINE attribute_actor< AttributeValueT > attr(attribute_name const& name)
230 typedef attribute_actor< AttributeValueT > result_type;
231 typedef typename result_type::terminal_type result_terminal;
232 typename result_type::base_type act = {{ result_terminal(name) }};
233 return result_type(act);
237 * The function generates a terminal node in a template expression. The node will extract the value of the attribute
238 * with the specified name and type.
240 template< typename AttributeValueT, typename TagT >
241 BOOST_FORCEINLINE attribute_actor< AttributeValueT, fallback_to_none, TagT > attr(attribute_name const& name)
243 typedef attribute_actor< AttributeValueT, fallback_to_none, TagT > result_type;
244 typedef typename result_type::terminal_type result_terminal;
245 typename result_type::base_type act = {{ result_terminal(name) }};
246 return result_type(act);
249 } // namespace expressions
251 BOOST_LOG_CLOSE_NAMESPACE // namespace log
253 #ifndef BOOST_LOG_DOXYGEN_PASS
257 namespace result_of {
259 template< typename T, typename FallbackPolicyT, typename TagT >
260 struct is_nullary< custom_terminal< boost::log::expressions::attribute_terminal< T, FallbackPolicyT, TagT > > > :
265 } // namespace result_of
267 } // namespace phoenix
273 #include <boost/log/detail/footer.hpp>
274 #if defined(BOOST_LOG_EXPRESSIONS_FORMATTERS_STREAM_HPP_INCLUDED_)
275 #include <boost/log/detail/attr_output_impl.hpp>
278 #endif // BOOST_LOG_EXPRESSIONS_ATTR_HPP_INCLUDED_