ptree_implementation.hpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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. template <typename C>
  355. struct equal_pred
  356. {
  357. template <typename P>
  358. bool operator ()(const P& lhs, const P& rhs) const {
  359. C c;
  360. return !c(lhs.first, rhs.first) &&
  361. !c(rhs.first, lhs.first) &&
  362. lhs.second == rhs.second;
  363. }
  364. };
  365. template <typename C, typename MI>
  366. bool equal_children(const MI& ch1, const MI& ch2) {
  367. // Assumes ch1.size() == ch2.size()
  368. return std::equal(ch1.begin(), ch1.end(),
  369. ch2.begin(), equal_pred<C>());
  370. }
  371. }
  372. template<class K, class D, class C> inline
  373. void basic_ptree<K, D, C>::sort()
  374. {
  375. sort(impl::by_first());
  376. }
  377. template<class K, class D, class C>
  378. template<class Compare> inline
  379. void basic_ptree<K, D, C>::sort(Compare comp)
  380. {
  381. subs::ch(this).sort(comp);
  382. }
  383. // Equality
  384. template<class K, class D, class C> inline
  385. bool basic_ptree<K, D, C>::operator ==(
  386. const basic_ptree<K, D, C> &rhs) const
  387. {
  388. // The size test is cheap, so add it as an optimization
  389. return size() == rhs.size() && data() == rhs.data() &&
  390. impl::equal_children<C>(subs::ch(this), subs::ch(&rhs));
  391. }
  392. template<class K, class D, class C> inline
  393. bool basic_ptree<K, D, C>::operator !=(
  394. const basic_ptree<K, D, C> &rhs) const
  395. {
  396. return !(*this == rhs);
  397. }
  398. // Associative view
  399. template<class K, class D, class C> inline
  400. typename basic_ptree<K, D, C>::assoc_iterator
  401. basic_ptree<K, D, C>::ordered_begin()
  402. {
  403. return assoc_iterator(subs::assoc(this).begin());
  404. }
  405. template<class K, class D, class C> inline
  406. typename basic_ptree<K, D, C>::const_assoc_iterator
  407. basic_ptree<K, D, C>::ordered_begin() const
  408. {
  409. return const_assoc_iterator(subs::assoc(this).begin());
  410. }
  411. template<class K, class D, class C> inline
  412. typename basic_ptree<K, D, C>::assoc_iterator
  413. basic_ptree<K, D, C>::not_found()
  414. {
  415. return assoc_iterator(subs::assoc(this).end());
  416. }
  417. template<class K, class D, class C> inline
  418. typename basic_ptree<K, D, C>::const_assoc_iterator
  419. basic_ptree<K, D, C>::not_found() const
  420. {
  421. return const_assoc_iterator(subs::assoc(this).end());
  422. }
  423. template<class K, class D, class C> inline
  424. typename basic_ptree<K, D, C>::assoc_iterator
  425. basic_ptree<K, D, C>::find(const key_type &key)
  426. {
  427. return assoc_iterator(subs::assoc(this).find(key));
  428. }
  429. template<class K, class D, class C> inline
  430. typename basic_ptree<K, D, C>::const_assoc_iterator
  431. basic_ptree<K, D, C>::find(const key_type &key) const
  432. {
  433. return const_assoc_iterator(subs::assoc(this).find(key));
  434. }
  435. template<class K, class D, class C> inline
  436. std::pair<
  437. typename basic_ptree<K, D, C>::assoc_iterator,
  438. typename basic_ptree<K, D, C>::assoc_iterator
  439. > basic_ptree<K, D, C>::equal_range(const key_type &key)
  440. {
  441. std::pair<typename subs::by_name_index::iterator,
  442. typename subs::by_name_index::iterator> r(
  443. subs::assoc(this).equal_range(key));
  444. return std::pair<assoc_iterator, assoc_iterator>(
  445. assoc_iterator(r.first), assoc_iterator(r.second));
  446. }
  447. template<class K, class D, class C> inline
  448. std::pair<
  449. typename basic_ptree<K, D, C>::const_assoc_iterator,
  450. typename basic_ptree<K, D, C>::const_assoc_iterator
  451. > basic_ptree<K, D, C>::equal_range(const key_type &key) const
  452. {
  453. std::pair<typename subs::by_name_index::const_iterator,
  454. typename subs::by_name_index::const_iterator> r(
  455. subs::assoc(this).equal_range(key));
  456. return std::pair<const_assoc_iterator, const_assoc_iterator>(
  457. const_assoc_iterator(r.first), const_assoc_iterator(r.second));
  458. }
  459. template<class K, class D, class C> inline
  460. typename basic_ptree<K, D, C>::size_type
  461. basic_ptree<K, D, C>::count(const key_type &key) const
  462. {
  463. return subs::assoc(this).count(key);
  464. }
  465. template<class K, class D, class C> inline
  466. typename basic_ptree<K, D, C>::size_type
  467. basic_ptree<K, D, C>::erase(const key_type &key)
  468. {
  469. return subs::assoc(this).erase(key);
  470. }
  471. template<class K, class D, class C> inline
  472. typename basic_ptree<K, D, C>::iterator
  473. basic_ptree<K, D, C>::to_iterator(assoc_iterator ai)
  474. {
  475. return iterator(subs::ch(this).
  476. BOOST_NESTED_TEMPLATE project<0>(ai.base()));
  477. }
  478. template<class K, class D, class C> inline
  479. typename basic_ptree<K, D, C>::const_iterator
  480. basic_ptree<K, D, C>::to_iterator(const_assoc_iterator ai) const
  481. {
  482. return const_iterator(subs::ch(this).
  483. BOOST_NESTED_TEMPLATE project<0>(ai.base()));
  484. }
  485. // Property tree view
  486. template<class K, class D, class C> inline
  487. typename basic_ptree<K, D, C>::data_type &
  488. basic_ptree<K, D, C>::data()
  489. {
  490. return m_data;
  491. }
  492. template<class K, class D, class C> inline
  493. const typename basic_ptree<K, D, C>::data_type &
  494. basic_ptree<K, D, C>::data() const
  495. {
  496. return m_data;
  497. }
  498. template<class K, class D, class C> inline
  499. void basic_ptree<K, D, C>::clear()
  500. {
  501. m_data = data_type();
  502. subs::ch(this).clear();
  503. }
  504. template<class K, class D, class C>
  505. basic_ptree<K, D, C> &
  506. basic_ptree<K, D, C>::get_child(const path_type &path)
  507. {
  508. path_type p(path);
  509. self_type *n = walk_path(p);
  510. if (!n) {
  511. BOOST_PROPERTY_TREE_THROW(ptree_bad_path("No such node", path));
  512. }
  513. return *n;
  514. }
  515. template<class K, class D, class C> inline
  516. const basic_ptree<K, D, C> &
  517. basic_ptree<K, D, C>::get_child(const path_type &path) const
  518. {
  519. return const_cast<self_type*>(this)->get_child(path);
  520. }
  521. template<class K, class D, class C> inline
  522. basic_ptree<K, D, C> &
  523. basic_ptree<K, D, C>::get_child(const path_type &path,
  524. self_type &default_value)
  525. {
  526. path_type p(path);
  527. self_type *n = walk_path(p);
  528. return n ? *n : default_value;
  529. }
  530. template<class K, class D, class C> inline
  531. const basic_ptree<K, D, C> &
  532. basic_ptree<K, D, C>::get_child(const path_type &path,
  533. const self_type &default_value) const
  534. {
  535. return const_cast<self_type*>(this)->get_child(path,
  536. const_cast<self_type&>(default_value));
  537. }
  538. template<class K, class D, class C>
  539. optional<basic_ptree<K, D, C> &>
  540. basic_ptree<K, D, C>::get_child_optional(const path_type &path)
  541. {
  542. path_type p(path);
  543. self_type *n = walk_path(p);
  544. if (!n) {
  545. return optional<self_type&>();
  546. }
  547. return *n;
  548. }
  549. template<class K, class D, class C>
  550. optional<const basic_ptree<K, D, C> &>
  551. basic_ptree<K, D, C>::get_child_optional(const path_type &path) const
  552. {
  553. path_type p(path);
  554. self_type *n = walk_path(p);
  555. if (!n) {
  556. return optional<const self_type&>();
  557. }
  558. return *n;
  559. }
  560. template<class K, class D, class C>
  561. basic_ptree<K, D, C> &
  562. basic_ptree<K, D, C>::put_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. Now get the correct child.
  568. key_type fragment = p.reduce();
  569. assoc_iterator el = parent.find(fragment);
  570. // If the new child exists, replace it.
  571. if(el != parent.not_found()) {
  572. return el->second = value;
  573. } else {
  574. return parent.push_back(value_type(fragment, value))->second;
  575. }
  576. }
  577. template<class K, class D, class C>
  578. basic_ptree<K, D, C> &
  579. basic_ptree<K, D, C>::add_child(const path_type &path,
  580. const self_type &value)
  581. {
  582. path_type p(path);
  583. self_type &parent = force_path(p);
  584. // Got the parent.
  585. key_type fragment = p.reduce();
  586. return parent.push_back(value_type(fragment, value))->second;
  587. }
  588. template<class K, class D, class C>
  589. template<class Type, class Translator>
  590. typename boost::enable_if<detail::is_translator<Translator>, Type>::type
  591. basic_ptree<K, D, C>::get_value(Translator tr) const
  592. {
  593. if(boost::optional<Type> o = get_value_optional<Type>(tr)) {
  594. return *o;
  595. }
  596. BOOST_PROPERTY_TREE_THROW(ptree_bad_data(
  597. std::string("conversion of data to type \"") +
  598. typeid(Type).name() + "\" failed", data()));
  599. }
  600. template<class K, class D, class C>
  601. template<class Type> inline
  602. Type basic_ptree<K, D, C>::get_value() const
  603. {
  604. return get_value<Type>(
  605. typename translator_between<data_type, Type>::type());
  606. }
  607. template<class K, class D, class C>
  608. template<class Type, class Translator> inline
  609. Type basic_ptree<K, D, C>::get_value(const Type &default_value,
  610. Translator tr) const
  611. {
  612. return get_value_optional<Type>(tr).get_value_or(default_value);
  613. }
  614. template<class K, class D, class C>
  615. template <class Ch, class Translator>
  616. typename boost::enable_if<
  617. detail::is_character<Ch>,
  618. std::basic_string<Ch>
  619. >::type
  620. basic_ptree<K, D, C>::get_value(const Ch *default_value, Translator tr)const
  621. {
  622. return get_value<std::basic_string<Ch>, Translator>(default_value, tr);
  623. }
  624. template<class K, class D, class C>
  625. template<class Type> inline
  626. typename boost::disable_if<detail::is_translator<Type>, Type>::type
  627. basic_ptree<K, D, C>::get_value(const Type &default_value) const
  628. {
  629. return get_value(default_value,
  630. typename translator_between<data_type, Type>::type());
  631. }
  632. template<class K, class D, class C>
  633. template <class Ch>
  634. typename boost::enable_if<
  635. detail::is_character<Ch>,
  636. std::basic_string<Ch>
  637. >::type
  638. basic_ptree<K, D, C>::get_value(const Ch *default_value) const
  639. {
  640. return get_value< std::basic_string<Ch> >(default_value);
  641. }
  642. template<class K, class D, class C>
  643. template<class Type, class Translator> inline
  644. optional<Type> basic_ptree<K, D, C>::get_value_optional(
  645. Translator tr) const
  646. {
  647. return tr.get_value(data());
  648. }
  649. template<class K, class D, class C>
  650. template<class Type> inline
  651. optional<Type> basic_ptree<K, D, C>::get_value_optional() const
  652. {
  653. return get_value_optional<Type>(
  654. typename translator_between<data_type, Type>::type());
  655. }
  656. template<class K, class D, class C>
  657. template<class Type, class Translator> inline
  658. typename boost::enable_if<detail::is_translator<Translator>, Type>::type
  659. basic_ptree<K, D, C>::get(const path_type &path,
  660. Translator tr) const
  661. {
  662. return get_child(path).BOOST_NESTED_TEMPLATE get_value<Type>(tr);
  663. }
  664. template<class K, class D, class C>
  665. template<class Type> inline
  666. Type basic_ptree<K, D, C>::get(const path_type &path) const
  667. {
  668. return get_child(path).BOOST_NESTED_TEMPLATE get_value<Type>();
  669. }
  670. template<class K, class D, class C>
  671. template<class Type, class Translator> inline
  672. Type basic_ptree<K, D, C>::get(const path_type &path,
  673. const Type &default_value,
  674. Translator tr) const
  675. {
  676. return get_optional<Type>(path, tr).get_value_or(default_value);
  677. }
  678. template<class K, class D, class C>
  679. template <class Ch, class Translator>
  680. typename boost::enable_if<
  681. detail::is_character<Ch>,
  682. std::basic_string<Ch>
  683. >::type
  684. basic_ptree<K, D, C>::get(
  685. const path_type &path, const Ch *default_value, Translator tr) const
  686. {
  687. return get<std::basic_string<Ch>, Translator>(path, default_value, tr);
  688. }
  689. template<class K, class D, class C>
  690. template<class Type> inline
  691. typename boost::disable_if<detail::is_translator<Type>, Type>::type
  692. basic_ptree<K, D, C>::get(const path_type &path,
  693. const Type &default_value) const
  694. {
  695. return get_optional<Type>(path).get_value_or(default_value);
  696. }
  697. template<class K, class D, class C>
  698. template <class Ch>
  699. typename boost::enable_if<
  700. detail::is_character<Ch>,
  701. std::basic_string<Ch>
  702. >::type
  703. basic_ptree<K, D, C>::get(
  704. const path_type &path, const Ch *default_value) const
  705. {
  706. return get< std::basic_string<Ch> >(path, default_value);
  707. }
  708. template<class K, class D, class C>
  709. template<class Type, class Translator>
  710. optional<Type> basic_ptree<K, D, C>::get_optional(const path_type &path,
  711. Translator tr) const
  712. {
  713. if (optional<const self_type&> child = get_child_optional(path))
  714. return child.get().
  715. BOOST_NESTED_TEMPLATE get_value_optional<Type>(tr);
  716. else
  717. return optional<Type>();
  718. }
  719. template<class K, class D, class C>
  720. template<class Type>
  721. optional<Type> basic_ptree<K, D, C>::get_optional(
  722. const path_type &path) const
  723. {
  724. if (optional<const self_type&> child = get_child_optional(path))
  725. return child.get().BOOST_NESTED_TEMPLATE get_value_optional<Type>();
  726. else
  727. return optional<Type>();
  728. }
  729. template<class K, class D, class C>
  730. template<class Type, class Translator>
  731. void basic_ptree<K, D, C>::put_value(const Type &value, Translator tr)
  732. {
  733. if(optional<data_type> o = tr.put_value(value)) {
  734. data() = *o;
  735. } else {
  736. BOOST_PROPERTY_TREE_THROW(ptree_bad_data(
  737. std::string("conversion of type \"") + typeid(Type).name() +
  738. "\" to data failed", boost::any()));
  739. }
  740. }
  741. template<class K, class D, class C>
  742. template<class Type> inline
  743. void basic_ptree<K, D, C>::put_value(const Type &value)
  744. {
  745. put_value(value, typename translator_between<data_type, Type>::type());
  746. }
  747. template<class K, class D, class C>
  748. template<class Type, typename Translator>
  749. basic_ptree<K, D, C> & basic_ptree<K, D, C>::put(
  750. const path_type &path, const Type &value, Translator tr)
  751. {
  752. if(optional<self_type &> child = get_child_optional(path)) {
  753. child.get().put_value(value, tr);
  754. return *child;
  755. } else {
  756. self_type &child2 = put_child(path, self_type());
  757. child2.put_value(value, tr);
  758. return child2;
  759. }
  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>::put(
  764. const path_type &path, const Type &value)
  765. {
  766. return put(path, value,
  767. typename translator_between<data_type, Type>::type());
  768. }
  769. template<class K, class D, class C>
  770. template<class Type, typename Translator> inline
  771. basic_ptree<K, D, C> & basic_ptree<K, D, C>::add(
  772. const path_type &path, const Type &value, Translator tr)
  773. {
  774. self_type &child = add_child(path, self_type());
  775. child.put_value(value, tr);
  776. return child;
  777. }
  778. template<class K, class D, class C>
  779. template<class Type> inline
  780. basic_ptree<K, D, C> & basic_ptree<K, D, C>::add(
  781. const path_type &path, const Type &value)
  782. {
  783. return add(path, value,
  784. typename translator_between<data_type, Type>::type());
  785. }
  786. template<class K, class D, class C>
  787. basic_ptree<K, D, C> *
  788. basic_ptree<K, D, C>::walk_path(path_type &p) const
  789. {
  790. if(p.empty()) {
  791. // I'm the child we're looking for.
  792. return const_cast<basic_ptree*>(this);
  793. }
  794. // Recurse down the tree to find the path.
  795. key_type fragment = p.reduce();
  796. const_assoc_iterator el = find(fragment);
  797. if(el == not_found()) {
  798. // No such child.
  799. return 0;
  800. }
  801. // Not done yet, recurse.
  802. return el->second.walk_path(p);
  803. }
  804. template<class K, class D, class C>
  805. basic_ptree<K, D, C> & basic_ptree<K, D, C>::force_path(path_type &p)
  806. {
  807. BOOST_ASSERT(!p.empty() && "Empty path not allowed for put_child.");
  808. if(p.single()) {
  809. // I'm the parent we're looking for.
  810. return *this;
  811. }
  812. key_type fragment = p.reduce();
  813. assoc_iterator el = find(fragment);
  814. // If we've found an existing child, go down that path. Else
  815. // create a new one.
  816. self_type& child = el == not_found() ?
  817. push_back(value_type(fragment, self_type()))->second : el->second;
  818. return child.force_path(p);
  819. }
  820. // Free functions
  821. template<class K, class D, class C>
  822. inline void swap(basic_ptree<K, D, C> &pt1, basic_ptree<K, D, C> &pt2)
  823. {
  824. pt1.swap(pt2);
  825. }
  826. } }
  827. #if defined(BOOST_PROPERTY_TREE_PAIR_BUG)
  828. #undef BOOST_PROPERTY_TREE_PAIR_BUG
  829. #endif
  830. #endif