Back to home page

EIC code displayed by LXR

 
 

    


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

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   attr.hpp
0009  * \author Andrey Semashev
0010  * \date   21.07.2012
0011  *
0012  * The header contains implementation of a generic attribute placeholder in template expressions.
0013  */
0014 
0015 #ifndef BOOST_LOG_EXPRESSIONS_ATTR_HPP_INCLUDED_
0016 #define BOOST_LOG_EXPRESSIONS_ATTR_HPP_INCLUDED_
0017 
0018 #include <boost/mpl/bool.hpp>
0019 #include <boost/utility/result_of.hpp>
0020 #include <boost/phoenix/core/actor.hpp>
0021 #include <boost/phoenix/core/terminal_fwd.hpp>
0022 #include <boost/phoenix/core/is_nullary.hpp>
0023 #include <boost/phoenix/core/environment.hpp>
0024 #include <boost/fusion/sequence/intrinsic/at_c.hpp>
0025 #include <boost/type_traits/remove_cv.hpp>
0026 #include <boost/type_traits/remove_reference.hpp>
0027 #include <boost/log/detail/config.hpp>
0028 #include <boost/log/detail/copy_cv.hpp>
0029 #include <boost/log/detail/custom_terminal_spec.hpp>
0030 #include <boost/log/attributes/attribute_name.hpp>
0031 #include <boost/log/attributes/value_extraction.hpp>
0032 #include <boost/log/attributes/fallback_policy.hpp>
0033 #include <boost/log/expressions/attr_fwd.hpp>
0034 #include <boost/log/detail/header.hpp>
0035 
0036 #ifdef BOOST_HAS_PRAGMA_ONCE
0037 #pragma once
0038 #endif
0039 
0040 namespace boost {
0041 
0042 BOOST_LOG_OPEN_NAMESPACE
0043 
0044 namespace expressions {
0045 
0046 /*!
0047  * An attribute value extraction terminal
0048  */
0049 template< typename T, typename FallbackPolicyT, typename TagT >
0050 class attribute_terminal
0051 {
0052 private:
0053     //! Value extractor type
0054     typedef value_extractor< T, FallbackPolicyT, TagT > value_extractor_type;
0055     //! Self type
0056     typedef attribute_terminal< T, FallbackPolicyT, TagT > this_type;
0057 
0058 public:
0059 #ifndef BOOST_LOG_DOXYGEN_PASS
0060     //! Internal typedef for type categorization
0061     typedef void _is_boost_log_terminal;
0062 #endif
0063 
0064     //! Attribute tag type
0065     typedef TagT tag_type;
0066     //! Attribute value type
0067     typedef typename value_extractor_type::value_type value_type;
0068     //! Fallback policy type
0069     typedef typename value_extractor_type::fallback_policy fallback_policy;
0070 
0071     //! Function result type
0072     template< typename >
0073     struct result;
0074 
0075     template< typename ThisT, typename ContextT >
0076     struct result< ThisT(ContextT) >
0077     {
0078         typedef typename remove_cv<
0079             typename remove_reference< typename phoenix::result_of::env< ContextT >::type >::type
0080         >::type env_type;
0081         typedef typename env_type::args_type args_type;
0082         typedef typename boost::log::aux::copy_cv< ThisT, value_extractor_type >::type cv_value_extractor_type;
0083 
0084         typedef typename boost::result_of< cv_value_extractor_type(attribute_name const&, typename fusion::result_of::at_c< args_type, 0 >::type) >::type type;
0085     };
0086 
0087 private:
0088     //! Attribute value name
0089     const attribute_name m_name;
0090     //! Attribute value extractor
0091     value_extractor_type m_value_extractor;
0092 
0093 public:
0094     /*!
0095      * Initializing constructor
0096      */
0097     explicit attribute_terminal(attribute_name const& name) : m_name(name)
0098     {
0099     }
0100 
0101     /*!
0102      * Initializing constructor
0103      */
0104     template< typename U >
0105     attribute_terminal(attribute_name const& name, U const& arg) : m_name(name), m_value_extractor(arg)
0106     {
0107     }
0108 
0109     /*!
0110      * \returns Attribute value name
0111      */
0112     attribute_name get_name() const
0113     {
0114         return m_name;
0115     }
0116 
0117     /*!
0118      * \returns Fallback policy
0119      */
0120     fallback_policy const& get_fallback_policy() const
0121     {
0122         return m_value_extractor.get_fallback_policy();
0123     }
0124 
0125     /*!
0126      * The operator extracts attribute value
0127      */
0128     template< typename ContextT >
0129     typename result< this_type(ContextT const&) >::type
0130     operator() (ContextT const& ctx)
0131     {
0132         return m_value_extractor(m_name, fusion::at_c< 0 >(phoenix::env(ctx).args()));
0133     }
0134 
0135     /*!
0136      * The operator extracts attribute value
0137      */
0138     template< typename ContextT >
0139     typename result< const this_type(ContextT const&) >::type
0140     operator() (ContextT const& ctx) const
0141     {
0142         return m_value_extractor(m_name, fusion::at_c< 0 >(phoenix::env(ctx).args()));
0143     }
0144 
0145     BOOST_DELETED_FUNCTION(attribute_terminal())
0146 };
0147 
0148 /*!
0149  * An attribute value extraction terminal actor
0150  */
0151 template< typename T, typename FallbackPolicyT, typename TagT, template< typename > class ActorT >
0152 class attribute_actor :
0153     public ActorT< attribute_terminal< T, FallbackPolicyT, TagT > >
0154 {
0155 public:
0156     //! Attribute tag type
0157     typedef TagT tag_type;
0158     //! Fallback policy
0159     typedef FallbackPolicyT fallback_policy;
0160     //! Base terminal type
0161     typedef attribute_terminal< T, fallback_policy, tag_type > terminal_type;
0162     //! Attribute value type
0163     typedef typename terminal_type::value_type value_type;
0164 
0165     //! Base actor type
0166     typedef ActorT< terminal_type > base_type;
0167 
0168 public:
0169     //! Initializing constructor
0170     explicit attribute_actor(base_type const& act) : base_type(act)
0171     {
0172     }
0173 
0174     /*!
0175      * \returns The attribute name
0176      */
0177     attribute_name get_name() const
0178     {
0179         return this->proto_expr_.child0.get_name();
0180     }
0181 
0182     /*!
0183      * \returns Fallback policy
0184      */
0185     fallback_policy const& get_fallback_policy() const
0186     {
0187         return this->proto_expr_.child0.get_fallback_policy();
0188     }
0189 
0190     //! Expression with cached attribute name
0191     typedef attribute_actor< value_type, fallback_to_none, tag_type, ActorT > or_none_result_type;
0192 
0193     //! Generates an expression that extracts the attribute value or a default value
0194     or_none_result_type or_none() const
0195     {
0196         typedef typename or_none_result_type::terminal_type result_terminal;
0197         typename or_none_result_type::base_type act = {{ result_terminal(get_name()) }};
0198         return or_none_result_type(act);
0199     }
0200 
0201     //! Expression with cached attribute name
0202     typedef attribute_actor< value_type, fallback_to_throw, tag_type, ActorT > or_throw_result_type;
0203 
0204     //! Generates an expression that extracts the attribute value or throws an exception
0205     or_throw_result_type or_throw() const
0206     {
0207         typedef typename or_throw_result_type::terminal_type result_terminal;
0208         typename or_throw_result_type::base_type act = {{ result_terminal(get_name()) }};
0209         return or_throw_result_type(act);
0210     }
0211 
0212     //! Generates an expression that extracts the attribute value or a default value
0213     template< typename DefaultT >
0214     attribute_actor< value_type, fallback_to_default< DefaultT >, tag_type, ActorT > or_default(DefaultT const& def_val) const
0215     {
0216         typedef attribute_actor< value_type, fallback_to_default< DefaultT >, tag_type, ActorT > or_default_result_type;
0217         typedef typename or_default_result_type::terminal_type result_terminal;
0218         typename or_default_result_type::base_type act = {{ result_terminal(get_name(), def_val) }};
0219         return or_default_result_type(act);
0220     }
0221 };
0222 
0223 /*!
0224  * The function generates a terminal node in a template expression. The node will extract the value of the attribute
0225  * with the specified name and type.
0226  */
0227 template< typename AttributeValueT >
0228 BOOST_FORCEINLINE attribute_actor< AttributeValueT > attr(attribute_name const& name)
0229 {
0230     typedef attribute_actor< AttributeValueT > result_type;
0231     typedef typename result_type::terminal_type result_terminal;
0232     typename result_type::base_type act = {{ result_terminal(name) }};
0233     return result_type(act);
0234 }
0235 
0236 /*!
0237  * The function generates a terminal node in a template expression. The node will extract the value of the attribute
0238  * with the specified name and type.
0239  */
0240 template< typename AttributeValueT, typename TagT >
0241 BOOST_FORCEINLINE attribute_actor< AttributeValueT, fallback_to_none, TagT > attr(attribute_name const& name)
0242 {
0243     typedef attribute_actor< AttributeValueT, fallback_to_none, TagT > result_type;
0244     typedef typename result_type::terminal_type result_terminal;
0245     typename result_type::base_type act = {{ result_terminal(name) }};
0246     return result_type(act);
0247 }
0248 
0249 } // namespace expressions
0250 
0251 BOOST_LOG_CLOSE_NAMESPACE // namespace log
0252 
0253 #ifndef BOOST_LOG_DOXYGEN_PASS
0254 
0255 namespace phoenix {
0256 
0257 namespace result_of {
0258 
0259 template< typename T, typename FallbackPolicyT, typename TagT >
0260 struct is_nullary< custom_terminal< boost::log::expressions::attribute_terminal< T, FallbackPolicyT, TagT > > > :
0261     public mpl::false_
0262 {
0263 };
0264 
0265 } // namespace result_of
0266 
0267 } // namespace phoenix
0268 
0269 #endif
0270 
0271 } // namespace boost
0272 
0273 #include <boost/log/detail/footer.hpp>
0274 #if defined(BOOST_LOG_EXPRESSIONS_FORMATTERS_STREAM_HPP_INCLUDED_)
0275 #include <boost/log/detail/attr_output_impl.hpp>
0276 #endif
0277 
0278 #endif // BOOST_LOG_EXPRESSIONS_ATTR_HPP_INCLUDED_