15 #ifndef SHAKA_EMBEDDED_MAPPING_ENUM_H_ 16 #define SHAKA_EMBEDDED_MAPPING_ENUM_H_ 19 #include <type_traits> 52 #define CONVERT_ENUM_AS_NUMBER(ns, type) \ 54 struct ::shaka::TypeName<ns::type, void> { \ 55 static std::string name() { \ 60 struct ::shaka::impl::ConvertHelper<ns::type> \ 61 : ::shaka::impl::NumberEnumConverter<ns::type> {} 63 #define DEFINE_ENUM_MAPPING(ns, type) \ 65 struct ::shaka::TypeName<ns::type, void> { \ 66 static std::string name() { \ 71 class ::shaka::impl::EnumConverter<ns::type> \ 72 : public ::shaka::impl::StringEnumConverter<ns::type> { \ 74 using Enum = ns::type; \ 78 struct ::shaka::impl::ConvertHelper<ns::type> { \ 79 static bool FromJsValue(Handle<JsValue> source, ns::type* dest) { \ 80 ::shaka::impl::EnumConverter<ns::type> conv; \ 81 return conv.FromJsValue(source, dest); \ 83 static ReturnVal<JsValue> ToJsValue(ns::type source) { \ 84 ::shaka::impl::EnumConverter<ns::type> conv; \ 85 return conv.ToJsValue(source); \ 88 inline ::shaka::impl::EnumConverter<ns::type>::EnumConverter() 94 template <
typename Enum>
101 for (
auto& entry : entries_) {
102 if (entry.name == temp) {
111 for (
auto& entry : entries_) {
112 if (entry.entry == value)
115 LOG(FATAL) <<
"Invalid enum value.";
120 entries_.emplace_back(entry, name);
125 Entry(Enum entry,
const std::string&
name) :
name(name), entry(entry) {}
131 std::vector<Entry> entries_;
139 template <
typename Enum>
141 static_assert(std::is_enum<Enum>::value,
"Must be an enum type.");
148 *dest =
static_cast<Enum
>(temp);
157 template <
typename T>
163 #endif // SHAKA_EMBEDDED_MAPPING_ENUM_H_
void AddMapping(Enum entry, const std::string &name)
ReturnVal< JsValue > ToJsValue(T &&source)
static ReturnVal< JsValue > ToJsValue(Enum value)
ReturnVal< JsValue > ToJsValue(Enum value)
bool FromJsValue(Handle< JsValue > given, Enum *dest)
static bool FromJsValue(Handle< JsValue > given, Enum *dest)
typename std::underlying_type< Enum >::type IntType