From f387448581ce29d923ebd42efb60b94834de74ca Mon Sep 17 00:00:00 2001 From: Glen Fernandes Date: Thu, 30 Aug 2018 22:35:23 -0400 Subject: [PATCH] Implement mp_starts_with --- doc/mp11/algorithm.adoc | 7 ++++ include/boost/mp11/algorithm.hpp | 24 ++++++++++++ test/Jamfile | 2 + test/mp_starts_with.cpp | 64 ++++++++++++++++++++++++++++++++ test/mp_starts_with_sf.cpp | 45 ++++++++++++++++++++++ 5 files changed, 142 insertions(+) create mode 100644 test/mp_starts_with.cpp create mode 100644 test/mp_starts_with_sf.cpp diff --git a/doc/mp11/algorithm.adoc b/doc/mp11/algorithm.adoc index aad27c4..8f53163 100644 --- a/doc/mp11/algorithm.adoc +++ b/doc/mp11/algorithm.adoc @@ -199,6 +199,13 @@ As `mp_count_if`, but takes a quoted metafunction. `mp_contains` is `mp_true` when `L` contains an element `V`, `mp_false` otherwise. +## mp_starts_with + + template using mp_starts_with = /*...*/; + +`mp_starts_with` is `mp_true` when `L1` starts with `L2`, `mp_false` +otherwise. + ## mp_repeat_c template using mp_repeat_c = /*...*/; diff --git a/include/boost/mp11/algorithm.hpp b/include/boost/mp11/algorithm.hpp index 5a6a200..581a213 100644 --- a/include/boost/mp11/algorithm.hpp +++ b/include/boost/mp11/algorithm.hpp @@ -987,6 +987,30 @@ template using mp_erase = mp_append, mp // mp_erase_c template using mp_erase_c = mp_append, mp_drop_c>; +// mp_starts_with +// contributed by Glen Joseph Fernandes (glenjofe@gmail.com) +namespace detail { + +template +struct mp_starts_with_impl { }; + +template class L1, class... T1, template class L2, + class... T2> +struct mp_starts_with_impl, L2 > { + template + static mp_false check(L); + + template + static mp_true check(mp_list); + + using type = decltype(check(mp_list())); +}; + +} // namespace detail + +template +using mp_starts_with = typename detail::mp_starts_with_impl::type; + // mp_min_element // mp_max_element // in detail/mp_min_element.hpp diff --git a/test/Jamfile b/test/Jamfile index 85ac992..168e0fd 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -46,6 +46,8 @@ run mp_count.cpp ; run mp_count_if.cpp ; run mp_count_if_q.cpp ; run mp_contains.cpp ; +run mp_starts_with.cpp ; +run mp_starts_with_sf.cpp ; run mp_repeat.cpp ; run mp_product.cpp ; run mp_drop.cpp ; diff --git a/test/mp_starts_with.cpp b/test/mp_starts_with.cpp new file mode 100644 index 0000000..8801220 --- /dev/null +++ b/test/mp_starts_with.cpp @@ -0,0 +1,64 @@ +// Copyright 2018 Glen Joseph Fernandes +// (glenjofe@gmail.com) +// +// Distributed under the Boost Software License, Version 1.0. +// +// See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include +#include +#include +#include +#include + +int main() +{ + using boost::mp11::mp_list; + using boost::mp11::mp_starts_with; + using boost::mp11::mp_true; + using boost::mp11::mp_false; + BOOST_TEST_TRAIT_TRUE((std::is_same, + mp_list<> >, mp_true>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, + mp_list >, mp_false>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, + mp_list<> >, mp_true>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, + std::tuple >, mp_false>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, + mp_list<> >, mp_true>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, + mp_list >, mp_true>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, + mp_list >, mp_false>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, + mp_list >, mp_true>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, + std::tuple >, mp_false>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, + mp_list >, mp_true>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, + mp_list >, mp_false>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, + mp_list >, mp_false>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, + mp_list >, mp_true>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, + std::pair >, mp_false>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, + mp_list >, mp_true>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, + mp_list >, mp_false>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, + mp_list >, mp_true>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, + std::tuple >, mp_false>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, + mp_list >, mp_true>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, + mp_list >, mp_false>)); + return boost::report_errors(); +} diff --git a/test/mp_starts_with_sf.cpp b/test/mp_starts_with_sf.cpp new file mode 100644 index 0000000..40d757e --- /dev/null +++ b/test/mp_starts_with_sf.cpp @@ -0,0 +1,45 @@ +// Copyright 2018 Glen Joseph Fernandes +// (glenjofe@gmail.com) +// +// Distributed under the Boost Software License, Version 1.0. +// +// See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include +#include + +int main() +{ + using boost::mp11::mp_valid; + using boost::mp11::mp_starts_with; + using boost::mp11::mp_list; + BOOST_TEST_TRAIT_FALSE((mp_valid)); + BOOST_TEST_TRAIT_FALSE((mp_valid)); + BOOST_TEST_TRAIT_FALSE((mp_valid >)); + BOOST_TEST_TRAIT_FALSE((mp_valid >)); + BOOST_TEST_TRAIT_FALSE((mp_valid)); + BOOST_TEST_TRAIT_FALSE((mp_valid, void>)); + BOOST_TEST_TRAIT_FALSE((mp_valid, void>)); + BOOST_TEST_TRAIT_FALSE((mp_valid >)); + BOOST_TEST_TRAIT_FALSE((mp_valid >)); + BOOST_TEST_TRAIT_TRUE((mp_valid, mp_list<> >)); + BOOST_TEST_TRAIT_TRUE((mp_valid, mp_list >)); + BOOST_TEST_TRAIT_TRUE((mp_valid, mp_list<> >)); + BOOST_TEST_TRAIT_TRUE((mp_valid, mp_list >)); + return boost::report_errors(); +}