Loading...
Searching...
No Matches
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
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
20PXR_NAMESPACE_OPEN_SCOPE
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.
26template <typename, typename = void>
27constexpr bool Vdf_IsEqualityComparableHelper = false;
28
29// Evaluates to true if there is an equality operator defined for type T.
30template <typename T>
31constexpr bool Vdf_IsEqualityComparableHelper<
32 T, std::void_t<decltype(std::declval<T>() == std::declval<T>())>> = true;
33
34
48template <typename T>
50 Vdf_IsEqualityComparableHelper<T>;
51
52// std::vector<T> is equality comparable iff T is equality comparable.
53template <class T, class Allocator>
54constexpr 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.
59template <class T1, class T2>
60constexpr 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.
65template <class Key, class T, class Compare, class Allocator>
66constexpr 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.
72template <class Key, class T, class Hash, class KeyEqual, class Allocator>
73constexpr 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.
83template <typename, bool>
84constexpr bool Vdf_AndTypeIsSmall = false;
85
86// Specialization that performs the size check when the second template
87// parameter is true.
88template <typename T>
89constexpr bool Vdf_AndTypeIsSmall<T, true> = sizeof(T) <= sizeof(void*);
90
103template <typename T>
104using VdfByValueOrConstRef = typename std::conditional_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
110PXR_NAMESPACE_CLOSE_SCOPE
111
112#endif
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
Template that evaluates to either T or const T & depending on whether T best be passed as value or co...
Definition: traits.h:108
constexpr bool VdfIsEqualityComparable
Variable template that returns true if equality comparison is a valid operation for type T.
Definition: traits.h:49