Add DynamicGeometry adaptation for std::any.

This commit is contained in:
Adam Wulkiewicz 2021-05-28 19:03:47 +02:00
parent 6e71295d64
commit 9849f23b5c
5 changed files with 213 additions and 49 deletions

View File

@ -15,12 +15,12 @@
#include <boost/any.hpp>
#include <boost/geometry/geometries/adapted/detail/any.hpp>
#include <boost/geometry/core/geometry_types.hpp>
#include <boost/geometry/core/tag.hpp>
#include <boost/geometry/core/tags.hpp>
#include <boost/geometry/core/visit.hpp>
#include <boost/geometry/util/sequence.hpp>
#include <boost/geometry/util/type_traits_std.hpp>
namespace boost { namespace geometry
@ -29,56 +29,16 @@ namespace boost { namespace geometry
namespace detail
{
template <typename TypeSequence, std::size_t N = util::sequence_size<TypeSequence>::value>
struct visit_boost_any
{
static const std::size_t M = N / 2;
template <std::size_t Offset, typename Function, typename Any>
static bool apply(Function && function, Any && any)
struct boost_any_cast_policy
{
template <typename T, typename Any>
static inline T * apply(Any * any_ptr)
{
return visit_boost_any<TypeSequence, M>::template apply<Offset>(
std::forward<Function>(function), std::forward<Any>(any))
|| visit_boost_any<TypeSequence, N - M>::template apply<Offset + M>(
std::forward<Function>(function), std::forward<Any>(any));
return boost::any_cast<T>(any_ptr);
}
};
template <typename TypeSequence>
struct visit_boost_any<TypeSequence, 1>
{
template <std::size_t Offset, typename Function, typename Any>
static bool apply(Function && function, Any && any)
{
using elem_t = typename util::sequence_element<Offset, TypeSequence>::type;
using geom_t = util::transcribe_const_t<std::remove_reference_t<Any>, elem_t>;
geom_t * g = boost::any_cast<geom_t>(boost::addressof(any));
if (g != nullptr)
{
using geom_ref_t = std::conditional_t
<
std::is_rvalue_reference<decltype(any)>::value,
geom_t&&, geom_t&
>;
function(static_cast<geom_ref_t>(*g));
return true;
}
else
{
return false;
}
}
};
template <typename TypeSequence>
struct visit_boost_any<TypeSequence, 0>
{
template <std::size_t Offset, typename Function, typename Any>
static bool apply(Function &&, Any &&)
{
return false;
}
};
} // namespace detail
@ -98,8 +58,10 @@ struct visit<boost::any>
static void apply(Function && function, Any && any)
{
using types_t = typename geometry_types<util::remove_cref_t<Any>>::type;
geometry::detail::visit_boost_any<types_t>::template apply<0>(
std::forward<Function>(function), std::forward<Any>(any));
geometry::detail::visit_any
<
geometry::detail::boost_any_cast_policy, types_t
>::template apply<0>(std::forward<Function>(function), std::forward<Any>(any));
}
};

View File

@ -0,0 +1,87 @@
// Boost.Geometry
// Copyright (c) 2021, Oracle and/or its affiliates.
// Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
// Licensed under the Boost Software License version 1.0.
// http://www.boost.org/users/license.html
#ifndef BOOST_GEOMETRY_GEOMETRIES_ADAPTED_DETAIL_ANY_HPP
#define BOOST_GEOMETRY_GEOMETRIES_ADAPTED_DETAIL_ANY_HPP
#include <utility>
#include <boost/geometry/util/sequence.hpp>
#include <boost/geometry/util/type_traits_std.hpp>
namespace boost { namespace geometry
{
namespace detail
{
template
<
typename CastPolicy,
typename TypeSequence,
std::size_t N = util::sequence_size<TypeSequence>::value
>
struct visit_any
{
static const std::size_t M = N / 2;
template <std::size_t Offset, typename Function, typename Any>
static bool apply(Function && function, Any && any)
{
return visit_any<CastPolicy, TypeSequence, M>::template apply<Offset>(
std::forward<Function>(function), std::forward<Any>(any))
|| visit_any<CastPolicy, TypeSequence, N - M>::template apply<Offset + M>(
std::forward<Function>(function), std::forward<Any>(any));
}
};
template <typename CastPolicy, typename TypeSequence>
struct visit_any<CastPolicy, TypeSequence, 1>
{
template <std::size_t Offset, typename Function, typename Any>
static bool apply(Function && function, Any && any)
{
using elem_t = typename util::sequence_element<Offset, TypeSequence>::type;
using geom_t = util::transcribe_const_t<std::remove_reference_t<Any>, elem_t>;
geom_t * g = CastPolicy::template apply<geom_t>(&any);
if (g != nullptr)
{
using geom_ref_t = std::conditional_t
<
std::is_rvalue_reference<decltype(any)>::value,
geom_t&&, geom_t&
>;
function(static_cast<geom_ref_t>(*g));
return true;
}
else
{
return false;
}
}
};
template <typename CastPolicy, typename TypeSequence>
struct visit_any<CastPolicy, TypeSequence, 0>
{
template <std::size_t Offset, typename Function, typename Any>
static bool apply(Function &&, Any &&)
{
return false;
}
};
} // namespace detail
}} // namespace boost::geometry
#endif // BOOST_GEOMETRY_GEOMETRIES_ADAPTED_DETAIL_ANY_HPP

View File

@ -0,0 +1,82 @@
// Boost.Geometry
// Copyright (c) 2021, Oracle and/or its affiliates.
// Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
// Licensed under the Boost Software License version 1.0.
// http://www.boost.org/users/license.html
#ifndef BOOST_GEOMETRY_GEOMETRIES_ADAPTED_STD_ANY_HPP
#define BOOST_GEOMETRY_GEOMETRIES_ADAPTED_STD_ANY_HPP
#include <boost/config.hpp>
#ifndef BOOST_NO_CXX17_HDR_ANY
#include <any>
#include <utility>
#include <boost/geometry/geometries/adapted/detail/any.hpp>
#include <boost/geometry/core/geometry_types.hpp>
#include <boost/geometry/core/tag.hpp>
#include <boost/geometry/core/tags.hpp>
#include <boost/geometry/core/visit.hpp>
namespace boost { namespace geometry
{
namespace detail
{
struct std_any_cast_policy
{
template <typename T, typename Any>
static inline T * apply(Any * any_ptr)
{
return std::any_cast<T>(any_ptr);
}
};
} // namespace detail
namespace traits
{
template <>
struct tag<std::any>
{
typedef dynamic_geometry_tag type;
};
template <>
struct visit<std::any>
{
template <typename Function, typename Any>
static void apply(Function && function, Any && any)
{
using types_t = typename geometry_types<util::remove_cref_t<Any>>::type;
geometry::detail::visit_any
<
geometry::detail::std_any_cast_policy, types_t
>::template apply<0>(std::forward<Function>(function), std::forward<Any>(any));
}
};
} // namespace traits
}} // namespace boost::geometry
#endif // BOOST_NO_CXX17_HDR_ANY
#endif // BOOST_GEOMETRY_GEOMETRIES_ADAPTED_STD_ANY_HPP

View File

@ -15,6 +15,7 @@
#include <boost/geometry/geometries/adapted/boost_any.hpp>
#include <boost/geometry/geometries/adapted/boost_variant.hpp>
#include <boost/geometry/geometries/adapted/boost_variant2.hpp>
#include <boost/geometry/geometries/adapted/std_any.hpp>
#include <boost/geometry/geometries/adapted/std_variant.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/util/type_traits.hpp>
@ -34,6 +35,16 @@ struct geometry_types<boost::any>
typedef util::type_sequence<point_t, linestring_t, polygon_t> type;
};
#ifndef BOOST_NO_CXX17_HDR_ANY
template <>
struct geometry_types<std::any>
{
typedef util::type_sequence<point_t, linestring_t, polygon_t> type;
};
#endif // BOOST_NO_CXX17_HDR_ANY
}}}
@ -200,6 +211,9 @@ int test_main(int, char* [])
test_all<boost::variant<point_t, linestring_t, polygon_t>>();
test_all<boost::variant2::variant<point_t, linestring_t, polygon_t>>();
#ifndef BOOST_NO_CXX17_HDR_ANY
test_all<std::any>();
#endif
#ifndef BOOST_NO_CXX17_HDR_VARIANT
test_all<std::variant<point_t, linestring_t, polygon_t>>();
#endif

