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