summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/bindings/templates/union_container.h.tmpl
blob: f2463c6893082e14c351acc8826b820abea23f4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
{% filter format_blink_cpp_source_code %}

{% include 'copyright_block.txt' %}
#ifndef {{header_guard}}
#define {{header_guard}}

{% for filename in header_includes %}
#include "{{filename}}"
{% endfor %}

namespace blink {

{% for decl in header_forward_decls %}
class {{decl}};
{% endfor %}

class {{exported}}{{cpp_class}} final {
  DISALLOW_NEW();
 public:
  {{cpp_class}}();
  bool IsNull() const { return type_ == SpecificType::kNone; }

  {% for member in members %}
  bool Is{{member.type_name}}() const { return type_ == SpecificType::{{member.specific_type_enum}}; }
  {{member.rvalue_cpp_type}} GetAs{{member.type_name}}() const;
  void Set{{member.type_name}}({{member.rvalue_cpp_type}});
  static {{cpp_class}} From{{member.type_name}}({{member.rvalue_cpp_type}});

  {% endfor %}
  {{cpp_class}}(const {{cpp_class}}&);
  ~{{cpp_class}}();
  {{cpp_class}}& operator=(const {{cpp_class}}&);
  void Trace(Visitor*);

 private:
  enum class SpecificType {
    kNone,
    {% for member in members %}
    {{member.specific_type_enum}},
    {% endfor %}
  };
  SpecificType type_;

  {% for member in members %}
  {{member.cpp_type}} {{member.cpp_name}}_;
  {% endfor %}

  friend {{exported}}v8::Local<v8::Value> ToV8(const {{cpp_class}}&, v8::Local<v8::Object>, v8::Isolate*);
};

class {{v8_class}} final {
 public:
  {{exported}}static void ToImpl(v8::Isolate*, v8::Local<v8::Value>, {{cpp_class}}&, UnionTypeConversionMode, ExceptionState&);
};

{{exported}}v8::Local<v8::Value> ToV8(const {{cpp_class}}&, v8::Local<v8::Object>, v8::Isolate*);

template <class CallbackInfo>
inline void V8SetReturnValue(const CallbackInfo& callbackInfo, {{cpp_class}}& impl) {
  V8SetReturnValue(callbackInfo, ToV8(impl, callbackInfo.Holder(), callbackInfo.GetIsolate()));
}

template <class CallbackInfo>
inline void V8SetReturnValue(const CallbackInfo& callbackInfo, {{cpp_class}}& impl, v8::Local<v8::Object> creationContext) {
  V8SetReturnValue(callbackInfo, ToV8(impl, creationContext, callbackInfo.GetIsolate()));
}

template <>
struct NativeValueTraits<{{cpp_class}}> : public NativeValueTraitsBase<{{cpp_class}}> {
  {{exported}}static {{cpp_class}} NativeValue(v8::Isolate*, v8::Local<v8::Value>, ExceptionState&);
  {{exported}}static {{cpp_class}} NullValue() { return {{cpp_class}}(); }
};

template <>
struct V8TypeOf<{{cpp_class}}> {
  typedef {{v8_class}} Type;
};

}  // namespace blink

// We need to set canInitializeWithMemset=true because HeapVector supports
// items that can initialize with memset or have a vtable. It is safe to
// set canInitializeWithMemset=true for a union type object in practice.
// See https://codereview.chromium.org/1118993002/#msg5 for more details.
WTF_ALLOW_MOVE_AND_INIT_WITH_MEM_FUNCTIONS(blink::{{cpp_class}})

#endif  // {{header_guard}}

{% endfilter %}{# format_blink_cpp_source_code #}