• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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)
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/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>
35 
36 #ifdef BOOST_HAS_PRAGMA_ONCE
37 #pragma once
38 #endif
39 
40 namespace boost {
41 
42 BOOST_LOG_OPEN_NAMESPACE
43 
44 namespace expressions {
45 
46 /*!
47  * An attribute value extraction terminal
48  */
49 template< typename T, typename FallbackPolicyT, typename TagT >
50 class attribute_terminal
51 {
52 private:
53     //! Value extractor type
54     typedef value_extractor< T, FallbackPolicyT, TagT > value_extractor_type;
55     //! Self type
56     typedef attribute_terminal< T, FallbackPolicyT, TagT > this_type;
57 
58 public:
59 #ifndef BOOST_LOG_DOXYGEN_PASS
60     //! Internal typedef for type categorization
61     typedef void _is_boost_log_terminal;
62 #endif
63 
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;
70 
71     //! Function result type
72     template< typename >
73     struct result;
74 
75     template< typename ThisT, typename ContextT >
76     struct result< ThisT(ContextT) >
77     {
78         typedef typename remove_cv<
79             typename remove_reference< typename phoenix::result_of::env< ContextT >::type >::type
80         >::type env_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;
83 
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;
85     };
86 
87 private:
88     //! Attribute value name
89     const attribute_name m_name;
90     //! Attribute value extractor
91     value_extractor_type m_value_extractor;
92 
93 public:
94     /*!
95      * Initializing constructor
96      */
attribute_terminal(attribute_name const & name)97     explicit attribute_terminal(attribute_name const& name) : m_name(name)
98     {
99     }
100 
101     /*!
102      * Initializing constructor
103      */
104     template< typename U >
attribute_terminal(attribute_name const & name,U const & arg)105     attribute_terminal(attribute_name const& name, U const& arg) : m_name(name), m_value_extractor(arg)
106     {
107     }
108 
109     /*!
110      * \returns Attribute value name
111      */
get_name() const112     attribute_name get_name() const
113     {
114         return m_name;
115     }
116 
117     /*!
118      * \returns Fallback policy
119      */
get_fallback_policy() const120     fallback_policy const& get_fallback_policy() const
121     {
122         return m_value_extractor.get_fallback_policy();
123     }
124 
125     /*!
126      * The operator extracts attribute value
127      */
128     template< typename ContextT >
129     typename result< this_type(ContextT const&) >::type
operator ()(ContextT const & ctx)130     operator() (ContextT const& ctx)
131     {
132         return m_value_extractor(m_name, fusion::at_c< 0 >(phoenix::env(ctx).args()));
133     }
134 
135     /*!
136      * The operator extracts attribute value
137      */
138     template< typename ContextT >
139     typename result< const this_type(ContextT const&) >::type
operator ()(ContextT const & ctx) const140     operator() (ContextT const& ctx) const
141     {
142         return m_value_extractor(m_name, fusion::at_c< 0 >(phoenix::env(ctx).args()));
143     }
144 
145     BOOST_DELETED_FUNCTION(attribute_terminal())
146 };
147 
148 /*!
149  * An attribute value extraction terminal actor
150  */
151 template< typename T, typename FallbackPolicyT, typename TagT, template< typename > class ActorT >
152 class attribute_actor :
153     public ActorT< attribute_terminal< T, FallbackPolicyT, TagT > >
154 {
155 public:
156     //! Attribute tag type
157     typedef TagT tag_type;
158     //! Fallback policy
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;
164 
165     //! Base actor type
166     typedef ActorT< terminal_type > base_type;
167 
168 public:
169     //! Initializing constructor
attribute_actor(base_type const & act)170     explicit attribute_actor(base_type const& act) : base_type(act)
171     {
172     }
173 
174     /*!
175      * \returns The attribute name
176      */
get_name() const177     attribute_name get_name() const
178     {
179         return this->proto_expr_.child0.get_name();
180     }
181 
182     /*!
183      * \returns Fallback policy
184      */
get_fallback_policy() const185     fallback_policy const& get_fallback_policy() const
186     {
187         return this->proto_expr_.child0.get_fallback_policy();
188     }
189 
190     //! Expression with cached attribute name
191     typedef attribute_actor< value_type, fallback_to_none, tag_type, ActorT > or_none_result_type;
192 
193     //! Generates an expression that extracts the attribute value or a default value
or_none() const194     or_none_result_type or_none() const
195     {
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);
199     }
200 
201     //! Expression with cached attribute name
202     typedef attribute_actor< value_type, fallback_to_throw, tag_type, ActorT > or_throw_result_type;
203 
204     //! Generates an expression that extracts the attribute value or throws an exception
or_throw() const205     or_throw_result_type or_throw() const
206     {
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);
210     }
211 
212     //! Generates an expression that extracts the attribute value or a default value
213     template< typename DefaultT >
or_default(DefaultT const & def_val) const214     attribute_actor< value_type, fallback_to_default< DefaultT >, tag_type, ActorT > or_default(DefaultT const& def_val) const
215     {
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);
220     }
221 };
222 
223 /*!
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.
226  */
227 template< typename AttributeValueT >
attr(attribute_name const & name)228 BOOST_FORCEINLINE attribute_actor< AttributeValueT > attr(attribute_name const& name)
229 {
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);
234 }
235 
236 /*!
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.
239  */
240 template< typename AttributeValueT, typename TagT >
attr(attribute_name const & name)241 BOOST_FORCEINLINE attribute_actor< AttributeValueT, fallback_to_none, TagT > attr(attribute_name const& name)
242 {
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);
247 }
248 
249 } // namespace expressions
250 
251 BOOST_LOG_CLOSE_NAMESPACE // namespace log
252 
253 #ifndef BOOST_LOG_DOXYGEN_PASS
254 
255 namespace phoenix {
256 
257 namespace result_of {
258 
259 template< typename T, typename FallbackPolicyT, typename TagT >
260 struct is_nullary< custom_terminal< boost::log::expressions::attribute_terminal< T, FallbackPolicyT, TagT > > > :
261     public mpl::false_
262 {
263 };
264 
265 } // namespace result_of
266 
267 } // namespace phoenix
268 
269 #endif
270 
271 } // namespace boost
272 
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>
276 #endif
277 
278 #endif // BOOST_LOG_EXPRESSIONS_ATTR_HPP_INCLUDED_
279