You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1154 lines
41 KiB

10 months ago
  1. // Copyright Toru Niina 2017.
  2. // Distributed under the MIT License.
  3. #ifndef TOML11_GET_HPP
  4. #define TOML11_GET_HPP
  5. #include <algorithm>
  6. #include "from.hpp"
  7. #include "result.hpp"
  8. #include "value.hpp"
  9. namespace toml
  10. {
  11. // ============================================================================
  12. // exact toml::* type
  13. template<typename T, typename C,
  14. template<typename ...> class M, template<typename ...> class V>
  15. detail::enable_if_t<detail::is_exact_toml_type<T, basic_value<C, M, V>>::value, T> &
  16. get(basic_value<C, M, V>& v)
  17. {
  18. return v.template cast<detail::type_to_enum<T, basic_value<C, M, V>>::value>();
  19. }
  20. template<typename T, typename C,
  21. template<typename ...> class M, template<typename ...> class V>
  22. detail::enable_if_t<detail::is_exact_toml_type<T, basic_value<C, M, V>>::value, T> const&
  23. get(const basic_value<C, M, V>& v)
  24. {
  25. return v.template cast<detail::type_to_enum<T, basic_value<C, M, V>>::value>();
  26. }
  27. template<typename T, typename C,
  28. template<typename ...> class M, template<typename ...> class V>
  29. detail::enable_if_t<detail::is_exact_toml_type<T, basic_value<C, M, V>>::value, T>
  30. get(basic_value<C, M, V>&& v)
  31. {
  32. return T(std::move(v).template cast<detail::type_to_enum<T, basic_value<C, M, V>>::value>());
  33. }
  34. // ============================================================================
  35. // T == toml::value; identity transformation.
  36. template<typename T, typename C,
  37. template<typename ...> class M, template<typename ...> class V>
  38. inline detail::enable_if_t<std::is_same<T, basic_value<C, M, V>>::value, T>&
  39. get(basic_value<C, M, V>& v)
  40. {
  41. return v;
  42. }
  43. template<typename T, typename C,
  44. template<typename ...> class M, template<typename ...> class V>
  45. inline detail::enable_if_t<std::is_same<T, basic_value<C, M, V>>::value, T> const&
  46. get(const basic_value<C, M, V>& v)
  47. {
  48. return v;
  49. }
  50. template<typename T, typename C,
  51. template<typename ...> class M, template<typename ...> class V>
  52. inline detail::enable_if_t<std::is_same<T, basic_value<C, M, V>>::value, T>
  53. get(basic_value<C, M, V>&& v)
  54. {
  55. return basic_value<C, M, V>(std::move(v));
  56. }
  57. // ============================================================================
  58. // T == toml::basic_value<C2, M2, V2>; basic_value -> basic_value
  59. template<typename T, typename C,
  60. template<typename ...> class M, template<typename ...> class V>
  61. inline detail::enable_if_t<detail::conjunction<detail::is_basic_value<T>,
  62. detail::negation<std::is_same<T, basic_value<C, M, V>>>
  63. >::value, T>
  64. get(const basic_value<C, M, V>& v)
  65. {
  66. return T(v);
  67. }
  68. // ============================================================================
  69. // integer convertible from toml::Integer
  70. template<typename T, typename C,
  71. template<typename ...> class M, template<typename ...> class V>
  72. inline detail::enable_if_t<detail::conjunction<
  73. std::is_integral<T>, // T is integral
  74. detail::negation<std::is_same<T, bool>>, // but not bool
  75. detail::negation< // but not toml::integer
  76. detail::is_exact_toml_type<T, basic_value<C, M, V>>>
  77. >::value, T>
  78. get(const basic_value<C, M, V>& v)
  79. {
  80. return static_cast<T>(v.as_integer());
  81. }
  82. // ============================================================================
  83. // floating point convertible from toml::Float
  84. template<typename T, typename C,
  85. template<typename ...> class M, template<typename ...> class V>
  86. inline detail::enable_if_t<detail::conjunction<
  87. std::is_floating_point<T>, // T is floating_point
  88. detail::negation< // but not toml::floating
  89. detail::is_exact_toml_type<T, basic_value<C, M, V>>>
  90. >::value, T>
  91. get(const basic_value<C, M, V>& v)
  92. {
  93. return static_cast<T>(v.as_floating());
  94. }
  95. // ============================================================================
  96. // std::string; toml uses its own toml::string, but it should be convertible to
  97. // std::string seamlessly
  98. template<typename T, typename C,
  99. template<typename ...> class M, template<typename ...> class V>
  100. inline detail::enable_if_t<std::is_same<T, std::string>::value, std::string>&
  101. get(basic_value<C, M, V>& v)
  102. {
  103. return v.as_string().str;
  104. }
  105. template<typename T, typename C,
  106. template<typename ...> class M, template<typename ...> class V>
  107. inline detail::enable_if_t<std::is_same<T, std::string>::value, std::string> const&
  108. get(const basic_value<C, M, V>& v)
  109. {
  110. return v.as_string().str;
  111. }
  112. template<typename T, typename C,
  113. template<typename ...> class M, template<typename ...> class V>
  114. inline detail::enable_if_t<std::is_same<T, std::string>::value, std::string>
  115. get(basic_value<C, M, V>&& v)
  116. {
  117. return std::string(std::move(v.as_string().str));
  118. }
  119. // ============================================================================
  120. // std::string_view
  121. #if defined(TOML11_USING_STRING_VIEW) && TOML11_USING_STRING_VIEW>0
  122. template<typename T, typename C,
  123. template<typename ...> class M, template<typename ...> class V>
  124. inline detail::enable_if_t<std::is_same<T, std::string_view>::value, std::string_view>
  125. get(const basic_value<C, M, V>& v)
  126. {
  127. return std::string_view(v.as_string().str);
  128. }
  129. #endif
  130. // ============================================================================
  131. // std::chrono::duration from toml::local_time.
  132. template<typename T, typename C,
  133. template<typename ...> class M, template<typename ...> class V>
  134. inline detail::enable_if_t<detail::is_chrono_duration<T>::value, T>
  135. get(const basic_value<C, M, V>& v)
  136. {
  137. return std::chrono::duration_cast<T>(
  138. std::chrono::nanoseconds(v.as_local_time()));
  139. }
  140. // ============================================================================
  141. // std::chrono::system_clock::time_point from toml::datetime variants
  142. template<typename T, typename C,
  143. template<typename ...> class M, template<typename ...> class V>
  144. inline detail::enable_if_t<
  145. std::is_same<std::chrono::system_clock::time_point, T>::value, T>
  146. get(const basic_value<C, M, V>& v)
  147. {
  148. switch(v.type())
  149. {
  150. case value_t::local_date:
  151. {
  152. return std::chrono::system_clock::time_point(v.as_local_date());
  153. }
  154. case value_t::local_datetime:
  155. {
  156. return std::chrono::system_clock::time_point(v.as_local_datetime());
  157. }
  158. case value_t::offset_datetime:
  159. {
  160. return std::chrono::system_clock::time_point(v.as_offset_datetime());
  161. }
  162. default:
  163. {
  164. throw type_error(detail::format_underline("toml::value: "
  165. "bad_cast to std::chrono::system_clock::time_point", {
  166. {v.location(), concat_to_string("the actual type is ", v.type())}
  167. }), v.location());
  168. }
  169. }
  170. }
  171. // ============================================================================
  172. // forward declaration to use this recursively. ignore this and go ahead.
  173. // array-like type with push_back(value) method
  174. template<typename T, typename C,
  175. template<typename ...> class M, template<typename ...> class V>
  176. detail::enable_if_t<detail::conjunction<
  177. detail::is_container<T>, // T is a container
  178. detail::has_push_back_method<T>, // T::push_back(value) works
  179. detail::negation< // but not toml::array
  180. detail::is_exact_toml_type<T, basic_value<C, M, V>>>
  181. >::value, T>
  182. get(const basic_value<C, M, V>&);
  183. // array-like type without push_back(value) method
  184. template<typename T, typename C,
  185. template<typename ...> class M, template<typename ...> class V>
  186. detail::enable_if_t<detail::conjunction<
  187. detail::is_container<T>, // T is a container
  188. detail::negation<detail::has_push_back_method<T>>, // w/o push_back(...)
  189. detail::negation<detail::has_specialized_from<T>>, // T does not have special conversion
  190. detail::negation< // not toml::array
  191. detail::is_exact_toml_type<T, basic_value<C, M, V>>>
  192. >::value, T>
  193. get(const basic_value<C, M, V>&);
  194. // std::pair<T1, T2>
  195. template<typename T, typename C,
  196. template<typename ...> class M, template<typename ...> class V>
  197. detail::enable_if_t<detail::is_std_pair<T>::value, T>
  198. get(const basic_value<C, M, V>&);
  199. // std::tuple<T1, T2, ...>
  200. template<typename T, typename C,
  201. template<typename ...> class M, template<typename ...> class V>
  202. detail::enable_if_t<detail::is_std_tuple<T>::value, T>
  203. get(const basic_value<C, M, V>&);
  204. // map-like classes
  205. template<typename T, typename C,
  206. template<typename ...> class M, template<typename ...> class V>
  207. detail::enable_if_t<detail::conjunction<
  208. detail::is_map<T>, // T is map
  209. detail::negation< // but not toml::table
  210. detail::is_exact_toml_type<T, basic_value<C, M, V>>>
  211. >::value, T>
  212. get(const basic_value<C, M, V>&);
  213. // T.from_toml(v)
  214. template<typename T, typename C,
  215. template<typename ...> class M, template<typename ...> class V>
  216. detail::enable_if_t<detail::conjunction<
  217. detail::negation< // not a toml::* type
  218. detail::is_exact_toml_type<T, basic_value<C, M, V>>>,
  219. detail::has_from_toml_method<T, C, M, V>, // but has from_toml(toml::value)
  220. std::is_default_constructible<T> // and default constructible
  221. >::value, T>
  222. get(const basic_value<C, M, V>&);
  223. // toml::from<T>::from_toml(v)
  224. template<typename T, typename C,
  225. template<typename ...> class M, template<typename ...> class V>
  226. detail::enable_if_t<detail::has_specialized_from<T>::value, T>
  227. get(const basic_value<C, M, V>&);
  228. template<typename T, typename C,
  229. template<typename ...> class M, template<typename ...> class V>
  230. detail::enable_if_t<detail::has_specialized_from<T>::value, T>
  231. get(basic_value<C, M, V>&);
  232. // T(const toml::value&) and T is not toml::basic_value,
  233. // and it does not have `from<T>` nor `from_toml`.
  234. template<typename T, typename C,
  235. template<typename ...> class M, template<typename ...> class V>
  236. detail::enable_if_t<detail::conjunction<
  237. detail::negation<detail::is_basic_value<T>>,
  238. std::is_constructible<T, const basic_value<C, M, V>&>,
  239. detail::negation<detail::has_from_toml_method<T, C, M, V>>,
  240. detail::negation<detail::has_specialized_from<T>>
  241. >::value, T>
  242. get(const basic_value<C, M, V>&);
  243. template<typename T, typename C,
  244. template<typename ...> class M, template<typename ...> class V>
  245. detail::enable_if_t<detail::conjunction<
  246. detail::negation<detail::is_basic_value<T>>,
  247. std::is_constructible<T, basic_value<C, M, V>&>,
  248. detail::negation<detail::has_from_toml_method<T, C, M, V>>,
  249. detail::negation<detail::has_specialized_from<T>>
  250. >::value, T>
  251. get(basic_value<C, M, V>&);
  252. // ============================================================================
  253. // array-like types; most likely STL container, like std::vector, etc.
  254. template<typename T, typename C,
  255. template<typename ...> class M, template<typename ...> class V>
  256. detail::enable_if_t<detail::conjunction<
  257. detail::is_container<T>, // T is a container
  258. detail::has_push_back_method<T>, // container.push_back(elem) works
  259. detail::negation< // but not toml::array
  260. detail::is_exact_toml_type<T, basic_value<C, M, V>>>
  261. >::value, T>
  262. get(const basic_value<C, M, V>& v)
  263. {
  264. using value_type = typename T::value_type;
  265. const auto& ary = v.as_array();
  266. T container;
  267. try_reserve(container, ary.size());
  268. for(const auto& elem : ary)
  269. {
  270. container.push_back(get<value_type>(elem));
  271. }
  272. return container;
  273. }
  274. // ============================================================================
  275. // std::forward_list does not have push_back, insert, or emplace.
  276. // It has insert_after, emplace_after, push_front.
  277. template<typename T, typename C,
  278. template<typename ...> class M, template<typename ...> class V>
  279. detail::enable_if_t<detail::is_std_forward_list<T>::value, T>
  280. get(const basic_value<C, M, V>& v)
  281. {
  282. using value_type = typename T::value_type;
  283. T container;
  284. for(const auto& elem : v.as_array())
  285. {
  286. container.push_front(get<value_type>(elem));
  287. }
  288. container.reverse();
  289. return container;
  290. }
  291. // ============================================================================
  292. // array-like types, without push_back(). most likely [std|boost]::array.
  293. template<typename T, typename C,
  294. template<typename ...> class M, template<typename ...> class V>
  295. detail::enable_if_t<detail::conjunction<
  296. detail::is_container<T>, // T is a container
  297. detail::negation<detail::has_push_back_method<T>>, // w/o push_back
  298. detail::negation<detail::has_specialized_from<T>>, // T does not have special conversion
  299. detail::negation< // T is not toml::array
  300. detail::is_exact_toml_type<T, basic_value<C, M, V>>>
  301. >::value, T>
  302. get(const basic_value<C, M, V>& v)
  303. {
  304. using value_type = typename T::value_type;
  305. const auto& ar = v.as_array();
  306. T container;
  307. if(ar.size() != container.size())
  308. {
  309. throw std::out_of_range(detail::format_underline(concat_to_string(
  310. "toml::get: specified container size is ", container.size(),
  311. " but there are ", ar.size(), " elements in toml array."), {
  312. {v.location(), "here"}
  313. }));
  314. }
  315. for(std::size_t i=0; i<ar.size(); ++i)
  316. {
  317. container[i] = ::toml::get<value_type>(ar[i]);
  318. }
  319. return container;
  320. }
  321. // ============================================================================
  322. // std::pair.
  323. template<typename T, typename C,
  324. template<typename ...> class M, template<typename ...> class V>
  325. detail::enable_if_t<detail::is_std_pair<T>::value, T>
  326. get(const basic_value<C, M, V>& v)
  327. {
  328. using first_type = typename T::first_type;
  329. using second_type = typename T::second_type;
  330. const auto& ar = v.as_array();
  331. if(ar.size() != 2)
  332. {
  333. throw std::out_of_range(detail::format_underline(concat_to_string(
  334. "toml::get: specified std::pair but there are ", ar.size(),
  335. " elements in toml array."), {{v.location(), "here"}}));
  336. }
  337. return std::make_pair(::toml::get<first_type >(ar.at(0)),
  338. ::toml::get<second_type>(ar.at(1)));
  339. }
  340. // ============================================================================
  341. // std::tuple.
  342. namespace detail
  343. {
  344. template<typename T, typename Array, std::size_t ... I>
  345. T get_tuple_impl(const Array& a, index_sequence<I...>)
  346. {
  347. return std::make_tuple(
  348. ::toml::get<typename std::tuple_element<I, T>::type>(a.at(I))...);
  349. }
  350. } // detail
  351. template<typename T, typename C,
  352. template<typename ...> class M, template<typename ...> class V>
  353. detail::enable_if_t<detail::is_std_tuple<T>::value, T>
  354. get(const basic_value<C, M, V>& v)
  355. {
  356. const auto& ar = v.as_array();
  357. if(ar.size() != std::tuple_size<T>::value)
  358. {
  359. throw std::out_of_range(detail::format_underline(concat_to_string(
  360. "toml::get: specified std::tuple with ",
  361. std::tuple_size<T>::value, " elements, but there are ", ar.size(),
  362. " elements in toml array."), {{v.location(), "here"}}));
  363. }
  364. return detail::get_tuple_impl<T>(ar,
  365. detail::make_index_sequence<std::tuple_size<T>::value>{});
  366. }
  367. // ============================================================================
  368. // map-like types; most likely STL map, like std::map or std::unordered_map.
  369. template<typename T, typename C,
  370. template<typename ...> class M, template<typename ...> class V>
  371. detail::enable_if_t<detail::conjunction<
  372. detail::is_map<T>, // T is map
  373. detail::negation< // but not toml::array
  374. detail::is_exact_toml_type<T, basic_value<C, M, V>>>
  375. >::value, T>
  376. get(const basic_value<C, M, V>& v)
  377. {
  378. using key_type = typename T::key_type;
  379. using mapped_type = typename T::mapped_type;
  380. static_assert(std::is_convertible<std::string, key_type>::value,
  381. "toml::get only supports map type of which key_type is "
  382. "convertible from std::string.");
  383. T map;
  384. for(const auto& kv : v.as_table())
  385. {
  386. map.emplace(key_type(kv.first), get<mapped_type>(kv.second));
  387. }
  388. return map;
  389. }
  390. // ============================================================================
  391. // user-defined, but compatible types.
  392. template<typename T, typename C,
  393. template<typename ...> class M, template<typename ...> class V>
  394. detail::enable_if_t<detail::conjunction<
  395. detail::negation< // not a toml::* type
  396. detail::is_exact_toml_type<T, basic_value<C, M, V>>>,
  397. detail::has_from_toml_method<T, C, M, V>, // but has from_toml(toml::value) memfn
  398. std::is_default_constructible<T> // and default constructible
  399. >::value, T>
  400. get(const basic_value<C, M, V>& v)
  401. {
  402. T ud;
  403. ud.from_toml(v);
  404. return ud;
  405. }
  406. template<typename T, typename C,
  407. template<typename ...> class M, template<typename ...> class V>
  408. detail::enable_if_t<detail::has_specialized_from<T>::value, T>
  409. get(const basic_value<C, M, V>& v)
  410. {
  411. return ::toml::from<T>::from_toml(v);
  412. }
  413. template<typename T, typename C,
  414. template<typename ...> class M, template<typename ...> class V>
  415. detail::enable_if_t<detail::has_specialized_from<T>::value, T>
  416. get(basic_value<C, M, V>& v)
  417. {
  418. return ::toml::from<T>::from_toml(v);
  419. }
  420. template<typename T, typename C,
  421. template<typename ...> class M, template<typename ...> class V>
  422. detail::enable_if_t<detail::conjunction<
  423. detail::negation<detail::is_basic_value<T>>, // T is not a toml::value
  424. std::is_constructible<T, const basic_value<C, M, V>&>, // T is constructible from toml::value
  425. detail::negation<detail::has_from_toml_method<T, C, M, V>>, // and T does not have T.from_toml(v);
  426. detail::negation<detail::has_specialized_from<T>> // and T does not have toml::from<T>{};
  427. >::value, T>
  428. get(const basic_value<C, M, V>& v)
  429. {
  430. return T(v);
  431. }
  432. template<typename T, typename C,
  433. template<typename ...> class M, template<typename ...> class V>
  434. detail::enable_if_t<detail::conjunction<
  435. detail::negation<detail::is_basic_value<T>>, // T is not a toml::value
  436. std::is_constructible<T, basic_value<C, M, V>&>, // T is constructible from toml::value
  437. detail::negation<detail::has_from_toml_method<T, C, M, V>>, // and T does not have T.from_toml(v);
  438. detail::negation<detail::has_specialized_from<T>> // and T does not have toml::from<T>{};
  439. >::value, T>
  440. get(basic_value<C, M, V>& v)
  441. {
  442. return T(v);
  443. }
  444. // ============================================================================
  445. // find
  446. // ----------------------------------------------------------------------------
  447. // these overloads do not require to set T. and returns value itself.
  448. template<typename C,
  449. template<typename ...> class M, template<typename ...> class V>
  450. basic_value<C, M, V> const& find(const basic_value<C, M, V>& v, const key& ky)
  451. {
  452. const auto& tab = v.as_table();
  453. if(tab.count(ky) == 0)
  454. {
  455. detail::throw_key_not_found_error(v, ky);
  456. }
  457. return tab.at(ky);
  458. }
  459. template<typename C,
  460. template<typename ...> class M, template<typename ...> class V>
  461. basic_value<C, M, V>& find(basic_value<C, M, V>& v, const key& ky)
  462. {
  463. auto& tab = v.as_table();
  464. if(tab.count(ky) == 0)
  465. {
  466. detail::throw_key_not_found_error(v, ky);
  467. }
  468. return tab.at(ky);
  469. }
  470. template<typename C,
  471. template<typename ...> class M, template<typename ...> class V>
  472. basic_value<C, M, V> find(basic_value<C, M, V>&& v, const key& ky)
  473. {
  474. typename basic_value<C, M, V>::table_type tab = std::move(v).as_table();
  475. if(tab.count(ky) == 0)
  476. {
  477. detail::throw_key_not_found_error(v, ky);
  478. }
  479. return basic_value<C, M, V>(std::move(tab.at(ky)));
  480. }
  481. // ----------------------------------------------------------------------------
  482. // find(value, idx)
  483. template<typename C,
  484. template<typename ...> class M, template<typename ...> class V>
  485. basic_value<C, M, V> const&
  486. find(const basic_value<C, M, V>& v, const std::size_t idx)
  487. {
  488. const auto& ary = v.as_array();
  489. if(ary.size() <= idx)
  490. {
  491. throw std::out_of_range(detail::format_underline(concat_to_string(
  492. "index ", idx, " is out of range"), {{v.location(), "in this array"}}));
  493. }
  494. return ary.at(idx);
  495. }
  496. template<typename C,
  497. template<typename ...> class M, template<typename ...> class V>
  498. basic_value<C, M, V>& find(basic_value<C, M, V>& v, const std::size_t idx)
  499. {
  500. auto& ary = v.as_array();
  501. if(ary.size() <= idx)
  502. {
  503. throw std::out_of_range(detail::format_underline(concat_to_string(
  504. "index ", idx, " is out of range"), {{v.location(), "in this array"}}));
  505. }
  506. return ary.at(idx);
  507. }
  508. template<typename C,
  509. template<typename ...> class M, template<typename ...> class V>
  510. basic_value<C, M, V> find(basic_value<C, M, V>&& v, const std::size_t idx)
  511. {
  512. auto& ary = v.as_array();
  513. if(ary.size() <= idx)
  514. {
  515. throw std::out_of_range(detail::format_underline(concat_to_string(
  516. "index ", idx, " is out of range"), {{v.location(), "in this array"}}));
  517. }
  518. return basic_value<C, M, V>(std::move(ary.at(idx)));
  519. }
  520. // ----------------------------------------------------------------------------
  521. // find<T>(value, key);
  522. template<typename T, typename C,
  523. template<typename ...> class M, template<typename ...> class V>
  524. decltype(::toml::get<T>(std::declval<basic_value<C, M, V> const&>()))
  525. find(const basic_value<C, M, V>& v, const key& ky)
  526. {
  527. const auto& tab = v.as_table();
  528. if(tab.count(ky) == 0)
  529. {
  530. detail::throw_key_not_found_error(v, ky);
  531. }
  532. return ::toml::get<T>(tab.at(ky));
  533. }
  534. template<typename T, typename C,
  535. template<typename ...> class M, template<typename ...> class V>
  536. decltype(::toml::get<T>(std::declval<basic_value<C, M, V>&>()))
  537. find(basic_value<C, M, V>& v, const key& ky)
  538. {
  539. auto& tab = v.as_table();
  540. if(tab.count(ky) == 0)
  541. {
  542. detail::throw_key_not_found_error(v, ky);
  543. }
  544. return ::toml::get<T>(tab.at(ky));
  545. }
  546. template<typename T, typename C,
  547. template<typename ...> class M, template<typename ...> class V>
  548. decltype(::toml::get<T>(std::declval<basic_value<C, M, V>&&>()))
  549. find(basic_value<C, M, V>&& v, const key& ky)
  550. {
  551. typename basic_value<C, M, V>::table_type tab = std::move(v).as_table();
  552. if(tab.count(ky) == 0)
  553. {
  554. detail::throw_key_not_found_error(v, ky);
  555. }
  556. return ::toml::get<T>(std::move(tab.at(ky)));
  557. }
  558. // ----------------------------------------------------------------------------
  559. // find<T>(value, idx)
  560. template<typename T, typename C,
  561. template<typename ...> class M, template<typename ...> class V>
  562. decltype(::toml::get<T>(std::declval<basic_value<C, M, V> const&>()))
  563. find(const basic_value<C, M, V>& v, const std::size_t idx)
  564. {
  565. const auto& ary = v.as_array();
  566. if(ary.size() <= idx)
  567. {
  568. throw std::out_of_range(detail::format_underline(concat_to_string(
  569. "index ", idx, " is out of range"), {{v.location(), "in this array"}}));
  570. }
  571. return ::toml::get<T>(ary.at(idx));
  572. }
  573. template<typename T, typename C,
  574. template<typename ...> class M, template<typename ...> class V>
  575. decltype(::toml::get<T>(std::declval<basic_value<C, M, V>&>()))
  576. find(basic_value<C, M, V>& v, const std::size_t idx)
  577. {
  578. auto& ary = v.as_array();
  579. if(ary.size() <= idx)
  580. {
  581. throw std::out_of_range(detail::format_underline(concat_to_string(
  582. "index ", idx, " is out of range"), {{v.location(), "in this array"}}));
  583. }
  584. return ::toml::get<T>(ary.at(idx));
  585. }
  586. template<typename T, typename C,
  587. template<typename ...> class M, template<typename ...> class V>
  588. decltype(::toml::get<T>(std::declval<basic_value<C, M, V>&&>()))
  589. find(basic_value<C, M, V>&& v, const std::size_t idx)
  590. {
  591. typename basic_value<C, M, V>::array_type ary = std::move(v).as_array();
  592. if(ary.size() <= idx)
  593. {
  594. throw std::out_of_range(detail::format_underline(concat_to_string(
  595. "index ", idx, " is out of range"), {{v.location(), "in this array"}}));
  596. }
  597. return ::toml::get<T>(std::move(ary.at(idx)));
  598. }
  599. // --------------------------------------------------------------------------
  600. // toml::find(toml::value, toml::key, Ts&& ... keys)
  601. namespace detail
  602. {
  603. // It suppresses warnings by -Wsign-conversion. Let's say we have the following
  604. // code.
  605. // ```cpp
  606. // const auto x = toml::find<std::string>(data, "array", 0);
  607. // ```
  608. // Here, the type of literal number `0` is `int`. `int` is a signed integer.
  609. // `toml::find` takes `std::size_t` as an index. So it causes implicit sign
  610. // conversion and `-Wsign-conversion` warns about it. Using `0u` instead of `0`
  611. // suppresses the warning, but it makes user code messy.
  612. // To suppress this warning, we need to be aware of type conversion caused
  613. // by `toml::find(v, key1, key2, ... keys)`. But the thing is that the types of
  614. // keys can be any combination of {string-like, size_t-like}. Of course we can't
  615. // write down all the combinations. Thus we need to use some function that
  616. // recognize the type of argument and cast it into `std::string` or
  617. // `std::size_t` depending on the context.
  618. // `key_cast` does the job. It has 2 overloads. One is invoked when the
  619. // argument type is an integer and cast the argument into `std::size_t`. The
  620. // other is invoked when the argument type is not an integer, possibly one of
  621. // std::string, const char[N] or const char*, and construct std::string from
  622. // the argument.
  623. // `toml::find(v, k1, k2, ... ks)` uses `key_cast` before passing `ks` to
  624. // `toml::find(v, k)` to suppress -Wsign-conversion.
  625. template<typename T>
  626. enable_if_t<conjunction<std::is_integral<remove_cvref_t<T>>,
  627. negation<std::is_same<remove_cvref_t<T>, bool>>>::value, std::size_t>
  628. key_cast(T&& v) noexcept
  629. {
  630. return std::size_t(v);
  631. }
  632. template<typename T>
  633. enable_if_t<negation<conjunction<std::is_integral<remove_cvref_t<T>>,
  634. negation<std::is_same<remove_cvref_t<T>, bool>>>>::value, std::string>
  635. key_cast(T&& v) noexcept
  636. {
  637. return std::string(std::forward<T>(v));
  638. }
  639. } // detail
  640. template<typename C,
  641. template<typename ...> class M, template<typename ...> class V,
  642. typename Key1, typename Key2, typename ... Keys>
  643. const basic_value<C, M, V>&
  644. find(const basic_value<C, M, V>& v, Key1&& k1, Key2&& k2, Keys&& ... keys)
  645. {
  646. return ::toml::find(::toml::find(v, detail::key_cast(k1)),
  647. detail::key_cast(k2), std::forward<Keys>(keys)...);
  648. }
  649. template<typename C,
  650. template<typename ...> class M, template<typename ...> class V,
  651. typename Key1, typename Key2, typename ... Keys>
  652. basic_value<C, M, V>&
  653. find(basic_value<C, M, V>& v, Key1&& k1, Key2&& k2, Keys&& ... keys)
  654. {
  655. return ::toml::find(::toml::find(v, detail::key_cast(k1)),
  656. detail::key_cast(k2), std::forward<Keys>(keys)...);
  657. }
  658. template<typename C,
  659. template<typename ...> class M, template<typename ...> class V,
  660. typename Key1, typename Key2, typename ... Keys>
  661. basic_value<C, M, V>
  662. find(basic_value<C, M, V>&& v, Key1&& k1, Key2&& k2, Keys&& ... keys)
  663. {
  664. return ::toml::find(::toml::find(std::move(v), std::forward<Key1>(k1)),
  665. detail::key_cast(k2), std::forward<Keys>(keys)...);
  666. }
  667. template<typename T, typename C,
  668. template<typename ...> class M, template<typename ...> class V,
  669. typename Key1, typename Key2, typename ... Keys>
  670. decltype(::toml::get<T>(std::declval<const basic_value<C, M, V>&>()))
  671. find(const basic_value<C, M, V>& v, Key1&& k1, Key2&& k2, Keys&& ... keys)
  672. {
  673. return ::toml::find<T>(::toml::find(v, detail::key_cast(k1)),
  674. detail::key_cast(k2), std::forward<Keys>(keys)...);
  675. }
  676. template<typename T, typename C,
  677. template<typename ...> class M, template<typename ...> class V,
  678. typename Key1, typename Key2, typename ... Keys>
  679. decltype(::toml::get<T>(std::declval<basic_value<C, M, V>&>()))
  680. find(basic_value<C, M, V>& v, Key1&& k1, Key2&& k2, Keys&& ... keys)
  681. {
  682. return ::toml::find<T>(::toml::find(v, detail::key_cast(k1)),
  683. detail::key_cast(k2), std::forward<Keys>(keys)...);
  684. }
  685. template<typename T, typename C,
  686. template<typename ...> class M, template<typename ...> class V,
  687. typename Key1, typename Key2, typename ... Keys>
  688. decltype(::toml::get<T>(std::declval<basic_value<C, M, V>&&>()))
  689. find(basic_value<C, M, V>&& v, Key1&& k1, Key2&& k2, Keys&& ... keys)
  690. {
  691. return ::toml::find<T>(::toml::find(std::move(v), detail::key_cast(k1)),
  692. detail::key_cast(k2), std::forward<Keys>(keys)...);
  693. }
  694. // ============================================================================
  695. // get_or(value, fallback)
  696. template<typename C,
  697. template<typename ...> class M, template<typename ...> class V>
  698. basic_value<C, M, V> const&
  699. get_or(const basic_value<C, M, V>& v, const basic_value<C, M, V>&)
  700. {
  701. return v;
  702. }
  703. template<typename C,
  704. template<typename ...> class M, template<typename ...> class V>
  705. basic_value<C, M, V>&
  706. get_or(basic_value<C, M, V>& v, basic_value<C, M, V>&)
  707. {
  708. return v;
  709. }
  710. template<typename C,
  711. template<typename ...> class M, template<typename ...> class V>
  712. basic_value<C, M, V>
  713. get_or(basic_value<C, M, V>&& v, basic_value<C, M, V>&&)
  714. {
  715. return v;
  716. }
  717. // ----------------------------------------------------------------------------
  718. // specialization for the exact toml types (return type becomes lvalue ref)
  719. template<typename T, typename C,
  720. template<typename ...> class M, template<typename ...> class V>
  721. detail::enable_if_t<
  722. detail::is_exact_toml_type<T, basic_value<C, M, V>>::value, T> const&
  723. get_or(const basic_value<C, M, V>& v, const T& opt)
  724. {
  725. try
  726. {
  727. return get<detail::remove_cvref_t<T>>(v);
  728. }
  729. catch(...)
  730. {
  731. return opt;
  732. }
  733. }
  734. template<typename T, typename C,
  735. template<typename ...> class M, template<typename ...> class V>
  736. detail::enable_if_t<
  737. detail::is_exact_toml_type<T, basic_value<C, M, V>>::value, T>&
  738. get_or(basic_value<C, M, V>& v, T& opt)
  739. {
  740. try
  741. {
  742. return get<detail::remove_cvref_t<T>>(v);
  743. }
  744. catch(...)
  745. {
  746. return opt;
  747. }
  748. }
  749. template<typename T, typename C,
  750. template<typename ...> class M, template<typename ...> class V>
  751. detail::enable_if_t<detail::is_exact_toml_type<detail::remove_cvref_t<T>,
  752. basic_value<C, M, V>>::value, detail::remove_cvref_t<T>>
  753. get_or(basic_value<C, M, V>&& v, T&& opt)
  754. {
  755. try
  756. {
  757. return get<detail::remove_cvref_t<T>>(std::move(v));
  758. }
  759. catch(...)
  760. {
  761. return detail::remove_cvref_t<T>(std::forward<T>(opt));
  762. }
  763. }
  764. // ----------------------------------------------------------------------------
  765. // specialization for std::string (return type becomes lvalue ref)
  766. template<typename T, typename C,
  767. template<typename ...> class M, template<typename ...> class V>
  768. detail::enable_if_t<std::is_same<detail::remove_cvref_t<T>, std::string>::value,
  769. std::string> const&
  770. get_or(const basic_value<C, M, V>& v, const T& opt)
  771. {
  772. try
  773. {
  774. return v.as_string().str;
  775. }
  776. catch(...)
  777. {
  778. return opt;
  779. }
  780. }
  781. template<typename T, typename C,
  782. template<typename ...> class M, template<typename ...> class V>
  783. detail::enable_if_t<std::is_same<T, std::string>::value, std::string>&
  784. get_or(basic_value<C, M, V>& v, T& opt)
  785. {
  786. try
  787. {
  788. return v.as_string().str;
  789. }
  790. catch(...)
  791. {
  792. return opt;
  793. }
  794. }
  795. template<typename T, typename C,
  796. template<typename ...> class M, template<typename ...> class V>
  797. detail::enable_if_t<
  798. std::is_same<detail::remove_cvref_t<T>, std::string>::value, std::string>
  799. get_or(basic_value<C, M, V>&& v, T&& opt)
  800. {
  801. try
  802. {
  803. return std::move(v.as_string().str);
  804. }
  805. catch(...)
  806. {
  807. return std::string(std::forward<T>(opt));
  808. }
  809. }
  810. // ----------------------------------------------------------------------------
  811. // specialization for string literal
  812. template<typename T, typename C,
  813. template<typename ...> class M, template<typename ...> class V>
  814. detail::enable_if_t<detail::is_string_literal<
  815. typename std::remove_reference<T>::type>::value, std::string>
  816. get_or(const basic_value<C, M, V>& v, T&& opt)
  817. {
  818. try
  819. {
  820. return std::move(v.as_string().str);
  821. }
  822. catch(...)
  823. {
  824. return std::string(std::forward<T>(opt));
  825. }
  826. }
  827. // ----------------------------------------------------------------------------
  828. // others (require type conversion and return type cannot be lvalue reference)
  829. template<typename T, typename C,
  830. template<typename ...> class M, template<typename ...> class V>
  831. detail::enable_if_t<detail::conjunction<
  832. detail::negation<detail::is_exact_toml_type<detail::remove_cvref_t<T>,
  833. basic_value<C, M, V>>>,
  834. detail::negation<std::is_same<std::string, detail::remove_cvref_t<T>>>,
  835. detail::negation<detail::is_string_literal<
  836. typename std::remove_reference<T>::type>>
  837. >::value, detail::remove_cvref_t<T>>
  838. get_or(const basic_value<C, M, V>& v, T&& opt)
  839. {
  840. try
  841. {
  842. return get<detail::remove_cvref_t<T>>(v);
  843. }
  844. catch(...)
  845. {
  846. return detail::remove_cvref_t<T>(std::forward<T>(opt));
  847. }
  848. }
  849. // ===========================================================================
  850. // find_or(value, key, fallback)
  851. template<typename C,
  852. template<typename ...> class M, template<typename ...> class V>
  853. basic_value<C, M, V> const&
  854. find_or(const basic_value<C, M, V>& v, const key& ky,
  855. const basic_value<C, M, V>& opt)
  856. {
  857. if(!v.is_table()) {return opt;}
  858. const auto& tab = v.as_table();
  859. if(tab.count(ky) == 0) {return opt;}
  860. return tab.at(ky);
  861. }
  862. template<typename C,
  863. template<typename ...> class M, template<typename ...> class V>
  864. basic_value<C, M, V>&
  865. find_or(basic_value<C, M, V>& v, const toml::key& ky, basic_value<C, M, V>& opt)
  866. {
  867. if(!v.is_table()) {return opt;}
  868. auto& tab = v.as_table();
  869. if(tab.count(ky) == 0) {return opt;}
  870. return tab.at(ky);
  871. }
  872. template<typename C,
  873. template<typename ...> class M, template<typename ...> class V>
  874. basic_value<C, M, V>
  875. find_or(basic_value<C, M, V>&& v, const toml::key& ky, basic_value<C, M, V>&& opt)
  876. {
  877. if(!v.is_table()) {return opt;}
  878. auto tab = std::move(v).as_table();
  879. if(tab.count(ky) == 0) {return opt;}
  880. return basic_value<C, M, V>(std::move(tab.at(ky)));
  881. }
  882. // ---------------------------------------------------------------------------
  883. // exact types (return type can be a reference)
  884. template<typename T, typename C,
  885. template<typename ...> class M, template<typename ...> class V>
  886. detail::enable_if_t<
  887. detail::is_exact_toml_type<T, basic_value<C, M, V>>::value, T> const&
  888. find_or(const basic_value<C, M, V>& v, const key& ky, const T& opt)
  889. {
  890. if(!v.is_table()) {return opt;}
  891. const auto& tab = v.as_table();
  892. if(tab.count(ky) == 0) {return opt;}
  893. return get_or(tab.at(ky), opt);
  894. }
  895. template<typename T, typename C,
  896. template<typename ...> class M, template<typename ...> class V>
  897. detail::enable_if_t<
  898. detail::is_exact_toml_type<T, basic_value<C, M, V>>::value, T>&
  899. find_or(basic_value<C, M, V>& v, const toml::key& ky, T& opt)
  900. {
  901. if(!v.is_table()) {return opt;}
  902. auto& tab = v.as_table();
  903. if(tab.count(ky) == 0) {return opt;}
  904. return get_or(tab.at(ky), opt);
  905. }
  906. template<typename T, typename C,
  907. template<typename ...> class M, template<typename ...> class V>
  908. detail::enable_if_t<
  909. detail::is_exact_toml_type<T, basic_value<C, M, V>>::value,
  910. detail::remove_cvref_t<T>>
  911. find_or(basic_value<C, M, V>&& v, const toml::key& ky, T&& opt)
  912. {
  913. if(!v.is_table()) {return std::forward<T>(opt);}
  914. auto tab = std::move(v).as_table();
  915. if(tab.count(ky) == 0) {return std::forward<T>(opt);}
  916. return get_or(std::move(tab.at(ky)), std::forward<T>(opt));
  917. }
  918. // ---------------------------------------------------------------------------
  919. // std::string (return type can be a reference)
  920. template<typename T, typename C,
  921. template<typename ...> class M, template<typename ...> class V>
  922. detail::enable_if_t<std::is_same<T, std::string>::value, std::string> const&
  923. find_or(const basic_value<C, M, V>& v, const key& ky, const T& opt)
  924. {
  925. if(!v.is_table()) {return opt;}
  926. const auto& tab = v.as_table();
  927. if(tab.count(ky) == 0) {return opt;}
  928. return get_or(tab.at(ky), opt);
  929. }
  930. template<typename T, typename C,
  931. template<typename ...> class M, template<typename ...> class V>
  932. detail::enable_if_t<std::is_same<T, std::string>::value, std::string>&
  933. find_or(basic_value<C, M, V>& v, const toml::key& ky, T& opt)
  934. {
  935. if(!v.is_table()) {return opt;}
  936. auto& tab = v.as_table();
  937. if(tab.count(ky) == 0) {return opt;}
  938. return get_or(tab.at(ky), opt);
  939. }
  940. template<typename T, typename C,
  941. template<typename ...> class M, template<typename ...> class V>
  942. detail::enable_if_t<std::is_same<T, std::string>::value, std::string>
  943. find_or(basic_value<C, M, V>&& v, const toml::key& ky, T&& opt)
  944. {
  945. if(!v.is_table()) {return std::forward<T>(opt);}
  946. auto tab = std::move(v).as_table();
  947. if(tab.count(ky) == 0) {return std::forward<T>(opt);}
  948. return get_or(std::move(tab.at(ky)), std::forward<T>(opt));
  949. }
  950. // ---------------------------------------------------------------------------
  951. // string literal (deduced as std::string)
  952. template<typename T, typename C,
  953. template<typename ...> class M, template<typename ...> class V>
  954. detail::enable_if_t<
  955. detail::is_string_literal<typename std::remove_reference<T>::type>::value,
  956. std::string>
  957. find_or(const basic_value<C, M, V>& v, const toml::key& ky, T&& opt)
  958. {
  959. if(!v.is_table()) {return std::string(opt);}
  960. const auto& tab = v.as_table();
  961. if(tab.count(ky) == 0) {return std::string(opt);}
  962. return get_or(tab.at(ky), std::forward<T>(opt));
  963. }
  964. // ---------------------------------------------------------------------------
  965. // others (require type conversion and return type cannot be lvalue reference)
  966. template<typename T, typename C,
  967. template<typename ...> class M, template<typename ...> class V>
  968. detail::enable_if_t<detail::conjunction<
  969. // T is not an exact toml type
  970. detail::negation<detail::is_exact_toml_type<
  971. detail::remove_cvref_t<T>, basic_value<C, M, V>>>,
  972. // T is not std::string
  973. detail::negation<std::is_same<std::string, detail::remove_cvref_t<T>>>,
  974. // T is not a string literal
  975. detail::negation<detail::is_string_literal<
  976. typename std::remove_reference<T>::type>>
  977. >::value, detail::remove_cvref_t<T>>
  978. find_or(const basic_value<C, M, V>& v, const toml::key& ky, T&& opt)
  979. {
  980. if(!v.is_table()) {return std::forward<T>(opt);}
  981. const auto& tab = v.as_table();
  982. if(tab.count(ky) == 0) {return std::forward<T>(opt);}
  983. return get_or(tab.at(ky), std::forward<T>(opt));
  984. }
  985. // ---------------------------------------------------------------------------
  986. // recursive find-or with type deduction (find_or(value, keys, opt))
  987. template<typename Value, typename ... Ks,
  988. typename detail::enable_if_t<(sizeof...(Ks) > 1), std::nullptr_t> = nullptr>
  989. // here we need to add SFINAE in the template parameter to avoid
  990. // infinite recursion in type deduction on gcc
  991. auto find_or(Value&& v, const toml::key& ky, Ks&& ... keys)
  992. -> decltype(find_or(std::forward<Value>(v), ky, detail::last_one(std::forward<Ks>(keys)...)))
  993. {
  994. if(!v.is_table())
  995. {
  996. return detail::last_one(std::forward<Ks>(keys)...);
  997. }
  998. auto&& tab = std::forward<Value>(v).as_table();
  999. if(tab.count(ky) == 0)
  1000. {
  1001. return detail::last_one(std::forward<Ks>(keys)...);
  1002. }
  1003. return find_or(std::forward<decltype(tab)>(tab).at(ky), std::forward<Ks>(keys)...);
  1004. }
  1005. // ---------------------------------------------------------------------------
  1006. // recursive find_or with explicit type specialization, find_or<int>(value, keys...)
  1007. template<typename T, typename Value, typename ... Ks,
  1008. typename detail::enable_if_t<(sizeof...(Ks) > 1), std::nullptr_t> = nullptr>
  1009. // here we need to add SFINAE in the template parameter to avoid
  1010. // infinite recursion in type deduction on gcc
  1011. auto find_or(Value&& v, const toml::key& ky, Ks&& ... keys)
  1012. -> decltype(find_or<T>(std::forward<Value>(v), ky, detail::last_one(std::forward<Ks>(keys)...)))
  1013. {
  1014. if(!v.is_table())
  1015. {
  1016. return detail::last_one(std::forward<Ks>(keys)...);
  1017. }
  1018. auto&& tab = std::forward<Value>(v).as_table();
  1019. if(tab.count(ky) == 0)
  1020. {
  1021. return detail::last_one(std::forward<Ks>(keys)...);
  1022. }
  1023. return find_or(std::forward<decltype(tab)>(tab).at(ky), std::forward<Ks>(keys)...);
  1024. }
  1025. // ============================================================================
  1026. // expect
  1027. template<typename T, typename C,
  1028. template<typename ...> class M, template<typename ...> class V>
  1029. result<T, std::string> expect(const basic_value<C, M, V>& v) noexcept
  1030. {
  1031. try
  1032. {
  1033. return ok(get<T>(v));
  1034. }
  1035. catch(const std::exception& e)
  1036. {
  1037. return err(e.what());
  1038. }
  1039. }
  1040. template<typename T, typename C,
  1041. template<typename ...> class M, template<typename ...> class V>
  1042. result<T, std::string>
  1043. expect(const basic_value<C, M, V>& v, const toml::key& k) noexcept
  1044. {
  1045. try
  1046. {
  1047. return ok(find<T>(v, k));
  1048. }
  1049. catch(const std::exception& e)
  1050. {
  1051. return err(e.what());
  1052. }
  1053. }
  1054. } // toml
  1055. #endif// TOML11_GET