Type Traits

group NVCV_CPP_CUDATOOLS_TYPETRAITS

Metastruct to define type traits for regular C types and CUDA built-in vector (or compound) types.

CUDA built-in vector types are also called compound types. The number of components in the metastruct is zero for regular C types and between 1 and 4 for CUDA compound types. On the flip side, the number of elements is between 1 and 4 for regular C types and CUDA compound types, i.e. the number of elements may be used regardless of the type. The base type of a CUDA compound type is the type of each of its components, for instance uchar4 has 4 elements of base type unsigned char. Type traits also provide the name of the type, each element minimum and maximum representable values.

using BaseType = typename nvcv::cuda::TypeTraits<T>::base_type;
int nc = nvcv::cuda::TypeTraits<T>::components;
int ne = nvcv::cuda::TypeTraits<T>::elements;
const char *name = nvcv::cuda::TypeTraits<T>::name;
T min = nvcv::cuda::TypeTraits<T>::min;
T max = nvcv::cuda::TypeTraits<T>::max;
tparam T:

Type to get traits from.

Typedefs

template<bool B>
using Require = std::enable_if_t<B>
template<class T, class = Require<HasTypeTraits<T>>>
using BaseType = typename TypeTraits<T>::base_type

Metatype to get the base type of a CUDA compound types.

using DataType = ...;
using ChannelType = nvcv::cuda::BaseType<DataType>;

Note

This is identity for regular C types.

Template Parameters:

T – Type to get the base type from.

template<class T, int C, class = Require<HasTypeTraits<T>>>
using MakeType = detail::MakeType_t<T, C>

Metatype to make a type from a base type and number of components.

When number of components is zero, it yields the identity (regular C) type, and when it is between 1 and 4 it yields the CUDA compound type.

using RGB8Type = MakeType<unsigned char, 3>; // yields uchar3

Note

Note that T=char might yield uchar1..4 types when char is equal unsigned char, i.e. CHAR_MIN == 0.

Template Parameters:
  • T – Base type to make the type from.

  • C – Number of components to make the type.

template<class BT, class T, class = Require<HasTypeTraits<BT, T>>>
using ConvertBaseTypeTo = detail::ConvertBaseTypeTo_t<BT, T>

Metatype to convert the base type of a type.

The base type of target type T is replaced to be BT.

using DataType = ...;
using FloatDataType = ConvertBaseTypeTo<float, DataType>; // yields float1..4
Template Parameters:
  • BT – Base type to use in the conversion.

  • T – Target type to convert its base type.

Functions

template<typename T, typename RT = detail::CopyConstness_t<T, std::conditional_t<IsCompound<T>, BaseType<T>, T>>, class = Require<HasTypeTraits<T>>> __host__ __device__ RT & GetElement (T &v, int eidx)

Metafunction to get an element by reference from a given value reference.

The value may be of CUDA compound type with 1 to 4 elements, where the corresponding element index is 0 to 3, and the return is a reference to the element with the base type of the compound type, copying the constness (that is the return reference is constant if the input value is constant). The value may be a regular C type, in which case the element index is ignored and the identity is returned. It is a requirement of the GetElement function that the type T has type traits.

using PixelRGB8Type = MakeType<unsigned char, 3>;
PixelRGB8Type pix = ...;
auto green = GetElement(pix, 1); // yields unsigned char
Template Parameters:

T – Type of the value to get the element from.

Parameters:
  • v[in] Value of type T to get an element from.

  • eidx[in] Element index in [0, 3] inside the compound value to get the reference from. This element index is ignored in case the value is not of a CUDA compound type.

Returns:

The reference of the value’s element.

template<typename T, class = Require<HasTypeTraits<T>>> __host__ __device__ T SetAll (BaseType< T > x)

Metafunction to set all elements to the same value.

Set all elements to the value x passed as argument. For instance, an int3 can have all its elements set to zero by calling SetAll and passing int3 as template argument and zero as argument (see example below). Another way to set all elements to a value is by using the type of the argument as base type and passing the number of channels of the return type (see example below).

auto idx = SetAll<int3>(0); // sets to zero all elements of an int3 index idx: {0, 0, 0}
unsigned char ch = 127;
auto pix = SetAll<4>(ch); // sets all elements of an uchar3 pixel pix: {127, 127, 127, 127}
Template Parameters:
  • T – Type to be returned with all elements set to the given value x.

  • N – Number of components as a second option instead of passing the type T.

Parameters:

x[in] Value to set all elements to.

Returns:

The object of type T with all elements set to x.

template<int N, typename BT, typename RT = MakeType<BT, N>, class = Require<HasTypeTraits<BT>>> __host__ __device__ RT SetAll (BT x)
template<class T, class = Require<HasTypeTraits<T>>> __host__ const char * GetTypeName ()

Metafunction to get the name of a type.

Unfortunately typeid().name() in C/C++ typeinfo yields different names depending on the platform. This function returns the name of the type resembling the CUDA compound type, that may be useful for debug printing.

std::cout << GetTypeName<DataType>();
Template Parameters:

T – Type to get the name from.

Returns:

String with the name of the type.

Variables

template<typename ...Ts>
constexpr bool HasTypeTraits = (detail::HasTypeTraits_t<Ts>::value && ...)
template<class T, class = Require<HasTypeTraits<T>>>
constexpr bool IsCompound = TypeTraits<T>::components >= 1
template<typename T, int N, class = Require<HasTypeTraits<T>>>
constexpr bool HasEnoughComponents = N <= TypeTraits<T>::components
template<class T, class = Require<HasTypeTraits<T>>>
constexpr int NumComponents = TypeTraits<T>::components

Metavariable to get the number of components of a type.

using DataType = ...;
int nc = nvcv::cuda::NumComponents<DataType>;

Note

This is zero for regular C types.

Template Parameters:

T – Type to get the number of components from.

template<class T, class = Require<HasTypeTraits<T>>>
constexpr int NumElements = TypeTraits<T>::elements

Metavariable to get the number of elements of a type.

using DataType = ...;
for (int e = 0; e < nvcv::cuda::NumElements<DataType>; ++e)
    // ...

Note

This is one for regular C types and one to four for CUDA compound types.

Template Parameters:

T – Type to get the number of elements from.

template<typename T, class = Require<HasTypeTraits<T>>>
constexpr BaseType<T> Lowest = std::is_floating_point_v<BaseType<T>> ? -TypeTraits<T>::max : TypeTraits<T>::min