map.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #ifndef BOOST_MP11_MAP_HPP_INCLUDED
  2. #define BOOST_MP11_MAP_HPP_INCLUDED
  3. // Copyright 2015-2017 Peter Dimov.
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. //
  7. // See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt
  9. #include <boost/mp11/detail/mp_map_find.hpp>
  10. #include <boost/mp11/list.hpp>
  11. #include <boost/mp11/integral.hpp>
  12. #include <boost/mp11/utility.hpp>
  13. #include <boost/mp11/algorithm.hpp>
  14. #include <boost/mp11/function.hpp>
  15. #include <boost/mp11/set.hpp>
  16. #include <type_traits>
  17. namespace boost
  18. {
  19. namespace mp11
  20. {
  21. // mp_map_contains<M, K>
  22. template<class M, class K> using mp_map_contains = mp_not<std::is_same<mp_map_find<M, K>, void>>;
  23. // mp_map_insert<M, T>
  24. template<class M, class T> using mp_map_insert = mp_if< mp_map_contains<M, mp_first<T>>, M, mp_push_back<M, T> >;
  25. // mp_map_replace<M, T>
  26. namespace detail
  27. {
  28. template<class M, class T> struct mp_map_replace_impl;
  29. template<template<class...> class M, class... U, class T> struct mp_map_replace_impl<M<U...>, T>
  30. {
  31. using K = mp_first<T>;
  32. // mp_replace_if is inlined here using a struct _f because of msvc-14.0
  33. template<class V> struct _f { using type = mp_if< std::is_same<mp_first<V>, K>, T, V >; };
  34. using type = mp_if< mp_map_contains<M<U...>, K>, M<typename _f<U>::type...>, M<U..., T> >;
  35. };
  36. } // namespace detail
  37. template<class M, class T> using mp_map_replace = typename detail::mp_map_replace_impl<M, T>::type;
  38. // mp_map_update<M, T, F>
  39. namespace detail
  40. {
  41. template<class M, class T, template<class...> class F> struct mp_map_update_impl
  42. {
  43. template<class U> using _f = std::is_same<mp_first<T>, mp_first<U>>;
  44. // _f3<L<X, Y...>> -> L<X, F<X, Y...>>
  45. template<class L> using _f3 = mp_assign<L, mp_list<mp_first<L>, mp_rename<L, F> > >;
  46. using type = mp_if< mp_map_contains<M, mp_first<T>>, mp_transform_if<_f, _f3, M>, mp_push_back<M, T> >;
  47. };
  48. } // namespace detail
  49. template<class M, class T, template<class...> class F> using mp_map_update = typename detail::mp_map_update_impl<M, T, F>::type;
  50. template<class M, class T, class Q> using mp_map_update_q = mp_map_update<M, T, Q::template fn>;
  51. // mp_map_erase<M, K>
  52. namespace detail
  53. {
  54. template<class M, class K> struct mp_map_erase_impl
  55. {
  56. template<class T> using _f = std::is_same<mp_first<T>, K>;
  57. using type = mp_remove_if<M, _f>;
  58. };
  59. } // namespace detail
  60. template<class M, class K> using mp_map_erase = typename detail::mp_map_erase_impl<M, K>::type;
  61. // mp_map_keys<M>
  62. template<class M> using mp_map_keys = mp_transform<mp_first, M>;
  63. // mp_is_map<M>
  64. namespace detail
  65. {
  66. template<class L> struct mp_is_map_element: mp_false
  67. {
  68. };
  69. template<template<class...> class L, class T1, class... T> struct mp_is_map_element<L<T1, T...>>: mp_true
  70. {
  71. };
  72. template<class M> using mp_keys_are_set = mp_is_set<mp_map_keys<M>>;
  73. template<class M> struct mp_is_map_impl
  74. {
  75. using type = mp_false;
  76. };
  77. template<template<class...> class M, class... T> struct mp_is_map_impl<M<T...>>
  78. {
  79. using type = mp_eval_if<mp_not<mp_all<mp_is_map_element<T>...>>, mp_false, mp_keys_are_set, M<T...>>;
  80. };
  81. } // namespace detail
  82. template<class M> using mp_is_map = typename detail::mp_is_map_impl<M>::type;
  83. } // namespace mp11
  84. } // namespace boost
  85. #endif // #ifndef BOOST_MP11_MAP_HPP_INCLUDED