View File

@ -18,6 +18,8 @@
#include <boost/geometry/geometries/adapted/boost_any.hpp>
#include <boost/geometry/geometries/adapted/boost_variant.hpp>
#include <boost/geometry/geometries/adapted/boost_variant2.hpp>
#include <boost/geometry/geometries/adapted/std_any.hpp>
#include <boost/geometry/geometries/adapted/std_variant.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/util/type_traits.hpp>
@ -36,6 +38,16 @@ struct geometry_types<boost::any>
typedef util::type_sequence<point_t, linestring_t, polygon_t> type;
};
#ifndef BOOST_NO_CXX17_HDR_ANY
template <>
struct geometry_types<std::any>
{
typedef util::type_sequence<point_t, linestring_t, polygon_t> type;
};
#endif // BOOST_NO_CXX17_HDR_ANY
}}}
@ -198,5 +210,12 @@ int test_main(int, char* [])
test_all<boost::variant<point_t, linestring_t, polygon_t>>();
test_all<boost::variant2::variant<point_t, linestring_t, polygon_t>>();
#ifndef BOOST_NO_CXX17_HDR_ANY
test_all<std::any>();
#endif
#ifndef BOOST_NO_CXX17_HDR_VARIANT
test_all<std::variant<point_t, linestring_t, polygon_t>>();
#endif
return 0;
}