Chris@16: /* Chris@101: * Copyright Andrey Semashev 2007 - 2015. Chris@16: * Distributed under the Boost Software License, Version 1.0. Chris@16: * (See accompanying file LICENSE_1_0.txt or copy at Chris@16: * http://www.boost.org/LICENSE_1_0.txt) Chris@16: */ Chris@16: /*! Chris@16: * \file attr.hpp Chris@16: * \author Andrey Semashev Chris@16: * \date 21.07.2012 Chris@16: * Chris@16: * The header contains implementation of a generic attribute placeholder in template expressions. Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_LOG_EXPRESSIONS_ATTR_HPP_INCLUDED_ Chris@16: #define BOOST_LOG_EXPRESSIONS_ATTR_HPP_INCLUDED_ Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #ifdef BOOST_HAS_PRAGMA_ONCE Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: BOOST_LOG_OPEN_NAMESPACE Chris@16: Chris@16: namespace expressions { Chris@16: Chris@16: /*! Chris@16: * An attribute value extraction terminal Chris@16: */ Chris@16: template< typename T, typename FallbackPolicyT, typename TagT > Chris@16: class attribute_terminal Chris@16: { Chris@16: private: Chris@16: //! Value extractor type Chris@16: typedef value_extractor< T, FallbackPolicyT, TagT > value_extractor_type; Chris@16: //! Self type Chris@16: typedef attribute_terminal< T, FallbackPolicyT, TagT > this_type; Chris@16: Chris@16: public: Chris@16: //! Internal typedef for type categorization Chris@16: typedef void _is_boost_log_terminal; Chris@16: Chris@16: //! Attribute tag type Chris@16: typedef TagT tag_type; Chris@16: //! Attribute value type Chris@16: typedef typename value_extractor_type::value_type value_type; Chris@16: //! Fallback policy type Chris@16: typedef typename value_extractor_type::fallback_policy fallback_policy; Chris@16: Chris@16: //! Function result type Chris@16: template< typename > Chris@16: struct result; Chris@16: Chris@101: template< typename ThisT, typename ContextT > Chris@101: struct result< ThisT(ContextT) > Chris@16: { Chris@16: typedef typename remove_cv< Chris@16: typename remove_reference< typename phoenix::result_of::env< ContextT >::type >::type Chris@16: >::type env_type; Chris@16: typedef typename env_type::args_type args_type; Chris@101: typedef typename boost::log::aux::copy_cv< ThisT, value_extractor_type >::type cv_value_extractor_type; Chris@16: Chris@101: typedef typename boost::result_of< cv_value_extractor_type(attribute_name const&, typename fusion::result_of::at_c< args_type, 0 >::type) >::type type; Chris@16: }; Chris@16: Chris@16: private: Chris@16: //! Attribute value name Chris@16: const attribute_name m_name; Chris@16: //! Attribute value extractor Chris@16: value_extractor_type m_value_extractor; Chris@16: Chris@16: public: Chris@16: /*! Chris@16: * Initializing constructor Chris@16: */ Chris@16: explicit attribute_terminal(attribute_name const& name) : m_name(name) Chris@16: { Chris@16: } Chris@16: Chris@16: /*! Chris@16: * Initializing constructor Chris@16: */ Chris@16: template< typename U > Chris@16: attribute_terminal(attribute_name const& name, U const& arg) : m_name(name), m_value_extractor(arg) Chris@16: { Chris@16: } Chris@16: Chris@16: /*! Chris@16: * \returns Attribute value name Chris@16: */ Chris@16: attribute_name get_name() const Chris@16: { Chris@16: return m_name; Chris@16: } Chris@16: Chris@16: /*! Chris@16: * \returns Fallback policy Chris@16: */ Chris@16: fallback_policy const& get_fallback_policy() const Chris@16: { Chris@16: return m_value_extractor.get_fallback_policy(); Chris@16: } Chris@16: Chris@16: /*! Chris@16: * The operator extracts attribute value Chris@16: */ Chris@16: template< typename ContextT > Chris@16: typename result< this_type(ContextT const&) >::type Chris@16: operator() (ContextT const& ctx) Chris@16: { Chris@16: return m_value_extractor(m_name, fusion::at_c< 0 >(phoenix::env(ctx).args())); Chris@16: } Chris@16: Chris@16: /*! Chris@16: * The operator extracts attribute value Chris@16: */ Chris@16: template< typename ContextT > Chris@16: typename result< const this_type(ContextT const&) >::type Chris@16: operator() (ContextT const& ctx) const Chris@16: { Chris@16: return m_value_extractor(m_name, fusion::at_c< 0 >(phoenix::env(ctx).args())); Chris@16: } Chris@16: Chris@16: BOOST_DELETED_FUNCTION(attribute_terminal()) Chris@16: }; Chris@16: Chris@16: /*! Chris@16: * An attribute value extraction terminal actor Chris@16: */ Chris@16: template< typename T, typename FallbackPolicyT, typename TagT, template< typename > class ActorT > Chris@16: class attribute_actor : Chris@16: public ActorT< attribute_terminal< T, FallbackPolicyT, TagT > > Chris@16: { Chris@16: public: Chris@16: //! Attribute tag type Chris@16: typedef TagT tag_type; Chris@16: //! Fallback policy Chris@16: typedef FallbackPolicyT fallback_policy; Chris@16: //! Base terminal type Chris@16: typedef attribute_terminal< T, fallback_policy, tag_type > terminal_type; Chris@16: //! Attribute value type Chris@16: typedef typename terminal_type::value_type value_type; Chris@16: Chris@16: //! Base actor type Chris@16: typedef ActorT< terminal_type > base_type; Chris@16: Chris@16: public: Chris@16: //! Initializing constructor Chris@16: explicit attribute_actor(base_type const& act) : base_type(act) Chris@16: { Chris@16: } Chris@16: Chris@16: /*! Chris@16: * \returns The attribute name Chris@16: */ Chris@16: attribute_name get_name() const Chris@16: { Chris@16: return this->proto_expr_.child0.get_name(); Chris@16: } Chris@16: Chris@16: /*! Chris@16: * \returns Fallback policy Chris@16: */ Chris@16: fallback_policy const& get_fallback_policy() const Chris@16: { Chris@16: return this->proto_expr_.child0.get_fallback_policy(); Chris@16: } Chris@16: Chris@16: //! Expression with cached attribute name Chris@16: typedef attribute_actor< value_type, fallback_to_none, tag_type, ActorT > or_none_result_type; Chris@16: Chris@16: //! Generates an expression that extracts the attribute value or a default value Chris@16: or_none_result_type or_none() const Chris@16: { Chris@16: typedef typename or_none_result_type::terminal_type result_terminal; Chris@16: typename or_none_result_type::base_type act = {{ result_terminal(get_name()) }}; Chris@16: return or_none_result_type(act); Chris@16: } Chris@16: Chris@16: //! Expression with cached attribute name Chris@16: typedef attribute_actor< value_type, fallback_to_throw, tag_type, ActorT > or_throw_result_type; Chris@16: Chris@16: //! Generates an expression that extracts the attribute value or throws an exception Chris@16: or_throw_result_type or_throw() const Chris@16: { Chris@16: typedef typename or_throw_result_type::terminal_type result_terminal; Chris@16: typename or_throw_result_type::base_type act = {{ result_terminal(get_name()) }}; Chris@16: return or_throw_result_type(act); Chris@16: } Chris@16: Chris@16: //! Generates an expression that extracts the attribute value or a default value Chris@16: template< typename DefaultT > Chris@16: attribute_actor< value_type, fallback_to_default< DefaultT >, tag_type, ActorT > or_default(DefaultT const& def_val) const Chris@16: { Chris@16: typedef attribute_actor< value_type, fallback_to_default< DefaultT >, tag_type, ActorT > or_default_result_type; Chris@16: typedef typename or_default_result_type::terminal_type result_terminal; Chris@16: typename or_default_result_type::base_type act = {{ result_terminal(get_name(), def_val) }}; Chris@16: return or_default_result_type(act); Chris@16: } Chris@16: }; Chris@16: Chris@16: /*! Chris@16: * The function generates a terminal node in a template expression. The node will extract the value of the attribute Chris@16: * with the specified name and type. Chris@16: */ Chris@16: template< typename AttributeValueT > Chris@16: BOOST_FORCEINLINE attribute_actor< AttributeValueT > attr(attribute_name const& name) Chris@16: { Chris@16: typedef attribute_actor< AttributeValueT > result_type; Chris@16: typedef typename result_type::terminal_type result_terminal; Chris@16: typename result_type::base_type act = {{ result_terminal(name) }}; Chris@16: return result_type(act); Chris@16: } Chris@16: Chris@16: /*! Chris@16: * The function generates a terminal node in a template expression. The node will extract the value of the attribute Chris@16: * with the specified name and type. Chris@16: */ Chris@16: template< typename AttributeValueT, typename TagT > Chris@16: BOOST_FORCEINLINE attribute_actor< AttributeValueT, fallback_to_none, TagT > attr(attribute_name const& name) Chris@16: { Chris@16: typedef attribute_actor< AttributeValueT, fallback_to_none, TagT > result_type; Chris@16: typedef typename result_type::terminal_type result_terminal; Chris@16: typename result_type::base_type act = {{ result_terminal(name) }}; Chris@16: return result_type(act); Chris@16: } Chris@16: Chris@16: } // namespace expressions Chris@16: Chris@16: BOOST_LOG_CLOSE_NAMESPACE // namespace log Chris@16: Chris@16: #ifndef BOOST_LOG_DOXYGEN_PASS Chris@16: Chris@16: namespace phoenix { Chris@16: Chris@16: namespace result_of { Chris@16: Chris@16: template< typename T, typename FallbackPolicyT, typename TagT > Chris@16: struct is_nullary< custom_terminal< boost::log::expressions::attribute_terminal< T, FallbackPolicyT, TagT > > > : Chris@16: public mpl::false_ Chris@16: { Chris@16: }; Chris@16: Chris@16: } // namespace result_of Chris@16: Chris@16: } // namespace phoenix Chris@16: Chris@16: #endif Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: #if defined(BOOST_LOG_EXPRESSIONS_FORMATTERS_STREAM_HPP_INCLUDED_) Chris@16: #include Chris@16: #endif Chris@16: Chris@16: #endif // BOOST_LOG_EXPRESSIONS_ATTR_HPP_INCLUDED_