All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
meta.h
1//
2// Copyright 2023 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7
8#ifndef PXR_BASE_TF_META_H
9#define PXR_BASE_TF_META_H
10
11#include "pxr/pxr.h"
12
13#include <cstddef>
14#include <tuple>
15#include <type_traits>
16
17// Some small metaprogramming utilities.
18
19PXR_NAMESPACE_OPEN_SCOPE
20
21// Simple compile-time type list.
22template <class... Args> struct TfMetaList {};
23
24// Helper for TfMetaApply.
25template<template <class...> class Cls, class List>
26struct Tf_MetaApplyImpl;
27
28template<template <class...> class Cls, class... Args>
29struct Tf_MetaApplyImpl<Cls, TfMetaList<Args...>>
30{
31 using Type = Cls<Args...>;
32};
33
34// Apply \p TypeList<Args...> to class template \p Cls, producing Cls<Args...>
35template <template <class...> class Cls, class TypeList>
36using TfMetaApply = typename Tf_MetaApplyImpl<Cls, TypeList>::Type;
37
38// TfMetaHead<A1, A2, ... An> -> A1
39template <class Head, class...>
40using TfMetaHead = Head;
41
42// TfMetaTail<A1, A2, ... An> -> TfMetaList<A2, ... An>.
43template <class Head, class... Tail>
44using TfMetaTail = TfMetaList<Tail...>;
45
46// TfMetaDecay<A1, A2, ... An> ->
47// TfMetaList<std::decay_t<A1>, ... std::decay_t<An>>
48template <class... Ts>
49using TfMetaDecay = TfMetaList<std::decay_t<Ts>...>;
50
51// TfMetaLength produces an integral_constant<size_t, N> where N is the number
52// of \p Xs.
53template <class... Xs>
54using TfMetaLength = std::integral_constant<size_t, sizeof...(Xs)>;
55
56// Lighter-weight compile-time conditional type selection implementation.
57template <bool Condition>
58struct Tf_ConditionalImpl {
59 template <class T, class>
60 using Type = T;
61};
62
63template <>
64struct Tf_ConditionalImpl<false> {
65 template <class, class F>
66 using Type = F;
67};
68
69// This is a bit lighter weight at compile time than std::conditional because it
70// instantiates a separate template for the condition from the selector.
71template <bool Cond, class T, class F>
72using TfConditionalType =
73 typename Tf_ConditionalImpl<Cond>::template Type<T, F>;
74
75PXR_NAMESPACE_CLOSE_SCOPE
76
77#endif // PXR_BASE_TF_META_H