Compute Graph Framework SDK Reference  5.6
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-2022 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 <dw/core/container/StringView.hpp>
36
37#include <dwcgf/Exception.hpp>
38
39#include <array>
40#include <utility>
41#include <type_traits>
42
43// This is the only language feature able to avoid duplicating enumerators as strings
44// coverity[autosar_cpp14_a16_0_1_violation]
45// coverity[autosar_cpp14_m16_3_2_violation]
46#define DW_ENUMERATOR_NAME_STRING_VIEW(NAME_STR) NAME_STR##_sv
56// This is the only language feature able to avoid duplicating enumerators as strings
57// Cannot enclose name of enumerator from an enum class in parentheses
58// coverity[autosar_cpp14_a16_0_1_violation]
59// coverity[autosar_cpp14_m16_0_6_violation]
60// coverity[autosar_cpp14_m16_3_2_violation]
61#define DW_DESCRIBE_ENUMERATOR(NAME) describeEnumerator(DW_ENUMERATOR_NAME_STRING_VIEW(#NAME), EnumT::NAME)
69// This is the only language feature able to avoid duplicating enumerators as strings
70// coverity[autosar_cpp14_a16_0_1_violation]
71// coverity[autosar_cpp14_m16_3_2_violation]
72#define DW_DESCRIBE_C_ENUMERATOR(NAME) describeEnumerator(DW_ENUMERATOR_NAME_STRING_VIEW(#NAME), (NAME))
73
74namespace dw
75{
76namespace framework
77{
78
79// API to declare the mapping of a concrete enum.
80
81template <typename EnumT>
83{
84 static_assert(std::is_enum<EnumT>::value, "EnumT must be an enumeration type");
85
94 static constexpr auto get()
95 {
96 static_assert(sizeof(EnumT) == 0, "Needs specialization for specific enum type");
97 constexpr const size_t numberOfEnumerators = 0;
98 std::array<std::pair<dw::core::StringView, EnumT>, numberOfEnumerators> ret;
99 return ret;
100 }
101};
102
103// API needed to declare the enumerators of an enum.
104
115// TODO(dwplc): FP -- The uppercase symbol is a type, the lowercase symbol is a variable which isn't ambiguous
116// coverity[autosar_cpp14_m2_10_1_violation]
117template <typename EnumT, typename... Args>
118constexpr auto describeEnumeratorCollection(Args const&&... args)
119{
120 return std::array<std::pair<dw::core::StringView, EnumT>, sizeof...(Args)>{std::forward<const Args>(args)...};
121}
122
135template <typename EnumT>
136constexpr auto describeEnumerator(dw::core::StringView const&& name, EnumT value)
137{
138 return std::make_pair(std::move(name), value);
139}
140
148template <typename EnumT>
149auto mapEnumNameToValue(dw::core::StringView const& name) -> EnumT
150{
151 const auto pairs = EnumDescription<EnumT>::get();
152 for (const auto& pair : pairs)
153 {
154 if (pair.first == name)
155 {
156 return pair.second;
157 }
158 }
159 FixedString<4096> validNames{};
160 for (const auto& pair : pairs)
161 {
162 validNames << " " << pair.first;
163 }
164 throw Exception(DW_INVALID_ARGUMENT, "Invalid enumerator name '", name, "' for enum type ", typeid(EnumT).name(), ", valid names:", validNames);
165}
166
174template <typename EnumT>
175dw::core::StringView mapEnumValueToName(EnumT value)
176{
177 const auto pairs = EnumDescription<EnumT>::get();
178 for (const auto& pair : pairs)
179 {
180 if (pair.second == value)
181 {
182 return pair.first;
183 }
184 }
185 throw Exception(DW_INVALID_ARGUMENT, "The enumerator value is invalid: ", typeid(EnumT).name(), " ", value);
186}
187
188} // namespace framework
189} // namespace dw
190
191#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)
Describe the enumerators.
constexpr auto describeEnumerator(dw::core::StringView const &&name, EnumT value)
Describe an enumerator.
auto mapEnumNameToValue(dw::core::StringView const &name) -> EnumT
Get the enumerator value based on the name.
Definition: Exception.hpp:47
static constexpr auto get()
Describe the enumerators.