regex_byref_matcher.hpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // regex_byref_matcher.hpp
  3. //
  4. // Copyright 2008 Eric Niebler. Distributed under the Boost
  5. // Software License, Version 1.0. (See accompanying file
  6. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_XPRESSIVE_DETAIL_CORE_MATCHER_REGEX_BYREF_MATCHER_HPP_EAN_10_04_2005
  8. #define BOOST_XPRESSIVE_DETAIL_CORE_MATCHER_REGEX_BYREF_MATCHER_HPP_EAN_10_04_2005
  9. // MS compatible compilers support #pragma once
  10. #if defined(_MSC_VER)
  11. # pragma once
  12. #endif
  13. #include <boost/assert.hpp>
  14. #include <boost/mpl/assert.hpp>
  15. #include <boost/shared_ptr.hpp>
  16. #include <boost/xpressive/regex_error.hpp>
  17. #include <boost/xpressive/regex_constants.hpp>
  18. #include <boost/xpressive/detail/detail_fwd.hpp>
  19. #include <boost/xpressive/detail/core/quant_style.hpp>
  20. #include <boost/xpressive/detail/core/state.hpp>
  21. #include <boost/xpressive/detail/core/regex_impl.hpp>
  22. #include <boost/xpressive/detail/core/adaptor.hpp>
  23. namespace boost { namespace xpressive { namespace detail
  24. {
  25. ///////////////////////////////////////////////////////////////////////////////
  26. // regex_byref_matcher
  27. //
  28. template<typename BidiIter>
  29. struct regex_byref_matcher
  30. : quant_style<quant_variable_width, unknown_width::value, false>
  31. {
  32. // avoid cyclic references by holding a weak_ptr to the
  33. // regex_impl struct
  34. weak_ptr<regex_impl<BidiIter> > wimpl_;
  35. // the basic_regex object holds a ref-count to this regex_impl, so
  36. // we don't have to worry about it going away.
  37. regex_impl<BidiIter> const *pimpl_;
  38. regex_byref_matcher(shared_ptr<regex_impl<BidiIter> > const &impl)
  39. : wimpl_(impl)
  40. , pimpl_(impl.get())
  41. {
  42. BOOST_ASSERT(this->pimpl_);
  43. }
  44. template<typename Next>
  45. bool match(match_state<BidiIter> &state, Next const &next) const
  46. {
  47. BOOST_ASSERT(this->pimpl_ == this->wimpl_.lock().get());
  48. BOOST_XPR_ENSURE_(this->pimpl_->xpr_, regex_constants::error_badref, "bad regex reference");
  49. return push_context_match(*this->pimpl_, state, this->wrap_(next, is_static_xpression<Next>()));
  50. }
  51. private:
  52. template<typename Next>
  53. static xpression_adaptor<reference_wrapper<Next const>, matchable<BidiIter> > wrap_(Next const &next, mpl::true_)
  54. {
  55. // wrap the static xpression in a matchable interface
  56. return xpression_adaptor<reference_wrapper<Next const>, matchable<BidiIter> >(boost::cref(next));
  57. }
  58. template<typename Next>
  59. static Next const &wrap_(Next const &next, mpl::false_)
  60. {
  61. return next;
  62. }
  63. };
  64. }}}
  65. #endif