matches.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #ifndef BOOST_LOG_EXPRESSIONS_PREDICATES_MATCHES_HPP_INCLUDED_
  15. #define BOOST_LOG_EXPRESSIONS_PREDICATES_MATCHES_HPP_INCLUDED_
  16. #include <boost/phoenix/core/actor.hpp>
  17. #include <boost/log/detail/config.hpp>
  18. #include <boost/log/detail/unary_function_terminal.hpp>
  19. #include <boost/log/detail/attribute_predicate.hpp>
  20. #include <boost/log/expressions/attr_fwd.hpp>
  21. #include <boost/log/expressions/keyword_fwd.hpp>
  22. #include <boost/log/attributes/attribute_name.hpp>
  23. #include <boost/log/attributes/fallback_policy.hpp>
  24. #include <boost/log/utility/functional/matches.hpp>
  25. #include <boost/log/detail/header.hpp>
  26. #ifdef BOOST_HAS_PRAGMA_ONCE
  27. #pragma once
  28. #endif
  29. namespace boost {
  30. BOOST_LOG_OPEN_NAMESPACE
  31. namespace expressions {
  32. /*!
  33. * The predicate checks if the attribute value matches a regular expression. The attribute value is assumed to be of a string type.
  34. */
  35. template< typename T, typename RegexT, typename FallbackPolicyT = fallback_to_none >
  36. class attribute_matches :
  37. public aux::attribute_predicate< T, typename boost::log::aux::match_traits< RegexT >::compiled_type, matches_fun, FallbackPolicyT >
  38. {
  39. typedef aux::attribute_predicate< T, typename boost::log::aux::match_traits< RegexT >::compiled_type, matches_fun, FallbackPolicyT > base_type;
  40. public:
  41. /*!
  42. * Initializing constructor
  43. *
  44. * \param name Attribute name
  45. * \param rex The regular expression to match the attribute value against
  46. */
  47. attribute_matches(attribute_name const& name, RegexT const& rex) : base_type(name, boost::log::aux::match_traits< RegexT >::compile(rex))
  48. {
  49. }
  50. /*!
  51. * Initializing constructor
  52. *
  53. * \param name Attribute name
  54. * \param rex The regular expression to match the attribute value against
  55. * \param arg Additional parameter for the fallback policy
  56. */
  57. template< typename U >
  58. attribute_matches(attribute_name const& name, RegexT const& rex, U const& arg) : base_type(name, boost::log::aux::match_traits< RegexT >::compile(rex), arg)
  59. {
  60. }
  61. };
  62. #if defined(BOOST_MSVC) && BOOST_MSVC == 1925
  63. // MSVC 14.2 has a codegen bug that makes inlined `matches` functions below crash on copy constructing the phoenix::actor on return.
  64. // https://developercommunity.visualstudio.com/content/problem/982738/bad-code-generated-in-boostlogboostregex-test-case.html
  65. #define BOOST_LOG_AUX_FORCEINLINE_MSVC_BUG982738 inline BOOST_NOINLINE
  66. #else
  67. #define BOOST_LOG_AUX_FORCEINLINE_MSVC_BUG982738 BOOST_FORCEINLINE
  68. #endif
  69. /*!
  70. * The function generates a terminal node in a template expression. The node will check if the attribute value,
  71. * which is assumed to be a string, matches the specified regular expression.
  72. */
  73. template< typename T, typename FallbackPolicyT, typename TagT, template< typename > class ActorT, typename RegexT >
  74. BOOST_LOG_AUX_FORCEINLINE_MSVC_BUG982738 ActorT< aux::unary_function_terminal< attribute_matches< T, RegexT, FallbackPolicyT > > >
  75. matches(attribute_actor< T, FallbackPolicyT, TagT, ActorT > const& attr, RegexT const& rex)
  76. {
  77. typedef aux::unary_function_terminal< attribute_matches< T, RegexT, FallbackPolicyT > > terminal_type;
  78. ActorT< terminal_type > act = {{ terminal_type(attr.get_name(), rex, attr.get_fallback_policy()) }};
  79. return act;
  80. }
  81. /*!
  82. * The function generates a terminal node in a template expression. The node will check if the attribute value,
  83. * which is assumed to be a string, matches the specified regular expression.
  84. */
  85. template< typename DescriptorT, template< typename > class ActorT, typename RegexT >
  86. BOOST_LOG_AUX_FORCEINLINE_MSVC_BUG982738 ActorT< aux::unary_function_terminal< attribute_matches< typename DescriptorT::value_type, RegexT > > >
  87. matches(attribute_keyword< DescriptorT, ActorT > const&, RegexT const& rex)
  88. {
  89. typedef aux::unary_function_terminal< attribute_matches< typename DescriptorT::value_type, RegexT > > terminal_type;
  90. ActorT< terminal_type > act = {{ terminal_type(DescriptorT::get_name(), rex) }};
  91. return act;
  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 T, typename RegexT >
  98. BOOST_LOG_AUX_FORCEINLINE_MSVC_BUG982738 phoenix::actor< aux::unary_function_terminal< attribute_matches< T, RegexT > > >
  99. matches(attribute_name const& name, RegexT const& rex)
  100. {
  101. typedef aux::unary_function_terminal< attribute_matches< T, RegexT > > terminal_type;
  102. phoenix::actor< terminal_type > act = {{ terminal_type(name, rex) }};
  103. return act;
  104. }
  105. #undef BOOST_LOG_AUX_FORCEINLINE_MSVC_BUG982738
  106. } // namespace expressions
  107. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  108. } // namespace boost
  109. #include <boost/log/detail/footer.hpp>
  110. #endif // BOOST_LOG_EXPRESSIONS_PREDICATES_MATCHES_HPP_INCLUDED_