matches.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2013.
  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 30.03.2008
  11. *
  12. * This header contains a predicate for checking if the provided string matches a regular expression.
  13. */
  14. #ifndef BOOST_LOG_UTILITY_FUNCTIONAL_MATCHES_HPP_INCLUDED_
  15. #define BOOST_LOG_UTILITY_FUNCTIONAL_MATCHES_HPP_INCLUDED_
  16. #include <boost/mpl/bool.hpp>
  17. #include <boost/mpl/identity.hpp>
  18. #include <boost/mpl/if.hpp>
  19. #include <boost/mpl/eval_if.hpp>
  20. #include <boost/log/detail/config.hpp>
  21. #include <boost/log/detail/header.hpp>
  22. #ifdef BOOST_HAS_PRAGMA_ONCE
  23. #pragma once
  24. #endif
  25. namespace boost {
  26. BOOST_LOG_OPEN_NAMESPACE
  27. namespace aux {
  28. //! This tag type is used if an expression is not supported for matching against strings
  29. struct unsupported_match_expression_tag;
  30. //! This tag type is used if an expression is recognized as a Boost.Regex expression
  31. struct boost_regex_expression_tag;
  32. //! This tag type is used if an expression is recognized as a Boost.Xpressive expression
  33. struct boost_xpressive_expression_tag;
  34. //! This tag type is used if an expression is recognized as a Boost.Spirit (classic) expression
  35. struct boost_spirit_classic_expression_tag;
  36. //! This tag type is used if an expression is recognized as a Boost.Spirit.Qi expression
  37. struct boost_spirit_qi_expression_tag;
  38. //! Preliminary declaration of a trait that detects if an expression is a Boost.Regex expression
  39. template< typename, bool = true >
  40. struct is_regex :
  41. public mpl::false_
  42. {
  43. };
  44. //! Preliminary declaration of a trait that detects if an expression is a Boost.Xpressive expression
  45. template< typename, bool = true >
  46. struct is_xpressive_regex :
  47. public mpl::false_
  48. {
  49. };
  50. //! Preliminary declaration of a trait that detects if an expression is a Boost.Spirit (classic) expression
  51. template< typename, bool = true >
  52. struct is_spirit_classic_parser :
  53. public mpl::false_
  54. {
  55. };
  56. //! Preliminary declaration of a trait that detects if an expression is a Boost.Spirit.Qi expression
  57. template< typename, bool = true >
  58. struct is_spirit_qi_parser :
  59. public mpl::false_
  60. {
  61. };
  62. //! The regex matching functor implementation
  63. template< typename TagT >
  64. struct matches_fun_impl;
  65. } // namespace aux
  66. //! The regex matching functor
  67. struct matches_fun
  68. {
  69. typedef bool result_type;
  70. private:
  71. //! A traits to obtain the tag of the expression
  72. template< typename ExpressionT >
  73. struct match_traits
  74. {
  75. typedef typename mpl::eval_if<
  76. aux::is_regex< ExpressionT >,
  77. mpl::identity< aux::boost_regex_expression_tag >,
  78. mpl::eval_if<
  79. aux::is_xpressive_regex< ExpressionT >,
  80. mpl::identity< aux::boost_xpressive_expression_tag >,
  81. mpl::eval_if<
  82. aux::is_spirit_classic_parser< ExpressionT >,
  83. mpl::identity< aux::boost_spirit_classic_expression_tag >,
  84. mpl::if_<
  85. aux::is_spirit_qi_parser< ExpressionT >,
  86. aux::boost_spirit_qi_expression_tag,
  87. aux::unsupported_match_expression_tag
  88. >
  89. >
  90. >
  91. >::type tag_type;
  92. };
  93. public:
  94. template< typename StringT, typename ExpressionT >
  95. bool operator() (StringT const& str, ExpressionT const& expr) const
  96. {
  97. typedef typename match_traits< ExpressionT >::tag_type tag_type;
  98. typedef aux::matches_fun_impl< tag_type > impl;
  99. return impl::matches(str, expr);
  100. }
  101. template< typename StringT, typename ExpressionT, typename ArgT >
  102. bool operator() (StringT const& str, ExpressionT const& expr, ArgT const& arg) const
  103. {
  104. typedef typename match_traits< ExpressionT >::tag_type tag_type;
  105. typedef aux::matches_fun_impl< tag_type > impl;
  106. return impl::matches(str, expr, arg);
  107. }
  108. };
  109. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  110. } // namespace boost
  111. #include <boost/log/detail/footer.hpp>
  112. #endif // BOOST_LOG_UTILITY_FUNCTIONAL_MATCHES_HPP_INCLUDED_