std_regex.hpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 support/std_regex.hpp
  9. * \author Andrey Semashev
  10. * \date 19.03.2014
  11. *
  12. * This header enables \c std::regex support for Boost.Log.
  13. */
  14. #ifndef BOOST_LOG_SUPPORT_STD_REGEX_HPP_INCLUDED_
  15. #define BOOST_LOG_SUPPORT_STD_REGEX_HPP_INCLUDED_
  16. #include <boost/log/detail/config.hpp>
  17. #ifdef BOOST_HAS_PRAGMA_ONCE
  18. #pragma once
  19. #endif
  20. #if defined(BOOST_NO_CXX11_HDR_REGEX)
  21. #if defined(__GNUC__)
  22. #pragma message "Boost.Log: This header requires support for std::regex in the standard library."
  23. #elif defined(_MSC_VER)
  24. #pragma message("Boost.Log: This header requires support for std::regex in the standard library.")
  25. #endif
  26. #else // defined(BOOST_NO_CXX11_HDR_REGEX)
  27. #include <regex>
  28. #include <string>
  29. #include <boost/log/utility/functional/matches.hpp>
  30. #include <boost/log/detail/header.hpp>
  31. namespace boost {
  32. BOOST_LOG_OPEN_NAMESPACE
  33. namespace aux {
  34. //! This tag type is used if an expression is recognized as \c std::regex
  35. struct std_regex_expression_tag;
  36. //! The metafunction detects the matching expression kind and returns a tag that is used to specialize \c match_traits
  37. template< typename CharT, typename ReTraitsT >
  38. struct matching_expression_kind< std::basic_regex< CharT, ReTraitsT > >
  39. {
  40. typedef std_regex_expression_tag type;
  41. };
  42. //! The matching function implementation
  43. template< typename ExpressionT >
  44. struct match_traits< ExpressionT, std_regex_expression_tag >
  45. {
  46. typedef ExpressionT compiled_type;
  47. static compiled_type compile(ExpressionT const& expr) { return expr; }
  48. template< typename StringT, typename CharT, typename ReTraitsT >
  49. static bool matches(StringT const& str, std::basic_regex< CharT, ReTraitsT > const& expr, std::regex_constants::match_flag_type flags = std::regex_constants::match_default)
  50. {
  51. return std::regex_match(str.begin(), str.end(), expr, flags);
  52. }
  53. template< typename CharT, typename StringTraitsT, typename AllocatorT, typename ReTraitsT >
  54. static bool matches(std::basic_string< CharT, StringTraitsT, AllocatorT > const& str, std::basic_regex< CharT, ReTraitsT > const& expr, std::regex_constants::match_flag_type flags = std::regex_constants::match_default)
  55. {
  56. const CharT* p = str.c_str();
  57. return std::regex_match(p, p + str.size(), expr, flags);
  58. }
  59. };
  60. } // namespace aux
  61. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  62. } // namespace boost
  63. #include <boost/log/detail/footer.hpp>
  64. #endif // defined(BOOST_NO_CXX11_HDR_REGEX)
  65. #endif // BOOST_LOG_SUPPORT_STD_REGEX_HPP_INCLUDED_