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 value_visitation.hpp Chris@16: * \author Andrey Semashev Chris@16: * \date 01.03.2008 Chris@16: * Chris@16: * The header contains implementation of convenience tools to apply visitors to an attribute value Chris@16: * in the view. Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_LOG_ATTRIBUTES_VALUE_VISITATION_HPP_INCLUDED_ Chris@16: #define BOOST_LOG_ATTRIBUTES_VALUE_VISITATION_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@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: /*! Chris@16: * \brief The class represents attribute value visitation result Chris@16: * Chris@16: * The main purpose of this class is to provide a convenient interface for checking Chris@16: * whether the attribute value visitation succeeded or not. It also allows to discover Chris@16: * the actual cause of failure, should the operation fail. Chris@16: */ Chris@16: class visitation_result Chris@16: { Chris@16: public: Chris@16: //! Error codes for attribute value visitation Chris@16: enum error_code Chris@16: { Chris@16: ok, //!< The attribute value has been visited successfully Chris@16: value_not_found, //!< The attribute value is not present in the view Chris@16: value_has_invalid_type //!< The attribute value is present in the view, but has an unexpected type Chris@16: }; Chris@16: Chris@16: private: Chris@16: error_code m_code; Chris@16: Chris@16: public: Chris@16: /*! Chris@16: * Initializing constructor. Creates the result that is equivalent to the Chris@16: * specified error code. Chris@16: */ Chris@16: BOOST_CONSTEXPR visitation_result(error_code code = ok) BOOST_NOEXCEPT : m_code(code) {} Chris@16: Chris@16: /*! Chris@16: * Checks if the visitation was successful. Chris@16: * Chris@16: * \return \c true if the value was visited successfully, \c false otherwise. Chris@16: */ Chris@101: BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT() Chris@16: /*! Chris@16: * Checks if the visitation was unsuccessful. Chris@16: * Chris@16: * \return \c false if the value was visited successfully, \c true otherwise. Chris@16: */ Chris@16: bool operator! () const BOOST_NOEXCEPT { return (m_code != ok); } Chris@16: Chris@16: /*! Chris@16: * \return The actual result code of value visitation Chris@16: */ Chris@16: error_code code() const BOOST_NOEXCEPT { return m_code; } Chris@16: }; Chris@16: Chris@16: /*! Chris@16: * \brief Generic attribute value visitor invoker Chris@16: * Chris@16: * Attribute value invoker is a functional object that attempts to find and extract the stored Chris@16: * attribute value from the attribute value view or a log record. The extracted value is passed to Chris@16: * a unary function object (the visitor) provided by user. Chris@16: * Chris@16: * The invoker can be specialized on one or several attribute value types that should be Chris@16: * specified in the second template argument. Chris@16: */ Chris@16: template< typename T, typename FallbackPolicyT > Chris@16: class value_visitor_invoker : Chris@16: private FallbackPolicyT Chris@16: { Chris@16: typedef value_visitor_invoker< T, FallbackPolicyT > this_type; Chris@16: Chris@16: public: Chris@16: //! Attribute value types Chris@16: typedef T value_type; Chris@16: Chris@16: //! Fallback policy Chris@16: typedef FallbackPolicyT fallback_policy; Chris@16: Chris@16: //! Function object result type Chris@16: typedef visitation_result result_type; Chris@16: Chris@16: public: Chris@16: /*! Chris@16: * Default constructor Chris@16: */ Chris@16: BOOST_DEFAULTED_FUNCTION(value_visitor_invoker(), {}) Chris@16: Chris@16: /*! Chris@16: * Copy constructor Chris@16: */ Chris@16: value_visitor_invoker(value_visitor_invoker const& that) : fallback_policy(static_cast< fallback_policy const& >(that)) Chris@16: { Chris@16: } Chris@16: Chris@16: /*! Chris@16: * Initializing constructor Chris@16: * Chris@16: * \param arg Fallback policy argument Chris@16: */ Chris@16: template< typename U > Chris@16: explicit value_visitor_invoker(U const& arg) : fallback_policy(arg) {} Chris@16: Chris@16: /*! Chris@16: * Visitation operator. Attempts to acquire the stored value of one of the supported types. If acquisition succeeds, Chris@16: * the value is passed to \a visitor. Chris@16: * Chris@16: * \param attr An attribute value to apply the visitor to. Chris@16: * \param visitor A receiving function object to pass the attribute value to. Chris@16: * \return The result of visitation. Chris@16: */ Chris@16: template< typename VisitorT > Chris@16: result_type operator() (attribute_value const& attr, VisitorT visitor) const Chris@16: { Chris@16: if (!!attr) Chris@16: { Chris@16: static_type_dispatcher< value_type > disp(visitor); Chris@16: if (attr.dispatch(disp) || fallback_policy::apply_default(visitor)) Chris@16: { Chris@16: return visitation_result::ok; Chris@16: } Chris@16: else Chris@16: { Chris@16: fallback_policy::on_invalid_type(attr.get_type()); Chris@16: return visitation_result::value_has_invalid_type; Chris@16: } Chris@16: } Chris@16: Chris@16: if (fallback_policy::apply_default(visitor)) Chris@16: return visitation_result::ok; Chris@16: Chris@16: fallback_policy::on_missing_value(); Chris@16: return visitation_result::value_not_found; Chris@16: } Chris@16: Chris@16: /*! Chris@16: * Visitation operator. Looks for an attribute value with the specified name Chris@16: * and tries to acquire the stored value of one of the supported types. If acquisition succeeds, Chris@16: * the value is passed to \a visitor. Chris@16: * Chris@16: * \param name Attribute value name. Chris@16: * \param attrs A set of attribute values in which to look for the specified attribute value. Chris@16: * \param visitor A receiving function object to pass the attribute value to. Chris@16: * \return The result of visitation. Chris@16: */ Chris@16: template< typename VisitorT > Chris@16: result_type operator() (attribute_name const& name, attribute_value_set const& attrs, VisitorT visitor) const Chris@16: { Chris@16: try Chris@16: { Chris@16: attribute_value_set::const_iterator it = attrs.find(name); Chris@16: if (it != attrs.end()) Chris@16: return operator() (it->second, visitor); Chris@16: else Chris@16: return operator() (attribute_value(), visitor); Chris@16: } Chris@16: catch (exception& e) Chris@16: { Chris@16: // Attach the attribute name to the exception Chris@16: boost::log::aux::attach_attribute_name_info(e, name); Chris@16: throw; Chris@16: } Chris@16: } Chris@16: Chris@16: /*! Chris@16: * Visitation operator. Looks for an attribute value with the specified name Chris@16: * and tries to acquire the stored value of one of the supported types. If acquisition succeeds, Chris@16: * the value is passed to \a visitor. Chris@16: * Chris@16: * \param name Attribute value name. Chris@16: * \param rec A log record. The attribute value will be sought among those associated with the record. Chris@16: * \param visitor A receiving function object to pass the attribute value to. Chris@16: * \return The result of visitation. Chris@16: */ Chris@16: template< typename VisitorT > Chris@16: result_type operator() (attribute_name const& name, record const& rec, VisitorT visitor) const Chris@16: { Chris@16: return operator() (name, rec.attribute_values(), visitor); Chris@16: } Chris@16: Chris@16: /*! Chris@16: * Visitation operator. Looks for an attribute value with the specified name Chris@16: * and tries to acquire the stored value of one of the supported types. If acquisition succeeds, Chris@16: * the value is passed to \a visitor. Chris@16: * Chris@16: * \param name Attribute value name. Chris@16: * \param rec A log record view. The attribute value will be sought among those associated with the record. Chris@16: * \param visitor A receiving function object to pass the attribute value to. Chris@16: * \return The result of visitation. Chris@16: */ Chris@16: template< typename VisitorT > Chris@16: result_type operator() (attribute_name const& name, record_view const& rec, VisitorT visitor) const Chris@16: { Chris@16: return operator() (name, rec.attribute_values(), visitor); 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 *static_cast< fallback_policy const* >(this); Chris@16: } Chris@16: }; Chris@16: Chris@16: /*! Chris@16: * The function applies a visitor to an attribute value from the view. The user has to explicitly specify the Chris@16: * type or set of possible types of the attribute value to be visited. Chris@16: * Chris@16: * \param name The name of the attribute value to visit. Chris@16: * \param attrs A set of attribute values in which to look for the specified attribute value. Chris@16: * \param visitor A receiving function object to pass the attribute value to. Chris@16: * \return The result of visitation. Chris@16: */ Chris@16: template< typename T, typename VisitorT > Chris@16: inline visitation_result Chris@16: visit(attribute_name const& name, attribute_value_set const& attrs, VisitorT visitor) Chris@16: { Chris@16: value_visitor_invoker< T > invoker; Chris@16: return invoker(name, attrs, visitor); Chris@16: } Chris@16: Chris@16: /*! Chris@16: * The function applies a visitor to an attribute value from the view. The user has to explicitly specify the Chris@16: * type or set of possible types of the attribute value to be visited. Chris@16: * Chris@16: * \param name The name of the attribute value to visit. Chris@16: * \param rec A log record. The attribute value will be sought among those associated with the record. Chris@16: * \param visitor A receiving function object to pass the attribute value to. Chris@16: * \return The result of visitation. Chris@16: */ Chris@16: template< typename T, typename VisitorT > Chris@16: inline visitation_result Chris@16: visit(attribute_name const& name, record const& rec, VisitorT visitor) Chris@16: { Chris@16: value_visitor_invoker< T > invoker; Chris@16: return invoker(name, rec, visitor); Chris@16: } Chris@16: Chris@16: /*! Chris@16: * The function applies a visitor to an attribute value from the view. The user has to explicitly specify the Chris@16: * type or set of possible types of the attribute value to be visited. Chris@16: * Chris@16: * \param name The name of the attribute value to visit. Chris@16: * \param rec A log record view. The attribute value will be sought among those associated with the record. Chris@16: * \param visitor A receiving function object to pass the attribute value to. Chris@16: * \return The result of visitation. Chris@16: */ Chris@16: template< typename T, typename VisitorT > Chris@16: inline visitation_result Chris@16: visit(attribute_name const& name, record_view const& rec, VisitorT visitor) Chris@16: { Chris@16: value_visitor_invoker< T > invoker; Chris@16: return invoker(name, rec, visitor); Chris@16: } Chris@16: Chris@16: /*! Chris@16: * The function applies a visitor to an attribute value. The user has to explicitly specify the Chris@16: * type or set of possible types of the attribute value to be visited. Chris@16: * Chris@16: * \param value The attribute value to visit. Chris@16: * \param visitor A receiving function object to pass the attribute value to. Chris@16: * \return The result of visitation. Chris@16: */ Chris@16: template< typename T, typename VisitorT > Chris@16: inline visitation_result Chris@16: visit(attribute_value const& value, VisitorT visitor) Chris@16: { Chris@16: value_visitor_invoker< T > invoker; Chris@16: return invoker(value, visitor); Chris@16: } Chris@16: Chris@16: /*! Chris@16: * The function applies a visitor to an attribute value from the view. The user has to explicitly specify the Chris@16: * type or set of possible types of the attribute value to be visited. Chris@16: * Chris@16: * \param keyword The keyword of the attribute value to visit. Chris@16: * \param attrs A set of attribute values in which to look for the specified attribute value. Chris@16: * \param visitor A receiving function object to pass the attribute value to. Chris@16: * \return The result of visitation. Chris@16: */ Chris@16: template< typename DescriptorT, template< typename > class ActorT, typename VisitorT > Chris@16: inline visitation_result Chris@16: visit(expressions::attribute_keyword< DescriptorT, ActorT > const& keyword, attribute_value_set const& attrs, VisitorT visitor) Chris@16: { Chris@16: value_visitor_invoker< typename DescriptorT::value_type > invoker; Chris@16: return invoker(keyword.get_name(), attrs, visitor); Chris@16: } Chris@16: Chris@16: /*! Chris@16: * The function applies a visitor to an attribute value from the view. The user has to explicitly specify the Chris@16: * type or set of possible types of the attribute value to be visited. Chris@16: * Chris@16: * \param keyword The keyword of the attribute value to visit. Chris@16: * \param rec A log record. The attribute value will be sought among those associated with the record. Chris@16: * \param visitor A receiving function object to pass the attribute value to. Chris@16: * \return The result of visitation. Chris@16: */ Chris@16: template< typename DescriptorT, template< typename > class ActorT, typename VisitorT > Chris@16: inline visitation_result Chris@16: visit(expressions::attribute_keyword< DescriptorT, ActorT > const& keyword, record const& rec, VisitorT visitor) Chris@16: { Chris@16: value_visitor_invoker< typename DescriptorT::value_type > invoker; Chris@16: return invoker(keyword.get_name(), rec, visitor); Chris@16: } Chris@16: Chris@16: /*! Chris@16: * The function applies a visitor to an attribute value from the view. The user has to explicitly specify the Chris@16: * type or set of possible types of the attribute value to be visited. Chris@16: * Chris@16: * \param keyword The keyword of the attribute value to visit. Chris@16: * \param rec A log record view. The attribute value will be sought among those associated with the record. Chris@16: * \param visitor A receiving function object to pass the attribute value to. Chris@16: * \return The result of visitation. Chris@16: */ Chris@16: template< typename DescriptorT, template< typename > class ActorT, typename VisitorT > Chris@16: inline visitation_result Chris@16: visit(expressions::attribute_keyword< DescriptorT, ActorT > const& keyword, record_view const& rec, VisitorT visitor) Chris@16: { Chris@16: value_visitor_invoker< typename DescriptorT::value_type > invoker; Chris@16: return invoker(keyword.get_name(), rec, visitor); Chris@16: } Chris@16: Chris@16: Chris@16: #if !defined(BOOST_LOG_DOXYGEN_PASS) Chris@16: Chris@16: template< typename T, typename VisitorT > Chris@16: inline visitation_result attribute_value::visit(VisitorT visitor) const Chris@16: { Chris@16: return boost::log::visit< T >(*this, visitor); Chris@16: } Chris@16: Chris@16: #endif // !defined(BOOST_LOG_DOXYGEN_PASS) Chris@16: Chris@16: BOOST_LOG_CLOSE_NAMESPACE // namespace log Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_LOG_ATTRIBUTES_VALUE_VISITATION_HPP_INCLUDED_