Shaka Player Embedded
struct.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_STRUCT_H_
16 #define SHAKA_EMBEDDED_MAPPING_STRUCT_H_
17 
18 #include <memory>
19 #include <string>
20 #include <type_traits>
21 #include <vector>
22 
23 #include "src/mapping/convert_js.h"
27 #include "src/memory/heap_tracer.h"
28 #include "src/util/templates.h"
29 
30 namespace shaka {
31 
32 // Gets the class for |*this|. Normally to get a pointer to member, you
33 // have to use |&MyType::member|, this allows us to avoid passing the current
34 // type name to the macro |&THIS_TYPE::member|.
35 #define THIS_TYPE std::decay<decltype(*this)>::type
36 
37 #define ADD_NAMED_DICT_FIELD(member, name, ...) \
38  __VA_ARGS__ member = CreateFieldConverter(name, &THIS_TYPE::member)
39 #define ADD_DICT_FIELD(member, ...) \
40  ADD_NAMED_DICT_FIELD(member, #member, __VA_ARGS__)
41 
42 #define DECLARE_STRUCT_SPECIAL_METHODS_COPYABLE(Type) \
43  static std::string name() { \
44  return #Type; \
45  } \
46  Type(); \
47  Type(const Type&); \
48  Type(Type&&); \
49  ~Type() override; \
50  Type& operator=(const Type&); \
51  Type& operator=(Type&&)
52 #define DEFINE_STRUCT_SPECIAL_METHODS_COPYABLE(Type) \
53  Type::Type() {} \
54  Type::Type(const Type&) = default; \
55  Type::Type(Type&&) = default; \
56  Type::~Type() {} \
57  Type& Type::operator=(const Type&) = default; \
58  Type& Type::operator=(Type&&) = default
59 #define DECLARE_STRUCT_SPECIAL_METHODS_MOVE_ONLY(Type) \
60  static std::string name() { \
61  return #Type; \
62  } \
63  Type(); \
64  Type(const Type&) = delete; \
65  Type(Type&&); \
66  ~Type() override; \
67  Type& operator=(const Type&) = delete; \
68  Type& operator=(Type&&)
69 #define DEFINE_STRUCT_SPECIAL_METHODS_MOVE_ONLY(Type) \
70  Type::Type() {} \
71  Type::Type(Type&&) = default; \
72  Type::~Type() {} \
73  Type& Type::operator=(Type&&) = default
74 
75 class Struct;
76 
77 namespace impl {
78 
89  public:
90  virtual ~FieldConverterBase() {}
91 
96  virtual void SearchAndStore(Struct* dict, Handle<JsObject> object) = 0;
98  virtual void AddToObject(const Struct* dict,
99  Handle<JsObject> object) const = 0;
101  virtual void Trace(const Struct* dict, memory::HeapTracer* tracer) const = 0;
102 };
103 
108 template <typename Parent, typename Field>
110  public:
111  FieldConverter(const std::string& name, Field Parent::*member)
112  : name_(name), member_(member) {}
113 
114 
115  void SearchAndStore(Struct* dict, Handle<JsObject> object) override {
116  auto parent = static_cast<Parent*>(dict);
117  LocalVar<JsValue> member(GetMemberRaw(object, name_));
118  (void)FromJsValue(member, &(parent->*member_));
119  }
120 
121  void AddToObject(const Struct* dict, Handle<JsObject> object) const override {
122  auto parent = static_cast<const Parent*>(dict);
123  LocalVar<JsValue> value(ToJsValue(parent->*member_));
124  SetMemberRaw(object, name_, value);
125  }
126 
127  void Trace(const Struct* dict, memory::HeapTracer* tracer) const override {
128  auto parent = static_cast<const Parent*>(dict);
129  tracer->Trace(&(parent->*member_));
130  }
131 
132  private:
133  std::string name_;
134  // Store as a pointer to member so if we are copied, we don't need to update
135  // the pointers (or make the type non-copyable).
136  Field Parent::*member_;
137 };
138 
139 } // namespace impl
140 
141 
166 class Struct : public GenericConverter, public memory::Traceable {
167  public:
168  Struct();
169  ~Struct() override;
170 
171  // Chromium style requires out-of-line copy/move constructors, even though
172  // they are still the default.
173  Struct(const Struct&);
174  Struct(Struct&&);
175  Struct& operator=(const Struct&);
176  Struct& operator=(Struct&&);
177 
178  bool TryConvert(Handle<JsValue> value) override;
179  ReturnVal<JsValue> ToJsValue() const override;
180  void Trace(memory::HeapTracer* tracer) const override;
181 
182  protected:
183  template <typename Parent, typename Field>
184  Field CreateFieldConverter(const std::string& name, Field Parent::*field) {
185  static_assert(std::is_base_of<Struct, Parent>::value,
186  "Must be derived from Struct");
187  auto convert = new impl::FieldConverter<Parent, Field>(name, field);
188  converters_.emplace_back(convert);
189  return Field();
190  }
191 
192  private:
193  mutable WeakJsPtr<JsObject> object_;
194  std::vector<std::shared_ptr<impl::FieldConverterBase>> converters_;
195 };
196 
197 } // namespace shaka
198 
199 #endif // SHAKA_EMBEDDED_MAPPING_STRUCT_H_
bool FromJsValue(Handle< JsValue > source, T *dest)
Definition: convert_js.h:370
virtual void AddToObject(const Struct *dict, Handle< JsObject > object) const =0
const char * name
void AddToObject(const Struct *dict, Handle< JsObject > object) const override
Definition: struct.h:121
ReturnVal< JsValue > ToJsValue(T &&source)
Definition: convert_js.h:381
virtual void SearchAndStore(Struct *dict, Handle< JsObject > object)=0
Field CreateFieldConverter(const std::string &name, Field Parent::*field)
Definition: struct.h:184
void Trace(const Traceable *ptr)
Definition: heap_tracer.cc:43
void SetMemberRaw(Handle< JsObject > object, const std::string &name, Handle< JsValue > value)
Definition: js_wrappers.cc:147
virtual void Trace(const Struct *dict, memory::HeapTracer *tracer) const =0
void Trace(const Struct *dict, memory::HeapTracer *tracer) const override
Definition: struct.h:127
ReturnVal< JsValue > GetMemberRaw(Handle< JsObject > object, const std::string &name, LocalVar< JsValue > *exception=nullptr)
Definition: js_wrappers.cc:136
void SearchAndStore(Struct *dict, Handle< JsObject > object) override
Definition: struct.h:115
FieldConverter(const std::string &name, Field Parent::*member)
Definition: struct.h:111