VCCC  2024.05
VisualCamp Common C++ library
convert_to.hpp
Go to the documentation of this file.
1 # /*
2 # * Created by YongGyu Lee on 2020/12/08.
3 # */
4 #
5 # ifndef VCCC_TYPE_SUPPORT_DETAIL_CONVERT_TO_HPP
6 # define VCCC_TYPE_SUPPORT_DETAIL_CONVERT_TO_HPP
7 #
8 # include <tuple>
9 # include <utility>
10 #
11 # include "vccc/utility.hpp"
12 # include "vccc/type_traits.hpp"
14 
15 namespace vccc {
16 namespace internal {
17 
18 template<typename T, typename = void>
19 struct has_size : std::false_type {};
20 
21 template<typename T>
22 struct has_size<T, void_t<decltype(std::declval<T&>().size())>> : std::true_type {};
23 
24 template<size_t sz, typename T> inline bool check_bound(std::true_type, const T& from) { return from.size() >= sz; }
25 template<size_t sz, typename T> inline bool check_bound(std::false_type, const T&) { return true; }
26 
28 struct tuple_size_or_zero : std::integral_constant<size_t, 0> {};
29 
30 template<typename T>
31 struct tuple_size_or_zero<T, true> : std::tuple_size<T> {};
32 
33 
34 // fill rest with integer_sequence
35 template<typename R, typename T, std::size_t... I, typename IT, IT... I2>
36 inline R
37 fill_rest(const T& from, std::index_sequence<I...> index_seq, std::integer_sequence<IT, I2...> rest_seq)
38 {
39  return R(vccc::at<I, typename R::value_type>(from)..., I2...);
40 }
41 
42 // convert to cv type
43 template<typename R, typename T, std::size_t... I>
44 inline R
45 convert_to_impl(std::true_type, const T& from, std::index_sequence<I...> index_seq)
46 {
48  "Following must be satisfied: from.size() >= tuple_size<R>::value");
49  using Zeros = make_zero_sequence<(
52  return fill_rest<R>(from, index_seq, Zeros{});
53 }
54 
55 // convert to non-cv type (such as container) with initializer-list
56 template<typename R, typename T, std::size_t... I>
57 inline R
58 convert_to_impl(std::false_type, const T& from, std::index_sequence<I...>)
59 {
60  return {vccc::at<I, typename R::value_type>(from)...};
61 }
62 
63 } // namespace internal
64 } // namespace vccc
65 
66 # endif // VCCC_TYPE_SUPPORT_DETAIL_CONVERT_TO_HPP
std::integral_constant< bool, v > bool_constant
Definition: bool_constant.hpp:19
void void_t
Definition: void_t.hpp:19
#define ENSURES(expr, msg)
Definition: assert.hpp:23
make_same_sequence< int, 0, N > make_zero_sequence
alias of zero std::size_t sequence
Definition: sequence.hpp:50
Definition: matrix.hpp:495
Definition: directory.h:12
constexpr auto size(const C &c) -> decltype(c.size())
Definition: size.hpp:16
constexpr VCCC_INLINE_OR_STATIC detail::element_niebloid< 1 > value
Definition: key_value.hpp:35