Compute Graph Framework SDK Reference  5.22
EnumDescriptor.hpp
Go to the documentation of this file.
1
2//
3// Notice
4// ALL NVIDIA DESIGN SPECIFICATIONS AND CODE ("MATERIALS") ARE PROVIDED "AS IS" NVIDIA MAKES
5// NO REPRESENTATIONS, WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
6// THE MATERIALS, AND EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTIES OF NONINFRINGEMENT,
7// MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8//
9// NVIDIA CORPORATION & AFFILIATES assumes no responsibility for the consequences of use of such
10// information or for any infringement of patents or other rights of third parties that may
11// result from its use. No license is granted by implication or otherwise under any patent
12// or patent rights of NVIDIA CORPORATION & AFFILIATES. No third party distribution is allowed unless
13// expressly authorized by NVIDIA. Details are subject to change without notice.
14// This code supersedes and replaces all information previously supplied.
15// NVIDIA CORPORATION & AFFILIATES products are not authorized for use as critical
16// components in life support devices or systems without express written approval of
17// NVIDIA CORPORATION & AFFILIATES.
18//
19// SPDX-FileCopyrightText: Copyright (c) 2021-2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
20// SPDX-License-Identifier: LicenseRef-NvidiaProprietary
21//
22// NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
23// property and proprietary rights in and to this material, related
24// documentation and any modifications thereto. Any use, reproduction,
25// disclosure or distribution of this material and related documentation
26// without an express license agreement from NVIDIA CORPORATION or
27// its affiliates is strictly prohibited.
28//
30
31#ifndef DW_FRAMEWORK_ENUMDESCRIPTOR_HPP_
32#define DW_FRAMEWORK_ENUMDESCRIPTOR_HPP_
33
34#include <dw/core/base/Status.h>
35#include <dwshared/dwfoundation/dw/core/container/StringView.hpp>
36
37#include <dwcgf/Exception.hpp>
38
39#include <array>
40#include <utility>
41#include <type_traits>
42
43#define DW_ENUMERATOR_NAME_STRING_VIEW(NAME_STR) \
44 dw::core::StringView { NAME_STR }
54#define DW_DESCRIBE_ENUMERATOR(NAME) describeEnumerator(DW_ENUMERATOR_NAME_STRING_VIEW(#NAME), EnumT::NAME)
62#define DW_DESCRIBE_C_ENUMERATOR(NAME) describeEnumerator(DW_ENUMERATOR_NAME_STRING_VIEW(#NAME), (NAME))
63
64namespace dw
65{
66namespace framework
67{
68
69// API to declare the mapping of a concrete enum.
70
71template <typename EnumT, size_t NumberOfEnumerators>
72using EnumDescriptionReturnType = std::array<std::pair<dw::core::StringView, EnumT>, NumberOfEnumerators>;
73
74template <typename EnumT>
76{
77 static_assert(std::is_enum<EnumT>::value, "EnumT must be an enumeration type");
78
87 // coverity[autosar_cpp14_a2_10_5_violation]
89 {
90 static_assert(sizeof(EnumT) == 0, "Needs specialization for specific enum type");
91 constexpr const size_t numberOfEnumerators = 0;
93 return ret;
94 }
95};
96
97// API needed to declare the enumerators of an enum.
98
109template <typename EnumT, typename... Args>
110// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
111constexpr auto describeEnumeratorCollection(Args const&&... args) -> std::array<std::pair<dw::core::StringView, EnumT>, sizeof...(Args)>
112{
113 return std::array<std::pair<dw::core::StringView, EnumT>, sizeof...(Args)>{std::forward<const Args>(args)...};
114}
115
128template <typename EnumT>
129// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
130constexpr auto describeEnumerator(dw::core::StringView&& name, EnumT value) -> std::pair<dw::core::StringView, EnumT>
131{
132 return std::make_pair(std::move(name), value);
133}
134
142template <typename EnumT>
143// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
144auto mapEnumNameToValue(dw::core::StringView const& name) -> EnumT
145{
146 // coverity[autosar_cpp14_a8_5_2_violation] FP: nvbugs/3904083
147 const auto pairs = EnumDescription<EnumT>::get();
148 for (const auto& pair : pairs)
149 {
150 if (pair.first == name)
151 {
152 return pair.second;
153 }
154 }
155 // coverity[autosar_cpp14_a18_5_8_violation] FP: nvbugs/3498833
156 FixedString<4096> validNames{};
157 for (const auto& pair : pairs)
158 {
159 validNames << dw::core::StringView{" "} << pair.first;
160 }
161 throw ExceptionWithStatus(DW_INVALID_ARGUMENT, "Invalid enumerator name '", name, "' for enum type ", typeid(EnumT).name(), ", valid names:", validNames);
162}
163
171template <typename EnumT>
172// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
173dw::core::StringView mapEnumValueToName(EnumT value)
174{
175 // coverity[autosar_cpp14_a8_5_2_violation] FP: nvbugs/3904083
176 const auto pairs = EnumDescription<EnumT>::get();
177 for (const auto& pair : pairs)
178 {
179 if (pair.second == value)
180 {
181 return pair.first;
182 }
183 }
184 throw ExceptionWithStatus(DW_INVALID_ARGUMENT, "The enumerator value is invalid: ", typeid(EnumT).name(), " ", static_cast<std::underlying_type_t<EnumT>>(value));
185}
186
187} // namespace framework
188} // namespace dw
189
190#endif // DW_FRAMEWORK_ENUMDESCRIPTOR_HPP_
dw::core::StringView mapEnumValueToName(EnumT value)
Get the enumerator name based on the value.
constexpr auto describeEnumeratorCollection(Args const &&... args) -> std::array< std::pair< dw::core::StringView, EnumT >, sizeof...(Args)>
Describe the enumerators.
constexpr auto describeEnumerator(dw::core::StringView &&name, EnumT value) -> std::pair< dw::core::StringView, EnumT >
Describe an enumerator.
auto mapEnumNameToValue(dw::core::StringView const &name) -> EnumT
Get the enumerator value based on the name.
std::array< std::pair< dw::core::StringView, EnumT >, NumberOfEnumerators > EnumDescriptionReturnType
Definition: Buffer.hpp:41
static constexpr EnumDescriptionReturnType< EnumT, 0 > get()
Describe the enumerators.