• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #ifndef BOOST_XPRESSIVE_DETAIL_CORE_MATCHER_REGEX_BYREF_MATCHER_HPP_EAN_10_04_2005
9 #define BOOST_XPRESSIVE_DETAIL_CORE_MATCHER_REGEX_BYREF_MATCHER_HPP_EAN_10_04_2005
10 
11 // MS compatible compilers support #pragma once
12 #if defined(_MSC_VER)
13 # pragma once
14 #endif
15 
16 #include <boost/assert.hpp>
17 #include <boost/mpl/assert.hpp>
18 #include <boost/shared_ptr.hpp>
19 #include <boost/xpressive/regex_error.hpp>
20 #include <boost/xpressive/regex_constants.hpp>
21 #include <boost/xpressive/detail/detail_fwd.hpp>
22 #include <boost/xpressive/detail/core/quant_style.hpp>
23 #include <boost/xpressive/detail/core/state.hpp>
24 #include <boost/xpressive/detail/core/regex_impl.hpp>
25 #include <boost/xpressive/detail/core/adaptor.hpp>
26 
27 namespace boost { namespace xpressive { namespace detail
28 {
29     ///////////////////////////////////////////////////////////////////////////////
30     // regex_byref_matcher
31     //
32     template<typename BidiIter>
33     struct regex_byref_matcher
34       : quant_style<quant_variable_width, unknown_width::value, false>
35     {
36         // avoid cyclic references by holding a weak_ptr to the
37         // regex_impl struct
38         weak_ptr<regex_impl<BidiIter> > wimpl_;
39 
40         // the basic_regex object holds a ref-count to this regex_impl, so
41         // we don't have to worry about it going away.
42         regex_impl<BidiIter> const *pimpl_;
43 
regex_byref_matcherboost::xpressive::detail::regex_byref_matcher44         regex_byref_matcher(shared_ptr<regex_impl<BidiIter> > const &impl)
45           : wimpl_(impl)
46           , pimpl_(impl.get())
47         {
48             BOOST_ASSERT(this->pimpl_);
49         }
50 
51         template<typename Next>
matchboost::xpressive::detail::regex_byref_matcher52         bool match(match_state<BidiIter> &state, Next const &next) const
53         {
54             BOOST_ASSERT(this->pimpl_ == this->wimpl_.lock().get());
55             BOOST_XPR_ENSURE_(this->pimpl_->xpr_, regex_constants::error_badref, "bad regex reference");
56 
57             return push_context_match(*this->pimpl_, state, this->wrap_(next, is_static_xpression<Next>()));
58         }
59 
60     private:
61         template<typename Next>
wrap_boost::xpressive::detail::regex_byref_matcher62         static xpression_adaptor<reference_wrapper<Next const>, matchable<BidiIter> > wrap_(Next const &next, mpl::true_)
63         {
64             // wrap the static xpression in a matchable interface
65             return xpression_adaptor<reference_wrapper<Next const>, matchable<BidiIter> >(boost::cref(next));
66         }
67 
68         template<typename Next>
wrap_boost::xpressive::detail::regex_byref_matcher69         static Next const &wrap_(Next const &next, mpl::false_)
70         {
71             return next;
72         }
73     };
74 
75 }}}
76 
77 #endif
78