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/. */
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
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"
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
39 // So we need to check for that before jumping.
40 if ((*tp
).isInitialized()) {
45 static bool isValid(const SmartPtrType
& v
) {
46 return !v
.isInitialized() || GCPolicy
<T
>::isValid(v
);
52 template <typename T
, typename Wrapper
>
53 struct WrappedPtrOperations
<mozilla::OwningNonNull
<T
>, Wrapper
> {
54 operator T
&() const { return static_cast<const Wrapper
*>(this)->get(); }
58 #endif /* mozilla_RootedOwningNonNull_h__ */