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 matches.hpp
9 * \author Andrey Semashev
10 * \date 02.09.2012
11 *
12 * The header contains implementation of a \c matches predicate in template expressions.
13 */
14
15 #ifndef BOOST_LOG_EXPRESSIONS_PREDICATES_MATCHES_HPP_INCLUDED_
16 #define BOOST_LOG_EXPRESSIONS_PREDICATES_MATCHES_HPP_INCLUDED_
17
18 #include <boost/phoenix/core/actor.hpp>
19 #include <boost/log/detail/config.hpp>
20 #include <boost/log/detail/unary_function_terminal.hpp>
21 #include <boost/log/detail/attribute_predicate.hpp>
22 #include <boost/log/expressions/attr_fwd.hpp>
23 #include <boost/log/expressions/keyword_fwd.hpp>
24 #include <boost/log/attributes/attribute_name.hpp>
25 #include <boost/log/attributes/fallback_policy.hpp>
26 #include <boost/log/utility/functional/matches.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 * The predicate checks if the attribute value matches a regular expression. The attribute value is assumed to be of a string type.
41 */
42 template< typename T, typename RegexT, typename FallbackPolicyT = fallback_to_none >
43 class attribute_matches :
44 public aux::attribute_predicate< T, typename boost::log::aux::match_traits< RegexT >::compiled_type, matches_fun, FallbackPolicyT >
45 {
46 typedef aux::attribute_predicate< T, typename boost::log::aux::match_traits< RegexT >::compiled_type, matches_fun, FallbackPolicyT > base_type;
47
48 public:
49 /*!
50 * Initializing constructor
51 *
52 * \param name Attribute name
53 * \param rex The regular expression to match the attribute value against
54 */
attribute_matches(attribute_name const & name,RegexT const & rex)55 attribute_matches(attribute_name const& name, RegexT const& rex) : base_type(name, boost::log::aux::match_traits< RegexT >::compile(rex))
56 {
57 }
58
59 /*!
60 * Initializing constructor
61 *
62 * \param name Attribute name
63 * \param rex The regular expression to match the attribute value against
64 * \param arg Additional parameter for the fallback policy
65 */
66 template< typename U >
attribute_matches(attribute_name const & name,RegexT const & rex,U const & arg)67 attribute_matches(attribute_name const& name, RegexT const& rex, U const& arg) : base_type(name, boost::log::aux::match_traits< RegexT >::compile(rex), arg)
68 {
69 }
70 };
71
72 #if defined(BOOST_MSVC) && BOOST_MSVC == 1925
73 // MSVC 14.2 has a codegen bug that makes inlined `matches` functions below crash on copy constructing the phoenix::actor on return.
74 // https://developercommunity.visualstudio.com/content/problem/982738/bad-code-generated-in-boostlogboostregex-test-case.html
75 #define BOOST_LOG_AUX_FORCEINLINE_MSVC_BUG982738 inline BOOST_NOINLINE
76 #else
77 #define BOOST_LOG_AUX_FORCEINLINE_MSVC_BUG982738 BOOST_FORCEINLINE
78 #endif
79
80 /*!
81 * The function generates a terminal node in a template expression. The node will check if the attribute value,
82 * which is assumed to be a string, matches the specified regular expression.
83 */
84 template< typename T, typename FallbackPolicyT, typename TagT, template< typename > class ActorT, typename RegexT >
85 BOOST_LOG_AUX_FORCEINLINE_MSVC_BUG982738 ActorT< aux::unary_function_terminal< attribute_matches< T, RegexT, FallbackPolicyT > > >
matches(attribute_actor<T,FallbackPolicyT,TagT,ActorT> const & attr,RegexT const & rex)86 matches(attribute_actor< T, FallbackPolicyT, TagT, ActorT > const& attr, RegexT const& rex)
87 {
88 typedef aux::unary_function_terminal< attribute_matches< T, RegexT, FallbackPolicyT > > terminal_type;
89 ActorT< terminal_type > act = {{ terminal_type(attr.get_name(), rex, attr.get_fallback_policy()) }};
90 return act;
91 }
92
93 /*!
94 * The function generates a terminal node in a template expression. The node will check if the attribute value,
95 * which is assumed to be a string, matches the specified regular expression.
96 */
97 template< typename DescriptorT, template< typename > class ActorT, typename RegexT >
98 BOOST_LOG_AUX_FORCEINLINE_MSVC_BUG982738 ActorT< aux::unary_function_terminal< attribute_matches< typename DescriptorT::value_type, RegexT > > >
matches(attribute_keyword<DescriptorT,ActorT> const &,RegexT const & rex)99 matches(attribute_keyword< DescriptorT, ActorT > const&, RegexT const& rex)
100 {
101 typedef aux::unary_function_terminal< attribute_matches< typename DescriptorT::value_type, RegexT > > terminal_type;
102 ActorT< terminal_type > act = {{ terminal_type(DescriptorT::get_name(), rex) }};
103 return act;
104 }
105
106 /*!
107 * The function generates a terminal node in a template expression. The node will check if the attribute value,
108 * which is assumed to be a string, matches the specified regular expression.
109 */
110 template< typename T, typename RegexT >
111 BOOST_LOG_AUX_FORCEINLINE_MSVC_BUG982738 phoenix::actor< aux::unary_function_terminal< attribute_matches< T, RegexT > > >
matches(attribute_name const & name,RegexT const & rex)112 matches(attribute_name const& name, RegexT const& rex)
113 {
114 typedef aux::unary_function_terminal< attribute_matches< T, RegexT > > terminal_type;
115 phoenix::actor< terminal_type > act = {{ terminal_type(name, rex) }};
116 return act;
117 }
118
119 #undef BOOST_LOG_AUX_FORCEINLINE_MSVC_BUG982738
120
121 } // namespace expressions
122
123 BOOST_LOG_CLOSE_NAMESPACE // namespace log
124
125 } // namespace boost
126
127 #include <boost/log/detail/footer.hpp>
128
129 #endif // BOOST_LOG_EXPRESSIONS_PREDICATES_MATCHES_HPP_INCLUDED_
130