1
0
forked from boostorg/mp11

Add mp_is_map

This commit is contained in:
Peter Dimov
2017-07-17 20:14:43 +03:00
parent 50d35a2964
commit 34946d2ae2
5 changed files with 108 additions and 0 deletions

View File

@@ -558,6 +558,7 @@ body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-b
</li>
<li><a href="#map">Map Operations, &lt;boost/mp11/map.hpp&gt;</a>
<ul class="sectlevel3">
<li><a href="#mp_is_map_m">mp_is_map&lt;M&gt;</a></li>
<li><a href="#mp_map_find_m_k">mp_map_find&lt;M, K&gt;</a></li>
<li><a href="#mp_map_contains_m_k">mp_map_contains&lt;M, K&gt;</a></li>
<li><a href="#mp_map_insert_m_t">mp_map_insert&lt;M, T&gt;</a></li>
@@ -3440,6 +3441,17 @@ Only <code>constexpr</code> on C++14 and higher.</p>
<p>A map is a list of lists, the inner lists having at least one element (the key.) The keys of the map must be unique.</p>
</div>
<div class="sect3">
<h4 id="mp_is_map_m">mp_is_map&lt;M&gt;</h4>
<div class="literalblock">
<div class="content">
<pre>template&lt;class M&gt; using mp_is_map = /*...*/;</pre>
</div>
</div>
<div class="paragraph">
<p><code>mp_is_map&lt;M&gt;</code> is <code>mp_true</code> if <code>M</code> is a map, <code>mp_false</code> otherwise.</p>
</div>
</div>
<div class="sect3">
<h4 id="mp_map_find_m_k">mp_map_find&lt;M, K&gt;</h4>
<div class="literalblock">
<div class="content">

View File

@@ -15,6 +15,12 @@ http://www.boost.org/LICENSE_1_0.txt
A map is a list of lists, the inner lists having at least one element (the key.) The keys of the map must be unique.
## mp_is_map<M>
template<class M> using mp_is_map = /*...*/;
`mp_is_map<M>` is `mp_true` if `M` is a map, `mp_false` otherwise.
## mp_map_find<M, K>
template<class M, class K> using mp_map_find = /*...*/;

View File

@@ -13,6 +13,8 @@
#include <boost/mp11/integral.hpp>
#include <boost/mp11/utility.hpp>
#include <boost/mp11/algorithm.hpp>
#include <boost/mp11/function.hpp>
#include <boost/mp11/set.hpp>
#include <type_traits>
namespace boost
@@ -82,6 +84,34 @@ template<class M, class K> using mp_map_erase = typename detail::mp_map_erase_im
// mp_map_keys<M>
template<class M> using mp_map_keys = mp_transform<mp_first, M>;
// mp_is_map<M>
namespace detail
{
template<class L> struct mp_is_map_element: mp_false
{
};
template<template<class...> class L, class T1, class... T> struct mp_is_map_element<L<T1, T...>>: mp_true
{
};
template<class M> using mp_keys_are_set = mp_is_set<mp_map_keys<M>>;
template<class M> struct mp_is_map_impl
{
using type = mp_false;
};
template<template<class...> class M, class... T> struct mp_is_map_impl<M<T...>>
{
using type = mp_eval_if<mp_not<mp_all<mp_is_map_element<T>...>>, mp_false, mp_keys_are_set, M<T...>>;
};
} // namespace detail
template<class M> using mp_is_map = typename detail::mp_is_map_impl<M>::type;
} // namespace mp11
} // namespace boost

View File

@@ -124,6 +124,7 @@ run mp_map_replace.cpp : : : $(REQ) ;
run mp_map_erase.cpp : : : $(REQ) ;
run mp_map_update.cpp : : : $(REQ) ;
run mp_map_keys.cpp : : : $(REQ) ;
run mp_is_map.cpp : : : $(REQ) ;
# bind
run mp_bind.cpp : : : $(REQ) ;

59
test/mp_is_map.cpp Normal file
View File

@@ -0,0 +1,59 @@
// Copyright 2017 Peter Dimov.
//
// 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 <boost/mp11/map.hpp>
#include <boost/mp11/list.hpp>
#include <boost/mp11/integral.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits>
#include <tuple>
#include <utility>
int main()
{
using boost::mp11::mp_is_map;
using boost::mp11::mp_list;
using boost::mp11::mp_true;
using boost::mp11::mp_false;
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_is_map<void>, mp_false>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_is_map<mp_list<>>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_is_map<std::tuple<>>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_is_map<mp_list<void>>, mp_false>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_is_map<std::tuple<void>>, mp_false>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_is_map<mp_list<mp_list<>>>, mp_false>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_is_map<std::tuple<std::tuple<>>>, mp_false>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_is_map<mp_list<mp_list<int>>>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_is_map<std::tuple<std::tuple<int>>>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_is_map<mp_list<std::pair<int, int const>>>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_is_map<std::tuple<std::pair<int, int const>>>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_is_map<mp_list<std::pair<int, int const>, std::pair<long, long const>>>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_is_map<std::tuple<std::pair<int, int const>, std::pair<long, long const>>>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_is_map<std::pair<std::pair<int, int const>, std::pair<long, long const>>>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_is_map<mp_list<std::pair<int, int const>, std::pair<int, long const>>>, mp_false>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_is_map<std::tuple<std::pair<int, int const>, std::pair<int, long const>>>, mp_false>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_is_map<std::pair<std::pair<int, int const>, std::pair<int, long const>>>, mp_false>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_is_map<mp_list<mp_list<int>, mp_list<long, long>, mp_list<long long, long long, long long>>>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_is_map<std::tuple<mp_list<int>, mp_list<long, long>, mp_list<long long, long long, long long>>>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_is_map<std::tuple<std::tuple<int>, std::pair<long, long>, std::tuple<long long, long long, long long>>>, mp_true>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_is_map<mp_list<mp_list<int>, mp_list<long, long>, mp_list<int, long long, long long>>>, mp_false>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_is_map<std::tuple<mp_list<int>, mp_list<long, long>, mp_list<int, long long, long long>>>, mp_false>));
BOOST_TEST_TRAIT_TRUE((std::is_same<mp_is_map<std::tuple<std::tuple<int>, std::pair<long, long>, std::tuple<int, long long, long long>>>, mp_false>));
return boost::report_errors();
}