Spicy
enum.h
1 #pragma once
2 
3 #include <limits>
4 #include <string>
5 #include <type_traits>
6 #include <variant>
7 
8 #include <hilti/rt/exception.h>
9 #include <hilti/rt/logging.h>
10 #include <hilti/rt/type-info.h>
11 
12 namespace hilti::rt {
13 
14 namespace enum_ {
15 
23 template<typename T>
24 bool has_label(const T& t, const TypeInfo* ti) {
25  if ( ti->tag != TypeInfo::Enum )
26  internalError("unexpected type info in enum_::has_label");
27 
28  for ( const auto& l : ti->enum_->labels() ) {
29  if ( l.value != -1 && static_cast<int64_t>(t) == l.value )
30  return true;
31  }
32 
33  return false;
34 }
35 
45 template<typename T>
46 T from_int(int64_t n) {
47  static_assert(std::is_enum<T>::value && std::is_same_v<std::underlying_type_t<T>, int64_t>);
48  return static_cast<T>(n);
49 }
50 
63 template<typename T>
64 T from_uint(uint64_t n) {
65  static_assert(std::is_enum<T>::value && std::is_same_v<std::underlying_type_t<T>, int64_t>);
66 
67  if ( n > static_cast<uint64_t>(std::numeric_limits<int64_t>::max()) )
68  throw InvalidValue("enum value exceeds range");
69 
70  return static_cast<T>(n);
71 }
72 
73 } // namespace enum_
74 } // namespace hilti::rt
Definition: any.h:7
void internalError(const std::string &msg) __attribute__((noreturn))
Definition: logging.cc:17