utree_detail2.hpp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Copyright (c) 2001-2011 Hartmut Kaiser
  4. Copyright (c) 2011 Bryce Lelbach
  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_UTREE_DETAIL2)
  9. #define BOOST_SPIRIT_UTREE_DETAIL2
  10. #include <boost/type_traits/remove_pointer.hpp>
  11. #include <boost/type_traits/is_pointer.hpp>
  12. #include <boost/utility/enable_if.hpp>
  13. #include <boost/throw_exception.hpp>
  14. #include <boost/iterator/iterator_traits.hpp>
  15. #include <cstring> // for std::memcpy
  16. #ifdef _MSC_VER
  17. # pragma warning(push)
  18. # pragma warning(disable: 4800) // forcing value to bool 'true' or 'false'
  19. # if _MSC_VER < 1800
  20. # pragma warning(disable: 4702) // unreachable code
  21. # endif
  22. #endif
  23. namespace boost { namespace spirit { namespace detail
  24. {
  25. inline char& fast_string::info()
  26. {
  27. return buff[small_string_size];
  28. }
  29. inline char fast_string::info() const
  30. {
  31. return buff[small_string_size];
  32. }
  33. inline int fast_string::get_type() const
  34. {
  35. return info() >> 1;
  36. }
  37. inline void fast_string::set_type(int t)
  38. {
  39. info() = (t << 1) | (info() & 1);
  40. }
  41. inline short fast_string::tag() const
  42. {
  43. boost::int16_t tmp;
  44. std::memcpy(&tmp, &buff[small_string_size-2], sizeof(tmp));
  45. return tmp;
  46. }
  47. inline void fast_string::tag(short tag)
  48. {
  49. boost::int16_t tmp = tag;
  50. std::memcpy(&buff[small_string_size-2], &tmp, sizeof(tmp));
  51. }
  52. inline bool fast_string::is_heap_allocated() const
  53. {
  54. return info() & 1;
  55. }
  56. inline std::size_t fast_string::size() const
  57. {
  58. if (is_heap_allocated())
  59. return heap.size;
  60. else
  61. return max_string_len - buff[max_string_len];
  62. }
  63. inline char const* fast_string::str() const
  64. {
  65. if (is_heap_allocated())
  66. return heap.str;
  67. else
  68. return buff;
  69. }
  70. template <typename Iterator>
  71. inline void fast_string::construct(Iterator f, Iterator l)
  72. {
  73. std::size_t const size = static_cast<std::size_t>(l-f);
  74. char* str;
  75. if (size < max_string_len)
  76. {
  77. // if it fits, store it in-situ; small_string_size minus the length
  78. // of the string is placed in buff[small_string_size - 1]
  79. str = buff;
  80. buff[max_string_len] = static_cast<char>(max_string_len - size);
  81. info() &= ~0x1;
  82. }
  83. else
  84. {
  85. // else, store it in the heap
  86. str = new char[size + 1]; // add one for the null char
  87. heap.str = str;
  88. heap.size = size;
  89. info() |= 0x1;
  90. }
  91. for (std::size_t i = 0; i != size; ++i)
  92. {
  93. *str++ = *f++;
  94. }
  95. *str = '\0'; // add the null char
  96. }
  97. inline void fast_string::swap(fast_string& other)
  98. {
  99. std::swap(*this, other);
  100. }
  101. inline void fast_string::free()
  102. {
  103. if (is_heap_allocated())
  104. {
  105. delete [] heap.str;
  106. }
  107. }
  108. inline void fast_string::copy(fast_string const& other)
  109. {
  110. construct(other.str(), other.str() + other.size());
  111. }
  112. inline void fast_string::initialize()
  113. {
  114. for (std::size_t i = 0; i != buff_size / (sizeof(long)/sizeof(char)); ++i)
  115. lbuff[i] = 0;
  116. }
  117. struct list::node : boost::noncopyable
  118. {
  119. template <typename T>
  120. node(T const& val, node* next, node* prev)
  121. : val(val), next(next), prev(prev) {}
  122. void unlink()
  123. {
  124. prev->next = next;
  125. next->prev = prev;
  126. }
  127. utree val;
  128. node* next;
  129. node* prev;
  130. };
  131. template <typename Value>
  132. class list::node_iterator
  133. : public boost::iterator_facade<
  134. node_iterator<Value>
  135. , Value
  136. , boost::bidirectional_traversal_tag>
  137. {
  138. public:
  139. node_iterator()
  140. : node(0), prev(0) {}
  141. node_iterator(list::node* node, list::node* prev)
  142. : node(node), prev(prev) {}
  143. private:
  144. friend class boost::iterator_core_access;
  145. friend class boost::spirit::utree;
  146. friend struct boost::spirit::detail::list;
  147. void increment()
  148. {
  149. if (node != 0) // not at end
  150. {
  151. prev = node;
  152. node = node->next;
  153. }
  154. }
  155. void decrement()
  156. {
  157. if (prev != 0) // not at begin
  158. {
  159. node = prev;
  160. prev = prev->prev;
  161. }
  162. }
  163. bool equal(node_iterator const& other) const
  164. {
  165. return node == other.node;
  166. }
  167. typename node_iterator::reference dereference() const
  168. {
  169. return node->val;
  170. }
  171. list::node* node;
  172. list::node* prev;
  173. };
  174. template <typename Value>
  175. class list::node_iterator<boost::reference_wrapper<Value> >
  176. : public boost::iterator_facade<
  177. node_iterator<boost::reference_wrapper<Value> >
  178. , boost::reference_wrapper<Value>
  179. , boost::bidirectional_traversal_tag>
  180. {
  181. public:
  182. node_iterator()
  183. : node(0), prev(0), curr(nil_node) {}
  184. node_iterator(list::node* node, list::node* prev)
  185. : node(node), prev(prev), curr(node ? node->val : nil_node) {}
  186. private:
  187. friend class boost::iterator_core_access;
  188. friend class boost::spirit::utree;
  189. friend struct boost::spirit::detail::list;
  190. void increment()
  191. {
  192. if (node != 0) // not at end
  193. {
  194. prev = node;
  195. node = node->next;
  196. curr = boost::ref(node ? node->val : nil_node);
  197. }
  198. }
  199. void decrement()
  200. {
  201. if (prev != 0) // not at begin
  202. {
  203. node = prev;
  204. prev = prev->prev;
  205. curr = boost::ref(node ? node->val : nil_node);
  206. }
  207. }
  208. bool equal(node_iterator const& other) const
  209. {
  210. return node == other.node;
  211. }
  212. typename node_iterator::reference dereference() const
  213. {
  214. return curr;
  215. }
  216. list::node* node;
  217. list::node* prev;
  218. static Value nil_node;
  219. mutable boost::reference_wrapper<Value> curr;
  220. };
  221. template <typename Value>
  222. Value list::node_iterator<boost::reference_wrapper<Value> >::nil_node = Value();
  223. inline void list::free()
  224. {
  225. node* p = first;
  226. while (p != 0)
  227. {
  228. node* next = p->next;
  229. delete p;
  230. p = next;
  231. }
  232. }
  233. inline void list::copy(list const& other)
  234. {
  235. node* p = other.first;
  236. while (p != 0)
  237. {
  238. push_back(p->val);
  239. p = p->next;
  240. }
  241. }
  242. inline void list::default_construct()
  243. {
  244. first = last = 0;
  245. size = 0;
  246. }
  247. template <typename T, typename Iterator>
  248. inline void list::insert(T const& val, Iterator pos)
  249. {
  250. if (!pos.node)
  251. {
  252. push_back(val);
  253. return;
  254. }
  255. detail::list::node* new_node =
  256. new detail::list::node(val, pos.node, pos.node->prev);
  257. if (pos.node->prev)
  258. pos.node->prev->next = new_node;
  259. else
  260. first = new_node;
  261. pos.node->prev = new_node;
  262. ++size;
  263. }
  264. template <typename T>
  265. inline void list::push_front(T const& val)
  266. {
  267. detail::list::node* new_node;
  268. if (first == 0)
  269. {
  270. new_node = new detail::list::node(val, 0, 0);
  271. first = last = new_node;
  272. ++size;
  273. }
  274. else
  275. {
  276. new_node = new detail::list::node(val, first, first->prev);
  277. first->prev = new_node;
  278. first = new_node;
  279. ++size;
  280. }
  281. }
  282. template <typename T>
  283. inline void list::push_back(T const& val)
  284. {
  285. if (last == 0)
  286. push_front(val);
  287. else {
  288. detail::list::node* new_node =
  289. new detail::list::node(val, last->next, last);
  290. last->next = new_node;
  291. last = new_node;
  292. ++size;
  293. }
  294. }
  295. inline void list::pop_front()
  296. {
  297. BOOST_ASSERT(size != 0);
  298. if (first == last) // there's only one item
  299. {
  300. delete first;
  301. size = 0;
  302. first = last = 0;
  303. }
  304. else
  305. {
  306. node* np = first;
  307. first = first->next;
  308. first->prev = 0;
  309. delete np;
  310. --size;
  311. }
  312. }
  313. inline void list::pop_back()
  314. {
  315. BOOST_ASSERT(size != 0);
  316. if (first == last) // there's only one item
  317. {
  318. delete first;
  319. size = 0;
  320. first = last = 0;
  321. }
  322. else
  323. {
  324. node* np = last;
  325. last = last->prev;
  326. last->next = 0;
  327. delete np;
  328. --size;
  329. }
  330. }
  331. inline list::node* list::erase(node* pos)
  332. {
  333. BOOST_ASSERT(pos != 0);
  334. if (pos == first)
  335. {
  336. pop_front();
  337. return first;
  338. }
  339. else if (pos == last)
  340. {
  341. pop_back();
  342. return 0;
  343. }
  344. else
  345. {
  346. node* next(pos->next);
  347. pos->unlink();
  348. delete pos;
  349. --size;
  350. return next;
  351. }
  352. }
  353. ///////////////////////////////////////////////////////////////////////////
  354. // simple binder for binary visitation (we don't want to bring in the big guns)
  355. template <typename F, typename X>
  356. struct bind_impl
  357. {
  358. typedef typename F::result_type result_type;
  359. X& x; // always by reference
  360. F f;
  361. bind_impl(F f, X& x) : x(x), f(f) {}
  362. template <typename Y>
  363. typename F::result_type operator()(Y& y) const
  364. {
  365. return f(x, y);
  366. }
  367. template <typename Y>
  368. typename F::result_type operator()(Y const& y) const
  369. {
  370. return f(x, y);
  371. }
  372. };
  373. template <typename F, typename X>
  374. bind_impl<F, X const> bind(F f, X const& x)
  375. {
  376. return bind_impl<F, X const>(f, x);
  377. }
  378. template <typename F, typename X>
  379. bind_impl<F, X> bind(F f, X& x)
  380. {
  381. return bind_impl<F, X>(f, x);
  382. }
  383. template <typename UTreeX, typename UTreeY = UTreeX>
  384. struct visit_impl
  385. {
  386. template <typename F>
  387. typename F::result_type
  388. static apply(UTreeX& x, F f) // single dispatch
  389. {
  390. typedef typename
  391. boost::mpl::if_<boost::is_const<UTreeX>,
  392. typename UTreeX::const_iterator,
  393. typename UTreeX::iterator>::type
  394. iterator;
  395. typedef boost::iterator_range<iterator> list_range;
  396. typedef utree_type type;
  397. switch (x.get_type())
  398. {
  399. default:
  400. BOOST_THROW_EXCEPTION(
  401. bad_type_exception("corrupt utree type", x.get_type()));
  402. break;
  403. case type::invalid_type:
  404. return f(invalid);
  405. case type::nil_type:
  406. return f(nil);
  407. case type::bool_type:
  408. return f(x.b);
  409. case type::int_type:
  410. return f(x.i);
  411. case type::double_type:
  412. return f(x.d);
  413. case type::list_type:
  414. return f(list_range(iterator(x.l.first, 0), iterator(0, x.l.last)));
  415. case type::range_type:
  416. return f(list_range(iterator(x.r.first, 0), iterator(0, x.r.last)));
  417. case type::string_type:
  418. return f(utf8_string_range_type(x.s.str(), x.s.size()));
  419. case type::string_range_type:
  420. return f(utf8_string_range_type(x.sr.first, x.sr.last));
  421. case type::symbol_type:
  422. return f(utf8_symbol_range_type(x.s.str(), x.s.size()));
  423. case type::binary_type:
  424. return f(binary_range_type(x.s.str(), x.s.size()));
  425. case type::reference_type:
  426. return apply(*x.p, f);
  427. case type::any_type:
  428. return f(any_ptr(x.v.p, x.v.i));
  429. case type::function_type:
  430. return f(*x.pf);
  431. }
  432. }
  433. template <typename F>
  434. typename F::result_type
  435. static apply(UTreeX& x, UTreeY& y, F f) // double dispatch
  436. {
  437. typedef typename
  438. boost::mpl::if_<boost::is_const<UTreeX>,
  439. typename UTreeX::const_iterator,
  440. typename UTreeX::iterator>::type
  441. iterator;
  442. typedef boost::iterator_range<iterator> list_range;
  443. typedef utree_type type;
  444. switch (x.get_type())
  445. {
  446. default:
  447. BOOST_THROW_EXCEPTION(
  448. bad_type_exception("corrupt utree type", x.get_type()));
  449. break;
  450. case type::invalid_type:
  451. return visit_impl::apply(y, detail::bind(f, invalid));
  452. case type::nil_type:
  453. return visit_impl::apply(y, detail::bind(f, nil));
  454. case type::bool_type:
  455. return visit_impl::apply(y, detail::bind(f, x.b));
  456. case type::int_type:
  457. return visit_impl::apply(y, detail::bind(f, x.i));
  458. case type::double_type:
  459. return visit_impl::apply(y, detail::bind(f, x.d));
  460. case type::list_type:
  461. return visit_impl::apply(
  462. y, detail::bind<F, list_range>(f,
  463. list_range(iterator(x.l.first, 0), iterator(0, x.l.last))));
  464. case type::range_type:
  465. return visit_impl::apply(
  466. y, detail::bind<F, list_range>(f,
  467. list_range(iterator(x.r.first, 0), iterator(0, x.r.last))));
  468. case type::string_type:
  469. return visit_impl::apply(y, detail::bind(
  470. f, utf8_string_range_type(x.s.str(), x.s.size())));
  471. case type::string_range_type:
  472. return visit_impl::apply(y, detail::bind(
  473. f, utf8_string_range_type(x.sr.first, x.sr.last)));
  474. case type::symbol_type:
  475. return visit_impl::apply(y, detail::bind(
  476. f, utf8_symbol_range_type(x.s.str(), x.s.size())));
  477. case type::binary_type:
  478. return visit_impl::apply(y, detail::bind(
  479. f, binary_range_type(x.s.str(), x.s.size())));
  480. case type::reference_type:
  481. return apply(*x.p, y, f);
  482. case type::any_type:
  483. return visit_impl::apply(
  484. y, detail::bind(f, any_ptr(x.v.p, x.v.i)));
  485. case type::function_type:
  486. return visit_impl::apply(y, detail::bind(f, *x.pf));
  487. }
  488. }
  489. };
  490. struct index_impl
  491. {
  492. static utree& apply(utree& ut, std::size_t i)
  493. {
  494. switch (ut.get_type())
  495. {
  496. case utree_type::reference_type:
  497. return apply(ut.deref(), i);
  498. case utree_type::range_type:
  499. return apply(ut.r.first, i);
  500. case utree_type::list_type:
  501. return apply(ut.l.first, i);
  502. default:
  503. BOOST_THROW_EXCEPTION(
  504. bad_type_exception
  505. ("index operation performed on non-list utree type",
  506. ut.get_type()));
  507. }
  508. }
  509. static utree const& apply(utree const& ut, std::size_t i)
  510. {
  511. switch (ut.get_type())
  512. {
  513. case utree_type::reference_type:
  514. return apply(ut.deref(), i);
  515. case utree_type::range_type:
  516. return apply(ut.r.first, i);
  517. case utree_type::list_type:
  518. return apply(ut.l.first, i);
  519. default:
  520. BOOST_THROW_EXCEPTION(
  521. bad_type_exception
  522. ("index operation performed on non-list utree type",
  523. ut.get_type()));
  524. }
  525. }
  526. static utree& apply(list::node* node, std::size_t i)
  527. {
  528. for (; i > 0; --i)
  529. node = node->next;
  530. return node->val;
  531. }
  532. static utree const& apply(list::node const* node, std::size_t i)
  533. {
  534. for (; i > 0; --i)
  535. node = node->next;
  536. return node->val;
  537. }
  538. };
  539. }}}
  540. namespace boost { namespace spirit
  541. {
  542. template <typename F>
  543. stored_function<F>::stored_function(F f)
  544. : f(f)
  545. {
  546. }
  547. template <typename F>
  548. stored_function<F>::~stored_function()
  549. {
  550. }
  551. template <typename F>
  552. utree stored_function<F>::operator()(utree const& env) const
  553. {
  554. return f(env);
  555. }
  556. template <typename F>
  557. utree stored_function<F>::operator()(utree& env) const
  558. {
  559. return f(env);
  560. }
  561. template <typename F>
  562. function_base*
  563. stored_function<F>::clone() const
  564. {
  565. return new stored_function<F>(f);
  566. }
  567. template <typename F>
  568. referenced_function<F>::referenced_function(F& f)
  569. : f(f)
  570. {
  571. }
  572. template <typename F>
  573. referenced_function<F>::~referenced_function()
  574. {
  575. }
  576. template <typename F>
  577. utree referenced_function<F>::operator()(utree const& env) const
  578. {
  579. return f(env);
  580. }
  581. template <typename F>
  582. utree referenced_function<F>::operator()(utree& env) const
  583. {
  584. return f(env);
  585. }
  586. template <typename F>
  587. function_base*
  588. referenced_function<F>::clone() const
  589. {
  590. return new referenced_function<F>(f);
  591. }
  592. inline utree::utree(utree::invalid_type)
  593. {
  594. s.initialize();
  595. set_type(type::invalid_type);
  596. }
  597. inline utree::utree(utree::nil_type)
  598. {
  599. s.initialize();
  600. set_type(type::nil_type);
  601. }
  602. inline utree::utree(bool b_)
  603. {
  604. s.initialize();
  605. b = b_;
  606. set_type(type::bool_type);
  607. }
  608. inline utree::utree(char c)
  609. {
  610. s.initialize();
  611. // char constructs a single element string
  612. s.construct(&c, &c+1);
  613. set_type(type::string_type);
  614. }
  615. inline utree::utree(unsigned int i_)
  616. {
  617. s.initialize();
  618. i = i_;
  619. set_type(type::int_type);
  620. }
  621. inline utree::utree(int i_)
  622. {
  623. s.initialize();
  624. i = i_;
  625. set_type(type::int_type);
  626. }
  627. inline utree::utree(double d_)
  628. {
  629. s.initialize();
  630. d = d_;
  631. set_type(type::double_type);
  632. }
  633. inline utree::utree(char const* str)
  634. {
  635. s.initialize();
  636. s.construct(str, str + strlen(str));
  637. set_type(type::string_type);
  638. }
  639. inline utree::utree(char const* str, std::size_t len)
  640. {
  641. s.initialize();
  642. s.construct(str, str + len);
  643. set_type(type::string_type);
  644. }
  645. inline utree::utree(std::string const& str)
  646. {
  647. s.initialize();
  648. s.construct(str.begin(), str.end());
  649. set_type(type::string_type);
  650. }
  651. template <typename Base, utree_type::info type_>
  652. inline utree::utree(basic_string<Base, type_> const& bin)
  653. {
  654. s.initialize();
  655. s.construct(bin.begin(), bin.end());
  656. set_type(type_);
  657. }
  658. inline utree::utree(boost::reference_wrapper<utree> ref)
  659. {
  660. s.initialize();
  661. p = ref.get_pointer();
  662. set_type(type::reference_type);
  663. }
  664. inline utree::utree(any_ptr const& p)
  665. {
  666. s.initialize();
  667. v.p = p.p;
  668. v.i = p.i;
  669. set_type(type::any_type);
  670. }
  671. inline utree::utree(function_base const& pf_)
  672. {
  673. s.initialize();
  674. pf = pf_.clone();
  675. set_type(type::function_type);
  676. }
  677. inline utree::utree(function_base* pf_)
  678. {
  679. s.initialize();
  680. pf = pf_;
  681. set_type(type::function_type);
  682. }
  683. template <typename Iter>
  684. inline utree::utree(boost::iterator_range<Iter> r)
  685. {
  686. s.initialize();
  687. assign(r.begin(), r.end());
  688. }
  689. inline utree::utree(range r, shallow_tag)
  690. {
  691. s.initialize();
  692. this->r.first = r.begin().node;
  693. this->r.last = r.end().prev;
  694. set_type(type::range_type);
  695. }
  696. inline utree::utree(const_range r, shallow_tag)
  697. {
  698. s.initialize();
  699. this->r.first = r.begin().node;
  700. this->r.last = r.end().prev;
  701. set_type(type::range_type);
  702. }
  703. inline utree::utree(utf8_string_range_type const& str, shallow_tag)
  704. {
  705. s.initialize();
  706. this->sr.first = str.begin();
  707. this->sr.last = str.end();
  708. set_type(type::string_range_type);
  709. }
  710. inline utree::utree(utree const& other)
  711. {
  712. s.initialize();
  713. copy(other);
  714. }
  715. inline utree::~utree()
  716. {
  717. free();
  718. }
  719. inline utree& utree::operator=(utree const& other)
  720. {
  721. if (this != &other)
  722. {
  723. free();
  724. copy(other);
  725. }
  726. return *this;
  727. }
  728. inline utree& utree::operator=(nil_type)
  729. {
  730. free();
  731. set_type(type::nil_type);
  732. return *this;
  733. }
  734. inline utree& utree::operator=(bool b_)
  735. {
  736. free();
  737. b = b_;
  738. set_type(type::bool_type);
  739. return *this;
  740. }
  741. inline utree& utree::operator=(char c)
  742. {
  743. // char constructs a single element string
  744. free();
  745. s.construct(&c, &c+1);
  746. set_type(type::string_type);
  747. return *this;
  748. }
  749. inline utree& utree::operator=(unsigned int i_)
  750. {
  751. free();
  752. i = i_;
  753. set_type(type::int_type);
  754. return *this;
  755. }
  756. inline utree& utree::operator=(int i_)
  757. {
  758. free();
  759. i = i_;
  760. set_type(type::int_type);
  761. return *this;
  762. }
  763. inline utree& utree::operator=(double d_)
  764. {
  765. free();
  766. d = d_;
  767. set_type(type::double_type);
  768. return *this;
  769. }
  770. inline utree& utree::operator=(char const* s_)
  771. {
  772. free();
  773. s.construct(s_, s_ + strlen(s_));
  774. set_type(type::string_type);
  775. return *this;
  776. }
  777. inline utree& utree::operator=(std::string const& s_)
  778. {
  779. free();
  780. s.construct(s_.begin(), s_.end());
  781. set_type(type::string_type);
  782. return *this;
  783. }
  784. template <typename Base, utree_type::info type_>
  785. inline utree& utree::operator=(basic_string<Base, type_> const& bin)
  786. {
  787. free();
  788. s.construct(bin.begin(), bin.end());
  789. set_type(type_);
  790. return *this;
  791. }
  792. inline utree& utree::operator=(boost::reference_wrapper<utree> ref)
  793. {
  794. free();
  795. p = ref.get_pointer();
  796. set_type(type::reference_type);
  797. return *this;
  798. }
  799. inline utree& utree::operator=(any_ptr const& p_)
  800. {
  801. free();
  802. v.p = p_.p;
  803. v.i = p_.i;
  804. set_type(type::any_type);
  805. return *this;
  806. }
  807. inline utree& utree::operator=(function_base const& pf_)
  808. {
  809. free();
  810. pf = pf_.clone();
  811. set_type(type::function_type);
  812. return *this;
  813. }
  814. inline utree& utree::operator=(function_base* pf_)
  815. {
  816. free();
  817. pf = pf_;
  818. set_type(type::function_type);
  819. return *this;
  820. }
  821. template <typename Iter>
  822. inline utree& utree::operator=(boost::iterator_range<Iter> r)
  823. {
  824. free();
  825. assign(r.begin(), r.end());
  826. return *this;
  827. }
  828. template <typename F>
  829. typename boost::result_of<F(utree const&)>::type
  830. inline utree::visit(utree const& x, F f)
  831. {
  832. return detail::visit_impl<utree const>::apply(x, f);
  833. }
  834. template <typename F>
  835. typename boost::result_of<F(utree&)>::type
  836. inline utree::visit(utree& x, F f)
  837. {
  838. return detail::visit_impl<utree>::apply(x, f);
  839. }
  840. template <typename F>
  841. typename boost::result_of<F(utree const&, utree const&)>::type
  842. inline utree::visit(utree const& x, utree const& y, F f)
  843. {
  844. return detail::visit_impl<utree const, utree const>::apply(x, y, f);
  845. }
  846. template <typename F>
  847. typename boost::result_of<F(utree const&, utree&)>::type
  848. inline utree::visit(utree const& x, utree& y, F f)
  849. {
  850. return detail::visit_impl<utree const, utree>::apply(x, y, f);
  851. }
  852. template <typename F>
  853. typename boost::result_of<F(utree&, utree const&)>::type
  854. inline utree::visit(utree& x, utree const& y, F f)
  855. {
  856. return detail::visit_impl<utree, utree const>::apply(x, y, f);
  857. }
  858. template <typename F>
  859. typename boost::result_of<F(utree&, utree&)>::type
  860. inline utree::visit(utree& x, utree& y, F f)
  861. {
  862. return detail::visit_impl<utree, utree>::apply(x, y, f);
  863. }
  864. inline utree::reference get(utree::reference ut, utree::size_type i)
  865. { return detail::index_impl::apply(ut, i); }
  866. inline utree::const_reference
  867. get(utree::const_reference ut, utree::size_type i)
  868. { return detail::index_impl::apply(ut, i); }
  869. template <typename T>
  870. inline void utree::push_front(T const& val)
  871. {
  872. if (get_type() == type::reference_type)
  873. return p->push_front(val);
  874. ensure_list_type("push_front()");
  875. l.push_front(val);
  876. }
  877. template <typename T>
  878. inline void utree::push_back(T const& val)
  879. {
  880. if (get_type() == type::reference_type)
  881. return p->push_back(val);
  882. ensure_list_type("push_back()");
  883. l.push_back(val);
  884. }
  885. template <typename T>
  886. inline utree::iterator utree::insert(iterator pos, T const& val)
  887. {
  888. if (get_type() == type::reference_type)
  889. return p->insert(pos, val);
  890. ensure_list_type("insert()");
  891. if (!pos.node)
  892. {
  893. l.push_back(val);
  894. return utree::iterator(l.last, l.last->prev);
  895. }
  896. l.insert(val, pos);
  897. return utree::iterator(pos.node->prev, pos.node->prev->prev);
  898. }
  899. template <typename T>
  900. inline void utree::insert(iterator pos, std::size_t n, T const& val)
  901. {
  902. if (get_type() == type::reference_type)
  903. return p->insert(pos, n, val);
  904. ensure_list_type("insert()");
  905. for (std::size_t i = 0; i != n; ++i)
  906. insert(pos, val);
  907. }
  908. template <typename Iterator>
  909. inline void utree::insert(iterator pos, Iterator first, Iterator last)
  910. {
  911. if (get_type() == type::reference_type)
  912. return p->insert(pos, first, last);
  913. ensure_list_type("insert()");
  914. while (first != last)
  915. insert(pos, *first++);
  916. }
  917. template <typename Iterator>
  918. inline void utree::assign(Iterator first, Iterator last)
  919. {
  920. if (get_type() == type::reference_type)
  921. return p->assign(first, last);
  922. clear();
  923. set_type(type::list_type);
  924. while (first != last)
  925. {
  926. push_back(*first);
  927. ++first;
  928. }
  929. }
  930. inline void utree::clear()
  931. {
  932. if (get_type() == type::reference_type)
  933. return p->clear();
  934. // clear will always make this an invalid type
  935. free();
  936. set_type(type::invalid_type);
  937. }
  938. inline void utree::pop_front()
  939. {
  940. if (get_type() == type::reference_type)
  941. return p->pop_front();
  942. if (get_type() != type::list_type)
  943. BOOST_THROW_EXCEPTION(
  944. bad_type_exception
  945. ("pop_front() called on non-list utree type",
  946. get_type()));
  947. l.pop_front();
  948. }
  949. inline void utree::pop_back()
  950. {
  951. if (get_type() == type::reference_type)
  952. return p->pop_back();
  953. if (get_type() != type::list_type)
  954. BOOST_THROW_EXCEPTION(
  955. bad_type_exception
  956. ("pop_back() called on non-list utree type",
  957. get_type()));
  958. l.pop_back();
  959. }
  960. inline utree::iterator utree::erase(iterator pos)
  961. {
  962. if (get_type() == type::reference_type)
  963. return p->erase(pos);
  964. if (get_type() != type::list_type)
  965. BOOST_THROW_EXCEPTION(
  966. bad_type_exception
  967. ("erase() called on non-list utree type",
  968. get_type()));
  969. detail::list::node* np = l.erase(pos.node);
  970. return iterator(np, np?np->prev:l.last);
  971. }
  972. inline utree::iterator utree::erase(iterator first, iterator last)
  973. {
  974. if (get_type() == type::reference_type)
  975. return p->erase(first, last);
  976. if (get_type() != type::list_type)
  977. BOOST_THROW_EXCEPTION(
  978. bad_type_exception
  979. ("erase() called on non-list utree type",
  980. get_type()));
  981. while (first != last)
  982. erase(first++);
  983. return last;
  984. }
  985. inline utree::iterator utree::begin()
  986. {
  987. if (get_type() == type::reference_type)
  988. return p->begin();
  989. else if (get_type() == type::range_type)
  990. return iterator(r.first, 0);
  991. // otherwise...
  992. ensure_list_type("begin()");
  993. return iterator(l.first, 0);
  994. }
  995. inline utree::iterator utree::end()
  996. {
  997. if (get_type() == type::reference_type)
  998. return p->end();
  999. else if (get_type() == type::range_type)
  1000. return iterator(0, r.first);
  1001. // otherwise...
  1002. ensure_list_type("end()");
  1003. return iterator(0, l.last);
  1004. }
  1005. inline utree::ref_iterator utree::ref_begin()
  1006. {
  1007. if (get_type() == type::reference_type)
  1008. return p->ref_begin();
  1009. else if (get_type() == type::range_type)
  1010. return ref_iterator(r.first, 0);
  1011. // otherwise...
  1012. ensure_list_type("ref_begin()");
  1013. return ref_iterator(l.first, 0);
  1014. }
  1015. inline utree::ref_iterator utree::ref_end()
  1016. {
  1017. if (get_type() == type::reference_type)
  1018. return p->ref_end();
  1019. else if (get_type() == type::range_type)
  1020. return ref_iterator(0, r.first);
  1021. // otherwise...
  1022. ensure_list_type("ref_end()");
  1023. return ref_iterator(0, l.last);
  1024. }
  1025. inline utree::const_iterator utree::begin() const
  1026. {
  1027. if (get_type() == type::reference_type)
  1028. return ((utree const*)p)->begin();
  1029. if (get_type() == type::range_type)
  1030. return const_iterator(r.first, 0);
  1031. // otherwise...
  1032. if (get_type() != type::list_type)
  1033. BOOST_THROW_EXCEPTION(
  1034. bad_type_exception
  1035. ("begin() called on non-list utree type",
  1036. get_type()));
  1037. return const_iterator(l.first, 0);
  1038. }
  1039. inline utree::const_iterator utree::end() const
  1040. {
  1041. if (get_type() == type::reference_type)
  1042. return ((utree const*)p)->end();
  1043. if (get_type() == type::range_type)
  1044. return const_iterator(0, r.first);
  1045. // otherwise...
  1046. if (get_type() != type::list_type)
  1047. BOOST_THROW_EXCEPTION(
  1048. bad_type_exception
  1049. ("end() called on non-list utree type",
  1050. get_type()));
  1051. return const_iterator(0, l.last);
  1052. }
  1053. inline bool utree::empty() const
  1054. {
  1055. type::info t = get_type();
  1056. if (t == type::reference_type)
  1057. return ((utree const*)p)->empty();
  1058. if (t == type::range_type)
  1059. return r.first == 0;
  1060. if (t == type::list_type)
  1061. return l.size == 0;
  1062. return t == type::nil_type || t == type::invalid_type;
  1063. }
  1064. inline std::size_t utree::size() const
  1065. {
  1066. type::info t = get_type();
  1067. if (t == type::reference_type)
  1068. return ((utree const*)p)->size();
  1069. if (t == type::range_type)
  1070. {
  1071. // FIXME: O(n), and we have the room to store the size of a range
  1072. // in the union if we compute it when assigned/constructed.
  1073. std::size_t size = 0;
  1074. detail::list::node* n = r.first;
  1075. while (n)
  1076. {
  1077. n = n->next;
  1078. ++size;
  1079. }
  1080. return size;
  1081. }
  1082. if (t == type::list_type)
  1083. return l.size;
  1084. if (t == type::string_type)
  1085. return s.size();
  1086. if (t == type::symbol_type)
  1087. return s.size();
  1088. if (t == type::binary_type)
  1089. return s.size();
  1090. if (t == type::string_range_type)
  1091. return sr.last - sr.first;
  1092. if (t != type::nil_type)
  1093. BOOST_THROW_EXCEPTION(
  1094. bad_type_exception
  1095. ("size() called on non-list and non-string utree type",
  1096. get_type()));
  1097. return 0;
  1098. }
  1099. inline utree_type::info utree::which() const
  1100. {
  1101. return get_type();
  1102. }
  1103. inline utree& utree::front()
  1104. {
  1105. if (get_type() == type::reference_type)
  1106. return p->front();
  1107. if (get_type() == type::range_type)
  1108. {
  1109. if (!r.first)
  1110. BOOST_THROW_EXCEPTION(
  1111. empty_exception("front() called on empty utree range"));
  1112. return r.first->val;
  1113. }
  1114. // otherwise...
  1115. if (get_type() != type::list_type)
  1116. BOOST_THROW_EXCEPTION(
  1117. bad_type_exception
  1118. ("front() called on non-list utree type", get_type()));
  1119. else if (!l.first)
  1120. BOOST_THROW_EXCEPTION(
  1121. empty_exception("front() called on empty utree list"));
  1122. return l.first->val;
  1123. }
  1124. inline utree& utree::back()
  1125. {
  1126. if (get_type() == type::reference_type)
  1127. return p->back();
  1128. if (get_type() == type::range_type)
  1129. {
  1130. if (!r.last)
  1131. BOOST_THROW_EXCEPTION(
  1132. empty_exception("back() called on empty utree range"));
  1133. return r.last->val;
  1134. }
  1135. // otherwise...
  1136. if (get_type() != type::list_type)
  1137. BOOST_THROW_EXCEPTION(
  1138. bad_type_exception
  1139. ("back() called on non-list utree type", get_type()));
  1140. else if (!l.last)
  1141. BOOST_THROW_EXCEPTION(
  1142. empty_exception("back() called on empty utree list"));
  1143. return l.last->val;
  1144. }
  1145. inline utree const& utree::front() const
  1146. {
  1147. if (get_type() == type::reference_type)
  1148. return ((utree const*)p)->front();
  1149. if (get_type() == type::range_type)
  1150. {
  1151. if (!r.first)
  1152. BOOST_THROW_EXCEPTION(
  1153. empty_exception("front() called on empty utree range"));
  1154. return r.first->val;
  1155. }
  1156. // otherwise...
  1157. if (get_type() != type::list_type)
  1158. BOOST_THROW_EXCEPTION(
  1159. bad_type_exception
  1160. ("front() called on non-list utree type", get_type()));
  1161. else if (!l.first)
  1162. BOOST_THROW_EXCEPTION(
  1163. empty_exception("front() called on empty utree list"));
  1164. return l.first->val;
  1165. }
  1166. inline utree const& utree::back() const
  1167. {
  1168. if (get_type() == type::reference_type)
  1169. return ((utree const*)p)->back();
  1170. if (get_type() == type::range_type)
  1171. {
  1172. if (!r.last)
  1173. BOOST_THROW_EXCEPTION(
  1174. empty_exception("back() called on empty utree range"));
  1175. return r.last->val;
  1176. }
  1177. // otherwise...
  1178. if (get_type() != type::list_type)
  1179. BOOST_THROW_EXCEPTION(
  1180. bad_type_exception
  1181. ("back() called on non-list utree type", get_type()));
  1182. else if (!l.last)
  1183. BOOST_THROW_EXCEPTION(
  1184. empty_exception("back() called on empty utree list"));
  1185. return l.last->val;
  1186. }
  1187. inline void utree::swap(utree& other)
  1188. {
  1189. s.swap(other.s);
  1190. }
  1191. inline utree::type::info utree::get_type() const
  1192. {
  1193. // the fast string holds the type info
  1194. return static_cast<utree::type::info>(s.get_type());
  1195. }
  1196. inline void utree::set_type(type::info t)
  1197. {
  1198. // the fast string holds the type info
  1199. s.set_type(t);
  1200. }
  1201. inline void utree::ensure_list_type(char const* failed_in)
  1202. {
  1203. type::info t = get_type();
  1204. if (t == type::invalid_type)
  1205. {
  1206. set_type(type::list_type);
  1207. l.default_construct();
  1208. }
  1209. else if (get_type() != type::list_type)
  1210. {
  1211. std::string msg = failed_in;
  1212. msg += "called on non-list and non-invalid utree type";
  1213. BOOST_THROW_EXCEPTION(bad_type_exception(msg.c_str(), get_type()));
  1214. }
  1215. }
  1216. inline void utree::free()
  1217. {
  1218. switch (get_type())
  1219. {
  1220. case type::binary_type:
  1221. case type::symbol_type:
  1222. case type::string_type:
  1223. s.free();
  1224. break;
  1225. case type::list_type:
  1226. l.free();
  1227. break;
  1228. case type::function_type:
  1229. delete pf;
  1230. break;
  1231. default:
  1232. break;
  1233. };
  1234. s.initialize();
  1235. }
  1236. inline void utree::copy(utree const& other)
  1237. {
  1238. set_type(other.get_type());
  1239. switch (other.get_type())
  1240. {
  1241. default:
  1242. BOOST_THROW_EXCEPTION(
  1243. bad_type_exception("corrupt utree type", other.get_type()));
  1244. break;
  1245. case type::invalid_type:
  1246. case type::nil_type:
  1247. s.tag(other.s.tag());
  1248. break;
  1249. case type::bool_type:
  1250. b = other.b;
  1251. s.tag(other.s.tag());
  1252. break;
  1253. case type::int_type:
  1254. i = other.i;
  1255. s.tag(other.s.tag());
  1256. break;
  1257. case type::double_type:
  1258. d = other.d;
  1259. s.tag(other.s.tag());
  1260. break;
  1261. case type::reference_type:
  1262. p = other.p;
  1263. s.tag(other.s.tag());
  1264. break;
  1265. case type::any_type:
  1266. v = other.v;
  1267. s.tag(other.s.tag());
  1268. break;
  1269. case type::range_type:
  1270. r = other.r;
  1271. s.tag(other.s.tag());
  1272. break;
  1273. case type::string_range_type:
  1274. sr = other.sr;
  1275. s.tag(other.s.tag());
  1276. break;
  1277. case type::function_type:
  1278. pf = other.pf->clone();
  1279. s.tag(other.s.tag());
  1280. break;
  1281. case type::string_type:
  1282. case type::symbol_type:
  1283. case type::binary_type:
  1284. s.copy(other.s);
  1285. s.tag(other.s.tag());
  1286. break;
  1287. case type::list_type:
  1288. l.copy(other.l);
  1289. s.tag(other.s.tag());
  1290. break;
  1291. }
  1292. }
  1293. template <typename T>
  1294. struct is_iterator_range
  1295. : boost::mpl::false_
  1296. {};
  1297. template <typename Iterator>
  1298. struct is_iterator_range<boost::iterator_range<Iterator> >
  1299. : boost::mpl::true_
  1300. {};
  1301. template <typename To>
  1302. struct utree_cast
  1303. {
  1304. typedef To result_type;
  1305. template <typename From>
  1306. To dispatch(From const& val, boost::mpl::true_) const
  1307. {
  1308. return To(val); // From is convertible to To
  1309. }
  1310. template <typename From>
  1311. BOOST_NORETURN To dispatch(From const&, boost::mpl::false_) const
  1312. {
  1313. // From is NOT convertible to To !!!
  1314. throw std::bad_cast();
  1315. BOOST_UNREACHABLE_RETURN(To())
  1316. }
  1317. template <typename From>
  1318. To operator()(From const& val) const
  1319. {
  1320. // boost::iterator_range has a templated constructor, accepting
  1321. // any argument and hence any type is 'convertible' to it.
  1322. typedef typename boost::mpl::eval_if<
  1323. is_iterator_range<To>
  1324. , boost::is_same<From, To>, boost::is_convertible<From, To>
  1325. >::type is_convertible;
  1326. return dispatch(val, is_convertible());
  1327. }
  1328. };
  1329. template <typename T>
  1330. struct utree_cast<T*>
  1331. {
  1332. typedef T* result_type;
  1333. template <typename From>
  1334. BOOST_NORETURN T* operator()(From const&) const
  1335. {
  1336. // From is NOT convertible to T !!!
  1337. throw std::bad_cast();
  1338. BOOST_UNREACHABLE_RETURN(NULL)
  1339. }
  1340. T* operator()(any_ptr const& p) const
  1341. {
  1342. return p.get<T*>();
  1343. }
  1344. };
  1345. template <typename T>
  1346. inline T utree::get() const
  1347. {
  1348. return utree::visit(*this, utree_cast<T>());
  1349. }
  1350. inline utree& utree::deref()
  1351. {
  1352. return (get_type() == type::reference_type) ? *p : *this;
  1353. }
  1354. inline utree const& utree::deref() const
  1355. {
  1356. return (get_type() == type::reference_type) ? *p : *this;
  1357. }
  1358. inline short utree::tag() const
  1359. {
  1360. return s.tag();
  1361. }
  1362. inline void utree::tag(short tag)
  1363. {
  1364. s.tag(tag);
  1365. }
  1366. inline utree utree::eval(utree const& env) const
  1367. {
  1368. if (get_type() == type::reference_type)
  1369. return deref().eval(env);
  1370. if (get_type() != type::function_type)
  1371. BOOST_THROW_EXCEPTION(
  1372. bad_type_exception(
  1373. "eval() called on non-function utree type", get_type()));
  1374. return (*pf)(env);
  1375. }
  1376. inline utree utree::eval(utree& env) const
  1377. {
  1378. if (get_type() == type::reference_type)
  1379. return deref().eval(env);
  1380. if (get_type() != type::function_type)
  1381. BOOST_THROW_EXCEPTION(
  1382. bad_type_exception(
  1383. "eval() called on non-function utree type", get_type()));
  1384. return (*pf)(env);
  1385. }
  1386. inline utree utree::operator() (utree const& env) const
  1387. {
  1388. return eval(env);
  1389. }
  1390. inline utree utree::operator() (utree& env) const
  1391. {
  1392. return eval(env);
  1393. }
  1394. }}
  1395. #ifdef _MSC_VER
  1396. # pragma warning(pop)
  1397. #endif
  1398. #endif