Skip to content

Commit

Permalink
add a way to check that only named parameters used by a function are …
Browse files Browse the repository at this point in the history
…passed
  • Loading branch information
sloriot committed Jan 8, 2024
1 parent 6df7424 commit 695246a
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
58 changes: 58 additions & 0 deletions STL_Extension/include/CGAL/Named_function_parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,64 @@ struct is_default_parameter
typedef CGAL::Boolean_tag<value> type;
};


// code used to make sure all options passed are used by a function
namespace authorized_parameters_impl
{

template <class ... Tag>
struct Tag_wrapper{};

template <class TagAllowed, class ... TagsAllowed, class Tag>
constexpr
bool is_tag_present(Tag_wrapper<TagAllowed, TagsAllowed...>, Tag)
{
if (std::is_same_v<TagAllowed, Tag>)
return true;
else
return is_tag_present(Tag_wrapper<TagsAllowed...>(), Tag());
}

template <class Tag>
constexpr
bool is_tag_present(Tag_wrapper<Tag>, Tag)
{
return true;
}

template <class TagAllowed, class Tag>
constexpr
bool is_tag_present(Tag_wrapper<TagAllowed>, Tag)
{
return false;
}

template <class ... TagsAllowed, class T, class Tag>
constexpr
bool authorized_options_rec(const Named_function_parameters<T, Tag>&)
{
return is_tag_present(Tag_wrapper<TagsAllowed...>(), Tag());
}

template <class ... TagsAllowed, class T, class Tag, class Base>
constexpr
bool authorized_options_rec(const Named_function_parameters<T, Tag, Base>& np)
{
if (is_tag_present(Tag_wrapper<TagsAllowed...>(), Tag()))
return authorized_options_rec<TagsAllowed...>(static_cast<const Base&>(np));
return false;
}

}// impl namespace

template <class ... TagsAllowed, class Named_function_parameters>
constexpr
bool authorized_options(const Named_function_parameters& np)
{
return authorized_parameters_impl::authorized_options_rec
<internal_np::all_default_t, TagsAllowed...>(np);
}

} // end of parameters namespace

#ifndef CGAL_NO_DEPRECATED_CODE
Expand Down
24 changes: 24 additions & 0 deletions STL_Extension/test/STL_Extension/test_cgal_named_params.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,28 @@ void test_references(const NamedParameters& np)
assert(&eim_ref==&default_value);
}

void test_authorized_options()
{
auto np_ok1 = CGAL::parameters::vertex_point_map(0).edge_index_map(2).face_index_map(3);
auto np_ok2 = CGAL::parameters::face_index_map(3).vertex_point_map(0).edge_index_map(4);
auto np_ko = CGAL::parameters::face_index_map(3).vertex_point_map(0).edge_index_map(4).halfedge_index_map(4);
auto np_default = CGAL::parameters::default_values();


static_assert(CGAL::parameters::authorized_options<CGAL::internal_np::vertex_point_t,
CGAL::internal_np::edge_index_t,
CGAL::internal_np::face_index_t>(np_default));
static_assert(CGAL::parameters::authorized_options<CGAL::internal_np::vertex_point_t,
CGAL::internal_np::edge_index_t,
CGAL::internal_np::face_index_t>(np_ok1));
static_assert(CGAL::parameters::authorized_options<CGAL::internal_np::vertex_point_t,
CGAL::internal_np::edge_index_t,
CGAL::internal_np::face_index_t>(np_ok2));
static_assert(!CGAL::parameters::authorized_options<CGAL::internal_np::vertex_point_t,
CGAL::internal_np::edge_index_t,
CGAL::internal_np::face_index_t>(np_ko));
}

int main()
{
test_values_and_types(params::vertex_index_map(A<0>(0)).visitor(A<1>(1)));
Expand All @@ -111,5 +133,7 @@ int main()
static_assert(std::is_same<decltype(d2),CGAL::parameters::Default_named_parameters>::value);
#endif

test_authorized_options();

return EXIT_SUCCESS;
}

0 comments on commit 695246a

Please sign in to comment.