]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/geometry/doc/src/examples/core/tag.cpp
1 // Boost.Geometry (aka GGL, Generic Geometry Library)
4 // Copyright (c) 2011-2012 Barend Gehrels, Amsterdam, the Netherlands.
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)
11 //` Shows how tag dispatching essentially works in Boost.Geometry
15 #include <boost/assign.hpp>
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>
22 BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian
)
24 template <typename Tag
> struct dispatch
{};
26 // Specialization for points
27 template <> struct dispatch
<boost::geometry::point_tag
>
29 template <typename Point
>
30 static inline void apply(Point
const& p
)
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
)
41 // Specialization for polygons
42 template <> struct dispatch
<boost::geometry::polygon_tag
>
44 template <typename Polygon
>
45 static inline void apply(Polygon
const& p
)
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
)
55 // Specialization for multipolygons
56 template <> struct dispatch
<boost::geometry::multi_polygon_tag
>
58 template <typename MultiPolygon
>
59 static inline void apply(MultiPolygon
const& m
)
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)"
69 template <typename Geometry
>
70 inline void hello(Geometry
const& geometry
)
72 // Call the metafunction "tag" to dispatch, and call method (here "apply")
75 typename
boost::geometry::tag
<Geometry
>::type
81 // Define polygon type (here: based on a Boost.Tuple)
82 typedef boost::geometry::model::polygon
<boost::tuple
<int, int> > polygon_type
;
84 // Declare and fill a polygon and a multipolygon
86 boost::geometry::exterior_ring(poly
) = boost::assign::tuple_list_of(0, 0)(0, 10)(10, 5)(0, 0);
88 boost::geometry::model::multi_polygon
<polygon_type
> multi
;
89 multi
.push_back(poly
);
91 // Call "hello" for point, polygon, multipolygon
92 hello(boost::make_tuple(2, 3));
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)