Shaka Player Embedded
enum.h
Go to the documentation of this file.
1 // Copyright 2016 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SHAKA_EMBEDDED_MAPPING_ENUM_H_
16 #define SHAKA_EMBEDDED_MAPPING_ENUM_H_
17 
18 #include <string>
19 #include <type_traits>
20 #include <vector>
21 
22 #include "src/mapping/convert_js.h"
24 #include "src/mapping/names.h"
25 
52 #define CONVERT_ENUM_AS_NUMBER(ns, type) \
53  template <> \
54  struct ::shaka::TypeName<ns::type, void> { \
55  static std::string name() { \
56  return #type; \
57  } \
58  }; \
59  template <> \
60  struct ::shaka::impl::ConvertHelper<ns::type> \
61  : ::shaka::impl::NumberEnumConverter<ns::type> {}
62 
63 #define DEFINE_ENUM_MAPPING(ns, type) \
64  template <> \
65  struct ::shaka::TypeName<ns::type, void> { \
66  static std::string name() { \
67  return #type; \
68  } \
69  }; \
70  template <> \
71  class ::shaka::impl::EnumConverter<ns::type> \
72  : public ::shaka::impl::StringEnumConverter<ns::type> { \
73  public: \
74  using Enum = ns::type; \
75  EnumConverter(); \
76  }; \
77  template <> \
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); \
82  } \
83  static ReturnVal<JsValue> ToJsValue(ns::type source) { \
84  ::shaka::impl::EnumConverter<ns::type> conv; \
85  return conv.ToJsValue(source); \
86  } \
87  }; \
88  inline ::shaka::impl::EnumConverter<ns::type>::EnumConverter()
89 
90 
91 namespace shaka {
92 namespace impl {
93 
94 template <typename Enum>
96  public:
97  bool FromJsValue(Handle<JsValue> given, Enum* dest) {
98  std::string temp;
99  if (!ConvertHelper<std::string>::FromJsValue(given, &temp))
100  return false;
101  for (auto& entry : entries_) {
102  if (entry.name == temp) {
103  *dest = entry.entry;
104  return true;
105  }
106  }
107  return false;
108  }
109 
110  ReturnVal<JsValue> ToJsValue(Enum value) {
111  for (auto& entry : entries_) {
112  if (entry.entry == value)
113  return ConvertHelper<std::string>::ToJsValue(entry.name);
114  }
115  LOG(FATAL) << "Invalid enum value.";
116  }
117 
118  protected:
119  void AddMapping(Enum entry, const std::string& name) {
120  entries_.emplace_back(entry, name);
121  }
122 
123  private:
124  struct Entry {
125  Entry(Enum entry, const std::string& name) : name(name), entry(entry) {}
126 
127  std::string name;
128  Enum entry;
129  };
130 
131  std::vector<Entry> entries_;
132 };
133 
139 template <typename Enum>
141  static_assert(std::is_enum<Enum>::value, "Must be an enum type.");
143 
144  static bool FromJsValue(Handle<JsValue> given, Enum* dest) {
145  IntType temp;
146  if (!ConvertHelper<IntType>::FromJsValue(given, &temp))
147  return false;
148  *dest = static_cast<Enum>(temp);
149  return true;
150  }
151 
152  static ReturnVal<JsValue> ToJsValue(Enum value) {
153  return ConvertHelper<IntType>::ToJsValue(static_cast<IntType>(value));
154  }
155 };
156 
157 template <typename T>
159 
160 } // namespace impl
161 } // namespace shaka
162 
163 #endif // SHAKA_EMBEDDED_MAPPING_ENUM_H_
const char * dest
Definition: media_utils.cc:31
void AddMapping(Enum entry, const std::string &name)
Definition: enum.h:119
const char * name
ReturnVal< JsValue > ToJsValue(T &&source)
Definition: convert_js.h:381
static ReturnVal< JsValue > ToJsValue(Enum value)
Definition: enum.h:152
ExceptionCode type
ReturnVal< JsValue > ToJsValue(Enum value)
Definition: enum.h:110
bool FromJsValue(Handle< JsValue > given, Enum *dest)
Definition: enum.h:97
static bool FromJsValue(Handle< JsValue > given, Enum *dest)
Definition: enum.h:144
typename std::underlying_type< Enum >::type IntType
Definition: enum.h:142