[gdb/testsuite] Call ldd --version in gdb.testsuite/dump-system-info.exp
[binutils-gdb.git] / gdbsupport / traits.h
blobeb97b96599048e3a75c3df2c9ef867cb11de03f4
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 COMMON_TRAITS_H
19 #define COMMON_TRAITS_H
21 #include <type_traits>
23 /* GCC does not understand __has_feature. */
24 #if !defined(__has_feature)
25 # define __has_feature(x) 0
26 #endif
28 /* HAVE_IS_TRIVIALLY_COPYABLE is defined as 1 iff
29 std::is_trivially_copyable is available. GCC only implemented it
30 in GCC 5. */
31 #if (__has_feature(is_trivially_copyable) \
32 || (defined __GNUC__ && __GNUC__ >= 5))
33 # define HAVE_IS_TRIVIALLY_COPYABLE 1
34 #endif
36 /* HAVE_IS_TRIVIALLY_CONSTRUCTIBLE is defined as 1 iff
37 std::is_trivially_constructible is available. GCC only implemented it
38 in GCC 5. */
39 #if (__has_feature(is_trivially_constructible) \
40 || (defined __GNUC__ && __GNUC__ >= 5))
41 # define HAVE_IS_TRIVIALLY_CONSTRUCTIBLE 1
42 #endif
44 namespace gdb {
46 /* Implementation of the detection idiom:
48 - http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4502.pdf
49 - http://en.cppreference.com/w/cpp/experimental/is_detected
53 struct nonesuch
55 nonesuch () = delete;
56 ~nonesuch () = delete;
57 nonesuch (const nonesuch &) = delete;
58 void operator= (const nonesuch &) = delete;
61 namespace detection_detail {
62 /* Implementation of the detection idiom (negative case). */
63 template<typename Default, typename AlwaysVoid,
64 template<typename...> class Op, typename... Args>
65 struct detector
67 using value_t = std::false_type;
68 using type = Default;
71 /* Implementation of the detection idiom (positive case). */
72 template<typename Default, template<typename...> class Op, typename... Args>
73 struct detector<Default, std::void_t<Op<Args...>>, Op, Args...>
75 using value_t = std::true_type;
76 using type = Op<Args...>;
79 /* Detect whether Op<Args...> is a valid type, use Default if not. */
80 template<typename Default, template<typename...> class Op,
81 typename... Args>
82 using detected_or = detector<Default, void, Op, Args...>;
84 /* Op<Args...> if that is a valid type, otherwise Default. */
85 template<typename Default, template<typename...> class Op,
86 typename... Args>
87 using detected_or_t
88 = typename detected_or<Default, Op, Args...>::type;
90 } /* detection_detail */
92 template<template<typename...> class Op, typename... Args>
93 using is_detected
94 = typename detection_detail::detector<nonesuch, void, Op, Args...>::value_t;
96 template<template<typename...> class Op, typename... Args>
97 using detected_t
98 = typename detection_detail::detector<nonesuch, void, Op, Args...>::type;
100 template<typename Default, template<typename...> class Op, typename... Args>
101 using detected_or = detection_detail::detected_or<Default, Op, Args...>;
103 template<typename Default, template<typename...> class Op, typename... Args>
104 using detected_or_t = typename detected_or<Default, Op, Args...>::type;
106 template<typename Expected, template<typename...> class Op, typename... Args>
107 using is_detected_exact = std::is_same<Expected, detected_t<Op, Args...>>;
109 template<typename To, template<typename...> class Op, typename... Args>
110 using is_detected_convertible
111 = std::is_convertible<detected_t<Op, Args...>, To>;
113 /* A few trait helpers, mainly stolen from libstdc++. Uppercase
114 because "and/or", etc. are reserved keywords. */
116 template<typename Predicate>
117 struct Not : public std::integral_constant<bool, !Predicate::value>
120 template<typename...>
121 struct Or;
123 template<>
124 struct Or<> : public std::false_type
127 template<typename B1>
128 struct Or<B1> : public B1
131 template<typename B1, typename B2>
132 struct Or<B1, B2>
133 : public std::conditional<B1::value, B1, B2>::type
136 template<typename B1,typename B2,typename B3, typename... Bn>
137 struct Or<B1, B2, B3, Bn...>
138 : public std::conditional<B1::value, B1, Or<B2, B3, Bn...>>::type
141 template<typename...>
142 struct And;
144 template<>
145 struct And<> : public std::true_type
148 template<typename B1>
149 struct And<B1> : public B1
152 template<typename B1, typename B2>
153 struct And<B1, B2>
154 : public std::conditional<B1::value, B2, B1>::type
157 template<typename B1, typename B2, typename B3, typename... Bn>
158 struct And<B1, B2, B3, Bn...>
159 : public std::conditional<B1::value, And<B2, B3, Bn...>, B1>::type
162 /* Concepts-light-like helper to make SFINAE logic easier to read. */
163 template<typename Condition>
164 using Requires = typename std::enable_if<Condition::value, void>::type;
167 #endif /* COMMON_TRAITS_H */