single_geometry.hpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // This file was modified by Oracle on 2013, 2014.
  4. // Modifications copyright (c) 2013-2014, Oracle and/or its affiliates.
  5. // Use, modification and distribution is subject to the Boost Software License,
  6. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  9. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_SINGLE_GEOMETRY_HPP
  10. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_SINGLE_GEOMETRY_HPP
  11. #include <boost/mpl/if.hpp>
  12. #include <boost/type_traits/is_base_of.hpp>
  13. #include <boost/geometry/core/assert.hpp>
  14. #include <boost/geometry/core/tag.hpp>
  15. #include <boost/geometry/util/range.hpp>
  16. namespace boost { namespace geometry {
  17. #ifndef DOXYGEN_NO_DISPATCH
  18. namespace detail_dispatch {
  19. // Returns single geometry by Id
  20. // for single geometries returns the geometry itself
  21. template <typename Geometry,
  22. bool IsMulti = boost::is_base_of
  23. <
  24. multi_tag,
  25. typename geometry::tag<Geometry>::type
  26. >::value
  27. >
  28. struct single_geometry
  29. {
  30. typedef Geometry & return_type;
  31. template <typename Id>
  32. static inline return_type apply(Geometry & g, Id const& ) { return g; }
  33. };
  34. // for multi geometries returns one of the stored single geometries
  35. template <typename Geometry>
  36. struct single_geometry<Geometry, true>
  37. {
  38. typedef typename boost::range_reference<Geometry>::type return_type;
  39. template <typename Id>
  40. static inline return_type apply(Geometry & g, Id const& id)
  41. {
  42. BOOST_GEOMETRY_ASSERT(id.multi_index >= 0);
  43. typedef typename boost::range_size<Geometry>::type size_type;
  44. return range::at(g, static_cast<size_type>(id.multi_index));
  45. }
  46. };
  47. } // namespace detail_dispatch
  48. #endif // DOXYGEN_NO_DISPATCH
  49. #ifndef DOXYGEN_NO_DETAIL
  50. namespace detail {
  51. template <typename Geometry>
  52. struct single_geometry_return_type
  53. {
  54. typedef typename detail_dispatch::single_geometry<Geometry>::return_type type;
  55. };
  56. template <typename Geometry, typename Id>
  57. inline
  58. typename single_geometry_return_type<Geometry>::type
  59. single_geometry(Geometry & geometry, Id const& id)
  60. {
  61. return detail_dispatch::single_geometry<Geometry>::apply(geometry, id);
  62. }
  63. template <typename Geometry, typename Id>
  64. inline
  65. typename single_geometry_return_type<Geometry const>::type
  66. single_geometry(Geometry const& geometry, Id const& id)
  67. {
  68. return detail_dispatch::single_geometry<Geometry const>::apply(geometry, id);
  69. }
  70. } // namespace detail
  71. #endif // DOXYGEN_NO_DETAIL
  72. }} // namespace boost::geometry
  73. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_SINGLE_GEOMETRY_HPP