2 // Copyright Oliver Kowalke 2009.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
10 #include <boost/coroutine/all.hpp>
15 #include <boost/assert.hpp>
16 #include <boost/config.hpp>
17 #include <boost/intrusive_ptr.hpp>
20 # pragma warning(push)
21 # pragma warning(disable:4355)
29 virtual ~visitor() {};
31 virtual void visit( branch & b) = 0;
32 virtual void visit( leaf & l) = 0;
37 typedef boost::intrusive_ptr< node > ptr_t;
39 std::size_t use_count;
47 virtual void accept( visitor & v) = 0;
49 friend inline void intrusive_ptr_add_ref( node * p)
52 friend inline void intrusive_ptr_release( node * p)
53 { if ( 0 == --p->use_count) delete p; }
56 struct branch : public node
61 static ptr_t create( node::ptr_t left_, node::ptr_t right_)
62 { return ptr_t( new branch( left_, right_) ); }
64 branch( node::ptr_t left_, node::ptr_t right_) :
65 left( left_), right( right_)
68 void accept( visitor & v)
72 struct leaf : public node
76 static ptr_t create( std::string const& value_)
77 { return ptr_t( new leaf( value_) ); }
79 leaf( std::string const& value_) :
83 void accept( visitor & v)
88 bool operator==( leaf const& l, leaf const& r)
89 { return l.value == r.value; }
92 bool operator!=( leaf const& l, leaf const& r)
93 { return l.value != r.value; }
95 class tree_visitor : public visitor
98 boost::coroutines::asymmetric_coroutine< leaf & >::push_type & c_;
101 tree_visitor( boost::coroutines::asymmetric_coroutine< leaf & >::push_type & c) :
105 void visit( branch & b)
107 if ( b.left) b.left->accept( * this);
108 if ( b.right) b.right->accept( * this);
111 void visit( leaf & l)
115 void enumerate_leafs( boost::coroutines::asymmetric_coroutine< leaf & >::push_type & c, node::ptr_t root)
121 #if defined(_MSC_VER)
122 # pragma warning(pop)