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 
9 #ifndef BOOST_MULTI_INDEX_DETAIL_CONS_STDTUPLE_HPP
10 #define BOOST_MULTI_INDEX_DETAIL_CONS_STDTUPLE_HPP
11 
12 #if defined(_MSC_VER)
13 #pragma once
14 #endif
15 
16 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
17 #include <boost/mpl/if.hpp>
18 #include <boost/tuple/tuple.hpp>
19 #include <tuple>
20 
21 namespace boost{
22 
23 namespace multi_index{
24 
25 namespace detail{
26 
27 /* std::tuple wrapper providing the cons-based interface of boost::tuple for
28  * composite_key interoperability.
29  */
30 
31 template<typename StdTuple,std::size_t N>
32 struct cons_stdtuple;
33 
34 struct cons_stdtuple_ctor_terminal
35 {
36   typedef boost::tuples::null_type result_type;
37 
38   template<typename StdTuple>
createboost::multi_index::detail::cons_stdtuple_ctor_terminal39   static result_type create(const StdTuple&)
40   {
41     return boost::tuples::null_type();
42   }
43 };
44 
45 template<typename StdTuple,std::size_t N>
46 struct cons_stdtuple_ctor_normal
47 {
48   typedef cons_stdtuple<StdTuple,N> result_type;
49 
createboost::multi_index::detail::cons_stdtuple_ctor_normal50   static result_type create(const StdTuple& t)
51   {
52     return result_type(t);
53   }
54 };
55 
56 template<typename StdTuple,std::size_t N=0>
57 struct cons_stdtuple_ctor:
58   boost::mpl::if_c<
59     N<std::tuple_size<StdTuple>::value,
60     cons_stdtuple_ctor_normal<StdTuple,N>,
61     cons_stdtuple_ctor_terminal
62   >::type
63 {};
64 
65 template<typename StdTuple,std::size_t N>
66 struct cons_stdtuple
67 {
68   typedef typename std::tuple_element<N,StdTuple>::type head_type;
69   typedef cons_stdtuple_ctor<StdTuple,N+1>              tail_ctor;
70   typedef typename tail_ctor::result_type               tail_type;
71 
cons_stdtupleboost::multi_index::detail::cons_stdtuple72   cons_stdtuple(const StdTuple& t_):t(t_){}
73 
get_headboost::multi_index::detail::cons_stdtuple74   const head_type& get_head()const{return std::get<N>(t);}
get_tailboost::multi_index::detail::cons_stdtuple75   tail_type get_tail()const{return tail_ctor::create(t);}
76 
77   const StdTuple& t;
78 };
79 
80 template<typename StdTuple>
81 typename cons_stdtuple_ctor<StdTuple>::result_type
make_cons_stdtuple(const StdTuple & t)82 make_cons_stdtuple(const StdTuple& t)
83 {
84   return cons_stdtuple_ctor<StdTuple>::create(t);
85 }
86 
87 } /* namespace multi_index::detail */
88 
89 } /* namespace multi_index */
90 
91 } /* namespace boost */
92 
93 #endif
94