adaptor.hpp 2.3 KB

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