cons_stdtuple.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* Copyright 2003-2014 Joaquin M Lopez Munoz.
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * See http://www.boost.org/libs/multi_index for library home page.
  7. */
  8. #ifndef BOOST_MULTI_INDEX_DETAIL_CONS_STDTUPLE_HPP
  9. #define BOOST_MULTI_INDEX_DETAIL_CONS_STDTUPLE_HPP
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
  14. #include <boost/mpl/if.hpp>
  15. #include <boost/tuple/tuple.hpp>
  16. #include <tuple>
  17. namespace boost{
  18. namespace multi_index{
  19. namespace detail{
  20. /* std::tuple wrapper providing the cons-based interface of boost::tuple for
  21. * composite_key interoperability.
  22. */
  23. template<typename StdTuple,std::size_t N>
  24. struct cons_stdtuple;
  25. struct cons_stdtuple_ctor_terminal
  26. {
  27. typedef boost::tuples::null_type result_type;
  28. template<typename StdTuple>
  29. static result_type create(const StdTuple&)
  30. {
  31. return boost::tuples::null_type();
  32. }
  33. };
  34. template<typename StdTuple,std::size_t N>
  35. struct cons_stdtuple_ctor_normal
  36. {
  37. typedef cons_stdtuple<StdTuple,N> result_type;
  38. static result_type create(const StdTuple& t)
  39. {
  40. return result_type(t);
  41. }
  42. };
  43. template<typename StdTuple,std::size_t N=0>
  44. struct cons_stdtuple_ctor:
  45. boost::mpl::if_c<
  46. N<std::tuple_size<StdTuple>::value,
  47. cons_stdtuple_ctor_normal<StdTuple,N>,
  48. cons_stdtuple_ctor_terminal
  49. >::type
  50. {};
  51. template<typename StdTuple,std::size_t N>
  52. struct cons_stdtuple
  53. {
  54. typedef typename std::tuple_element<N,StdTuple>::type head_type;
  55. typedef cons_stdtuple_ctor<StdTuple,N+1> tail_ctor;
  56. typedef typename tail_ctor::result_type tail_type;
  57. cons_stdtuple(const StdTuple& t_):t(t_){}
  58. const head_type& get_head()const{return std::get<N>(t);}
  59. tail_type get_tail()const{return tail_ctor::create(t);}
  60. const StdTuple& t;
  61. };
  62. template<typename StdTuple>
  63. typename cons_stdtuple_ctor<StdTuple>::result_type
  64. make_cons_stdtuple(const StdTuple& t)
  65. {
  66. return cons_stdtuple_ctor<StdTuple>::create(t);
  67. }
  68. } /* namespace multi_index::detail */
  69. } /* namespace multi_index */
  70. } /* namespace boost */
  71. #endif