| 1 |
// |
| 2 |
// SPDX-License-Identifier: BSD-3-Clause |
| 3 |
// Copyright Contributors to the OpenEXR Project. |
| 4 |
// |
| 5 |
|
| 6 |
// |
| 7 |
// This file contains type traits related to or used by the Imath library. |
| 8 |
// |
| 9 |
|
| 10 |
#ifndef INCLUDED_IMATHTYPETRAITS_H |
| 11 |
#define INCLUDED_IMATHTYPETRAITS_H |
| 12 |
|
| 13 |
#include <type_traits> |
| 14 |
|
| 15 |
#include "ImathPlatform.h" |
| 16 |
|
| 17 |
IMATH_INTERNAL_NAMESPACE_HEADER_ENTER |
| 18 |
|
| 19 |
|
| 20 |
/// Define Imath::enable_if_t to be std for C++14, equivalent for C++11. |
| 21 |
#if (IMATH_CPLUSPLUS_VERSION >= 14) |
| 22 |
using std::enable_if_t; // Use C++14 std::enable_if_t |
| 23 |
#else |
| 24 |
// Define enable_if_t for C++11 |
| 25 |
template <bool B, class T = void> |
| 26 |
using enable_if_t = typename std::enable_if<B, T>::type; |
| 27 |
#endif |
| 28 |
|
| 29 |
|
| 30 |
/// An enable_if helper to be used in template parameters which results in |
| 31 |
/// much shorter symbols. |
| 32 |
#define IMATH_ENABLE_IF(...) IMATH_INTERNAL_NAMESPACE::enable_if_t<(__VA_ARGS__), int> = 0 |
| 33 |
|
| 34 |
|
| 35 |
#if IMATH_FOREIGN_VECTOR_INTEROP |
| 36 |
|
| 37 |
/// @{ |
| 38 |
/// @name Detecting interoperable types. |
| 39 |
/// |
| 40 |
/// In order to construct or assign from external "compatible" types without |
| 41 |
/// prior knowledge of their definitions, we have a few helper type traits. |
| 42 |
/// The intent of these is to allow custom linear algebra types in an |
| 43 |
/// application that have seamless conversion to and from Imath types. |
| 44 |
/// |
| 45 |
/// `has_xy<T,Base>`, `has_xyz<T,Base>`, `has_xyzw<T,Base>` detect if class |
| 46 |
/// `T` has elements `.x`, `.y`, and `.z` all of type `Base` and seems to be |
| 47 |
/// the right size to hold exactly those members and nothing more. |
| 48 |
/// |
| 49 |
/// `has_subscript<T,Base,N>` detects if class `T` can perform `T[int]` |
| 50 |
/// to yield a `Base`, and that it seems to be exactly the right size to |
| 51 |
/// hold `N` of those elements. |
| 52 |
/// |
| 53 |
/// This is not exact. It's possible that for a particular user-defined |
| 54 |
/// type, this may yield a false negative or false positive. For example: |
| 55 |
/// * A class for a 3-vector that contains an extra element of padding |
| 56 |
/// so that it will have the right size and alignment to use 4-wide |
| 57 |
/// SIMD math ops will appear to be the wrong size. |
| 58 |
/// * A `std::vector<T>` is subscriptable and might have N elements at |
| 59 |
/// runtime, but the size is dynamic and so would fail this test. |
| 60 |
/// * A foreign type may have .x, .y, .z that are not matching our base |
| 61 |
/// type but we still want it to work (with appropriate conversions). |
| 62 |
/// |
| 63 |
/// In these cases, user code may declare an exception -- for example, |
| 64 |
/// stating that `mytype` should be considered implicitly convertible to |
| 65 |
/// an Imath::V3f by subscripting: |
| 66 |
/// |
| 67 |
/// template<> |
| 68 |
/// struct Imath::has_subscript<mytype, float, 3> : public std::true_type { }; |
| 69 |
/// |
| 70 |
/// And similarly, user code may correct a potential false positive (that |
| 71 |
/// is, a `mytype` looks like it should be convertible to a V3f, but you |
| 72 |
/// don't want it to ever happen): |
| 73 |
/// |
| 74 |
/// template<typename B, int N> |
| 75 |
/// struct Imath::has_subscript<mytype, B, N> : public std::false_type { }; |
| 76 |
/// |
| 77 |
|
| 78 |
|
| 79 |
/// `has_xy<T,Base>::value` will be true if type `T` has member variables |
| 80 |
/// `.x` and `.y`, all of type `Base`, and the size of a `T` is exactly big |
| 81 |
/// enough to hold 2 Base values. |
| 82 |
template <typename T, typename Base> |
| 83 |
struct has_xy { |
| 84 |
private: |
| 85 |
typedef char Yes[1]; |
| 86 |
typedef char No[2]; |
| 87 |
|
| 88 |
// Valid only if .x, .y exist and are the right type: return a Yes. |
| 89 |
template < |
| 90 |
typename C, |
| 91 |
IMATH_ENABLE_IF (std::is_same<decltype (C {}.x), Base>::value), |
| 92 |
IMATH_ENABLE_IF (std::is_same<decltype (C {}.y), Base>::value)> |
| 93 |
static Yes& test (int); |
| 94 |
|
| 95 |
// Fallback, default to returning a No. |
| 96 |
template<typename C> static No& test(...); |
| 97 |
public: |
| 98 |
enum { value = (sizeof(test<T>(0)) == sizeof(Yes) |
| 99 |
&& sizeof(T) == 2*sizeof(Base)) |
| 100 |
}; |
| 101 |
}; |
| 102 |
|
| 103 |
|
| 104 |
/// `has_xyz<T,Base>::value` will be true if type `T` has member variables |
| 105 |
/// `.x`, `.y`, and `.z`, all of type `Base`, and the size of a `T` is |
| 106 |
/// exactly big enough to hold 3 Base values. |
| 107 |
template <typename T, typename Base> |
| 108 |
struct has_xyz { |
| 109 |
private: |
| 110 |
typedef char Yes[1]; |
| 111 |
typedef char No[2]; |
| 112 |
|
| 113 |
// Valid only if .x, .y, .z exist and are the right type: return a Yes. |
| 114 |
template < |
| 115 |
typename C, |
| 116 |
IMATH_ENABLE_IF (std::is_same<decltype (C {}.x), Base>::value), |
| 117 |
IMATH_ENABLE_IF (std::is_same<decltype (C {}.y), Base>::value), |
| 118 |
IMATH_ENABLE_IF (std::is_same<decltype (C {}.z), Base>::value)> |
| 119 |
static Yes& test (int); |
| 120 |
|
| 121 |
// Fallback, default to returning a No. |
| 122 |
template<typename C> static No& test(...); |
| 123 |
public: |
| 124 |
enum { value = (sizeof(test<T>(0)) == sizeof(Yes) |
| 125 |
&& sizeof(T) == 3*sizeof(Base)) |
| 126 |
}; |
| 127 |
}; |
| 128 |
|
| 129 |
|
| 130 |
/// `has_xyzw<T,Base>::value` will be true if type `T` has member variables |
| 131 |
/// `.x`, `.y`, `.z`, and `.w`, all of type `Base`, and the size of a `T` is |
| 132 |
/// exactly big enough to hold 4 Base values. |
| 133 |
template <typename T, typename Base> |
| 134 |
struct has_xyzw { |
| 135 |
private: |
| 136 |
typedef char Yes[1]; |
| 137 |
typedef char No[2]; |
| 138 |
|
| 139 |
// Valid only if .x, .y, .z, .w exist and are the right type: return a Yes. |
| 140 |
template < |
| 141 |
typename C, |
| 142 |
IMATH_ENABLE_IF (std::is_same<decltype (C {}.x), Base>::value), |
| 143 |
IMATH_ENABLE_IF (std::is_same<decltype (C {}.y), Base>::value), |
| 144 |
IMATH_ENABLE_IF (std::is_same<decltype (C {}.z), Base>::value), |
| 145 |
IMATH_ENABLE_IF (std::is_same<decltype (C {}.w), Base>::value)> |
| 146 |
static Yes& test (int); |
| 147 |
|
| 148 |
// Fallback, default to returning a No. |
| 149 |
template<typename C> static No& test(...); |
| 150 |
public: |
| 151 |
enum { value = (sizeof(test<T>(0)) == sizeof(Yes) |
| 152 |
&& sizeof(T) == 4*sizeof(Base)) |
| 153 |
}; |
| 154 |
}; |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
/// `has_subscript<T,Base,Nelem>::value` will be true if type `T` has |
| 159 |
/// subscripting syntax, a `T[int]` returns a `Base`, and the size of a `T` |
| 160 |
/// is exactly big enough to hold `Nelem` `Base` values. |
| 161 |
template <typename T, typename Base, int Nelem> |
| 162 |
struct has_subscript { |
| 163 |
private: |
| 164 |
typedef char Yes[1]; |
| 165 |
typedef char No[2]; |
| 166 |
|
| 167 |
// Valid only if T[] is possible and is the right type: return a Yes. |
| 168 |
template < |
| 169 |
typename C, |
| 170 |
IMATH_ENABLE_IF (std::is_same< |
| 171 |
typename std::decay<decltype (C {}[0])>::type, |
| 172 |
Base>::value)> |
| 173 |
static Yes& test (int); |
| 174 |
|
| 175 |
// Fallback, default to returning a No. |
| 176 |
template<typename C> static No& test(...); |
| 177 |
public: |
| 178 |
enum { value = (sizeof(test<T>(0)) == sizeof(Yes) |
| 179 |
&& sizeof(T) == Nelem*sizeof(Base)) |
| 180 |
}; |
| 181 |
}; |
| 182 |
|
| 183 |
|
| 184 |
/// C arrays of just the right length also are qualified for has_subscript. |
| 185 |
template<typename Base, int Nelem> |
| 186 |
struct has_subscript<Base[Nelem], Base, Nelem> : public std::true_type { }; |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
/// `has_double_subscript<T,Base,Rows,Cols>::value` will be true if type `T` |
| 191 |
/// has 2-level subscripting syntax, a `T[int][int]` returns a `Base`, and |
| 192 |
/// the size of a `T` is exactly big enough to hold `R*C` `Base` values. |
| 193 |
template <typename T, typename Base, int Rows, int Cols> |
| 194 |
struct has_double_subscript { |
| 195 |
private: |
| 196 |
typedef char Yes[1]; |
| 197 |
typedef char No[2]; |
| 198 |
|
| 199 |
// Valid only if T[][] is possible and is the right type: return a Yes. |
| 200 |
template < |
| 201 |
typename C, |
| 202 |
IMATH_ENABLE_IF (std::is_same< |
| 203 |
typename std::decay<decltype (C {}[0][0])>::type, |
| 204 |
Base>::value)> |
| 205 |
static Yes& test (int); |
| 206 |
|
| 207 |
// Fallback, default to returning a No. |
| 208 |
template<typename C> static No& test(...); |
| 209 |
public: |
| 210 |
enum { value = (sizeof(test<T>(0)) == sizeof(Yes) |
| 211 |
&& sizeof(T) == (Rows*Cols)*sizeof(Base)) |
| 212 |
}; |
| 213 |
}; |
| 214 |
|
| 215 |
|
| 216 |
/// C arrays of just the right length also are qualified for has_double_subscript. |
| 217 |
template<typename Base, int Rows, int Cols> |
| 218 |
struct has_double_subscript<Base[Rows][Cols], Base, Rows, Cols> : public std::true_type { }; |
| 219 |
|
| 220 |
/// @} |
| 221 |
|
| 222 |
#endif |
| 223 |
|
| 224 |
IMATH_INTERNAL_NAMESPACE_HEADER_EXIT |
| 225 |
|
| 226 |
#endif // INCLUDED_IMATHTYPETRAITS_H |