stored_rule.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*=============================================================================
  2. Copyright (c) 1998-2003 Joel de Guzman
  3. http://spirit.sourceforge.net/
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. =============================================================================*/
  7. #if !defined(BOOST_SPIRIT_STORED_RULE_HPP)
  8. #define BOOST_SPIRIT_STORED_RULE_HPP
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #include <boost/spirit/home/classic/namespace.hpp>
  11. #include <boost/spirit/home/classic/core/non_terminal/impl/rule.ipp>
  12. #include <boost/spirit/home/classic/dynamic/rule_alias.hpp>
  13. #include <boost/shared_ptr.hpp>
  14. #include <boost/spirit/home/classic/dynamic/stored_rule_fwd.hpp>
  15. ///////////////////////////////////////////////////////////////////////////////
  16. namespace boost { namespace spirit {
  17. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  18. ///////////////////////////////////////////////////////////////////////////
  19. //
  20. // stored_rule class
  21. //
  22. ///////////////////////////////////////////////////////////////////////////
  23. template <
  24. typename T0
  25. , typename T1
  26. , typename T2
  27. , bool EmbedByValue
  28. >
  29. class stored_rule
  30. : public impl::rule_base<
  31. stored_rule<T0, T1, T2, EmbedByValue>
  32. , typename mpl::if_c<
  33. EmbedByValue
  34. , stored_rule<T0, T1, T2, true>
  35. , stored_rule<T0, T1, T2> const&>::type
  36. , T0, T1, T2>
  37. {
  38. public:
  39. typedef stored_rule<T0, T1, T2, EmbedByValue> self_t;
  40. typedef impl::rule_base<
  41. self_t
  42. , typename mpl::if_c<
  43. EmbedByValue
  44. , stored_rule<T0, T1, T2, true>
  45. , self_t const&>::type
  46. , T0, T1, T2>
  47. base_t;
  48. typedef typename base_t::scanner_t scanner_t;
  49. typedef typename base_t::attr_t attr_t;
  50. typedef impl::abstract_parser<scanner_t, attr_t> abstract_parser_t;
  51. typedef rule_alias<self_t> alias_t;
  52. stored_rule() : ptr() {}
  53. ~stored_rule() {}
  54. stored_rule(stored_rule const& r)
  55. : ptr(r.ptr) {}
  56. template <typename ParserT>
  57. stored_rule(ParserT const& p)
  58. : ptr(new impl::concrete_parser<ParserT, scanner_t, attr_t>(p)) {}
  59. template <typename ParserT>
  60. stored_rule& operator=(ParserT const& p)
  61. {
  62. ptr.reset(new impl::concrete_parser<ParserT, scanner_t, attr_t>(p));
  63. return *this;
  64. }
  65. stored_rule& operator=(stored_rule const& r)
  66. {
  67. // If this is placed above the templatized assignment
  68. // operator, VC6 incorrectly complains ambiguity with
  69. // r1 = r2, where r1 and r2 are both rules.
  70. ptr = r.ptr;
  71. return *this;
  72. }
  73. stored_rule<T0, T1, T2, true>
  74. copy() const
  75. {
  76. return stored_rule<T0, T1, T2, true>(ptr);
  77. }
  78. alias_t
  79. alias() const
  80. {
  81. return alias_t(*this);
  82. }
  83. private:
  84. friend class impl::rule_base_access;
  85. friend class stored_rule<T0, T1, T2, !EmbedByValue>;
  86. abstract_parser_t*
  87. get() const
  88. {
  89. return ptr.get();
  90. }
  91. stored_rule(shared_ptr<abstract_parser_t> const& ptr)
  92. : ptr(ptr) {}
  93. shared_ptr<abstract_parser_t> ptr;
  94. };
  95. ///////////////////////////////////////////////////////////////////////////////
  96. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  97. }} // namespace BOOST_SPIRIT_CLASSIC_NS
  98. #endif