change type naming style
[kazan.git] / src / util / is_swappable.h
1 /*
2 * Copyright 2017 Jacob Lifshay
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 *
22 */
23
24 #ifndef UTIL_IS_SWAPPABLE_H_
25 #define UTIL_IS_SWAPPABLE_H_
26
27 #include <utility>
28 #include <type_traits>
29 #include "is_referenceable.h"
30
31 namespace vulkan_cpu_util_is_swappable_unrelated_namespace
32 {
33 using std::swap;
34 template <typename T, typename U, bool Is_Void = std::is_void<T>::value || std::is_void<T>::value>
35 class Is_swappable_with
36 {
37 private:
38 template <typename L,
39 typename R,
40 typename = decltype(swap(std::declval<L>(), std::declval<R>()))>
41 static void fn(int);
42 template <typename L, typename R>
43 static char fn(...);
44
45 public:
46 static constexpr bool value =
47 std::is_void<decltype(fn<T, U>(0))>::value && std::is_void<decltype(fn<U, T>(0))>::value;
48 };
49
50 template <typename T, typename U>
51 class Is_swappable_with<T, U, true>
52 {
53 public:
54 static constexpr bool value = false;
55 };
56
57 template <typename T, typename U, bool Is_Swappable_With = Is_swappable_with<T, U>::value>
58 struct Is_nothrow_swappable_with
59 {
60 static constexpr bool value = noexcept(swap(std::declval<T>(), std::declval<U>()))
61 && noexcept(swap(std::declval<U>(), std::declval<T>()));
62 };
63
64 template <typename T, typename U>
65 struct Is_nothrow_swappable_with<T, U, false>
66 {
67 static constexpr bool value = false;
68 };
69 }
70
71 namespace vulkan_cpu
72 {
73 namespace util
74 {
75 template <typename T, typename U>
76 struct is_swappable_with
77 : public std::integral_constant<bool,
78 vulkan_cpu_util_is_swappable_unrelated_namespace::
79 Is_swappable_with<T, U>::value>
80 {
81 };
82
83 template <typename T, typename U>
84 constexpr bool is_swappable_with_v = is_swappable_with<T, U>::value;
85
86 template <typename T, typename U>
87 struct is_nothrow_swappable_with
88 : public std::integral_constant<bool,
89 vulkan_cpu_util_is_swappable_unrelated_namespace::
90 Is_nothrow_swappable_with<T, U>::value>
91 {
92 };
93
94 template <typename T, typename U>
95 constexpr bool is_nothrow_swappable_with_v = is_nothrow_swappable_with<T, U>::value;
96
97 namespace detail
98 {
99 template <typename T, bool Is_Referenceable = is_referenceable<T>::value>
100 struct is_swappable_helper
101 {
102 static constexpr bool value = is_swappable_with<T &, T &>::value;
103 };
104
105 template <typename T>
106 struct is_swappable_helper<T, false>
107 {
108 static constexpr bool value = false;
109 };
110
111 template <typename T, bool Is_Referenceable = is_referenceable<T>::value>
112 struct is_nothrow_swappable_helper
113 {
114 static constexpr bool value = is_nothrow_swappable_with<T &, T &>::value;
115 };
116
117 template <typename T>
118 struct is_nothrow_swappable_helper<T, false>
119 {
120 static constexpr bool value = false;
121 };
122 }
123
124 template <typename T>
125 struct is_swappable : public std::integral_constant<bool, detail::is_swappable_helper<T>::value>
126 {
127 };
128
129 template <typename T>
130 constexpr bool is_swappable_v = is_swappable<T>::value;
131
132 template <typename T>
133 struct is_nothrow_swappable
134 : public std::integral_constant<bool, detail::is_nothrow_swappable_helper<T>::value>
135 {
136 };
137
138 template <typename T>
139 constexpr bool is_nothrow_swappable_v = is_nothrow_swappable<T>::value;
140 }
141 }
142
143 #endif /* UTIL_IS_SWAPPABLE_H_ */