CheckType

Description

Checks whether a given data type is in the list of valid data types. If the data type is found in the list, true is returned. Otherwise, false is returned.

Prototype

bool CheckType(const DataType dtype, const std::initializer_list<DataType>& validTypes)

template <size_t N>
bool CheckType(const DataType dtype, const std::array<DataType, N>& validTypes)

bool CheckType(const DataType dtype, const std::vector<DataType>& validTypes)

Parameters

Parameter

Input/Output

Description

dtype

Input

Type of the data to check.

validTypes

Input

List of valid data types. The type is std::initializer_list<DataType>, std::array<DataType, N>, or std::vector<DataType>.

Return Value

If dtype is in the valid data type list, true is returned. Otherwise, false is returned. The three overloads have the same behavior: they apply linear search. If dtype matches the list, true is returned. If the list is empty, false is returned.

Constraints

None

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// Check whether dtype is float16 or float32. If not, a return statement is executed in advance.
void Func(const ge::DataType dtype) {
    if (!CheckType(dtype, {DT_FLOAT16, DT_FLOAT})) {
        return;
    }
    // Run the operator computation logic.
}

// Use the fixed data type list (std::array) at build time for validation.
void FuncArray(const ge::DataType dtype) {
    static constexpr std::array<ge::DataType, 2> validTypes = {DT_FLOAT16, DT_FLOAT};
    if (!CheckType(dtype, validTypes)) {
        return;
    }
    // Run the operator computation logic.
}