• 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   attribute_predicate.hpp
9  * \author Andrey Semashev
10  * \date   02.09.2012
11  *
12  * The header contains implementation of a generic predicate in template expressions.
13  */
14 
15 #ifndef BOOST_LOG_DETAIL_ATTRIBUTE_PREDICATE_HPP_INCLUDED_
16 #define BOOST_LOG_DETAIL_ATTRIBUTE_PREDICATE_HPP_INCLUDED_
17 
18 #include <boost/phoenix/core/actor.hpp>
19 #include <boost/utility/result_of.hpp>
20 #include <boost/log/detail/config.hpp>
21 #include <boost/log/attributes/attribute_name.hpp>
22 #include <boost/log/attributes/value_visitation.hpp>
23 #include <boost/log/attributes/fallback_policy.hpp>
24 #include <boost/log/utility/functional/bind.hpp>
25 #include <boost/log/utility/functional/save_result.hpp>
26 #include <boost/log/detail/header.hpp>
27 
28 #ifdef BOOST_HAS_PRAGMA_ONCE
29 #pragma once
30 #endif
31 
32 namespace boost {
33 
34 BOOST_LOG_OPEN_NAMESPACE
35 
36 namespace expressions {
37 
38 namespace aux {
39 
40 /*!
41  * The predicate checks if the attribute value satisfies a predicate.
42  */
43 template< typename T, typename ArgT, typename PredicateT, typename FallbackPolicyT = fallback_to_none >
44 class attribute_predicate
45 {
46 public:
47     //! Function result_type
48     typedef bool result_type;
49     //! Expected attribute value type
50     typedef T value_type;
51     //! Predicate type
52     typedef PredicateT predicate_type;
53     //! Argument type for the predicate
54     typedef ArgT argument_type;
55     //! Fallback policy
56     typedef FallbackPolicyT fallback_policy;
57 
58 private:
59     //! Argument for the predicate
60     const argument_type m_arg;
61     //! Attribute value name
62     const attribute_name m_name;
63     //! Visitor invoker
64     value_visitor_invoker< value_type, fallback_policy > m_visitor_invoker;
65 
66 public:
67     /*!
68      * Initializing constructor
69      *
70      * \param name Attribute name
71      * \param pred_arg The predicate argument
72      */
attribute_predicate(attribute_name const & name,argument_type const & pred_arg)73     attribute_predicate(attribute_name const& name, argument_type const& pred_arg) : m_arg(pred_arg), m_name(name)
74     {
75     }
76 
77     /*!
78      * Initializing constructor
79      *
80      * \param name Attribute name
81      * \param pred_arg The predicate argument
82      * \param arg Additional parameter for the fallback policy
83      */
84     template< typename U >
attribute_predicate(attribute_name const & name,argument_type const & pred_arg,U const & arg)85     attribute_predicate(attribute_name const& name, argument_type const& pred_arg, U const& arg) : m_arg(pred_arg), m_name(name), m_visitor_invoker(arg)
86     {
87     }
88 
89     /*!
90      * Checking operator
91      *
92      * \param arg A set of attribute values or a log record
93      * \return \c true if the log record contains the sought attribute value, \c false otherwise
94      */
95     template< typename ArgumentT >
operator ()(ArgumentT const & arg) const96     result_type operator() (ArgumentT const& arg) const
97     {
98         typedef binder2nd< predicate_type, argument_type const& > visitor_type;
99 
100         bool res = false;
101         m_visitor_invoker(m_name, arg, boost::log::save_result(visitor_type(predicate_type(), m_arg), res));
102         return res;
103     }
104 };
105 
106 } // namespace aux
107 
108 } // namespace expressions
109 
110 BOOST_LOG_CLOSE_NAMESPACE // namespace log
111 
112 } // namespace boost
113 
114 #include <boost/log/detail/footer.hpp>
115 
116 #endif // BOOST_LOG_DETAIL_ATTRIBUTE_PREDICATE_HPP_INCLUDED_
117