Treelite
typeinfo.h
Go to the documentation of this file.
1 
8 #ifndef TREELITE_TYPEINFO_H_
9 #define TREELITE_TYPEINFO_H_
10 
11 #include <cstdint>
12 #include <typeinfo>
13 #include <string>
14 #include <unordered_map>
15 #include <sstream>
16 #include <utility>
17 #include <type_traits>
18 
19 namespace treelite {
20 
22 enum class TypeInfo : uint8_t {
23  kInvalid = 0,
24  kUInt32 = 1,
25  kFloat32 = 2,
26  kFloat64 = 3
27 };
28 static_assert(std::is_same<std::underlying_type<TypeInfo>::type, uint8_t>::value,
29  "TypeInfo must use uint8_t as underlying type");
30 
32 TypeInfo GetTypeInfoByName(const std::string& str);
33 
39 inline std::string TypeInfoToString(treelite::TypeInfo type) {
40  switch (type) {
41  case treelite::TypeInfo::kInvalid:
42  return "invalid";
43  case treelite::TypeInfo::kUInt32:
44  return "uint32";
45  case treelite::TypeInfo::kFloat32:
46  return "float32";
47  case treelite::TypeInfo::kFloat64:
48  return "float64";
49  default:
50  throw std::runtime_error("Unrecognized type");
51  return "";
52  }
53 }
54 
60 template <typename T>
61 inline TypeInfo TypeToInfo() {
62  if (std::is_same<T, uint32_t>::value) {
63  return TypeInfo::kUInt32;
64  } else if (std::is_same<T, float>::value) {
65  return TypeInfo::kFloat32;
66  } else if (std::is_same<T, double>::value) {
67  return TypeInfo::kFloat64;
68  } else {
69  throw std::runtime_error(std::string("Unrecognized Value type") + typeid(T).name());
70  return TypeInfo::kInvalid;
71  }
72 }
73 
86 template <template<class> class Dispatcher, typename ...Args>
87 inline auto DispatchWithTypeInfo(TypeInfo type, Args&& ...args) {
88  switch (type) {
89  case TypeInfo::kUInt32:
90  return Dispatcher<uint32_t>::Dispatch(std::forward<Args>(args)...);
91  case TypeInfo::kFloat32:
92  return Dispatcher<float>::Dispatch(std::forward<Args>(args)...);
93  case TypeInfo::kFloat64:
94  return Dispatcher<double>::Dispatch(std::forward<Args>(args)...);
95  case TypeInfo::kInvalid:
96  default:
97  throw std::runtime_error(std::string("Invalid type: ") + TypeInfoToString(type));
98  }
99  return Dispatcher<double>::Dispatch(std::forward<Args>(args)...); // avoid missing return error
100 }
101 
116 template <template<class, class> class Dispatcher, typename ...Args>
118  TypeInfo threshold_type, TypeInfo leaf_output_type, Args&& ...args) {
119  auto error_threshold_type = [threshold_type]() {
120  std::ostringstream oss;
121  oss << "Invalid threshold type: " << treelite::TypeInfoToString(threshold_type);
122  return oss.str();
123  };
124  auto error_leaf_output_type = [threshold_type, leaf_output_type]() {
125  std::ostringstream oss;
126  oss << "Cannot use leaf output type " << treelite::TypeInfoToString(leaf_output_type)
127  << " with threshold type " << treelite::TypeInfoToString(threshold_type);
128  return oss.str();
129  };
130  switch (threshold_type) {
131  case treelite::TypeInfo::kFloat32:
132  switch (leaf_output_type) {
133  case treelite::TypeInfo::kUInt32:
134  return Dispatcher<float, uint32_t>::Dispatch(std::forward<Args>(args)...);
135  case treelite::TypeInfo::kFloat32:
136  return Dispatcher<float, float>::Dispatch(std::forward<Args>(args)...);
137  default:
138  throw std::runtime_error(error_leaf_output_type());
139  break;
140  }
141  break;
142  case treelite::TypeInfo::kFloat64:
143  switch (leaf_output_type) {
144  case treelite::TypeInfo::kUInt32:
145  return Dispatcher<double, uint32_t>::Dispatch(std::forward<Args>(args)...);
146  case treelite::TypeInfo::kFloat64:
147  return Dispatcher<double, double>::Dispatch(std::forward<Args>(args)...);
148  default:
149  throw std::runtime_error(error_leaf_output_type());
150  break;
151  }
152  break;
153  default:
154  throw std::runtime_error(error_threshold_type());
155  break;
156  }
157  return Dispatcher<double, double>::Dispatch(std::forward<Args>(args)...);
158  // avoid missing return value warning
159 }
160 
161 } // namespace treelite
162 
163 #endif // TREELITE_TYPEINFO_H_
auto DispatchWithTypeInfo(TypeInfo type, Args &&...args)
Given a TypeInfo, dispatch a function with the corresponding template arg. More precisely, we shall call Dispatcher<T>::Dispatch() where the template arg T corresponds to the type parameter.
Definition: typeinfo.h:87
TypeInfo
Types used by thresholds and leaf outputs.
Definition: typeinfo.h:22
std::string TypeInfoToString(treelite::TypeInfo type)
Get string representation of type info.
Definition: typeinfo.h:39
auto DispatchWithModelTypes(TypeInfo threshold_type, TypeInfo leaf_output_type, Args &&...args)
Given the types for thresholds and leaf outputs, validate that they consist of a valid combination fo...
Definition: typeinfo.h:117
TypeInfo GetTypeInfoByName(const std::string &str)
conversion table from string to TypeInfo, defined in tables.cc
Definition: typeinfo.cc:16
TypeInfo TypeToInfo()
Convert a template type into a type info.
Definition: typeinfo.h:61