VCCC  2024.05
VisualCamp Common C++ library
vtype_convert.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_VTYPE_CONVERT_HPP
6 # define VCCC_TYPE_SUPPORT_VTYPE_CONVERT_HPP
7 #
8 # include <algorithm>
11 # include "vccc/type_traits.hpp"
12 
13 namespace vccc {
14 
38 template<typename NewType, template<typename, int...> class CVType, typename OldType, int ...CVParams,
40 inline decltype(auto) vtype_convert(const CVType<OldType, CVParams...>& cv_type)
41 {
42  return convert_to<CVType<NewType, CVParams...>>(cv_type);
43 }
44 
45 
47 template<typename NewType, template<typename, int...> class CVType, int ...CVParams>
48 inline decltype(auto) vtype_convert(const CVType<NewType, CVParams...>& cv_type)
49 {
50  return cv_type;
51 }
52 
53 template<typename NewType, template<typename, int...> class CVType, int ...CVParams>
54 inline decltype(auto) vtype_convert(CVType<NewType, CVParams...>&& cv_type)
55 {
56  return cv_type;
57 }
59 
60 
73 template<typename NewType, typename Func, template<typename...> class Container, typename OldType, typename ...Params,
74  std::enable_if_t<is_range<Container<OldType, Params...>>::value, int> = 0>
75 decltype(auto) vtype_convert(const Container<OldType, Params...>& container, Func func)
76 {
77  Container<NewType, std::allocator<NewType>> res(container.size());
78  std::transform(std::begin(container), std::end(container), std::begin(res), func);
79  return res;
80 }
81 
82 template<typename NewType, template<typename...> class Container, typename ...Params, typename UnaryOperation,
83  std::enable_if_t<is_range<Container<NewType, Params...>>::value, int> = 0>
84 inline decltype(auto) vtype_convert(const Container<NewType, Params...>& container, UnaryOperation func)
85 {
86  return container;
87 }
88 
89 
100 template<typename NewType, template<typename...> class Container, typename OldType, typename ...Params,
101  std::enable_if_t<is_range<Container<OldType, Params...>>::value, int> = 0>
102 decltype(auto) vtype_convert(const Container<OldType, Params...>& container)
103 {
104  return vtype_convert<NewType>(container, [](auto &val) { return cast<NewType>(val); });
105 }
106 
107 
108 template<typename NewType, template<typename...> class Container, typename ...Params,
109  std::enable_if_t<is_range<Container<NewType, Params...>>::value, int> = 0>
110 inline decltype(auto) vtype_convert(const Container<NewType, Params...>& container)
111 {
112  return container;
113 }
114 
115 template<typename NewType, template<typename...> class Container, typename ...Params,
116  std::enable_if_t<is_range<Container<NewType, Params...>>::value, int> = 0>
117 inline decltype(auto) vtype_convert(Container<NewType, Params...>&& container)
118 {
119  return std::move(container);
120 }
121 
122 
134 template<typename NewType, typename OldType, std::size_t n, typename UnaryOperation,
136 constexpr decltype(auto)
137 vtype_convert(const std::array<OldType, n>& container, UnaryOperation func)
138 {
139  std::array<NewType, n> res;
140  std::transform(std::begin(container), std::end(container), std::begin(res), func);
141  return res;
142 }
143 
144 template<typename NewType, std::size_t n, typename UnaryOperation>
145 constexpr decltype(auto)
146 vtype_convert(const std::array<NewType, n>& container, UnaryOperation func)
147 {
148  std::array<NewType, n> res;
149  std::transform(std::begin(container), std::end(container), std::begin(res), func);
150  return res;
151 }
152 
153 template<typename NewType, std::size_t n, typename UnaryOperation>
154 constexpr decltype(auto)
155 vtype_convert(std::array<NewType, n>&& container, UnaryOperation func)
156 {
157  std::for_each(std::begin(container), std::end(container), func);
158  return container;
159 }
160 
161 
164 template<typename NewType, typename OldType, std::size_t n,
166 constexpr decltype(auto)
167 vtype_convert(const std::array<OldType, n>& container)
168 {
169  std::array<NewType, n> res;
170  std::transform(std::begin(container), std::end(container), std::begin(res),
171  [](auto& val) { return cast<NewType>(val); });
172  return res;
173 }
174 
175 template<typename NewType, std::size_t n>
176 constexpr inline decltype(auto)
177 vtype_convert(const std::array<NewType, n>& container)
178 {
179  return container;
180 }
181 
182 template<typename NewType, std::size_t n>
183 constexpr inline decltype(auto)
184 vtype_convert(std::array<NewType, n>&& container)
185 {
186  return std::move(container);
187 }
188 
190 
191 } // namespace vccc
192 
193 # endif // VCCC_TYPE_SUPPORT_VTYPE_CONVERT_HPP
constexpr VCCC_INLINE_OR_STATIC detail::for_each_niebloid for_each
Definition: for_each.hpp:54
constexpr VCCC_INLINE_OR_STATIC detail::transform_niebloid transform
Definition: transform.hpp:61
constexpr VCCC_INLINE_OR_STATIC detail::begin_niebloid begin
returns an iterator to the beginning of a range
Definition: begin.hpp:116
constexpr VCCC_INLINE_OR_STATIC detail::end_niebloid end
returns a sentinel indicating the end of a range
Definition: end.hpp:120
std::enable_if_t< disjunction< is_tuple_like< To >, is_tuple_like< From > >::value, To > convert_to(const From &from)
Definition: convert_to.hpp:67
decltype(auto) vtype_convert(const CVType< OldType, CVParams... > &cv_type)
returns similar opencv type, but the value_type is different
Definition: vtype_convert.hpp:40
Definition: matrix.hpp:495
Definition: directory.h:12
constexpr VCCC_INLINE_OR_STATIC detail::element_niebloid< 1 > value
Definition: key_value.hpp:35