spirit_classic.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/spirit_classic.hpp
  9. * \author Andrey Semashev
  10. * \date 19.07.2009
  11. *
  12. * This header enables Boost.Spirit (classic) support for Boost.Log.
  13. */
  14. #ifndef BOOST_LOG_SUPPORT_SPIRIT_CLASSIC_HPP_INCLUDED_
  15. #define BOOST_LOG_SUPPORT_SPIRIT_CLASSIC_HPP_INCLUDED_
  16. #include <boost/mpl/bool.hpp>
  17. #include <boost/core/enable_if.hpp>
  18. #include <boost/log/detail/config.hpp>
  19. #include <boost/log/utility/functional/matches.hpp>
  20. #ifdef BOOST_HAS_PRAGMA_ONCE
  21. #pragma once
  22. #endif
  23. #if !defined(BOOST_LOG_NO_THREADS) && !defined(BOOST_SPIRIT_THREADSAFE) && !defined(BOOST_LOG_DOXYGEN_PASS)
  24. /*
  25. * As Boost.Log filters may be called in multiple threads concurrently,
  26. * this may lead to using Boost.Spirit parsers in a multithreaded context.
  27. * In order to protect parsers properly, BOOST_SPIRIT_THREADSAFE macro should
  28. * be defined.
  29. *
  30. * If we got here, it means that the user did not define that macro and we
  31. * have to define it ourselves. However, it may also lead to ODR violations
  32. * or even total ignorance of this macro, if the user has included Boost.Spirit
  33. * headers before including this header, or uses Boost.Spirit without the macro
  34. * in other translation units. The only reliable way to settle this problem is to
  35. * define the macro for the whole project (i.e. all translation units).
  36. */
  37. #if defined(__GNUC__)
  38. #pragma message "Boost.Log: Boost.Spirit requires BOOST_SPIRIT_THREADSAFE macro to be defined if parsers are used in a multithreaded context. It is strongly recommended to define this macro project-wide."
  39. #elif defined(_MSC_VER)
  40. #pragma message("Boost.Log: Boost.Spirit requires BOOST_SPIRIT_THREADSAFE macro to be defined if parsers are used in a multithreaded context. It is strongly recommended to define this macro project-wide.")
  41. #endif
  42. #define BOOST_SPIRIT_THREADSAFE 1
  43. #endif // !defined(BOOST_LOG_NO_THREADS) && !defined(BOOST_SPIRIT_THREADSAFE)
  44. #include <boost/spirit/include/classic_parser.hpp>
  45. #include <boost/log/detail/header.hpp>
  46. namespace boost {
  47. BOOST_LOG_OPEN_NAMESPACE
  48. namespace aux {
  49. //! This tag type is used if an expression is recognized as a Boost.Spirit.Classic expression
  50. struct boost_spirit_classic_expression_tag;
  51. //! The trait verifies if the type can be converted to a Boost.Spirit (classic) parser
  52. template< typename T >
  53. struct is_spirit_classic_parser
  54. {
  55. private:
  56. typedef char yes_type;
  57. struct no_type { char dummy[2]; };
  58. template< typename U >
  59. static yes_type check_spirit_classic_parser(spirit::classic::parser< U > const&);
  60. static no_type check_spirit_classic_parser(...);
  61. static T& get_T();
  62. public:
  63. enum { value = sizeof(check_spirit_classic_parser(get_T())) == sizeof(yes_type) };
  64. typedef mpl::bool_< value > type;
  65. };
  66. //! The metafunction detects the matching expression kind and returns a tag that is used to specialize \c match_traits
  67. template< typename ExpressionT >
  68. struct matching_expression_kind< ExpressionT, typename boost::enable_if_c< is_spirit_classic_parser< ExpressionT >::value >::type >
  69. {
  70. typedef boost_spirit_classic_expression_tag type;
  71. };
  72. //! The matching function implementation
  73. template< typename ExpressionT >
  74. struct match_traits< ExpressionT, boost_spirit_classic_expression_tag >
  75. {
  76. typedef ExpressionT compiled_type;
  77. static compiled_type compile(ExpressionT const& expr) { return expr; }
  78. template< typename StringT >
  79. static bool matches(StringT const& str, ExpressionT const& expr)
  80. {
  81. typedef typename StringT::const_iterator const_iterator;
  82. spirit::classic::parse_info< const_iterator > info =
  83. spirit::classic::parse(str.begin(), str.end(), expr);
  84. return info.full;
  85. }
  86. };
  87. } // namespace aux
  88. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  89. } // namespace boost
  90. #include <boost/log/detail/footer.hpp>
  91. #endif // BOOST_LOG_SUPPORT_SPIRIT_CLASSIC_HPP_INCLUDED_