tupled_output.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. // Boost.Geometry
  2. // Copyright (c) 2019-2020, Oracle and/or its affiliates.
  3. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  4. // Licensed under the Boost Software License version 1.0.
  5. // http://www.boost.org/users/license.html
  6. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_TUPLED_OUTPUT_HPP
  7. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_TUPLED_OUTPUT_HPP
  8. #include <boost/range/value_type.hpp>
  9. #include <boost/geometry/algorithms/convert.hpp>
  10. #include <boost/geometry/core/config.hpp>
  11. #include <boost/geometry/core/static_assert.hpp>
  12. #include <boost/geometry/core/tag.hpp>
  13. #include <boost/geometry/core/tag_cast.hpp>
  14. #include <boost/geometry/core/tags.hpp>
  15. #include <boost/geometry/geometries/concepts/check.hpp>
  16. #include <boost/geometry/util/range.hpp>
  17. #include <boost/geometry/util/tuples.hpp>
  18. #include <boost/geometry/util/type_traits.hpp>
  19. namespace boost { namespace geometry
  20. {
  21. #ifndef DOXYGEN_NO_DETAIL
  22. namespace detail
  23. {
  24. template <typename T, bool IsRange = range::detail::is_range<T>::value>
  25. struct is_tupled_output_element_base
  26. : util::bool_constant<false>
  27. {};
  28. template <typename T>
  29. struct is_tupled_output_element_base<T, true>
  30. : util::bool_constant
  31. <
  32. (util::is_multi<T>::value
  33. ||
  34. (util::is_not_geometry<T>::value
  35. &&
  36. util::is_multi_element
  37. <
  38. typename boost::range_value<T>::type
  39. >::value))
  40. >
  41. {};
  42. // true if T is a multi-geometry or is a range of points, linestrings or
  43. // polygons
  44. template <typename T>
  45. struct is_tupled_output_element
  46. : is_tupled_output_element_base<T>
  47. {};
  48. // true if Output is not a geometry (so e.g. tuple was not adapted to any
  49. // concept) and at least one of the tuple elements is a multi-geometry or
  50. // a range of points, linestrings or polygons
  51. template <typename Output>
  52. struct is_tupled_output_check
  53. : util::bool_constant
  54. <
  55. (util::is_not_geometry<Output>::value
  56. && geometry::tuples::exists_if<Output, is_tupled_output_element>::value)
  57. >
  58. {};
  59. // true if T is not a geometry (so e.g. tuple was not adapted to any
  60. // concept) and at least one of the tuple elements is a point, linesting
  61. // or polygon
  62. template <typename T>
  63. struct is_tupled_single_output_check
  64. : util::bool_constant
  65. <
  66. (util::is_not_geometry<T>::value
  67. && geometry::tuples::exists_if<T, util::is_multi_element>::value)
  68. >
  69. {};
  70. // true if Output is boost::tuple, boost::tuples::cons, std::pair or std::tuple
  71. // and is_tupled_output_check defiend above passes
  72. template <typename Output, bool IsTupled = tuples::is_tuple<Output>::value>
  73. struct is_tupled_output
  74. : util::bool_constant<false>
  75. {};
  76. template <typename Output>
  77. struct is_tupled_output<Output, true>
  78. : is_tupled_output_check<Output>
  79. {};
  80. // true if T is boost::tuple, boost::tuples::cons, std::pair or std::tuple
  81. // and is_tupled_single_output_check defiend above passes
  82. template <typename T, bool IsTupled = tuples::is_tuple<T>::value>
  83. struct is_tupled_single_output
  84. : util::bool_constant<false>
  85. {};
  86. template <typename T>
  87. struct is_tupled_single_output<T, true>
  88. : is_tupled_single_output_check<T>
  89. {};
  90. template <typename Tag>
  91. struct tupled_output_find_index_pred
  92. {
  93. template <typename T>
  94. struct pred : std::is_same<geometry::tag_t<T>, Tag>
  95. {};
  96. };
  97. // Valid only if tupled_output_has<Output, Tag> is true
  98. template <typename Output, typename Tag>
  99. struct tupled_output_find_index
  100. : geometry::tuples::find_index_if
  101. <
  102. Output,
  103. tupled_output_find_index_pred<Tag>::template pred
  104. >
  105. {};
  106. template
  107. <
  108. typename Output,
  109. typename Tag,
  110. bool IsTupledOutput = is_tupled_output<Output>::value
  111. >
  112. struct tupled_output_has
  113. : util::bool_constant<false>
  114. {};
  115. template <typename Output, typename Tag>
  116. struct tupled_output_has<Output, Tag, true>
  117. : util::bool_constant
  118. <
  119. ((tupled_output_find_index<Output, Tag>::value)
  120. < (geometry::tuples::size<Output>::value))
  121. >
  122. {};
  123. // Valid only if tupled_output_has<Output, Tag> is true
  124. template <typename Tag, typename Output>
  125. inline typename geometry::tuples::element
  126. <
  127. tupled_output_find_index<Output, Tag>::value,
  128. Output
  129. >::type &
  130. tupled_output_get(Output & output)
  131. {
  132. return geometry::tuples::get<tupled_output_find_index<Output, Tag>::value>(output);
  133. }
  134. // defines a tuple-type holding value-types of ranges being elements of
  135. // Output pair/tuple
  136. template <typename Tuple>
  137. struct tupled_range_values;
  138. template <typename ...Ts>
  139. struct tupled_range_values<std::tuple<Ts...> >
  140. {
  141. typedef std::tuple<typename boost::range_value<Ts>::type...> type;
  142. };
  143. template <typename F, typename S>
  144. struct tupled_range_values<std::pair<F, S> >
  145. {
  146. typedef std::pair
  147. <
  148. typename boost::range_value<F>::type,
  149. typename boost::range_value<S>::type
  150. > type;
  151. };
  152. template
  153. <
  154. typename Tuple,
  155. size_t I = 0,
  156. size_t N = boost::tuples::length<Tuple>::value
  157. >
  158. struct tupled_range_values_bt
  159. {
  160. typedef boost::tuples::cons
  161. <
  162. typename boost::range_value
  163. <
  164. typename boost::tuples::element<I, Tuple>::type
  165. >::type,
  166. typename tupled_range_values_bt<Tuple, I+1, N>::type
  167. > type;
  168. };
  169. template <typename Tuple, size_t N>
  170. struct tupled_range_values_bt<Tuple, N, N>
  171. {
  172. typedef boost::tuples::null_type type;
  173. };
  174. template <typename ...Ts>
  175. struct tupled_range_values<boost::tuples::tuple<Ts...>>
  176. : tupled_range_values_bt<boost::tuples::tuple<Ts...>>
  177. {};
  178. template <typename HT, typename TT>
  179. struct tupled_range_values<boost::tuples::cons<HT, TT>>
  180. : tupled_range_values_bt<boost::tuples::cons<HT, TT>>
  181. {};
  182. // util defining a type and creating a tuple holding back-insert-iterators to
  183. // ranges being elements of Output pair/tuple
  184. template <typename Tuple>
  185. struct tupled_back_inserters;
  186. template <typename Is, typename Tuple>
  187. struct tupled_back_inserters_st;
  188. template <std::size_t ...Is, typename ...Ts>
  189. struct tupled_back_inserters_st<std::index_sequence<Is...>, std::tuple<Ts...> >
  190. {
  191. typedef std::tuple<geometry::range::back_insert_iterator<Ts>...> type;
  192. static type apply(std::tuple<Ts...> & tup)
  193. {
  194. return type(geometry::range::back_inserter(std::get<Is>(tup))...);
  195. }
  196. };
  197. template <typename ...Ts>
  198. struct tupled_back_inserters<std::tuple<Ts...> >
  199. : tupled_back_inserters_st
  200. <
  201. std::make_index_sequence<sizeof...(Ts)>,
  202. std::tuple<Ts...>
  203. >
  204. {};
  205. template <typename F, typename S>
  206. struct tupled_back_inserters<std::pair<F, S> >
  207. {
  208. typedef std::pair
  209. <
  210. geometry::range::back_insert_iterator<F>,
  211. geometry::range::back_insert_iterator<S>
  212. > type;
  213. static type apply(std::pair<F, S> & p)
  214. {
  215. return type(geometry::range::back_inserter(p.first),
  216. geometry::range::back_inserter(p.second));
  217. }
  218. };
  219. template <typename Tuple,
  220. size_t I = 0,
  221. size_t N = boost::tuples::length<Tuple>::value>
  222. struct tupled_back_inserters_bt
  223. {
  224. typedef boost::tuples::cons
  225. <
  226. geometry::range::back_insert_iterator
  227. <
  228. typename boost::tuples::element<I, Tuple>::type
  229. >,
  230. typename tupled_back_inserters_bt<Tuple, I+1, N>::type
  231. > type;
  232. static type apply(Tuple & tup)
  233. {
  234. return type(geometry::range::back_inserter(boost::get<I>(tup)),
  235. tupled_back_inserters_bt<Tuple, I+1, N>::apply(tup));
  236. }
  237. };
  238. template <typename Tuple, size_t N>
  239. struct tupled_back_inserters_bt<Tuple, N, N>
  240. {
  241. typedef boost::tuples::null_type type;
  242. static type apply(Tuple const&)
  243. {
  244. return type();
  245. }
  246. };
  247. template <typename ...Ts>
  248. struct tupled_back_inserters<boost::tuples::tuple<Ts...>>
  249. : tupled_back_inserters_bt<boost::tuples::tuple<Ts...>>
  250. {};
  251. template <typename HT, typename TT>
  252. struct tupled_back_inserters<boost::tuples::cons<HT, TT>>
  253. : tupled_back_inserters_bt<boost::tuples::cons<HT, TT>>
  254. {};
  255. template
  256. <
  257. typename GeometryOut,
  258. bool IsTupled = is_tupled_output<GeometryOut>::value
  259. >
  260. struct output_geometry_value
  261. : boost::range_value<GeometryOut>
  262. {};
  263. template <typename GeometryOut>
  264. struct output_geometry_value<GeometryOut, true>
  265. : tupled_range_values<GeometryOut>
  266. {};
  267. template
  268. <
  269. typename GeometryOut,
  270. bool IsTupled = is_tupled_output<GeometryOut>::value
  271. >
  272. struct output_geometry_back_inserter_
  273. {
  274. typedef geometry::range::back_insert_iterator<GeometryOut> type;
  275. static type apply(GeometryOut & out)
  276. {
  277. return geometry::range::back_inserter(out);
  278. }
  279. };
  280. template <typename GeometryOut>
  281. struct output_geometry_back_inserter_<GeometryOut, true>
  282. : tupled_back_inserters<GeometryOut>
  283. {};
  284. template <typename GeometryOut>
  285. inline typename output_geometry_back_inserter_<GeometryOut>::type
  286. output_geometry_back_inserter(GeometryOut & out)
  287. {
  288. return output_geometry_back_inserter_<GeometryOut>::apply(out);
  289. }
  290. // is_tag_same_as_pred
  291. // Defines a predicate true if type's tag is the same as Tag
  292. template <typename Tag>
  293. struct is_tag_same_as_pred
  294. {
  295. template <typename T>
  296. struct pred : std::is_same<geometry::tag_t<T>, Tag>
  297. {};
  298. };
  299. // Allows to access a type/object in a pair/tuple corresponding to an index in
  300. // GeometryOut pair/tuple of a geometry defined by Tag.
  301. // If GeometryOut is a geometry then it's expected to be defined by DefaultTag.
  302. template
  303. <
  304. typename GeometryOut,
  305. typename Tag,
  306. typename DefaultTag,
  307. typename GeometryTag = geometry::tag_t<GeometryOut>
  308. >
  309. struct output_geometry_access
  310. {};
  311. // assume GeometryTag is void because not adapted tuple holding geometries was passed
  312. template <typename TupledOut, typename Tag, typename DefaultTag>
  313. struct output_geometry_access<TupledOut, Tag, DefaultTag, void>
  314. {
  315. static const int index = geometry::tuples::find_index_if
  316. <
  317. TupledOut, is_tag_same_as_pred<Tag>::template pred
  318. >::value;
  319. typedef typename geometry::tuples::element<index, TupledOut>::type type;
  320. template <typename Tuple>
  321. static typename geometry::tuples::element<index, Tuple>::type&
  322. get(Tuple & tup)
  323. {
  324. return geometry::tuples::get<index>(tup);
  325. }
  326. };
  327. template <typename GeometryOut, typename Tag, typename DefaultTag>
  328. struct output_geometry_access<GeometryOut, Tag, DefaultTag, DefaultTag>
  329. {
  330. typedef GeometryOut type;
  331. template <typename T>
  332. static T& get(T & v)
  333. {
  334. return v;
  335. }
  336. };
  337. template <typename Geometry>
  338. struct output_geometry_concept_check
  339. {
  340. static void apply()
  341. {
  342. concepts::check<Geometry>();
  343. }
  344. };
  345. template <typename First, typename Second>
  346. struct output_geometry_concept_check<std::pair<First, Second> >
  347. {
  348. static void apply()
  349. {
  350. concepts::check<First>();
  351. concepts::check<Second>();
  352. }
  353. };
  354. template <typename Tuple,
  355. size_t I = 0,
  356. size_t N = geometry::tuples::size<Tuple>::value>
  357. struct output_geometry_concept_check_t
  358. {
  359. static void apply()
  360. {
  361. concepts::check<typename geometry::tuples::element<I, Tuple>::type>();
  362. output_geometry_concept_check_t<Tuple, I + 1, N>::apply();
  363. }
  364. };
  365. template <typename Tuple, size_t N>
  366. struct output_geometry_concept_check_t<Tuple, N, N>
  367. {
  368. static void apply()
  369. {}
  370. };
  371. template <typename ...Ts>
  372. struct output_geometry_concept_check<std::tuple<Ts...> >
  373. : output_geometry_concept_check_t<std::tuple<Ts...> >
  374. {};
  375. template <typename ...Ts>
  376. struct output_geometry_concept_check<boost::tuple<Ts...> >
  377. : output_geometry_concept_check_t<boost::tuple<Ts...> >
  378. {};
  379. template <typename HT, typename TT>
  380. struct output_geometry_concept_check<boost::tuples::cons<HT, TT> >
  381. : output_geometry_concept_check_t<boost::tuples::cons<HT, TT> >
  382. {};
  383. struct tupled_output_tag {};
  384. template <typename GeometryOut>
  385. struct setop_insert_output_tag
  386. : std::conditional
  387. <
  388. geometry::detail::is_tupled_single_output<GeometryOut>::value,
  389. tupled_output_tag,
  390. geometry::tag_t<GeometryOut>
  391. >
  392. {};
  393. template <typename Geometry1, typename Geometry2, typename TupledOut, bool IsFound, typename Tag>
  394. struct expect_output_assert_base;
  395. template <typename Geometry1, typename Geometry2, typename TupledOut, bool IsFound>
  396. struct expect_output_assert_base<Geometry1, Geometry2, TupledOut, IsFound, pointlike_tag>
  397. {
  398. BOOST_GEOMETRY_STATIC_ASSERT(
  399. IsFound,
  400. "PointLike Geometry expected in tupled output.",
  401. Geometry1, Geometry2, TupledOut);
  402. };
  403. template <typename Geometry1, typename Geometry2, typename TupledOut, bool IsFound>
  404. struct expect_output_assert_base<Geometry1, Geometry2, TupledOut, IsFound, linear_tag>
  405. {
  406. BOOST_GEOMETRY_STATIC_ASSERT(
  407. IsFound,
  408. "Linear Geometry expected in tupled output.",
  409. Geometry1, Geometry2, TupledOut);
  410. };
  411. template <typename Geometry1, typename Geometry2, typename TupledOut, bool IsFound>
  412. struct expect_output_assert_base<Geometry1, Geometry2, TupledOut, IsFound, areal_tag>
  413. {
  414. BOOST_GEOMETRY_STATIC_ASSERT(
  415. IsFound,
  416. "Areal Geometry expected in tupled output.",
  417. Geometry1, Geometry2, TupledOut);
  418. };
  419. template <typename Geometry1, typename Geometry2, typename TupledOut, typename Tag>
  420. struct expect_output_assert
  421. : expect_output_assert_base
  422. <
  423. Geometry1, Geometry2, TupledOut,
  424. geometry::tuples::exists_if
  425. <
  426. TupledOut,
  427. is_tag_same_as_pred<Tag>::template pred
  428. >::value,
  429. tag_cast_t
  430. <
  431. Tag, pointlike_tag, linear_tag, areal_tag
  432. >
  433. >
  434. {};
  435. template <typename Geometry1, typename Geometry2, typename TupledOut>
  436. struct expect_output_assert<Geometry1, Geometry2, TupledOut, void>
  437. {};
  438. template
  439. <
  440. typename Geometry1, typename Geometry2, typename TupledOut,
  441. typename ...Tags
  442. >
  443. struct expect_output
  444. : expect_output_assert<Geometry1, Geometry2, TupledOut, Tags>...
  445. {};
  446. template
  447. <
  448. typename Geometry,
  449. typename SingleOut,
  450. bool IsMulti = util::is_multi<Geometry>::value
  451. >
  452. struct convert_to_output
  453. {
  454. template <typename OutputIterator>
  455. static OutputIterator apply(Geometry const& geometry,
  456. OutputIterator oit)
  457. {
  458. SingleOut single_out;
  459. geometry::convert(geometry, single_out);
  460. *oit++ = single_out;
  461. return oit;
  462. }
  463. };
  464. template
  465. <
  466. typename Geometry,
  467. typename SingleOut
  468. >
  469. struct convert_to_output<Geometry, SingleOut, true>
  470. {
  471. template <typename OutputIterator>
  472. static OutputIterator apply(Geometry const& geometry,
  473. OutputIterator oit)
  474. {
  475. for (auto it = boost::begin(geometry); it != boost::end(geometry); ++it)
  476. {
  477. SingleOut single_out;
  478. geometry::convert(*it, single_out);
  479. *oit++ = single_out;
  480. }
  481. return oit;
  482. }
  483. };
  484. } // namespace detail
  485. #endif // DOXYGEN_NO_DETAIL
  486. }} // namespace boost::geometry
  487. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_TUPLED_OUTPUT_HPP