1 /*=============================================================================
2 Copyright (c) 2014-2015 Kohei Takahashi
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 ==============================================================================*/
7 #ifndef FUSION_VECTOR_11052014_1625
8 #define FUSION_VECTOR_11052014_1625
10 #include <boost/config.hpp>
11 #include <boost/fusion/support/config.hpp>
12 #include <boost/fusion/container/vector/detail/config.hpp>
13 #include <boost/fusion/container/vector/vector_fwd.hpp>
15 ///////////////////////////////////////////////////////////////////////////////
16 // Without variadics, we will use the PP version
17 ///////////////////////////////////////////////////////////////////////////////
18 #if !defined(BOOST_FUSION_HAS_VARIADIC_VECTOR)
19 # include <boost/fusion/container/vector/detail/cpp03/vector.hpp>
22 ///////////////////////////////////////////////////////////////////////////////
24 ///////////////////////////////////////////////////////////////////////////////
25 #include <boost/fusion/support/sequence_base.hpp>
26 #include <boost/fusion/support/is_sequence.hpp>
27 #include <boost/fusion/support/detail/and.hpp>
28 #include <boost/fusion/support/detail/index_sequence.hpp>
29 #include <boost/fusion/container/vector/detail/at_impl.hpp>
30 #include <boost/fusion/container/vector/detail/value_at_impl.hpp>
31 #include <boost/fusion/container/vector/detail/begin_impl.hpp>
32 #include <boost/fusion/container/vector/detail/end_impl.hpp>
33 #include <boost/fusion/sequence/intrinsic/begin.hpp>
34 #include <boost/fusion/sequence/intrinsic/size.hpp>
35 #include <boost/fusion/iterator/advance.hpp>
36 #include <boost/fusion/iterator/deref.hpp>
37 #include <boost/core/enable_if.hpp>
38 #include <boost/mpl/int.hpp>
39 #include <boost/type_traits/integral_constant.hpp>
40 #include <boost/type_traits/is_base_of.hpp>
41 #include <boost/type_traits/is_convertible.hpp>
42 #include <boost/type_traits/remove_reference.hpp>
46 namespace boost { namespace fusion
49 struct random_access_traversal_tag;
51 namespace vector_detail
56 typename This, typename T, typename T_, std::size_t Size, bool IsSeq
58 struct can_convert_impl : false_type {};
60 template <typename This, typename T, typename Sequence, std::size_t Size>
61 struct can_convert_impl<This, T, Sequence, Size, true> : true_type {};
63 template <typename This, typename Sequence, typename T>
64 struct can_convert_impl<This, Sequence, T, 1, true>
69 , typename fusion::extension::value_at_impl<vector_tag>::
70 template apply< This, mpl::int_<0> >::type
75 template <typename This, typename T, typename T_, std::size_t Size>
78 This, T, T_, Size, traits::is_sequence<T_>::value
82 template <typename T, bool IsSeq, std::size_t Size>
83 struct is_longer_sequence_impl : false_type {};
85 template <typename Sequence, std::size_t Size>
86 struct is_longer_sequence_impl<Sequence, true, Size>
88 bool, (fusion::result_of::size<Sequence>::value >= Size)
92 template<typename T, std::size_t Size>
93 struct is_longer_sequence
94 : is_longer_sequence_impl<T, traits::is_sequence<T>::value, Size>
97 // forward_at_c allows to access Nth element even if ForwardSequence
98 // since fusion::at_c requires RandomAccessSequence.
101 template <typename Sequence, int N>
103 : fusion::result_of::deref<
104 typename fusion::result_of::advance_c<
105 typename fusion::result_of::begin<
106 typename remove_reference<Sequence>::type
114 template <int N, typename Sequence>
115 BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
116 inline typename result_of::forward_at_c<Sequence, N>::type
117 forward_at_c(Sequence&& seq)
120 result_of::forward_at_c<Sequence, N>::type
122 return std::forward<result>(*advance_c<N>(begin(seq)));
125 // Object proxy since preserve object order
126 template <std::size_t, typename T>
129 BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
131 : elem() // value-initialized explicitly
134 BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
135 store(store const& rhs)
139 BOOST_CXX14_CONSTEXPR BOOST_FUSION_GPU_ENABLED
141 operator=(store const& rhs)
147 BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
149 : elem(static_cast<T&&>(rhs.get()))
152 BOOST_CXX14_CONSTEXPR BOOST_FUSION_GPU_ENABLED
154 operator=(store&& rhs)
156 elem = static_cast<T&&>(rhs.get());
162 , typename = typename boost::disable_if<
163 is_base_of<store, typename remove_reference<U>::type>
166 BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
168 : elem(std::forward<U>(rhs))
171 BOOST_CXX14_CONSTEXPR BOOST_FUSION_GPU_ENABLED
172 T & get() { return elem; }
173 BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
174 T const& get() const { return elem; }
179 template <typename I, typename ...T>
182 template <std::size_t ...I, typename ...T>
183 struct vector_data<detail::index_sequence<I...>, T...>
185 , sequence_base<vector_data<detail::index_sequence<I...>, T...> >
187 typedef vector_tag fusion_tag;
188 typedef fusion_sequence_tag tag; // this gets picked up by MPL
189 typedef mpl::false_ is_view;
190 typedef random_access_traversal_tag category;
191 typedef mpl::int_<sizeof...(T)> size;
192 typedef vector<T...> type_sequence;
194 BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
200 , typename Sequence_ = typename remove_reference<Sequence>::type
201 , typename = typename boost::enable_if<
202 can_convert<vector_data, Sequence, Sequence_, sizeof...(I)>
205 BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
207 vector_data(each_elem, Sequence&& rhs)
208 : store<I, T>(forward_at_c<I>(std::forward<Sequence>(rhs)))...
211 template <typename ...U>
212 BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
214 vector_data(each_elem, U&&... var)
215 : store<I, T>(std::forward<U>(var))...
218 template <typename Sequence>
219 BOOST_CXX14_CONSTEXPR BOOST_FUSION_GPU_ENABLED
221 assign_sequence(Sequence&& seq)
223 assign(std::forward<Sequence>(seq), detail::index_sequence<I...>());
226 template <typename Sequence>
227 BOOST_CXX14_CONSTEXPR BOOST_FUSION_GPU_ENABLED
229 assign(Sequence&&, detail::index_sequence<>) {}
231 template <typename Sequence, std::size_t N, std::size_t ...M>
232 BOOST_CXX14_CONSTEXPR BOOST_FUSION_GPU_ENABLED
234 assign(Sequence&& seq, detail::index_sequence<N, M...>)
236 at_impl(mpl::int_<N>()) = vector_detail::forward_at_c<N>(seq);
237 assign(std::forward<Sequence>(seq), detail::index_sequence<M...>());
240 template <std::size_t N, typename U>
241 static BOOST_CXX14_CONSTEXPR BOOST_FUSION_GPU_ENABLED
242 auto at_detail(store<N, U>* this_) -> decltype(this_->get())
247 template <std::size_t N, typename U>
248 static BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
249 auto at_detail(store<N, U> const* this_) -> decltype(this_->get())
254 template <typename J>
255 BOOST_CXX14_CONSTEXPR BOOST_FUSION_GPU_ENABLED
256 auto at_impl(J) -> decltype(at_detail<J::value>(this))
258 return at_detail<J::value>(this);
261 template <typename J>
262 BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
263 auto at_impl(J) const -> decltype(at_detail<J::value>(this))
265 return at_detail<J::value>(this);
268 template <std::size_t N, typename U>
269 static BOOST_FUSION_GPU_ENABLED
270 mpl::identity<U> value_at_impl(store<N, U>*);
272 } // namespace boost::fusion::vector_detail
274 template <typename... T>
276 : vector_detail::vector_data<
277 typename detail::make_index_sequence<sizeof...(T)>::type
281 typedef vector_detail::vector_data<
282 typename detail::make_index_sequence<sizeof...(T)>::type
286 BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
292 , typename = typename boost::enable_if_c<(
294 fusion::detail::and_<is_convertible<U, T>...>::value &&
295 !fusion::detail::and_<
296 is_base_of<vector, typename remove_reference<U>::type>...
300 // XXX: constexpr become error due to pull-request #79, booooo!!
301 // In the (near) future release, should be fixed.
302 /* BOOST_CONSTEXPR */ BOOST_FUSION_GPU_ENABLED
303 explicit vector(U&&... u)
304 : base(vector_detail::each_elem(), std::forward<U>(u)...)
309 , typename Sequence_ = typename remove_reference<Sequence>::type
310 , typename = typename boost::enable_if_c<(
311 !is_base_of<vector, Sequence_>::value &&
312 vector_detail::is_longer_sequence<
313 Sequence_, sizeof...(T)
317 BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
318 vector(Sequence&& seq)
319 : base(vector_detail::each_elem(), std::forward<Sequence>(seq))
322 template <typename Sequence>
323 BOOST_CXX14_CONSTEXPR BOOST_FUSION_GPU_ENABLED
325 operator=(Sequence&& rhs)
327 base::assign_sequence(std::forward<Sequence>(rhs));