VCCC  2024.05
VisualCamp Common C++ library
cast.hpp
Go to the documentation of this file.
1 # /*
2 # * Created by YongGyu Lee on 2021/03/17.
3 # */
4 #
5 # ifndef VCCC_TYPE_SUPPORT_CAST_HPP
6 # define VCCC_TYPE_SUPPORT_CAST_HPP
7 #
9 # include "vccc/type_traits.hpp"
10 # include <cstddef>
11 
12 namespace vccc {
13 
14 namespace internal {
15 namespace type_support {
16 
17 template<typename To, typename From, typename = void>
18 struct saturate_cast_possible :
19 # if VCCC_USE_OPENCV_FEATURES
20  public std::integral_constant<bool, are_arithmetic<std::decay_t<To>, std::decay_t<From>>::value>
21 # else
22  public std::false_type
23 # endif
24  {};
25 
26 template<typename To, typename From>
27 using saturate_cast_possible_t = typename saturate_cast_possible<To, From>::type;
28 
29 template<typename To, typename From>
30 constexpr inline
31 decltype(auto)
32 cast(std::false_type, From&& from) {
33  return static_cast<To>(std::forward<From>(from));
34 }
35 
36 template<typename To, typename From>
37 constexpr inline
38 decltype(auto)
39 cast(std::true_type, From from) {
40 # if VCCC_USE_OPENCV_FEATURES
41  return cv::saturate_cast<To>(from);
42 # else
43  cast(std::false_type{}, from);
44 # endif
45 }
46 
47 
48 } // namespace type_support
49 } // namespace internal
50 
51 template<typename ToType, typename FromType>
52 constexpr inline
53 decltype(auto)
54 cast(FromType&& from) {
55  return internal::type_support::cast<ToType>(
56  internal::type_support::saturate_cast_possible_t<ToType, FromType>{},
57  std::forward<FromType>(from));
58 }
59 
60 } // namespace vccc
61 
62 # endif // VCCC_TYPE_SUPPORT_CAST_HPP
Definition: matrix.hpp:495
Definition: directory.h:12
constexpr decltype(auto) cast(FromType &&from)
Definition: cast.hpp:54