1 /*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
3 Copyright (c) 2001-2011 Hartmut Kaiser
4 Copyright (c) 2011 Bryce Lelbach
6 Distributed under the Boost Software License, Version 1.0. (See accompanying
7 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 =============================================================================*/
9 #if !defined(BOOST_SPIRIT_UTREE_DETAIL2)
10 #define BOOST_SPIRIT_UTREE_DETAIL2
12 #if defined(BOOST_MSVC)
13 # pragma warning(push)
14 # pragma warning(disable: 4800)
17 #include <boost/type_traits/remove_pointer.hpp>
18 #include <boost/type_traits/is_pointer.hpp>
19 #include <boost/utility/enable_if.hpp>
20 #include <boost/throw_exception.hpp>
21 #include <boost/iterator/iterator_traits.hpp>
23 namespace boost { namespace spirit { namespace detail
25 inline char& fast_string::info()
27 return buff[small_string_size];
30 inline char fast_string::info() const
32 return buff[small_string_size];
35 inline int fast_string::get_type() const
40 inline void fast_string::set_type(int t)
42 info() = (t << 1) | (info() & 1);
45 inline short fast_string::tag() const
47 return (int(buff[small_string_size-2]) << 8) + (unsigned char)buff[small_string_size-1];
50 inline void fast_string::tag(short tag)
52 buff[small_string_size-2] = tag >> 8;
53 buff[small_string_size-1] = tag & 0xff;
56 inline bool fast_string::is_heap_allocated() const
61 inline std::size_t fast_string::size() const
63 if (is_heap_allocated())
66 return max_string_len - buff[max_string_len];
69 inline char const* fast_string::str() const
71 if (is_heap_allocated())
77 template <typename Iterator>
78 inline void fast_string::construct(Iterator f, Iterator l)
80 unsigned const size = l-f;
82 if (size < max_string_len)
84 // if it fits, store it in-situ; small_string_size minus the length
85 // of the string is placed in buff[small_string_size - 1]
87 buff[max_string_len] = static_cast<char>(max_string_len - size);
92 // else, store it in the heap
93 str = new char[size + 1]; // add one for the null char
98 for (std::size_t i = 0; i != size; ++i)
102 *str = '\0'; // add the null char
105 inline void fast_string::swap(fast_string& other)
107 std::swap(*this, other);
110 inline void fast_string::free()
112 if (is_heap_allocated())
118 inline void fast_string::copy(fast_string const& other)
120 construct(other.str(), other.str() + other.size());
123 inline void fast_string::initialize()
125 for (std::size_t i = 0; i != buff_size / (sizeof(long)/sizeof(char)); ++i)
129 struct list::node : boost::noncopyable
131 template <typename T>
132 node(T const& val, node* next, node* prev)
133 : val(val), next(next), prev(prev) {}
146 template <typename Value>
147 class list::node_iterator
148 : public boost::iterator_facade<
151 , boost::bidirectional_traversal_tag>
156 : node(0), prev(0) {}
158 node_iterator(list::node* node, list::node* prev)
159 : node(node), prev(prev) {}
163 friend class boost::iterator_core_access;
164 friend class boost::spirit::utree;
165 friend struct boost::spirit::detail::list;
169 if (node != 0) // not at end
178 if (prev != 0) // not at begin
185 bool equal(node_iterator const& other) const
187 return node == other.node;
190 typename node_iterator::reference dereference() const
199 template <typename Value>
200 class list::node_iterator<boost::reference_wrapper<Value> >
201 : public boost::iterator_facade<
202 node_iterator<boost::reference_wrapper<Value> >
203 , boost::reference_wrapper<Value>
204 , boost::bidirectional_traversal_tag>
209 : node(0), prev(0), curr(nil_node) {}
211 node_iterator(list::node* node, list::node* prev)
212 : node(node), prev(prev), curr(node ? node->val : nil_node) {}
216 friend class boost::iterator_core_access;
217 friend class boost::spirit::utree;
218 friend struct boost::spirit::detail::list;
222 if (node != 0) // not at end
226 curr = boost::ref(node ? node->val : nil_node);
232 if (prev != 0) // not at begin
236 curr = boost::ref(node ? node->val : nil_node);
240 bool equal(node_iterator const& other) const
242 return node == other.node;
245 typename node_iterator::reference dereference() const
253 static Value nil_node;
254 mutable boost::reference_wrapper<Value> curr;
257 template <typename Value>
258 Value list::node_iterator<boost::reference_wrapper<Value> >::nil_node = Value();
260 inline void list::free()
265 node* next = p->next;
271 inline void list::copy(list const& other)
273 node* p = other.first;
281 inline void list::default_construct()
287 template <typename T, typename Iterator>
288 inline void list::insert(T const& val, Iterator pos)
296 detail::list::node* new_node =
297 new detail::list::node(val, pos.node, pos.node->prev);
300 pos.node->prev->next = new_node;
304 pos.node->prev = new_node;
308 template <typename T>
309 inline void list::push_front(T const& val)
311 detail::list::node* new_node;
314 new_node = new detail::list::node(val, 0, 0);
315 first = last = new_node;
320 new_node = new detail::list::node(val, first, first->prev);
321 first->prev = new_node;
327 template <typename T>
328 inline void list::push_back(T const& val)
333 detail::list::node* new_node =
334 new detail::list::node(val, last->next, last);
335 last->next = new_node;
341 inline void list::pop_front()
343 BOOST_ASSERT(size != 0);
344 if (first == last) // there's only one item
360 inline void list::pop_back()
362 BOOST_ASSERT(size != 0);
363 if (first == last) // there's only one item
379 inline list::node* list::erase(node* pos)
381 BOOST_ASSERT(pos != 0);
387 else if (pos == last)
394 node* next(pos->next);
402 ///////////////////////////////////////////////////////////////////////////
403 // simple binder for binary visitation (we don't want to bring in the big guns)
404 template <typename F, typename X>
407 typedef typename F::result_type result_type;
408 X& x; // always by reference
410 bind_impl(F f, X& x) : x(x), f(f) {}
412 template <typename Y>
413 typename F::result_type operator()(Y& y) const
418 template <typename Y>
419 typename F::result_type operator()(Y const& y) const
425 template <typename F, typename X>
426 bind_impl<F, X const> bind(F f, X const& x)
428 return bind_impl<F, X const>(f, x);
431 template <typename F, typename X>
432 bind_impl<F, X> bind(F f, X& x)
434 return bind_impl<F, X>(f, x);
437 template <typename UTreeX, typename UTreeY = UTreeX>
440 template <typename F>
441 typename F::result_type
442 static apply(UTreeX& x, F f) // single dispatch
445 boost::mpl::if_<boost::is_const<UTreeX>,
446 typename UTreeX::const_iterator,
447 typename UTreeX::iterator>::type
450 typedef boost::iterator_range<iterator> list_range;
451 typedef utree_type type;
453 switch (x.get_type())
456 BOOST_THROW_EXCEPTION(
457 bad_type_exception("corrupt utree type", x.get_type()));
460 case type::invalid_type:
466 case type::bool_type:
472 case type::double_type:
475 case type::list_type:
476 return f(list_range(iterator(x.l.first, 0), iterator(0, x.l.last)));
478 case type::range_type:
479 return f(list_range(iterator(x.r.first, 0), iterator(0, x.r.last)));
481 case type::string_type:
482 return f(utf8_string_range_type(x.s.str(), x.s.size()));
484 case type::string_range_type:
485 return f(utf8_string_range_type(x.sr.first, x.sr.last));
487 case type::symbol_type:
488 return f(utf8_symbol_range_type(x.s.str(), x.s.size()));
490 case type::binary_type:
491 return f(binary_range_type(x.s.str(), x.s.size()));
493 case type::reference_type:
494 return apply(*x.p, f);
497 return f(any_ptr(x.v.p, x.v.i));
499 case type::function_type:
504 template <typename F>
505 typename F::result_type
506 static apply(UTreeX& x, UTreeY& y, F f) // double dispatch
509 boost::mpl::if_<boost::is_const<UTreeX>,
510 typename UTreeX::const_iterator,
511 typename UTreeX::iterator>::type
514 typedef boost::iterator_range<iterator> list_range;
515 typedef utree_type type;
517 switch (x.get_type())
520 BOOST_THROW_EXCEPTION(
521 bad_type_exception("corrupt utree type", x.get_type()));
524 case type::invalid_type:
525 return visit_impl::apply(y, detail::bind(f, invalid));
528 return visit_impl::apply(y, detail::bind(f, nil));
530 case type::bool_type:
531 return visit_impl::apply(y, detail::bind(f, x.b));
534 return visit_impl::apply(y, detail::bind(f, x.i));
536 case type::double_type:
537 return visit_impl::apply(y, detail::bind(f, x.d));
539 case type::list_type:
540 return visit_impl::apply(
541 y, detail::bind<F, list_range>(f,
542 list_range(iterator(x.l.first, 0), iterator(0, x.l.last))));
544 case type::range_type:
545 return visit_impl::apply(
546 y, detail::bind<F, list_range>(f,
547 list_range(iterator(x.r.first, 0), iterator(0, x.r.last))));
549 case type::string_type:
550 return visit_impl::apply(y, detail::bind(
551 f, utf8_string_range_type(x.s.str(), x.s.size())));
553 case type::string_range_type:
554 return visit_impl::apply(y, detail::bind(
555 f, utf8_string_range_type(x.sr.first, x.sr.last)));
557 case type::symbol_type:
558 return visit_impl::apply(y, detail::bind(
559 f, utf8_symbol_range_type(x.s.str(), x.s.size())));
561 case type::binary_type:
562 return visit_impl::apply(y, detail::bind(
563 f, binary_range_type(x.s.str(), x.s.size())));
565 case type::reference_type:
566 return apply(*x.p, y, f);
569 return visit_impl::apply(
570 y, detail::bind(f, any_ptr(x.v.p, x.v.i)));
572 case type::function_type:
573 return visit_impl::apply(y, detail::bind(f, *x.pf));
580 static utree& apply(utree& ut, std::size_t i)
582 switch (ut.get_type())
584 case utree_type::reference_type:
585 return apply(ut.deref(), i);
586 case utree_type::range_type:
587 return apply(ut.r.first, i);
588 case utree_type::list_type:
589 return apply(ut.l.first, i);
591 BOOST_THROW_EXCEPTION(
593 ("index operation performed on non-list utree type",
598 static utree const& apply(utree const& ut, std::size_t i)
600 switch (ut.get_type())
602 case utree_type::reference_type:
603 return apply(ut.deref(), i);
604 case utree_type::range_type:
605 return apply(ut.r.first, i);
606 case utree_type::list_type:
607 return apply(ut.l.first, i);
609 BOOST_THROW_EXCEPTION(
611 ("index operation performed on non-list utree type",
616 static utree& apply(list::node* node, std::size_t i)
623 static utree const& apply(list::node const* node, std::size_t i)
632 namespace boost { namespace spirit
634 template <typename F>
635 stored_function<F>::stored_function(F f)
640 template <typename F>
641 stored_function<F>::~stored_function()
645 template <typename F>
646 utree stored_function<F>::operator()(utree const& env) const
651 template <typename F>
652 utree stored_function<F>::operator()(utree& env) const
657 template <typename F>
659 stored_function<F>::clone() const
661 return new stored_function<F>(f);
664 template <typename F>
665 referenced_function<F>::referenced_function(F& f)
670 template <typename F>
671 referenced_function<F>::~referenced_function()
675 template <typename F>
676 utree referenced_function<F>::operator()(utree const& env) const
681 template <typename F>
682 utree referenced_function<F>::operator()(utree& env) const
687 template <typename F>
689 referenced_function<F>::clone() const
691 return new referenced_function<F>(f);
694 inline utree::utree(utree::invalid_type)
697 set_type(type::invalid_type);
700 inline utree::utree(utree::nil_type)
703 set_type(type::nil_type);
706 inline utree::utree(bool b_)
710 set_type(type::bool_type);
713 inline utree::utree(char c)
716 // char constructs a single element string
717 s.construct(&c, &c+1);
718 set_type(type::string_type);
721 inline utree::utree(unsigned int i_)
725 set_type(type::int_type);
728 inline utree::utree(int i_)
732 set_type(type::int_type);
735 inline utree::utree(double d_)
739 set_type(type::double_type);
742 inline utree::utree(char const* str)
745 s.construct(str, str + strlen(str));
746 set_type(type::string_type);
749 inline utree::utree(char const* str, std::size_t len)
752 s.construct(str, str + len);
753 set_type(type::string_type);
756 inline utree::utree(std::string const& str)
759 s.construct(str.begin(), str.end());
760 set_type(type::string_type);
763 template <typename Base, utree_type::info type_>
764 inline utree::utree(basic_string<Base, type_> const& bin)
767 s.construct(bin.begin(), bin.end());
771 inline utree::utree(boost::reference_wrapper<utree> ref)
774 p = ref.get_pointer();
775 set_type(type::reference_type);
778 inline utree::utree(any_ptr const& p)
783 set_type(type::any_type);
786 inline utree::utree(function_base const& pf_)
790 set_type(type::function_type);
793 inline utree::utree(function_base* pf_)
797 set_type(type::function_type);
800 template <typename Iter>
801 inline utree::utree(boost::iterator_range<Iter> r)
805 assign(r.begin(), r.end());
808 inline utree::utree(range r, shallow_tag)
811 this->r.first = r.begin().node;
812 this->r.last = r.end().prev;
813 set_type(type::range_type);
816 inline utree::utree(const_range r, shallow_tag)
819 this->r.first = r.begin().node;
820 this->r.last = r.end().prev;
821 set_type(type::range_type);
824 inline utree::utree(utf8_string_range_type const& str, shallow_tag)
827 this->sr.first = str.begin();
828 this->sr.last = str.end();
829 set_type(type::string_range_type);
832 inline utree::utree(utree const& other)
838 inline utree::~utree()
843 inline utree& utree::operator=(utree const& other)
853 inline utree& utree::operator=(nil_type)
856 set_type(type::nil_type);
860 inline utree& utree::operator=(bool b_)
864 set_type(type::bool_type);
868 inline utree& utree::operator=(char c)
870 // char constructs a single element string
872 s.construct(&c, &c+1);
873 set_type(type::string_type);
877 inline utree& utree::operator=(unsigned int i_)
881 set_type(type::int_type);
885 inline utree& utree::operator=(int i_)
889 set_type(type::int_type);
893 inline utree& utree::operator=(double d_)
897 set_type(type::double_type);
901 inline utree& utree::operator=(char const* s_)
904 s.construct(s_, s_ + strlen(s_));
905 set_type(type::string_type);
909 inline utree& utree::operator=(std::string const& s_)
912 s.construct(s_.begin(), s_.end());
913 set_type(type::string_type);
917 template <typename Base, utree_type::info type_>
918 inline utree& utree::operator=(basic_string<Base, type_> const& bin)
921 s.construct(bin.begin(), bin.end());
926 inline utree& utree::operator=(boost::reference_wrapper<utree> ref)
929 p = ref.get_pointer();
930 set_type(type::reference_type);
934 inline utree& utree::operator=(any_ptr const& p)
939 set_type(type::any_type);
943 inline utree& utree::operator=(function_base const& pf_)
947 set_type(type::function_type);
951 inline utree& utree::operator=(function_base* pf_)
955 set_type(type::function_type);
959 template <typename Iter>
960 inline utree& utree::operator=(boost::iterator_range<Iter> r)
963 assign(r.begin(), r.end());
967 template <typename F>
968 typename boost::result_of<F(utree const&)>::type
969 inline utree::visit(utree const& x, F f)
971 return detail::visit_impl<utree const>::apply(x, f);
974 template <typename F>
975 typename boost::result_of<F(utree&)>::type
976 inline utree::visit(utree& x, F f)
978 return detail::visit_impl<utree>::apply(x, f);
981 template <typename F>
982 typename boost::result_of<F(utree const&, utree const&)>::type
983 inline utree::visit(utree const& x, utree const& y, F f)
985 return detail::visit_impl<utree const, utree const>::apply(x, y, f);
988 template <typename F>
989 typename boost::result_of<F(utree const&, utree&)>::type
990 inline utree::visit(utree const& x, utree& y, F f)
992 return detail::visit_impl<utree const, utree>::apply(x, y, f);
995 template <typename F>
996 typename boost::result_of<F(utree&, utree const&)>::type
997 inline utree::visit(utree& x, utree const& y, F f)
999 return detail::visit_impl<utree, utree const>::apply(x, y, f);
1002 template <typename F>
1003 typename boost::result_of<F(utree&, utree&)>::type
1004 inline utree::visit(utree& x, utree& y, F f)
1006 return detail::visit_impl<utree, utree>::apply(x, y, f);
1009 inline utree::reference get(utree::reference ut, utree::size_type i)
1010 { return detail::index_impl::apply(ut, i); }
1012 inline utree::const_reference
1013 get(utree::const_reference ut, utree::size_type i)
1014 { return detail::index_impl::apply(ut, i); }
1016 template <typename T>
1017 inline void utree::push_front(T const& val)
1019 if (get_type() == type::reference_type)
1020 return p->push_front(val);
1022 ensure_list_type("push_front()");
1026 template <typename T>
1027 inline void utree::push_back(T const& val)
1029 if (get_type() == type::reference_type)
1030 return p->push_back(val);
1032 ensure_list_type("push_back()");
1036 template <typename T>
1037 inline utree::iterator utree::insert(iterator pos, T const& val)
1039 if (get_type() == type::reference_type)
1040 return p->insert(pos, val);
1042 ensure_list_type("insert()");
1046 return utree::iterator(l.last, l.last->prev);
1049 return utree::iterator(pos.node->prev, pos.node->prev->prev);
1052 template <typename T>
1053 inline void utree::insert(iterator pos, std::size_t n, T const& val)
1055 if (get_type() == type::reference_type)
1056 return p->insert(pos, n, val);
1058 ensure_list_type("insert()");
1059 for (std::size_t i = 0; i != n; ++i)
1063 template <typename Iterator>
1064 inline void utree::insert(iterator pos, Iterator first, Iterator last)
1066 if (get_type() == type::reference_type)
1067 return p->insert(pos, first, last);
1069 ensure_list_type("insert()");
1070 while (first != last)
1071 insert(pos, *first++);
1074 template <typename Iterator>
1075 inline void utree::assign(Iterator first, Iterator last)
1077 if (get_type() == type::reference_type)
1078 return p->assign(first, last);
1081 set_type(type::list_type);
1083 while (first != last)
1090 inline void utree::clear()
1092 if (get_type() == type::reference_type)
1095 // clear will always make this an invalid type
1097 set_type(type::invalid_type);
1100 inline void utree::pop_front()
1102 if (get_type() == type::reference_type)
1103 return p->pop_front();
1104 if (get_type() != type::list_type)
1105 BOOST_THROW_EXCEPTION(
1107 ("pop_front() called on non-list utree type",
1113 inline void utree::pop_back()
1115 if (get_type() == type::reference_type)
1116 return p->pop_back();
1117 if (get_type() != type::list_type)
1118 BOOST_THROW_EXCEPTION(
1120 ("pop_back() called on non-list utree type",
1126 inline utree::iterator utree::erase(iterator pos)
1128 if (get_type() == type::reference_type)
1129 return p->erase(pos);
1130 if (get_type() != type::list_type)
1131 BOOST_THROW_EXCEPTION(
1133 ("erase() called on non-list utree type",
1136 detail::list::node* np = l.erase(pos.node);
1137 return iterator(np, np?np->prev:l.last);
1140 inline utree::iterator utree::erase(iterator first, iterator last)
1142 if (get_type() == type::reference_type)
1143 return p->erase(first, last);
1145 if (get_type() != type::list_type)
1146 BOOST_THROW_EXCEPTION(
1148 ("erase() called on non-list utree type",
1150 while (first != last)
1155 inline utree::iterator utree::begin()
1157 if (get_type() == type::reference_type)
1159 else if (get_type() == type::range_type)
1160 return iterator(r.first, 0);
1163 ensure_list_type("begin()");
1164 return iterator(l.first, 0);
1167 inline utree::iterator utree::end()
1169 if (get_type() == type::reference_type)
1171 else if (get_type() == type::range_type)
1172 return iterator(0, r.first);
1175 ensure_list_type("end()");
1176 return iterator(0, l.last);
1179 inline utree::ref_iterator utree::ref_begin()
1181 if (get_type() == type::reference_type)
1182 return p->ref_begin();
1183 else if (get_type() == type::range_type)
1184 return ref_iterator(r.first, 0);
1187 ensure_list_type("ref_begin()");
1188 return ref_iterator(l.first, 0);
1191 inline utree::ref_iterator utree::ref_end()
1193 if (get_type() == type::reference_type)
1194 return p->ref_end();
1195 else if (get_type() == type::range_type)
1196 return ref_iterator(0, r.first);
1199 ensure_list_type("ref_end()");
1200 return ref_iterator(0, l.last);
1203 inline utree::const_iterator utree::begin() const
1205 if (get_type() == type::reference_type)
1206 return ((utree const*)p)->begin();
1207 if (get_type() == type::range_type)
1208 return const_iterator(r.first, 0);
1211 if (get_type() != type::list_type)
1212 BOOST_THROW_EXCEPTION(
1214 ("begin() called on non-list utree type",
1217 return const_iterator(l.first, 0);
1220 inline utree::const_iterator utree::end() const
1222 if (get_type() == type::reference_type)
1223 return ((utree const*)p)->end();
1224 if (get_type() == type::range_type)
1225 return const_iterator(0, r.first);
1228 if (get_type() != type::list_type)
1229 BOOST_THROW_EXCEPTION(
1231 ("end() called on non-list utree type",
1234 return const_iterator(0, l.last);
1237 inline bool utree::empty() const
1239 type::info t = get_type();
1240 if (t == type::reference_type)
1241 return ((utree const*)p)->empty();
1243 if (t == type::range_type)
1244 return r.first == 0;
1245 if (t == type::list_type)
1248 return t == type::nil_type || t == type::invalid_type;
1251 inline std::size_t utree::size() const
1253 type::info t = get_type();
1254 if (t == type::reference_type)
1255 return ((utree const*)p)->size();
1257 if (t == type::range_type)
1259 // FIXME: O(n), and we have the room to store the size of a range
1260 // in the union if we compute it when assigned/constructed.
1261 std::size_t size = 0;
1262 detail::list::node* n = r.first;
1270 if (t == type::list_type)
1273 if (t == type::string_type)
1276 if (t == type::symbol_type)
1279 if (t == type::binary_type)
1282 if (t == type::string_range_type)
1283 return sr.last - sr.first;
1285 if (t != type::nil_type)
1286 BOOST_THROW_EXCEPTION(
1288 ("size() called on non-list and non-string utree type",
1294 inline utree_type::info utree::which() const
1299 inline utree& utree::front()
1301 if (get_type() == type::reference_type)
1303 if (get_type() == type::range_type)
1306 BOOST_THROW_EXCEPTION(
1307 empty_exception("front() called on empty utree range"));
1308 return r.first->val;
1312 if (get_type() != type::list_type)
1313 BOOST_THROW_EXCEPTION(
1315 ("front() called on non-list utree type", get_type()));
1317 BOOST_THROW_EXCEPTION(
1318 empty_exception("front() called on empty utree list"));
1320 return l.first->val;
1323 inline utree& utree::back()
1325 if (get_type() == type::reference_type)
1327 if (get_type() == type::range_type)
1330 BOOST_THROW_EXCEPTION(
1331 empty_exception("back() called on empty utree range"));
1336 if (get_type() != type::list_type)
1337 BOOST_THROW_EXCEPTION(
1339 ("back() called on non-list utree type", get_type()));
1341 BOOST_THROW_EXCEPTION(
1342 empty_exception("back() called on empty utree list"));
1347 inline utree const& utree::front() const
1349 if (get_type() == type::reference_type)
1350 return ((utree const*)p)->front();
1351 if (get_type() == type::range_type)
1354 BOOST_THROW_EXCEPTION(
1355 empty_exception("front() called on empty utree range"));
1356 return r.first->val;
1360 if (get_type() != type::list_type)
1361 BOOST_THROW_EXCEPTION(
1363 ("front() called on non-list utree type", get_type()));
1365 BOOST_THROW_EXCEPTION(
1366 empty_exception("front() called on empty utree list"));
1368 return l.first->val;
1371 inline utree const& utree::back() const
1373 if (get_type() == type::reference_type)
1374 return ((utree const*)p)->back();
1375 if (get_type() == type::range_type)
1378 BOOST_THROW_EXCEPTION(
1379 empty_exception("back() called on empty utree range"));
1384 if (get_type() != type::list_type)
1385 BOOST_THROW_EXCEPTION(
1387 ("back() called on non-list utree type", get_type()));
1389 BOOST_THROW_EXCEPTION(
1390 empty_exception("back() called on empty utree list"));
1395 inline void utree::swap(utree& other)
1400 inline utree::type::info utree::get_type() const
1402 // the fast string holds the type info
1403 return static_cast<utree::type::info>(s.get_type());
1406 inline void utree::set_type(type::info t)
1408 // the fast string holds the type info
1412 inline void utree::ensure_list_type(char const* failed_in)
1414 type::info t = get_type();
1415 if (t == type::invalid_type)
1417 set_type(type::list_type);
1418 l.default_construct();
1420 else if (get_type() != type::list_type)
1422 std::string msg = failed_in;
1423 msg += "called on non-list and non-invalid utree type";
1424 BOOST_THROW_EXCEPTION(bad_type_exception(msg.c_str(), get_type()));
1428 inline void utree::free()
1432 case type::binary_type:
1433 case type::symbol_type:
1434 case type::string_type:
1437 case type::list_type:
1440 case type::function_type:
1449 inline void utree::copy(utree const& other)
1451 set_type(other.get_type());
1452 switch (other.get_type())
1455 BOOST_THROW_EXCEPTION(
1456 bad_type_exception("corrupt utree type", other.get_type()));
1458 case type::invalid_type:
1459 case type::nil_type:
1460 s.tag(other.s.tag());
1462 case type::bool_type:
1464 s.tag(other.s.tag());
1466 case type::int_type:
1468 s.tag(other.s.tag());
1470 case type::double_type:
1472 s.tag(other.s.tag());
1474 case type::reference_type:
1476 s.tag(other.s.tag());
1478 case type::any_type:
1480 s.tag(other.s.tag());
1482 case type::range_type:
1484 s.tag(other.s.tag());
1486 case type::string_range_type:
1488 s.tag(other.s.tag());
1490 case type::function_type:
1491 pf = other.pf->clone();
1492 s.tag(other.s.tag());
1494 case type::string_type:
1495 case type::symbol_type:
1496 case type::binary_type:
1498 s.tag(other.s.tag());
1500 case type::list_type:
1502 s.tag(other.s.tag());
1507 template <typename T>
1508 struct is_iterator_range
1509 : boost::mpl::false_
1512 template <typename Iterator>
1513 struct is_iterator_range<boost::iterator_range<Iterator> >
1517 template <typename To>
1520 typedef To result_type;
1522 template <typename From>
1523 To dispatch(From const& val, boost::mpl::true_) const
1525 return To(val); // From is convertible to To
1528 template <typename From>
1529 To dispatch(From const&, boost::mpl::false_) const
1531 // From is NOT convertible to To !!!
1532 throw std::bad_cast();
1536 template <typename From>
1537 To operator()(From const& val) const
1539 // boost::iterator_range has a templated constructor, accepting
1540 // any argument and hence any type is 'convertible' to it.
1541 typedef typename boost::mpl::eval_if<
1542 is_iterator_range<To>
1543 , boost::is_same<From, To>, boost::is_convertible<From, To>
1544 >::type is_convertible;
1545 return dispatch(val, is_convertible());
1549 template <typename T>
1550 struct utree_cast<T*>
1552 typedef T* result_type;
1554 template <typename From>
1555 T* operator()(From const&) const
1557 // From is NOT convertible to T !!!
1558 throw std::bad_cast();
1562 T* operator()(any_ptr const& p) const
1568 template <typename T>
1569 inline T utree::get() const
1571 return utree::visit(*this, utree_cast<T>());
1574 inline utree& utree::deref()
1576 return (get_type() == type::reference_type) ? *p : *this;
1579 inline utree const& utree::deref() const
1581 return (get_type() == type::reference_type) ? *p : *this;
1584 inline short utree::tag() const
1589 inline void utree::tag(short tag)
1594 inline utree utree::eval(utree const& env) const
1596 if (get_type() == type::reference_type)
1597 return deref().eval(env);
1599 if (get_type() != type::function_type)
1600 BOOST_THROW_EXCEPTION(
1602 "eval() called on non-function utree type", get_type()));
1606 inline utree utree::eval(utree& env) const
1608 if (get_type() == type::reference_type)
1609 return deref().eval(env);
1611 if (get_type() != type::function_type)
1612 BOOST_THROW_EXCEPTION(
1614 "eval() called on non-function utree type", get_type()));
1618 inline utree utree::operator() (utree const& env) const
1623 inline utree utree::operator() (utree& env) const
1629 #if defined(BOOST_MSVC)
1630 # pragma warning(pop)