ptree_implementation.hpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. // ----------------------------------------------------------------------------
  2. // Copyright (C) 2002-2006 Marcin Kalicinski
  3. // Copyright (C) 2009 Sebastian Redl
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // For more information, see www.boost.org
  10. // ----------------------------------------------------------------------------
  11. #ifndef BOOST_PROPERTY_TREE_DETAIL_PTREE_IMPLEMENTATION_HPP_INCLUDED
  12. #define BOOST_PROPERTY_TREE_DETAIL_PTREE_IMPLEMENTATION_HPP_INCLUDED
  13. #include <boost/iterator/iterator_adaptor.hpp>
  14. #include <boost/iterator/reverse_iterator.hpp>
  15. #include <boost/assert.hpp>
  16. #include <boost/utility/swap.hpp>
  17. #include <memory>
  18. #if (defined(BOOST_MSVC) && \
  19. (_MSC_FULL_VER >= 160000000 && _MSC_FULL_VER < 170000000)) || \
  20. (defined(BOOST_INTEL_WIN) && \
  21. defined(BOOST_DINKUMWARE_STDLIB))
  22. #define BOOST_PROPERTY_TREE_PAIR_BUG
  23. #endif
  24. namespace boost { namespace property_tree
  25. {
  26. template <class K, class D, class C>
  27. struct basic_ptree<K, D, C>::subs
  28. {
  29. struct by_name {};
  30. // The actual child container.
  31. #if defined(BOOST_PROPERTY_TREE_PAIR_BUG)
  32. // MSVC 10 has moved std::pair's members to a base
  33. // class. Unfortunately this does break the interface.
  34. BOOST_STATIC_CONSTANT(unsigned,
  35. first_offset = offsetof(value_type, first));
  36. #endif
  37. typedef multi_index_container<value_type,
  38. multi_index::indexed_by<
  39. multi_index::sequenced<>,
  40. multi_index::ordered_non_unique<multi_index::tag<by_name>,
  41. #if defined(BOOST_PROPERTY_TREE_PAIR_BUG)
  42. multi_index::member_offset<value_type, const key_type,
  43. first_offset>,
  44. #else
  45. multi_index::member<value_type, const key_type,
  46. &value_type::first>,
  47. #endif
  48. key_compare
  49. >
  50. >
  51. > base_container;
  52. // The by-name lookup index.
  53. typedef typename base_container::template index<by_name>::type
  54. by_name_index;
  55. // Access functions for getting to the children of a tree.
  56. static base_container& ch(self_type *s) {
  57. return *static_cast<base_container*>(s->m_children);
  58. }
  59. static const base_container& ch(const self_type *s) {
  60. return *static_cast<const base_container*>(s->m_children);
  61. }
  62. static by_name_index& assoc(self_type *s) {
  63. return ch(s).BOOST_NESTED_TEMPLATE get<by_name>();
  64. }
  65. static const by_name_index& assoc(const self_type *s) {
  66. return ch(s).BOOST_NESTED_TEMPLATE get<by_name>();
  67. }
  68. };
  69. template <class K, class D, class C>
  70. class basic_ptree<K, D, C>::iterator : public boost::iterator_adaptor<
  71. iterator, typename subs::base_container::iterator, value_type>
  72. {
  73. friend class boost::iterator_core_access;
  74. typedef boost::iterator_adaptor<
  75. iterator, typename subs::base_container::iterator, value_type>
  76. baset;
  77. public:
  78. typedef typename baset::reference reference;
  79. iterator() {}
  80. explicit iterator(typename iterator::base_type b)
  81. : iterator::iterator_adaptor_(b)
  82. {}
  83. reference dereference() const
  84. {
  85. // multi_index doesn't allow modification of its values, because
  86. // indexes could sort by anything, and modification screws that up.
  87. // However, we only sort by the key, and it's protected against
  88. // modification in the value_type, so this const_cast is safe.
  89. return const_cast<reference>(*this->base_reference());
  90. }
  91. };
  92. template <class K, class D, class C>
  93. class basic_ptree<K, D, C>::const_iterator : public boost::iterator_adaptor<
  94. const_iterator, typename subs::base_container::const_iterator>
  95. {
  96. public:
  97. const_iterator() {}
  98. explicit const_iterator(typename const_iterator::base_type b)
  99. : const_iterator::iterator_adaptor_(b)
  100. {}
  101. const_iterator(iterator b)
  102. : const_iterator::iterator_adaptor_(b.base())
  103. {}
  104. };
  105. template <class K, class D, class C>
  106. class basic_ptree<K, D, C>::reverse_iterator
  107. : public boost::reverse_iterator<iterator>
  108. {
  109. public:
  110. reverse_iterator() {}
  111. explicit reverse_iterator(iterator b)
  112. : boost::reverse_iterator<iterator>(b)
  113. {}
  114. };
  115. template <class K, class D, class C>
  116. class basic_ptree<K, D, C>::const_reverse_iterator
  117. : public boost::reverse_iterator<const_iterator>
  118. {
  119. public:
  120. const_reverse_iterator() {}
  121. explicit const_reverse_iterator(const_iterator b)
  122. : boost::reverse_iterator<const_iterator>(b)
  123. {}
  124. const_reverse_iterator(
  125. typename basic_ptree<K, D, C>::reverse_iterator b)
  126. : boost::reverse_iterator<const_iterator>(b)
  127. {}
  128. };
  129. template <class K, class D, class C>
  130. class basic_ptree<K, D, C>::assoc_iterator
  131. : public boost::iterator_adaptor<assoc_iterator,
  132. typename subs::by_name_index::iterator,
  133. value_type>
  134. {
  135. friend class boost::iterator_core_access;
  136. typedef boost::iterator_adaptor<assoc_iterator,
  137. typename subs::by_name_index::iterator,
  138. value_type>
  139. baset;
  140. public:
  141. typedef typename baset::reference reference;
  142. assoc_iterator() {}
  143. explicit assoc_iterator(typename assoc_iterator::base_type b)
  144. : assoc_iterator::iterator_adaptor_(b)
  145. {}
  146. reference dereference() const
  147. {
  148. return const_cast<reference>(*this->base_reference());
  149. }
  150. };
  151. template <class K, class D, class C>
  152. class basic_ptree<K, D, C>::const_assoc_iterator
  153. : public boost::iterator_adaptor<const_assoc_iterator,
  154. typename subs::by_name_index::const_iterator>
  155. {
  156. public:
  157. const_assoc_iterator() {}
  158. explicit const_assoc_iterator(
  159. typename const_assoc_iterator::base_type b)
  160. : const_assoc_iterator::iterator_adaptor_(b)
  161. {}
  162. const_assoc_iterator(assoc_iterator b)
  163. : const_assoc_iterator::iterator_adaptor_(b.base())
  164. {}
  165. };
  166. // Big five
  167. // Perhaps the children collection could be created on-demand only, to
  168. // reduce heap traffic. But that's a lot more work to implement.
  169. template<class K, class D, class C> inline
  170. basic_ptree<K, D, C>::basic_ptree()
  171. : m_children(new typename subs::base_container)
  172. {
  173. }
  174. template<class K, class D, class C> inline
  175. basic_ptree<K, D, C>::basic_ptree(const data_type &d)
  176. : m_data(d), m_children(new typename subs::base_container)
  177. {
  178. }
  179. template<class K, class D, class C> inline
  180. basic_ptree<K, D, C>::basic_ptree(const basic_ptree<K, D, C> &rhs)
  181. : m_data(rhs.m_data),
  182. m_children(new typename subs::base_container(subs::ch(&rhs)))
  183. {
  184. }
  185. template<class K, class D, class C>
  186. basic_ptree<K, D, C> &
  187. basic_ptree<K, D, C>::operator =(const basic_ptree<K, D, C> &rhs)
  188. {
  189. self_type(rhs).swap(*this);
  190. return *this;
  191. }
  192. template<class K, class D, class C>
  193. basic_ptree<K, D, C>::~basic_ptree()
  194. {
  195. delete &subs::ch(this);
  196. }
  197. template<class K, class D, class C> inline
  198. void basic_ptree<K, D, C>::swap(basic_ptree<K, D, C> &rhs)
  199. {
  200. boost::swap(m_data, rhs.m_data);
  201. // Void pointers, no ADL necessary
  202. std::swap(m_children, rhs.m_children);
  203. }
  204. // Container view
  205. template<class K, class D, class C> inline
  206. typename basic_ptree<K, D, C>::size_type
  207. basic_ptree<K, D, C>::size() const
  208. {
  209. return subs::ch(this).size();
  210. }
  211. template<class K, class D, class C> inline
  212. typename basic_ptree<K, D, C>::size_type
  213. basic_ptree<K, D, C>::max_size() const
  214. {
  215. return subs::ch(this).max_size();
  216. }
  217. template<class K, class D, class C> inline
  218. bool basic_ptree<K, D, C>::empty() const
  219. {
  220. return subs::ch(this).empty();
  221. }
  222. template<class K, class D, class C> inline
  223. typename basic_ptree<K, D, C>::iterator
  224. basic_ptree<K, D, C>::begin()
  225. {
  226. return iterator(subs::ch(this).begin());
  227. }
  228. template<class K, class D, class C> inline
  229. typename basic_ptree<K, D, C>::const_iterator
  230. basic_ptree<K, D, C>::begin() const
  231. {
  232. return const_iterator(subs::ch(this).begin());
  233. }
  234. template<class K, class D, class C> inline
  235. typename basic_ptree<K, D, C>::iterator
  236. basic_ptree<K, D, C>::end()
  237. {
  238. return iterator(subs::ch(this).end());
  239. }
  240. template<class K, class D, class C> inline
  241. typename basic_ptree<K, D, C>::const_iterator
  242. basic_ptree<K, D, C>::end() const
  243. {
  244. return const_iterator(subs::ch(this).end());
  245. }
  246. template<class K, class D, class C> inline
  247. typename basic_ptree<K, D, C>::reverse_iterator
  248. basic_ptree<K, D, C>::rbegin()
  249. {
  250. return reverse_iterator(this->end());
  251. }
  252. template<class K, class D, class C> inline
  253. typename basic_ptree<K, D, C>::const_reverse_iterator
  254. basic_ptree<K, D, C>::rbegin() const
  255. {
  256. return const_reverse_iterator(this->end());
  257. }
  258. template<class K, class D, class C> inline
  259. typename basic_ptree<K, D, C>::reverse_iterator
  260. basic_ptree<K, D, C>::rend()
  261. {
  262. return reverse_iterator(this->begin());
  263. }
  264. template<class K, class D, class C> inline
  265. typename basic_ptree<K, D, C>::const_reverse_iterator
  266. basic_ptree<K, D, C>::rend() const
  267. {
  268. return const_reverse_iterator(this->begin());
  269. }
  270. template<class K, class D, class C> inline
  271. typename basic_ptree<K, D, C>::value_type &
  272. basic_ptree<K, D, C>::front()
  273. {
  274. return const_cast<value_type&>(subs::ch(this).front());
  275. }
  276. template<class K, class D, class C> inline
  277. const typename basic_ptree<K, D, C>::value_type &
  278. basic_ptree<K, D, C>::front() const
  279. {
  280. return subs::ch(this).front();
  281. }
  282. template<class K, class D, class C> inline
  283. typename basic_ptree<K, D, C>::value_type &
  284. basic_ptree<K, D, C>::back()
  285. {
  286. return const_cast<value_type&>(subs::ch(this).back());
  287. }
  288. template<class K, class D, class C> inline
  289. const typename basic_ptree<K, D, C>::value_type &
  290. basic_ptree<K, D, C>::back() const
  291. {
  292. return subs::ch(this).back();
  293. }
  294. template<class K, class D, class C> inline
  295. typename basic_ptree<K, D, C>::iterator
  296. basic_ptree<K, D, C>::insert(iterator where, const value_type &value)
  297. {
  298. return iterator(subs::ch(this).insert(where.base(), value).first);
  299. }
  300. template<class K, class D, class C>
  301. template<class It> inline
  302. void basic_ptree<K, D, C>::insert(iterator where, It first, It last)
  303. {
  304. subs::ch(this).insert(where.base(), first, last);
  305. }
  306. template<class K, class D, class C> inline
  307. typename basic_ptree<K, D, C>::iterator
  308. basic_ptree<K, D, C>::erase(iterator where)
  309. {
  310. return iterator(subs::ch(this).erase(where.base()));
  311. }
  312. template<class K, class D, class C> inline
  313. typename basic_ptree<K, D, C>::iterator
  314. basic_ptree<K, D, C>::erase(iterator first, iterator last)
  315. {
  316. return iterator(subs::ch(this).erase(first.base(), last.base()));
  317. }
  318. template<class K, class D, class C> inline
  319. typename basic_ptree<K, D, C>::iterator
  320. basic_ptree<K, D, C>::push_front(const value_type &value)
  321. {
  322. return iterator(subs::ch(this).push_front(value).first);
  323. }
  324. template<class K, class D, class C> inline
  325. typename basic_ptree<K, D, C>::iterator
  326. basic_ptree<K, D, C>::push_back(const value_type &value)
  327. {
  328. return iterator(subs::ch(this).push_back(value).first);
  329. }
  330. template<class K, class D, class C> inline
  331. void basic_ptree<K, D, C>::pop_front()
  332. {
  333. subs::ch(this).pop_front();
  334. }
  335. template<class K, class D, class C> inline
  336. void basic_ptree<K, D, C>::pop_back()
  337. {
  338. subs::ch(this).pop_back();
  339. }
  340. template<class K, class D, class C> inline
  341. void basic_ptree<K, D, C>::reverse()
  342. {
  343. subs::ch(this).reverse();
  344. }
  345. namespace impl
  346. {
  347. struct by_first
  348. {
  349. template <typename P>
  350. bool operator ()(const P& lhs, const P& rhs) const {
  351. return lhs.first < rhs.first;
  352. }
  353. };
  354. }
  355. template<class K, class D, class C> inline
  356. void basic_ptree<K, D, C>::sort()
  357. {
  358. sort(impl::by_first());
  359. }
  360. template<class K, class D, class C>
  361. template<class Compare> inline
  362. void basic_ptree<K, D, C>::sort(Compare comp)
  363. {
  364. subs::ch(this).sort(comp);
  365. }
  366. // Equality
  367. template<class K, class D, class C> inline
  368. bool basic_ptree<K, D, C>::operator ==(
  369. const basic_ptree<K, D, C> &rhs) const
  370. {
  371. // The size test is cheap, so add it as an optimization
  372. return size() == rhs.size() && data() == rhs.data() &&
  373. subs::ch(this) == subs::ch(&rhs);
  374. }
  375. template<class K, class D, class C> inline
  376. bool basic_ptree<K, D, C>::operator !=(
  377. const basic_ptree<K, D, C> &rhs) const
  378. {
  379. return !(*this == rhs);
  380. }
  381. // Associative view
  382. template<class K, class D, class C> inline
  383. typename basic_ptree<K, D, C>::assoc_iterator
  384. basic_ptree<K, D, C>::ordered_begin()
  385. {
  386. return assoc_iterator(subs::assoc(this).begin());
  387. }
  388. template<class K, class D, class C> inline
  389. typename basic_ptree<K, D, C>::const_assoc_iterator
  390. basic_ptree<K, D, C>::ordered_begin() const
  391. {
  392. return const_assoc_iterator(subs::assoc(this).begin());
  393. }
  394. template<class K, class D, class C> inline
  395. typename basic_ptree<K, D, C>::assoc_iterator
  396. basic_ptree<K, D, C>::not_found()
  397. {
  398. return assoc_iterator(subs::assoc(this).end());
  399. }
  400. template<class K, class D, class C> inline
  401. typename basic_ptree<K, D, C>::const_assoc_iterator
  402. basic_ptree<K, D, C>::not_found() const
  403. {
  404. return const_assoc_iterator(subs::assoc(this).end());
  405. }
  406. template<class K, class D, class C> inline
  407. typename basic_ptree<K, D, C>::assoc_iterator
  408. basic_ptree<K, D, C>::find(const key_type &key)
  409. {
  410. return assoc_iterator(subs::assoc(this).find(key));
  411. }
  412. template<class K, class D, class C> inline
  413. typename basic_ptree<K, D, C>::const_assoc_iterator
  414. basic_ptree<K, D, C>::find(const key_type &key) const
  415. {
  416. return const_assoc_iterator(subs::assoc(this).find(key));
  417. }
  418. template<class K, class D, class C> inline
  419. std::pair<
  420. typename basic_ptree<K, D, C>::assoc_iterator,
  421. typename basic_ptree<K, D, C>::assoc_iterator
  422. > basic_ptree<K, D, C>::equal_range(const key_type &key)
  423. {
  424. std::pair<typename subs::by_name_index::iterator,
  425. typename subs::by_name_index::iterator> r(
  426. subs::assoc(this).equal_range(key));
  427. return std::pair<assoc_iterator, assoc_iterator>(
  428. assoc_iterator(r.first), assoc_iterator(r.second));
  429. }
  430. template<class K, class D, class C> inline
  431. std::pair<
  432. typename basic_ptree<K, D, C>::const_assoc_iterator,
  433. typename basic_ptree<K, D, C>::const_assoc_iterator
  434. > basic_ptree<K, D, C>::equal_range(const key_type &key) const
  435. {
  436. std::pair<typename subs::by_name_index::const_iterator,
  437. typename subs::by_name_index::const_iterator> r(
  438. subs::assoc(this).equal_range(key));
  439. return std::pair<const_assoc_iterator, const_assoc_iterator>(
  440. const_assoc_iterator(r.first), const_assoc_iterator(r.second));
  441. }
  442. template<class K, class D, class C> inline
  443. typename basic_ptree<K, D, C>::size_type
  444. basic_ptree<K, D, C>::count(const key_type &key) const
  445. {
  446. return subs::assoc(this).count(key);
  447. }
  448. template<class K, class D, class C> inline
  449. typename basic_ptree<K, D, C>::size_type
  450. basic_ptree<K, D, C>::erase(const key_type &key)
  451. {
  452. return subs::assoc(this).erase(key);
  453. }
  454. template<class K, class D, class C> inline
  455. typename basic_ptree<K, D, C>::iterator
  456. basic_ptree<K, D, C>::to_iterator(assoc_iterator ai)
  457. {
  458. return iterator(subs::ch(this).
  459. BOOST_NESTED_TEMPLATE project<0>(ai.base()));
  460. }
  461. template<class K, class D, class C> inline
  462. typename basic_ptree<K, D, C>::const_iterator
  463. basic_ptree<K, D, C>::to_iterator(const_assoc_iterator ai) const
  464. {
  465. return const_iterator(subs::ch(this).
  466. BOOST_NESTED_TEMPLATE project<0>(ai.base()));
  467. }
  468. // Property tree view
  469. template<class K, class D, class C> inline
  470. typename basic_ptree<K, D, C>::data_type &
  471. basic_ptree<K, D, C>::data()
  472. {
  473. return m_data;
  474. }
  475. template<class K, class D, class C> inline
  476. const typename basic_ptree<K, D, C>::data_type &
  477. basic_ptree<K, D, C>::data() const
  478. {
  479. return m_data;
  480. }
  481. template<class K, class D, class C> inline
  482. void basic_ptree<K, D, C>::clear()
  483. {
  484. m_data = data_type();
  485. subs::ch(this).clear();
  486. }
  487. template<class K, class D, class C>
  488. basic_ptree<K, D, C> &
  489. basic_ptree<K, D, C>::get_child(const path_type &path)
  490. {
  491. path_type p(path);
  492. self_type *n = walk_path(p);
  493. if (!n) {
  494. BOOST_PROPERTY_TREE_THROW(ptree_bad_path("No such node", path));
  495. }
  496. return *n;
  497. }
  498. template<class K, class D, class C> inline
  499. const basic_ptree<K, D, C> &
  500. basic_ptree<K, D, C>::get_child(const path_type &path) const
  501. {
  502. return const_cast<self_type*>(this)->get_child(path);
  503. }
  504. template<class K, class D, class C> inline
  505. basic_ptree<K, D, C> &
  506. basic_ptree<K, D, C>::get_child(const path_type &path,
  507. self_type &default_value)
  508. {
  509. path_type p(path);
  510. self_type *n = walk_path(p);
  511. return n ? *n : default_value;
  512. }
  513. template<class K, class D, class C> inline
  514. const basic_ptree<K, D, C> &
  515. basic_ptree<K, D, C>::get_child(const path_type &path,
  516. const self_type &default_value) const
  517. {
  518. return const_cast<self_type*>(this)->get_child(path,
  519. const_cast<self_type&>(default_value));
  520. }
  521. template<class K, class D, class C>
  522. optional<basic_ptree<K, D, C> &>
  523. basic_ptree<K, D, C>::get_child_optional(const path_type &path)
  524. {
  525. path_type p(path);
  526. self_type *n = walk_path(p);
  527. if (!n) {
  528. return optional<self_type&>();
  529. }
  530. return *n;
  531. }
  532. template<class K, class D, class C>
  533. optional<const basic_ptree<K, D, C> &>
  534. basic_ptree<K, D, C>::get_child_optional(const path_type &path) const
  535. {
  536. path_type p(path);
  537. self_type *n = walk_path(p);
  538. if (!n) {
  539. return optional<const self_type&>();
  540. }
  541. return *n;
  542. }
  543. template<class K, class D, class C>
  544. basic_ptree<K, D, C> &
  545. basic_ptree<K, D, C>::put_child(const path_type &path,
  546. const self_type &value)
  547. {
  548. path_type p(path);
  549. self_type &parent = force_path(p);
  550. // Got the parent. Now get the correct child.
  551. key_type fragment = p.reduce();
  552. assoc_iterator el = parent.find(fragment);
  553. // If the new child exists, replace it.
  554. if(el != parent.not_found()) {
  555. return el->second = value;
  556. } else {
  557. return parent.push_back(value_type(fragment, value))->second;
  558. }
  559. }
  560. template<class K, class D, class C>
  561. basic_ptree<K, D, C> &
  562. basic_ptree<K, D, C>::add_child(const path_type &path,
  563. const self_type &value)
  564. {
  565. path_type p(path);
  566. self_type &parent = force_path(p);
  567. // Got the parent.
  568. key_type fragment = p.reduce();
  569. return parent.push_back(value_type(fragment, value))->second;
  570. }
  571. template<class K, class D, class C>
  572. template<class Type, class Translator>
  573. typename boost::enable_if<detail::is_translator<Translator>, Type>::type
  574. basic_ptree<K, D, C>::get_value(Translator tr) const
  575. {
  576. if(boost::optional<Type> o = get_value_optional<Type>(tr)) {
  577. return *o;
  578. }
  579. BOOST_PROPERTY_TREE_THROW(ptree_bad_data(
  580. std::string("conversion of data to type \"") +
  581. typeid(Type).name() + "\" failed", data()));
  582. }
  583. template<class K, class D, class C>
  584. template<class Type> inline
  585. Type basic_ptree<K, D, C>::get_value() const
  586. {
  587. return get_value<Type>(
  588. typename translator_between<data_type, Type>::type());
  589. }
  590. template<class K, class D, class C>
  591. template<class Type, class Translator> inline
  592. Type basic_ptree<K, D, C>::get_value(const Type &default_value,
  593. Translator tr) const
  594. {
  595. return get_value_optional<Type>(tr).get_value_or(default_value);
  596. }
  597. template<class K, class D, class C>
  598. template <class Ch, class Translator>
  599. typename boost::enable_if<
  600. detail::is_character<Ch>,
  601. std::basic_string<Ch>
  602. >::type
  603. basic_ptree<K, D, C>::get_value(const Ch *default_value, Translator tr)const
  604. {
  605. return get_value<std::basic_string<Ch>, Translator>(default_value, tr);
  606. }
  607. template<class K, class D, class C>
  608. template<class Type> inline
  609. typename boost::disable_if<detail::is_translator<Type>, Type>::type
  610. basic_ptree<K, D, C>::get_value(const Type &default_value) const
  611. {
  612. return get_value(default_value,
  613. typename translator_between<data_type, Type>::type());
  614. }
  615. template<class K, class D, class C>
  616. template <class Ch>
  617. typename boost::enable_if<
  618. detail::is_character<Ch>,
  619. std::basic_string<Ch>
  620. >::type
  621. basic_ptree<K, D, C>::get_value(const Ch *default_value) const
  622. {
  623. return get_value< std::basic_string<Ch> >(default_value);
  624. }
  625. template<class K, class D, class C>
  626. template<class Type, class Translator> inline
  627. optional<Type> basic_ptree<K, D, C>::get_value_optional(
  628. Translator tr) const
  629. {
  630. return tr.get_value(data());
  631. }
  632. template<class K, class D, class C>
  633. template<class Type> inline
  634. optional<Type> basic_ptree<K, D, C>::get_value_optional() const
  635. {
  636. return get_value_optional<Type>(
  637. typename translator_between<data_type, Type>::type());
  638. }
  639. template<class K, class D, class C>
  640. template<class Type, class Translator> inline
  641. typename boost::enable_if<detail::is_translator<Translator>, Type>::type
  642. basic_ptree<K, D, C>::get(const path_type &path,
  643. Translator tr) const
  644. {
  645. return get_child(path).BOOST_NESTED_TEMPLATE get_value<Type>(tr);
  646. }
  647. template<class K, class D, class C>
  648. template<class Type> inline
  649. Type basic_ptree<K, D, C>::get(const path_type &path) const
  650. {
  651. return get_child(path).BOOST_NESTED_TEMPLATE get_value<Type>();
  652. }
  653. template<class K, class D, class C>
  654. template<class Type, class Translator> inline
  655. Type basic_ptree<K, D, C>::get(const path_type &path,
  656. const Type &default_value,
  657. Translator tr) const
  658. {
  659. return get_optional<Type>(path, tr).get_value_or(default_value);
  660. }
  661. template<class K, class D, class C>
  662. template <class Ch, class Translator>
  663. typename boost::enable_if<
  664. detail::is_character<Ch>,
  665. std::basic_string<Ch>
  666. >::type
  667. basic_ptree<K, D, C>::get(
  668. const path_type &path, const Ch *default_value, Translator tr) const
  669. {
  670. return get<std::basic_string<Ch>, Translator>(path, default_value, tr);
  671. }
  672. template<class K, class D, class C>
  673. template<class Type> inline
  674. typename boost::disable_if<detail::is_translator<Type>, Type>::type
  675. basic_ptree<K, D, C>::get(const path_type &path,
  676. const Type &default_value) const
  677. {
  678. return get_optional<Type>(path).get_value_or(default_value);
  679. }
  680. template<class K, class D, class C>
  681. template <class Ch>
  682. typename boost::enable_if<
  683. detail::is_character<Ch>,
  684. std::basic_string<Ch>
  685. >::type
  686. basic_ptree<K, D, C>::get(
  687. const path_type &path, const Ch *default_value) const
  688. {
  689. return get< std::basic_string<Ch> >(path, default_value);
  690. }
  691. template<class K, class D, class C>
  692. template<class Type, class Translator>
  693. optional<Type> basic_ptree<K, D, C>::get_optional(const path_type &path,
  694. Translator tr) const
  695. {
  696. if (optional<const self_type&> child = get_child_optional(path))
  697. return child.get().
  698. BOOST_NESTED_TEMPLATE get_value_optional<Type>(tr);
  699. else
  700. return optional<Type>();
  701. }
  702. template<class K, class D, class C>
  703. template<class Type>
  704. optional<Type> basic_ptree<K, D, C>::get_optional(
  705. const path_type &path) const
  706. {
  707. if (optional<const self_type&> child = get_child_optional(path))
  708. return child.get().BOOST_NESTED_TEMPLATE get_value_optional<Type>();
  709. else
  710. return optional<Type>();
  711. }
  712. template<class K, class D, class C>
  713. template<class Type, class Translator>
  714. void basic_ptree<K, D, C>::put_value(const Type &value, Translator tr)
  715. {
  716. if(optional<data_type> o = tr.put_value(value)) {
  717. data() = *o;
  718. } else {
  719. BOOST_PROPERTY_TREE_THROW(ptree_bad_data(
  720. std::string("conversion of type \"") + typeid(Type).name() +
  721. "\" to data failed", boost::any()));
  722. }
  723. }
  724. template<class K, class D, class C>
  725. template<class Type> inline
  726. void basic_ptree<K, D, C>::put_value(const Type &value)
  727. {
  728. put_value(value, typename translator_between<data_type, Type>::type());
  729. }
  730. template<class K, class D, class C>
  731. template<class Type, typename Translator>
  732. basic_ptree<K, D, C> & basic_ptree<K, D, C>::put(
  733. const path_type &path, const Type &value, Translator tr)
  734. {
  735. if(optional<self_type &> child = get_child_optional(path)) {
  736. child.get().put_value(value, tr);
  737. return *child;
  738. } else {
  739. self_type &child2 = put_child(path, self_type());
  740. child2.put_value(value, tr);
  741. return child2;
  742. }
  743. }
  744. template<class K, class D, class C>
  745. template<class Type> inline
  746. basic_ptree<K, D, C> & basic_ptree<K, D, C>::put(
  747. const path_type &path, const Type &value)
  748. {
  749. return put(path, value,
  750. typename translator_between<data_type, Type>::type());
  751. }
  752. template<class K, class D, class C>
  753. template<class Type, typename Translator> inline
  754. basic_ptree<K, D, C> & basic_ptree<K, D, C>::add(
  755. const path_type &path, const Type &value, Translator tr)
  756. {
  757. self_type &child = add_child(path, self_type());
  758. child.put_value(value, tr);
  759. return child;
  760. }
  761. template<class K, class D, class C>
  762. template<class Type> inline
  763. basic_ptree<K, D, C> & basic_ptree<K, D, C>::add(
  764. const path_type &path, const Type &value)
  765. {
  766. return add(path, value,
  767. typename translator_between<data_type, Type>::type());
  768. }
  769. template<class K, class D, class C>
  770. basic_ptree<K, D, C> *
  771. basic_ptree<K, D, C>::walk_path(path_type &p) const
  772. {
  773. if(p.empty()) {
  774. // I'm the child we're looking for.
  775. return const_cast<basic_ptree*>(this);
  776. }
  777. // Recurse down the tree to find the path.
  778. key_type fragment = p.reduce();
  779. const_assoc_iterator el = find(fragment);
  780. if(el == not_found()) {
  781. // No such child.
  782. return 0;
  783. }
  784. // Not done yet, recurse.
  785. return el->second.walk_path(p);
  786. }
  787. template<class K, class D, class C>
  788. basic_ptree<K, D, C> & basic_ptree<K, D, C>::force_path(path_type &p)
  789. {
  790. BOOST_ASSERT(!p.empty() && "Empty path not allowed for put_child.");
  791. if(p.single()) {
  792. // I'm the parent we're looking for.
  793. return *this;
  794. }
  795. key_type fragment = p.reduce();
  796. assoc_iterator el = find(fragment);
  797. // If we've found an existing child, go down that path. Else
  798. // create a new one.
  799. self_type& child = el == not_found() ?
  800. push_back(value_type(fragment, self_type()))->second : el->second;
  801. return child.force_path(p);
  802. }
  803. // Free functions
  804. template<class K, class D, class C>
  805. inline void swap(basic_ptree<K, D, C> &pt1, basic_ptree<K, D, C> &pt2)
  806. {
  807. pt1.swap(pt2);
  808. }
  809. } }
  810. #if defined(BOOST_PROPERTY_TREE_PAIR_BUG)
  811. #undef BOOST_PROPERTY_TREE_PAIR_BUG
  812. #endif
  813. #endif