blob: 6fcb4bdb41452664aab37a0c3d137620bd691506 [file] [log] [blame]
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef AAPT_UTIL_TYPETRAITS_H
18#define AAPT_UTIL_TYPETRAITS_H
19
20#include <type_traits>
21
22namespace aapt {
23
Adam Lesinskicacb28f2016-10-19 12:18:14 -070024#define DEFINE_HAS_BINARY_OP_TRAIT(name, op) \
25 template <typename T, typename U> \
26 struct name { \
27 template <typename V, typename W> \
28 static constexpr decltype(std::declval<V>() op std::declval<W>(), bool()) \
29 test(int) { \
30 return true; \
31 } \
32 template <typename V, typename W> \
33 static constexpr bool test(...) { \
34 return false; \
35 } \
36 static constexpr bool value = test<T, U>(int()); \
37 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080038
39DEFINE_HAS_BINARY_OP_TRAIT(has_eq_op, ==);
40DEFINE_HAS_BINARY_OP_TRAIT(has_lt_op, <);
Adam Lesinski5b7337f2017-06-26 14:57:22 -070041DEFINE_HAS_BINARY_OP_TRAIT(has_lte_op, <=);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080042
43/**
44 * Type trait that checks if two types can be equated (==) and compared (<).
45 */
46template <typename T, typename U>
47struct is_comparable {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 static constexpr bool value =
49 has_eq_op<T, U>::value && has_lt_op<T, U>::value;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080050};
51
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052} // namespace aapt
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080053
54#endif /* AAPT_UTIL_TYPETRAITS_H */