HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
traits.h
Go to the documentation of this file.
1 //
2 // Copyright 2025 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_EXEC_VDF_TRAITS_H
8 #define PXR_EXEC_VDF_TRAITS_H
9 
10 /// \file
11 
12 #include "pxr/pxr.h"
13 
14 #include <map>
15 #include <type_traits>
16 #include <unordered_map>
17 #include <utility>
18 #include <vector>
19 
21 
22 // Helper template for determining whether or not equality comparison is a
23 // valid operation for a given type.
24 //
25 // Primary template, evaluates to false.
26 template <typename, typename = void>
27 constexpr bool Vdf_IsEqualityComparableHelper = false;
28 
29 // Evaluates to true if there is an equality operator defined for type T.
30 template <typename T>
31 constexpr bool Vdf_IsEqualityComparableHelper<
32  T, std::void_t<decltype(std::declval<T>() == std::declval<T>())>> = true;
33 
34 
35 /// Variable template that returns `true` if equality comparison is a valid
36 /// operation for type \p T.
37 ///
38 /// For compound types, specializations must be provided along the lines of the
39 /// following example:
40 ///
41 /// ```cpp
42 /// // ContainerType<T> is equality comparable iff T is equality comparable.
43 /// template <typename T>
44 /// constexpr bool VdfIsEqualityComparable<ContainerType<T>> =
45 /// VdfIsEqualityComparable<T>;
46 /// ```
47 ///
48 template <typename T>
49 constexpr bool VdfIsEqualityComparable =
50  Vdf_IsEqualityComparableHelper<T>;
51 
52 // std::vector<T> is equality comparable iff T is equality comparable.
53 template <class T, class Allocator>
54 constexpr bool VdfIsEqualityComparable<std::vector<T, Allocator>> =
55  VdfIsEqualityComparable<T>;
56 
57 // std::pair<T1, T2> is equality comparable iff T1 and T2 are equality
58 // comparable.
59 template <class T1, class T2>
60 constexpr bool VdfIsEqualityComparable<std::pair<T1, T2>> =
61  VdfIsEqualityComparable<T1> && VdfIsEqualityComparable<T2>;
62 
63 // std::map<Key, T> is equality comparable iff Key and T are equality
64 // comparable.
65 template <class Key, class T, class Compare, class Allocator>
66 constexpr bool VdfIsEqualityComparable<
67  std::map<Key, T, Compare, Allocator>> =
68  VdfIsEqualityComparable<Key> && VdfIsEqualityComparable<T>;
69 
70 // std::unordered_map<Key, T> is equality comparable iff Key and T are equality
71 // comparable.
72 template <class Key, class T, class Hash, class KeyEqual, class Allocator>
73 constexpr bool VdfIsEqualityComparable<
74  std::unordered_map<Key, T, Hash, KeyEqual, Allocator>> =
75  VdfIsEqualityComparable<Key> && VdfIsEqualityComparable<T>;
76 
77 
78 // Helper template that returns if a type is small, but only performs the size
79 // check if the second template parameter is true. This allows us to prevent
80 // invoking sizeof(T) when T is forward declared.
81 //
82 // Primary template evaluates to false.
83 template <typename, bool>
84 constexpr bool Vdf_AndTypeIsSmall = false;
85 
86 // Specialization that performs the size check when the second template
87 // parameter is true.
88 template <typename T>
89 constexpr bool Vdf_AndTypeIsSmall<T, true> = sizeof(T) <= sizeof(void*);
90 
91 /// Template that evaluates to either `T` or `const T &` depending on whether
92 /// \p T best be passed as value or const reference.
93 ///
94 /// The heuristic here is as follows:
95 /// * `T` if \p T is a ptr, arithmetic type, or enum, and also <= size of a ptr
96 /// * `const T &` in all other cases
97 ///
98 /// Note that this heuristic is a best guess given a generic type `T`, but if
99 /// the type is known the call site may be able to make a more informed decision
100 /// on how to pass parameters and return values without the use of this
101 /// facility.
102 ///
103 template <typename T>
105  std::is_pointer_v<T> ||
106  Vdf_AndTypeIsSmall<T, std::is_arithmetic_v<T>> ||
107  Vdf_AndTypeIsSmall<T, std::is_enum_v<T>>,
108  T, const T &>;
109 
111 
112 #endif
typename std::conditional< B, T, F >::type conditional_t
Definition: core.h:266
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
PXR_NAMESPACE_OPEN_SCOPE constexpr bool Vdf_IsEqualityComparableHelper
Definition: traits.h:27
constexpr bool Vdf_AndTypeIsSmall
Definition: traits.h:84
constexpr bool VdfIsEqualityComparable
Definition: traits.h:49
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
typename std::conditional_t< std::is_pointer_v< T >||Vdf_AndTypeIsSmall< T, std::is_arithmetic_v< T >>||Vdf_AndTypeIsSmall< T, std::is_enum_v< T >>, T, const T & > VdfByValueOrConstRef
Definition: traits.h:108
constexpr bool Vdf_AndTypeIsSmall< T, true >
Definition: traits.h:89