diff --git a/Property_map/include/CGAL/property_map.h b/Property_map/include/CGAL/property_map.h index 891de5f6a00..bb5aa16041e 100644 --- a/Property_map/include/CGAL/property_map.h +++ b/Property_map/include/CGAL/property_map.h @@ -32,6 +32,7 @@ #endif #include #include +#include #include // defines std::pair @@ -435,6 +436,60 @@ make_property_map(const std::vector& v) return make_property_map(&v[0]); } +/// \ingroup PkgProperty_map +/// +/// Property map that maps an input `k` and a functor `f` to `f(k)` via +/// the `get(k)` function. It is the opposite of `Property_map_to_unary_function`. +/// +/// \tparam Input The type of the input argument `k` +/// \tparam Functor The type of the function `f`. `f` must be a model of the `Assignable` +/// concept and provide the operator: `operator()(const Input&)`. +/// \tparam Output The type of `f(k)`. If it is not provided, it defaults to +/// `result_of::type` +/// +/// \warning `get(const Input&)` returns an object of type `value_type`, +/// with `value_type` defined as `typedef Output value_type`. Therefore, +/// copies may be created depending on the `Output` type. +/// +/// \cgalModels `ReadablePropertyMap` +/// +template ::type > +struct Unary_function_to_property_map +{ + typedef Input key_type; ///< typedef to `Input` + typedef Output value_type; ///< typedef to `Ouput` + typedef const value_type& reference; ///< typedef to `const value_type&` + typedef boost::readable_property_map_tag category; ///< boost::readable_property_map_tag + + typedef Unary_function_to_property_map Self; + + /// \name get free function + /// @{ + friend value_type get(const Self& pmap, const key_type& k) {return pmap.f(k);} + /// @} + + /// \name Constructor + Unary_function_to_property_map(const Functor& f) : f(f) { } + + // can't take a reference since `f` might have been initialized from a + // temporary passed to the constructor + const Functor f; +}; + +/// Free function to create a `Unary_function_to_property_map` property map. +/// +/// \relates Unary_function_to_property_map +template ::type> +Unary_function_to_property_map + make_unary_function_to_property_map(const Functor& f) +{ + return Unary_function_to_property_map(f); +} + } // namespace CGAL #endif // CGAL_POINT_SET_PROPERTY_MAP_H