HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
provider_options_utils.h
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License.
3 
4 #pragma once
5 
6 #include <algorithm>
7 #include <functional>
8 #include <type_traits>
9 #include <unordered_map>
10 #include <vector>
11 
12 #include "core/common/common.h"
15 
16 namespace onnxruntime {
17 
18 template <typename TEnum>
19 using EnumNameMapping = std::vector<std::pair<TEnum, std::string>>;
20 
21 /**
22  * Given a mapping and an enumeration value, gets the corresponding name.
23  */
24 template <typename TEnum>
26  const auto it = std::find_if(
27  mapping.begin(), mapping.end(),
28  [&value](const std::pair<TEnum, std::string>& entry) {
29  return entry.first == value;
30  });
32  it == mapping.end(),
33  "Failed to map enum value to name: ", static_cast<typename std::underlying_type<TEnum>::type>(value));
34  name = it->second;
35  return Status::OK();
36 }
37 
38 template <typename TEnum>
41  ORT_THROW_IF_ERROR(EnumToName(mapping, value, name));
42  return name;
43 }
44 
45 /**
46  * Given a mapping and a name, gets the corresponding enumeration value.
47  */
48 template <typename TEnum>
50  const EnumNameMapping<TEnum>& mapping, const std::string& name, TEnum& value) {
51  const auto it = std::find_if(
52  mapping.begin(), mapping.end(),
53  [&name](const std::pair<TEnum, std::string>& entry) {
54  return entry.second == name;
55  });
57  it == mapping.end(),
58  "Failed to map enum name to value: ", name);
59  value = it->first;
60  return Status::OK();
61 }
62 
63 template <typename TEnum>
64 TEnum NameToEnum(const EnumNameMapping<TEnum>& mapping, const std::string& name) {
65  TEnum value;
66  ORT_THROW_IF_ERROR(NameToEnum(mapping, name, value));
67  return value;
68 }
69 
71  public:
72  /**
73  * Adds a parser for a particular provider option value.
74  *
75  * @param name The provider option name.
76  * @param value_parser An object that parses the option value.
77  * It should be callable with the following signature and return
78  * whether the parsing was successful:
79  * Status value_parser(const std::string&)
80  *
81  * @return The current ProviderOptionsParser instance.
82  */
83  template <typename ValueParserType>
85  const std::string& name, ValueParserType value_parser) {
87  value_parsers_.emplace(name, ValueParser{value_parser}).second,
88  "Provider option \"", name, "\" already has a value parser.");
89  return *this;
90  }
91 
92  /**
93  * Adds a parser for a particular provider option value which converts a
94  * value to the right type and assigns it to the given reference.
95  *
96  * IMPORTANT: This function stores a reference to the destination variable.
97  * The caller must ensure that the reference is valid when Parse() is called!
98  *
99  * @param name The provider option name.
100  * @param dest The destination variable reference.
101  *
102  * @return The current ProviderOptionsParser instance.
103  */
104  template <typename ValueType>
106  const std::string& name, ValueType& dest) {
107  return AddValueParser(
108  name,
109  [&dest](const std::string& value_str) -> Status {
110  return ParseStringWithClassicLocale(value_str, dest);
111  });
112  }
113 
114  /**
115  * Adds a parser for a particular provider option value which maps an
116  * enumeration name to a value and assigns it to the given reference.
117  *
118  * IMPORTANT: This function stores references to the mapping and destination
119  * variables. The caller must ensure that the references are valid when
120  * Parse() is called!
121  *
122  * @param name The provider option name.
123  * @param mapping The enumeration value to name mapping.
124  * @param dest The destination variable reference.
125  *
126  * @return The current ProviderOptionsParser instance.
127  */
128  template <typename EnumType>
130  const std::string& name, const EnumNameMapping<EnumType>& mapping, EnumType& dest) {
131  return AddValueParser(
132  name,
133  [&mapping, &dest](const std::string& value_str) -> Status {
134  return NameToEnum(mapping, value_str, dest);
135  });
136  }
137 
138  /**
139  * Parses the given provider options.
140  */
141  Status Parse(const ProviderOptions& options) const {
142  for (const auto& option : options) {
143  const auto& name = option.first;
144  const auto& value_str = option.second;
145  const auto value_parser_it = value_parsers_.find(name);
147  value_parser_it == value_parsers_.end(),
148  "Unknown provider option: \"", name, "\".");
149 
150  const auto parse_status = value_parser_it->second(value_str);
152  parse_status.IsOK(),
153  "Failed to parse provider option \"", name, "\": ", parse_status.ErrorMessage());
154  }
155 
156  return Status::OK();
157  }
158 
159  private:
160  using ValueParser = std::function<Status(const std::string&)>;
161  std::unordered_map<std::string, ValueParser> value_parsers_;
162 };
163 
164 } // namespace onnxruntime
ProviderOptionsParser & AddValueParser(const std::string &name, ValueParserType value_parser)
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
GLsizei const GLfloat * value
Definition: glcorearb.h:824
#define ORT_ENFORCE(condition,...)
Definition: common.h:172
Status Parse(const ProviderOptions &options) const
std::vector< std::pair< TEnum, std::string >> EnumNameMapping
ProviderOptionsParser & AddAssignmentToEnumReference(const std::string &name, const EnumNameMapping< EnumType > &mapping, EnumType &dest)
ProviderOptionsParser & AddAssignmentToReference(const std::string &name, ValueType &dest)
GLuint const GLchar * name
Definition: glcorearb.h:786
Status EnumToName(const EnumNameMapping< TEnum > &mapping, TEnum value, std::string &name)
#define ORT_RETURN_IF_NOT(condition,...)
Definition: common.h:201
std::unordered_map< std::string, std::string > ProviderOptions
Status NameToEnum(const EnumNameMapping< TEnum > &mapping, const std::string &name, TEnum &value)
Status ParseStringWithClassicLocale(std::string_view s, T &value)
Definition: parse_string.h:70
#define ORT_RETURN_IF(condition,...)
Definition: common.h:191
Definition: core.h:1131
type
Definition: core.h:1059
bool ValueType
Definition: NanoVDB.h:5729
#define ORT_THROW_IF_ERROR(expr)
Definition: common.h:235