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