• 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   has_attr.hpp
9  * \author Andrey Semashev
10  * \date   23.07.2012
11  *
12  * The header contains implementation of a generic attribute presence checker in template expressions.
13  */
14 
15 #ifndef BOOST_LOG_EXPRESSIONS_PREDICATES_HAS_ATTR_HPP_INCLUDED_
16 #define BOOST_LOG_EXPRESSIONS_PREDICATES_HAS_ATTR_HPP_INCLUDED_
17 
18 #include <boost/phoenix/core/actor.hpp>
19 #include <boost/log/detail/config.hpp>
20 #include <boost/log/core/record_view.hpp>
21 #include <boost/log/attributes/attribute_name.hpp>
22 #include <boost/log/attributes/attribute_value_set.hpp>
23 #include <boost/log/attributes/value_visitation.hpp>
24 #include <boost/log/expressions/keyword_fwd.hpp>
25 #include <boost/log/detail/unary_function_terminal.hpp>
26 #include <boost/log/utility/functional/nop.hpp>
27 #include <boost/log/detail/header.hpp>
28 
29 #ifdef BOOST_HAS_PRAGMA_ONCE
30 #pragma once
31 #endif
32 
33 namespace boost {
34 
35 BOOST_LOG_OPEN_NAMESPACE
36 
37 namespace expressions {
38 
39 /*!
40  * An attribute value presence checker.
41  */
42 template< typename T >
43 class has_attribute
44 {
45 public:
46     //! Function result_type
47     typedef bool result_type;
48     //! Expected attribute value type
49     typedef T value_type;
50 
51 private:
52     //! Attribute value name
53     const attribute_name m_name;
54     //! Visitor invoker
55     value_visitor_invoker< value_type > m_visitor_invoker;
56 
57 public:
58     /*!
59      * Initializing constructor
60      *
61      * \param name Attribute name
62      */
has_attribute(attribute_name const & name)63     explicit has_attribute(attribute_name const& name) : m_name(name)
64     {
65     }
66 
67     /*!
68      * Checking operator
69      *
70      * \param arg A set of attribute values or a log record
71      * \return \c true if the log record contains the sought attribute value, \c false otherwise
72      */
73     template< typename ArgT >
operator ()(ArgT const & arg) const74     result_type operator() (ArgT const& arg) const
75     {
76         return m_visitor_invoker(m_name, arg, nop()).code() == visitation_result::ok;
77     }
78 };
79 
80 /*!
81  * An attribute value presence checker. This specialization does not check the type of the attribute value.
82  */
83 template< >
84 class has_attribute< void >
85 {
86 public:
87     //! Function result_type
88     typedef bool result_type;
89     //! Expected attribute value type
90     typedef void value_type;
91 
92 private:
93     //! Attribute name
94     const attribute_name m_name;
95 
96 public:
97     /*!
98      * Initializing constructor
99      *
100      * \param name Attribute name
101      */
has_attribute(attribute_name const & name)102     explicit has_attribute(attribute_name const& name) : m_name(name)
103     {
104     }
105 
106     /*!
107      * Checking operator
108      *
109      * \param attrs A set of attribute values
110      * \return \c true if the log record contains the sought attribute value, \c false otherwise
111      */
operator ()(attribute_value_set const & attrs) const112     result_type operator() (attribute_value_set const& attrs) const
113     {
114         return attrs.find(m_name) != attrs.end();
115     }
116 
117     /*!
118      * Checking operator
119      *
120      * \param rec A log record
121      * \return \c true if the log record contains the sought attribute value, \c false otherwise
122      */
operator ()(boost::log::record_view const & rec) const123     result_type operator() (boost::log::record_view const& rec) const
124     {
125         return operator()(rec.attribute_values());
126     }
127 };
128 
129 /*!
130  * The function generates a terminal node in a template expression. The node will check for the attribute value
131  * presence in a log record. The node will also check that the attribute value has the specified type, if present.
132  */
133 template< typename AttributeValueT >
has_attr(attribute_name const & name)134 BOOST_FORCEINLINE phoenix::actor< aux::unary_function_terminal< has_attribute< AttributeValueT > > > has_attr(attribute_name const& name)
135 {
136     typedef aux::unary_function_terminal< has_attribute< AttributeValueT > > terminal_type;
137     phoenix::actor< terminal_type > act = {{ terminal_type(name) }};
138     return act;
139 }
140 
141 /*!
142  * The function generates a terminal node in a template expression. The node will check for the attribute value
143  * presence in a log record.
144  */
has_attr(attribute_name const & name)145 BOOST_FORCEINLINE phoenix::actor< aux::unary_function_terminal< has_attribute< void > > > has_attr(attribute_name const& name)
146 {
147     typedef aux::unary_function_terminal< has_attribute< void > > terminal_type;
148     phoenix::actor< terminal_type > act = {{ terminal_type(name) }};
149     return act;
150 }
151 
152 /*!
153  * The function generates a terminal node in a template expression. The node will check for the attribute value
154  * presence in a log record. The node will also check that the attribute value has the specified type, if present.
155  */
156 template< typename DescriptorT, template< typename > class ActorT >
has_attr(attribute_keyword<DescriptorT,ActorT> const &)157 BOOST_FORCEINLINE ActorT< aux::unary_function_terminal< has_attribute< typename DescriptorT::value_type > > > has_attr(attribute_keyword< DescriptorT, ActorT > const&)
158 {
159     typedef aux::unary_function_terminal< has_attribute< typename DescriptorT::value_type > > terminal_type;
160     ActorT< terminal_type > act = {{ terminal_type(DescriptorT::get_name()) }};
161     return act;
162 }
163 
164 } // namespace expressions
165 
166 BOOST_LOG_CLOSE_NAMESPACE // namespace log
167 
168 } // namespace boost
169 
170 #include <boost/log/detail/footer.hpp>
171 
172 #endif // BOOST_LOG_EXPRESSIONS_PREDICATES_HAS_ATTR_HPP_INCLUDED_
173