Imported Upstream version 1.57.0
[platform/upstream/boost.git] / boost / xpressive / detail / core / adaptor.hpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // adaptor.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_ADAPTOR_HPP_EAN_10_04_2005
9 #define BOOST_XPRESSIVE_DETAIL_CORE_ADAPTOR_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/ref.hpp>
17 #include <boost/implicit_cast.hpp>
18 #include <boost/intrusive_ptr.hpp>
19 #include <boost/xpressive/detail/detail_fwd.hpp>
20 #include <boost/xpressive/detail/dynamic/matchable.hpp>
21
22 namespace boost { namespace xpressive { namespace detail
23 {
24
25 ///////////////////////////////////////////////////////////////////////////////
26 // xpression_adaptor
27 //
28 //   wrap a static xpression in a matchable interface so it can be stored
29 //   in and invoked from a basic_regex object.
30 template<typename Xpr, typename Base>
31 struct xpression_adaptor
32   : Base // either matchable or matchable_ex
33 {
34     typedef typename Base::iterator_type iterator_type;
35     typedef typename iterator_value<iterator_type>::type char_type;
36
37     Xpr xpr_;
38
39     xpression_adaptor(Xpr const &xpr)
40     #if BOOST_WORKAROUND(__GNUC__, BOOST_TESTED_AT(4))
41         // Ugh, gcc has an optimizer bug which elides this c'tor call
42         // resulting in pure virtual function calls.
43         __attribute__((__noinline__))
44     #endif
45       : xpr_(xpr)
46     {
47     }
48
49     virtual bool match(match_state<iterator_type> &state) const
50     {
51         typedef typename boost::unwrap_reference<Xpr const>::type xpr_type;
52         return implicit_cast<xpr_type &>(this->xpr_).match(state);
53     }
54
55     void link(xpression_linker<char_type> &linker) const
56     {
57         this->xpr_.link(linker);
58     }
59
60     void peek(xpression_peeker<char_type> &peeker) const
61     {
62         this->xpr_.peek(peeker);
63     }
64
65 private:
66     xpression_adaptor &operator =(xpression_adaptor const &);
67 };
68
69 ///////////////////////////////////////////////////////////////////////////////
70 // make_adaptor
71 //
72 template<typename Base, typename Xpr>
73 inline intrusive_ptr<Base const> make_adaptor(Xpr const &xpr)
74 {
75     return intrusive_ptr<Base const>(new xpression_adaptor<Xpr, Base>(xpr));
76 }
77
78 }}} // namespace boost::xpressive::detail
79
80 #endif