Add translations for various sub-directories
[binutils-gdb.git] / gdbsupport / traits.h
blob62e6de7deaa9dbbceb0a6a6e500d493e0f53d4b5
1 /* Copyright (C) 2017-2024 Free Software Foundation, Inc.
3 This file is part of GDB.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 #ifndef GDBSUPPORT_TRAITS_H
19 #define GDBSUPPORT_TRAITS_H
21 #include <type_traits>
23 namespace gdb {
25 /* Implementation of the detection idiom:
27 - http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4502.pdf
28 - http://en.cppreference.com/w/cpp/experimental/is_detected
32 struct nonesuch
34 nonesuch () = delete;
35 ~nonesuch () = delete;
36 nonesuch (const nonesuch &) = delete;
37 void operator= (const nonesuch &) = delete;
40 namespace detection_detail {
41 /* Implementation of the detection idiom (negative case). */
42 template<typename Default, typename AlwaysVoid,
43 template<typename...> class Op, typename... Args>
44 struct detector
46 using value_t = std::false_type;
47 using type = Default;
50 /* Implementation of the detection idiom (positive case). */
51 template<typename Default, template<typename...> class Op, typename... Args>
52 struct detector<Default, std::void_t<Op<Args...>>, Op, Args...>
54 using value_t = std::true_type;
55 using type = Op<Args...>;
58 /* Detect whether Op<Args...> is a valid type, use Default if not. */
59 template<typename Default, template<typename...> class Op,
60 typename... Args>
61 using detected_or = detector<Default, void, Op, Args...>;
63 /* Op<Args...> if that is a valid type, otherwise Default. */
64 template<typename Default, template<typename...> class Op,
65 typename... Args>
66 using detected_or_t
67 = typename detected_or<Default, Op, Args...>::type;
69 } /* detection_detail */
71 template<template<typename...> class Op, typename... Args>
72 using is_detected
73 = typename detection_detail::detector<nonesuch, void, Op, Args...>::value_t;
75 template<template<typename...> class Op, typename... Args>
76 using detected_t
77 = typename detection_detail::detector<nonesuch, void, Op, Args...>::type;
79 template<typename Default, template<typename...> class Op, typename... Args>
80 using detected_or = detection_detail::detected_or<Default, Op, Args...>;
82 template<typename Default, template<typename...> class Op, typename... Args>
83 using detected_or_t = typename detected_or<Default, Op, Args...>::type;
85 template<typename Expected, template<typename...> class Op, typename... Args>
86 using is_detected_exact = std::is_same<Expected, detected_t<Op, Args...>>;
88 template<typename To, template<typename...> class Op, typename... Args>
89 using is_detected_convertible
90 = std::is_convertible<detected_t<Op, Args...>, To>;
92 /* A few trait helpers -- standard traits but with slightly nicer
93 names. Uppercase because "and/or", etc. are reserved keywords. */
95 template<typename Predicate>
96 using Not = std::negation<Predicate>;
98 template<typename ...T>
99 using Or = std::disjunction<T...>;
101 template<typename ...T>
102 using And = std::conjunction<T...>;
104 /* Concepts-light-like helper to make SFINAE logic easier to read. */
105 template<typename Condition>
106 using Requires = typename std::enable_if<Condition::value, void>::type;
109 template<typename T>
110 using RequireLongest = gdb::Requires<gdb::Or<std::is_same<T, LONGEST>,
111 std::is_same<T, ULONGEST>>>;
113 #endif /* GDBSUPPORT_TRAITS_H */