/Src/Dependencies/Boost/libs/geometry/doc/src/examples/core/tag.cpp

http://hadesmem.googlecode.com/ · C++ · 110 lines · 59 code · 19 blank · 32 comment · 0 complexity · 043e3555907ba49ca6dfba73f357cbf4 MD5 · raw file

  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // QuickBook Example
  3. // Copyright (c) 2011 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Use, modification and distribution is subject to the Boost Software License,
  5. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //[tag
  8. //` Shows how tag dispatching essentially works in Boost.Geometry
  9. #include <iostream>
  10. #include <boost/assign.hpp>
  11. #include <boost/geometry.hpp>
  12. #include <boost/geometry/geometries/polygon.hpp>
  13. #include <boost/geometry/multi/geometries/multi_polygon.hpp>
  14. #include <boost/geometry/geometries/adapted/boost_tuple.hpp>
  15. BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
  16. template <typename Tag> struct dispatch {};
  17. // Specialization for points
  18. template <> struct dispatch<boost::geometry::point_tag>
  19. {
  20. template <typename Point>
  21. static inline void apply(Point const& p)
  22. {
  23. // Use the Boost.Geometry free function "get"
  24. // working on all supported point types
  25. std::cout << "Hello POINT, you are located at: "
  26. << boost::geometry::get<0>(p) << ", "
  27. << boost::geometry::get<1>(p)
  28. << std::endl;
  29. }
  30. };
  31. // Specialization for polygons
  32. template <> struct dispatch<boost::geometry::polygon_tag>
  33. {
  34. template <typename Polygon>
  35. static inline void apply(Polygon const& p)
  36. {
  37. // Use the Boost.Geometry manipulator "dsv"
  38. // working on all supported geometries
  39. std::cout << "Hello POLYGON, you look like: "
  40. << boost::geometry::dsv(p)
  41. << std::endl;
  42. }
  43. };
  44. // Specialization for multipolygons
  45. template <> struct dispatch<boost::geometry::multi_polygon_tag>
  46. {
  47. template <typename MultiPolygon>
  48. static inline void apply(MultiPolygon const& m)
  49. {
  50. // Use the Boost.Range free function "size" because all
  51. // multigeometries comply to Boost.Range
  52. std::cout << "Hello MULTIPOLYGON, you contain: "
  53. << boost::size(m) << " polygon(s)"
  54. << std::endl;
  55. }
  56. };
  57. template <typename Geometry>
  58. inline void hello(Geometry const& geometry)
  59. {
  60. // Call the metafunction "tag" to dispatch, and call method (here "apply")
  61. dispatch
  62. <
  63. typename boost::geometry::tag<Geometry>::type
  64. >::apply(geometry);
  65. }
  66. int main()
  67. {
  68. // Define polygon type (here: based on a Boost.Tuple)
  69. typedef boost::geometry::model::polygon<boost::tuple<int, int> > polygon_type;
  70. // Declare and fill a polygon and a multipolygon
  71. polygon_type poly;
  72. boost::geometry::exterior_ring(poly) = boost::assign::tuple_list_of(0, 0)(0, 10)(10, 5)(0, 0);
  73. boost::geometry::model::multi_polygon<polygon_type> multi;
  74. multi.push_back(poly);
  75. // Call "hello" for point, polygon, multipolygon
  76. hello(boost::make_tuple(2, 3));
  77. hello(poly);
  78. hello(multi);
  79. return 0;
  80. }
  81. //]
  82. //[tag_output
  83. /*`
  84. Output:
  85. [pre
  86. Hello POINT, you are located at: 2, 3
  87. Hello POLYGON, you look like: (((0, 0), (0, 10), (10, 5), (0, 0)))
  88. Hello MULTIPOLYGON, you contain: 1 polygon(s)
  89. ]
  90. */
  91. //]