openPMD-api
TypeTraits.hpp
1 /* Copyright 2022 Franz Poeschel
2  *
3  * This file is part of openPMD-api.
4  *
5  * openPMD-api is free software: you can redistribute it and/or modify
6  * it under the terms of of either the GNU General Public License or
7  * the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * openPMD-api is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License and the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * and the GNU Lesser General Public License along with openPMD-api.
19  * If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #pragma once
23 
24 #include "openPMD/auxiliary/UniquePtr.hpp"
25 
26 #include <array>
27 #include <complex>
28 #include <cstddef> // size_t
29 #include <memory>
30 #include <variant>
31 #include <vector>
32 
33 namespace openPMD::auxiliary
34 {
35 namespace detail
36 {
37  template <typename>
38  struct IsVector
39  {
40  static constexpr bool value = false;
41  };
42 
43  template <typename T>
44  struct IsVector<std::vector<T>>
45  {
46  static constexpr bool value = true;
47  };
48 
49  template <typename>
50  struct IsArray
51  {
52  static constexpr bool value = false;
53  };
54 
55  template <typename T, size_t n>
56  struct IsArray<std::array<T, n>>
57  {
58  static constexpr bool value = true;
59  };
60 
61  template <typename>
62  struct IsComplex
63  {
64  static constexpr bool value = false;
65  };
66 
67  template <typename T>
68  struct IsComplex<std::complex<T>>
69  {
70  static constexpr bool value = true;
71  };
72 
73  template <typename T>
74  struct IsPointer
75  {
76  constexpr static bool value = false;
77  };
78 
79  template <typename T>
80  struct IsPointer<T *>
81  {
82  constexpr static bool value = true;
83  using type = T;
84  };
85 
86  template <typename T>
87  struct IsPointer<std::shared_ptr<T>>
88  {
89  constexpr static bool value = true;
90  using type = T;
91  };
92 
93  template <typename T, typename Del>
94  struct IsPointer<std::unique_ptr<T, Del>>
95  {
96  constexpr static bool value = true;
97  using type = T;
98  };
99 
100  template <typename T>
102  {
103  constexpr static bool value = true;
104  using type = T;
105  };
106 
107  template <typename>
108  struct IsChar
109  {
110  constexpr static bool value = false;
111  };
112  template <>
113  struct IsChar<char>
114  {
115  constexpr static bool value = true;
116  };
117  template <>
118  struct IsChar<signed char>
119  {
120  constexpr static bool value = true;
121  };
122  template <>
123  struct IsChar<unsigned char>
124  {
125  constexpr static bool value = true;
126  };
127 } // namespace detail
128 
129 template <typename T>
130 inline constexpr bool IsVector_v = detail::IsVector<T>::value;
131 
132 template <typename T>
133 inline constexpr bool IsArray_v = detail::IsArray<T>::value;
134 
135 template <typename T>
136 inline constexpr bool IsPointer_v = detail::IsPointer<T>::value;
137 
138 template <typename T>
139 using IsPointer_t = typename detail::IsPointer<T>::type;
140 
141 template <typename C>
142 inline constexpr bool IsChar_v = detail::IsChar<C>::value;
143 
152 template <typename T>
153 inline constexpr bool IsContiguousContainer_v = IsVector_v<T> || IsArray_v<T>;
154 
155 template <typename T>
156 inline constexpr bool IsComplex_v = detail::IsComplex<T>::value;
157 
158 namespace
159 {
160  // see https://en.cppreference.com/w/cpp/language/if
161  template <typename>
162  inline constexpr bool dependent_false_v = false;
163 } // namespace
164 
165 namespace detail
166 {
168  {
169  template <typename T>
170  using type = std::shared_ptr<T>;
171  };
172 
173  template <typename...>
175 
176  template <typename first_type, typename... other_types>
177  struct append_to_variant<first_type, std::variant<other_types...>>
178  {
179  using type = std::variant<first_type, other_types...>;
180  };
181 
182  template <typename...>
183  struct map_variant;
184 
185  template <typename F, typename first_type, typename... other_types>
186  struct map_variant<F, std::variant<first_type, other_types...>>
187  {
188  using type = typename append_to_variant<
189  typename F::template type<first_type>,
190  typename map_variant<F, std::variant<other_types...>>::type>::type;
191  };
192 
193  template <typename F>
194  struct map_variant<F, std::variant<>>
195  {
196  using type = std::variant<>;
197  };
198 } // namespace detail
199 
200 } // namespace openPMD::auxiliary
Unique Pointer class that uses a dynamic destructor type.
Definition: UniquePtr.hpp:80
Definition: TypeTraits.hpp:51
Definition: TypeTraits.hpp:109
Definition: TypeTraits.hpp:63
Definition: TypeTraits.hpp:75
Definition: TypeTraits.hpp:39
Definition: TypeTraits.hpp:174
Definition: TypeTraits.hpp:168
Definition: TypeTraits.hpp:183