Halide
ParamParser.h
Go to the documentation of this file.
1 #ifndef PARSE_H
2 #define PARSE_H
3 
4 #include "Errors.h"
5 #include <sstream>
6 #include <type_traits>
7 
8 namespace Halide {
9 namespace Internal {
10 namespace Autoscheduler {
11 
12 class ParamParser {
13  std::map<std::string, std::string> extra;
14 
15  // If the string can be parsed as a valid "T", set *value to it.
16  // If not, assert-fail.
17  template<typename T>
18  static void parse_or_die(const std::string &str, T *value) {
19  std::istringstream iss(str);
20  T t;
21  // All one-byte ints int8 and uint8 should be parsed as integers, not chars --
22  // including 'char' itself. (Note that sizeof(bool) is often-but-not-always-1,
23  // so be sure to exclude that case.)
24  if constexpr (sizeof(T) == sizeof(char) && !std::is_same<T, bool>::value) {
25  int i;
26  iss >> i;
27  t = (T)i;
28  } else {
29  iss >> t;
30  }
31  user_assert(!iss.fail() && iss.get() == EOF) << "Unable to parse: " << str;
32  *value = t;
33  }
34 
35 public:
36  explicit ParamParser(const std::map<std::string, std::string> &m)
37  : extra(m) {
38  }
39 
40  // If the given key is present in m, parse the result into *value and return true.
41  // (If the string cannot be parsed as a valid "T", assert-fail.)
42  // If the given key is not present, leave *value untouched and return false.
43  template<typename T>
44  bool parse(const std::string &key, T *value) {
45  auto it = extra.find(key);
46  if (it == extra.end()) {
47  return false;
48  }
49  parse_or_die(it->second, value);
50  extra.erase(it);
51  return true;
52  }
53 
54  void finish() {
55  if (!extra.empty()) {
56  std::ostringstream oss;
57  oss << "Autoscheduler Params contain unknown keys:\n";
58  for (const auto &it : extra) {
59  oss << " " << it.first << "\n";
60  }
61  user_error << oss.str();
62  }
63  }
64 
66  finish();
67  }
68 };
69 
70 } // namespace Autoscheduler
71 } // namespace Internal
72 } // namespace Halide
73 
74 #endif
Errors.h
Halide::Internal::Autoscheduler::ParamParser
Definition: ParamParser.h:12
user_assert
#define user_assert(c)
Definition: test.h:10
Halide::Internal::Autoscheduler::ParamParser::ParamParser
ParamParser(const std::map< std::string, std::string > &m)
Definition: ParamParser.h:36
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition: AbstractGenerator.h:19
Halide::LinkageType::Internal
@ Internal
Not visible externally, similar to 'static' linkage in C.
Halide::Internal::Autoscheduler::ParamParser::finish
void finish()
Definition: ParamParser.h:54
user_error
#define user_error
Definition: Errors.h:7
Halide::Internal::Autoscheduler::ParamParser::parse
bool parse(const std::string &key, T *value)
Definition: ParamParser.h:44
Halide::Internal::Autoscheduler::ParamParser::~ParamParser
~ParamParser()
Definition: ParamParser.h:65