Bug 1664775 [wpt PR 25515] - Fieldset NG: Fix hit-testing for the rendered legend...
[gecko.git] / dom / bindings / RootedOwningNonNull.h
blobe587ec4605efc4da0e594550c6ee39332d162263
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /**
8 * An implementation of Rooted for OwningNonNull<T>. This works by assuming
9 * that T has a Trace() method defined on it which will trace whatever things
10 * inside the T instance need tracing.
12 * This implementation has one serious drawback: operator= doesn't work right
13 * because it's declared on Rooted directly and expects the type Rooted is
14 * templated over.
17 #ifndef mozilla_RootedOwningNonNull_h__
18 #define mozilla_RootedOwningNonNull_h__
20 #include "mozilla/OwningNonNull.h"
21 #include "js/GCPolicyAPI.h"
22 #include "js/RootingAPI.h"
24 namespace JS {
25 template <typename T>
26 struct GCPolicy<mozilla::OwningNonNull<T>> {
27 typedef mozilla::OwningNonNull<T> SmartPtrType;
29 static SmartPtrType initial() { return SmartPtrType(); }
31 static void trace(JSTracer* trc, SmartPtrType* tp, const char* name) {
32 // We have to be very careful here. Normally, OwningNonNull can't be null.
33 // But binding code can end up in a situation where it sets up a
34 // Rooted<OwningNonNull> and then before it gets a chance to assign to it
35 // (e.g. from the constructor of the thing being assigned) a GC happens. So
36 // we can land here when *tp stores a null pointer because it's not
37 // initialized.
39 // So we need to check for that before jumping.
40 if ((*tp).isInitialized()) {
41 (*tp)->Trace(trc);
45 static bool isValid(const SmartPtrType& v) {
46 return !v.isInitialized() || GCPolicy<T>::isValid(v);
49 } // namespace JS
51 namespace js {
52 template <typename T, typename Wrapper>
53 struct WrappedPtrOperations<mozilla::OwningNonNull<T>, Wrapper> {
54 operator T&() const { return static_cast<const Wrapper*>(this)->get(); }
56 } // namespace js
58 #endif /* mozilla_RootedOwningNonNull_h__ */